《利用遗传算法求函数的极大值(共11页).doc》由会员分享,可在线阅读,更多相关《利用遗传算法求函数的极大值(共11页).doc(11页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上利用遗传算法求函数的极大值 该函数有两个局部极大值点,分别是f(2.048,-2.048)=3897.7342和f(2.048,-2.048)=3905.9262,其中,后者为全局最大点。可以分别用二进制编码和十进制编码遗传算法求函数极大值遗传算法二进制编码求函数极大值程序%Generic Algorithm for function f(x1,x2) optimumclear all;close all;%Parameters参数Size=80; %群体大小G=100; %终止进化代数CodeL=10; %代码长度 umax=2.048;umin=-2.048;E=
2、round(rand(Size,2*CodeL); %Initial Code 最初代码%Main Program主程序for k=1:1:Gtime(k)=k;for s=1:1:Sizem=E(s,:);y1=0;y2=0; %X对应的十进制代码%Uncodingm1=m(1:1:CodeL);for i=1:1:CodeL y1=y1+m1(i)*2(i-1); %将y1转换为十进制数endx1=(umax-umin)*y1/1023+umin;m2=m(CodeL+1:1:2*CodeL);for i=1:1:CodeL y2=y2+m2(i)*2(i-1); %将y2转换为十进制数e
3、ndx2=(umax-umin)*y2/1023+umin; %求x对应的十进制数F(s)=100*(x12-x2)2+(1-x1)2; %个体适应度函数endJi=1./F; %个体适应度函数的倒数%* Step 1 : Evaluate BestJ *BestJ(k)=min(Ji);fi=F; %Fitness Function 适应函数Oderfi,Indexfi=sort(fi); %Arranging fi small to biggerBestfi=Oderfi(Size); %Let Bestfi=max(fi)BestS=E(Indexfi(Size),:); %Let Be
4、stS=E(m), m is the Indexfi belong to max(fi) %最佳样本bfi(k)=Bestfi;%* Step 2 : Select and Reproduct Operation* fi_sum=sum(fi); fi_Size=(Oderfi/fi_sum)*Size; fi_S=floor(fi_Size); %Selecting Bigger fi value kk=1; for i=1:1:Size for j=1:1:fi_S(i) %Select and Reproduce TempE(kk,:)=E(Indexfi(i),:); kk=kk+1;
5、 %kk is used to reproduce end end %* Step 3 : Crossover Operation *pc=0.60; %交叉概率n=ceil(20*rand); %种群大小for i=1:2:(Size-1) temp=rand; if pctemp %Crossover Condition for j=n:1:20 TempE(i,j)=E(i+1,j); %交换E(i,j)和E(i+1,j) TempE(i+1,j)=E(i,j); end endendTempE(Size,:)=BestS;E=TempE; %* Step 4: Mutation Ope
6、ration *%pm=0.001; %变异概率%pm=0.001-1:1:Size*(0.001)/Size; %Bigger fi, smaller Pm%pm=0.0; %No mutationpm=0.1; %Big mutation for i=1:1:Size for j=1:1:2*CodeL temp=rand; if pmtemp %Mutation Condition if TempE(i,j)=0 TempE(i,j)=1; else TempE(i,j)=0; end end end end %Guarantee TempPop(30,:) is the code be
7、long to the best individual(max(fi)TempE(Size,:)=BestS;E=TempE;end Max_Value=BestfiBestSx1x2figure(1);plot(time,BestJ); %目标函数和时间的坐标系xlabel(Times);ylabel(Best J);figure(2);plot(time,bfi);xlabel(times);ylabel(Best F);遗传算法十进制编码求函数极大值程序%Generic Algorithm for function f(x1,x2) optimumclear all;close all;
8、%Parameters参数Size=80; G=100; %迭代次数 CodeL=10; %编码长度 umax=2.048;umin=-2.048;E=round(rand(Size,2*CodeL); %Initial Code ?%Main Programfor k=1:1:Gtime(k)=k;for s=1:1:Sizem=E(s,:);y1=0;y2=0;%Uncodingm1=m(1:1:CodeL); %?for i=1:1:CodeL y1=y1+m1(i)*2(i-1);endx1=(umax-umin)*y1/1023+umin;m2=m(CodeL+1:1:2*CodeL
9、);for i=1:1:CodeL y2=y2+m2(i)*2(i-1);endx2=(umax-umin)*y2/1023+umin;F(s)=100*(x12-x2)2+(1-x1)2;endJi=1./F;%* Step 1 : Evaluate BestJ *BestJ(k)=min(Ji);fi=F; %Fitness FunctionOderfi,Indexfi=sort(fi); %Arranging fi small to biggerBestfi=Oderfi(Size); %Let Bestfi=max(fi)BestS=E(Indexfi(Size),:); %Let B
10、estS=E(m), m is the Indexfi belong to max(fi)bfi(k)=Bestfi;%* Step 2 : Select and Reproduct Operation* fi_sum=sum(fi); fi_Size=(Oderfi/fi_sum)*Size; fi_S=floor(fi_Size); %Selecting Bigger fi value kk=1; for i=1:1:Size for j=1:1:fi_S(i) %Select and Reproduce TempE(kk,:)=E(Indexfi(i),:); kk=kk+1; %kk
11、is used to reproduce end end %* Step 3 : Crossover Operation *pc=0.60;n=ceil(20*rand);for i=1:2:(Size-1) temp=rand; if pctemp %Crossover Condition for j=n:1:20 TempE(i,j)=E(i+1,j); TempE(i+1,j)=E(i,j); end endendTempE(Size,:)=BestS;E=TempE; %* Step 4: Mutation Operation *%pm=0.001;%pm=0.001-1:1:Size
12、*(0.001)/Size; %Bigger fi, smaller Pm%pm=0.0; %No mutationpm=0.1; %Big mutation for i=1:1:Size for j=1:1:2*CodeL temp=rand; if pmtemp %Mutation Condition if TempE(i,j)=0 TempE(i,j)=1; else TempE(i,j)=0; end end end end %Guarantee TempPop(30,:) is the code belong to the best individual(max(fi)TempE(Size,:)=BestS;E=TempE;end Max_Value=BestfiBestSx1x2figure(1);plot(time,BestJ); xlabel(Times);ylabel(Best J);figure(2);plot(time,bfi);xlabel(times);ylabel(Best F);思考:通过改变群体大小、终止进化代数G、交叉概率Pc和变异概率Pm,分析群体大小、终止进化代数、交叉概率和变异概率对优化效果的影响。专心-专注-专业