《遗传算法十进制编码求函数极大值程序》由会员分享,可在线阅读,更多相关《遗传算法十进制编码求函数极大值程序(1页珍藏版)》请在金锄头文库上搜索。
1、遗传算法十进制编码求函数极大值程序%GenericAlgorithmforfunctionf(x1,x2)optimumclearall;closeall;Size=500;CodeL=2;MinX(1)=-2.048;MaxX(1)=2.048;MinX(2)=-2.048;MaxX(2)=2.048;E(:,1)=MinX(1)+(MaxX(1)-MinX(1)*rand(Size,1);E(:,2)=MinX(2)+(MaxX(2)-MinX(2)*rand(Size,1);G=200;*BsJ=0;%*StartRunningforkg=1:1:Gtime(kg)=kg;%*Step1
2、:EvaluateBestJ*fori=1:1:Sizexi=E(i,:);x1=xi(1);x2=xi(2);F(i)=100*(x1A2-x2)A2+(1-x1)A2;Ji=1./F;BsJi(i)=min(Ji);endOderJi,IndexJi=sort(BsJi);BestJ(kg)=OderJi(1);BJ=BestJ(kg);Ji=BsJi+1e-10;%Avoidingdevidingzerofi=F;Oderfi,Indexfi=sort(fi);%ArrangingfismalltobiggerBestfi=Oderfi(Size);%LetBestfi=max(fi)B
3、estS=E(Indexfi(Size),:);%LetBestS=E(m),mistheIndexfibelongtomax(fi)bfi(kg)=Bestfi;kgBestS%*Step2:SelectandReproductOperation*fi_sum=sum(fi);fi_Size=(Oderfi/fi_sum)*Size;fi_S=floor(fi_Size);%SelectingBiggerfivaluer=Size-sum(fi_S);Rest=fi_Size-fi_S;RestValue,Index=sort(Rest);fori=Size:-1:Size-r+1fi_S(
4、Index(i)=fi_S(Index(i)+1;%AddingresttoequalSizeendk=1;fori=Size:-1:1%SelecttheSizethandReproducefirstlyforj=1:1:fi_S(i)TempE(k,:)=E(Indexfi(i),:);%SelectandReproducek=k+1;%kisusedtoreproduceendend%*Step3:CrossoverOperation*Pc=0.90;fori=1:2:(Size-1)temp=rand;ifPctemp%CrossoverConditionalfa=rand;TempE
5、(i,:)=alfa*E(i+1,:)+(1-alfa)*E(i,:);TempE(i+1,:)=alfa*E(i,:)+(1-alfa)*E(i+1,:);endendTempE(Size,:)=BestS;E=TempE;%*Step4:MutationOperation*Pm=0.10-1:1:Size*(0.01)/Size;%Biggerfi,smallerPmPm_rand=rand(Size,CodeL);Mean=(MaxX+MinX)/2;Dif=(MaxX-MinX);fori=1:1:Sizeforj=1:1:CodeLifPm(i)Pm_rand(i,j)%MutationConditionTempE(i,j)=Mean(j)+Dif(j)*(rand-0.5);endendend%GuaranteeTempE(Size,:)belongtothebestindividualTempE(Size,:)=BestS;E=TempE;endBestSBestfifigure(1);plot(time,BestJ,k);xlabel(Times);ylabel(BestJ);figure(2);plot(time,bfi,k);xlabel(times);ylabel(BestF);