资源描述:
《遗传算法matlab源程序》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、问题:求在[-2,2]之间的最小值。Pc=0.7Pm=0.3L=20.原始程序:function[Bestvalue,Bestmember]=Genetic(gen,min,max,fitnessfunction,crossrate,mutationrate,membernumber,memberlength)gen=input('Pleaseinputthevalueofgen:');min=input('Pleaseinputthevalueofmin:');max=input('Pleaseinputth
2、evalueofmax:');crossrate=input('Pleaseinputthevalueofcrossrate:');mutationrate=input('Pleaseinputthevalueofmutationrate:');membernumber=input('Pleaseinputthevalueofmembernumber:');memberlength=input('Pleaseinputthevalueofmemberlength:');population=population
3、initialize1(membernumber,memberlength);mmm=population;globalcount;count=1;populationcode=population;%populationdata=populationdata1(populationcode,min,max);populationdata=zeros(size(populationcode,1),1);fori=1:size(populationcode,1)forj=1:size(populationcode
4、,2)populationdata(i)=populationdata(i)+populationcode(i,j).*(2.^(size(populationcode,2)-j));endpopulationdata(i)=min+populationdata(i).*(max-min)./(2.^(size(populationcode,2))-1);l=0;endfitness=fitness1(populationdata);whilecount5、s,population);newpopulation=select(population,fitnessp);population=newpopulation;newpopulation=crossing(population,crossrate);population=newpopulation;newpopulation=mutation(population,mutationrate);population=newpopulation;%populationdata=populationdata1(po
6、pulation,min,max);populationdata=zeros(size(population,1),1);fori=1:size(population,1)forj=1:size(population,2)populationdata(i)=populationdata(i)+population(i,j).*(2.^(size(population,2)-j));endpopulationdata(i)=min+populationdata(i).*(max-min)./(2.^(size(p
7、opulation,2))-1);l=0;endfitness=fitness1(populationdata);[fitness,B]=sort(fitness);%根据实际问题调整排序方式Bestvalue=log(fitness(1));Bestmember=populationdata(B(1));fori=1:membernumberfitness(i)=fitness(i);endcount=count+1;enddisp(fitness);functionnewpopulation=select(
8、population,fitnessp)rate(1)=fitnessp(1);fori=2:size(population,1)rate(i)=rate(i-1)+fitnessp(i);endfori=1:(size(population,1)+1)r=rand(1);o=1;whiler>rate(o)o=o+1;endnewpopulation(i,:)=population(