您好,欢迎访问三七文档
当前位置:首页 > 金融/证券 > 投融资/租赁 > 遗传算法求解函数最大值(matlab)
遗传算法求解函数F(x1,x2)=100*(x1^2-x2)^2+(1-x1)^2;的最大值(MATLAB)%GenericAlgorithmforfunctionf(x1,x2)optimum(最大值)clearall;closeall;%ParametersSize=80;G=100;CodeL=10;umax=2.048;umin=-2.048;E=round(rand(Size,2*CodeL));%InitialCode%MainProgramfork=1:1:Gtime(k)=k;fors=1:1:Sizem=E(s,:);y1=0;y2=0;%Uncodingm1=m(1:1:CodeL);fori=1:1:CodeLy1=y1+m1(i)*2^(i-1);endx1=(umax-umin)*y1/1023+umin;m2=m(CodeL+1:1:2*CodeL);fori=1:1:CodeLy2=y2+m2(i)*2^(i-1);endx2=(umax-umin)*y2/1023+umin;F(s)=100*(x1^2-x2)^2+(1-x1)^2;endJi=1./(F+1);%******Step1:EvaluateBestJ******BestJ(k)=min(Ji);fi=F;%FitnessFunction[Oderfi,Indexfi]=sort(fi);%ArrangingfismalltobiggerBestfi=Oderfi(Size);%LetBestfi=max(fi)BestS=E(Indexfi(Size),:);%LetBestS=E(m),mistheIndexfibelongtomax(fi)bfi(k)=Bestfi;%******Step2:SelectandReproductOperation******fi_sum=sum(fi);fi_Size=(Oderfi/fi_sum)*Size;fi_S=floor(fi_Size);%SelectingBiggerfivalue(取整)kk=1;fori=1:1:Sizeforj=1:1:fi_S(i)%SelectandReproduceTempE(kk,:)=E(Indexfi(i),:);kk=kk+1;%kkisusedtoreproduceendend%************Step3:CrossoverOperation************pc=0.60;n=ceil(20*rand);fori=1:2:(Size-1)temp=rand;ifpctemp%CrossoverConditionforj=n:1:20TempE(i,j)=E(i+1,j);TempE(i+1,j)=E(i,j);endendendTempE(Size,:)=BestS;E=TempE;%************Step4:MutationOperation**************%pm=0.001;%pm=0.001-[1:1:Size]*(0.001)/Size;%Biggerfi,smallerPm%pm=0.0;%Nomutationpm=0.1;%Bigmutationfori=1:1:Sizeforj=1:1:2*CodeLtemp=rand;ifpmtemp%MutationConditionifTempE(i,j)==0TempE(i,j)=1;elseTempE(i,j)=0;endendendend%GuaranteeTempPop(30,:)isthecodebelongtothebestindividual(max(fi))TempE(Size,:)=BestS;E=TempE;endMax_Value=BestfiBestSx1x2figure(1);plot(time,BestJ);xlabel('Times');ylabel('BestJ');figure(2);plot(time,bfi);xlabel('times');ylabel('BestF');轐行绒果Map_Falue=3.9059e+03BestS=000000000000000000Р0ɸ1=-2&0480x2=-2.0080
本文标题:遗传算法求解函数最大值(matlab)
链接地址:https://www.777doc.com/doc-4684612 .html