2022年遗传算法多元单峰函数的优化实例 .pdf
多元单峰函数的优化实例1,工具:谢菲尔德遗传算法工具箱2,运行环境:Matlab7 3,表达式f(x)=sum(x(i)2),-512=x(i)=512 i 为定义问题的一个值。4,本例选择个体数量40;最大遗传代数500;变量个数即i 为 20,每个变量用20 位表示,代沟选择0.9 5,De Jong 函数代码%OBJFUN1.M(OBJective function for De Jongs FUNction 1)%This function implements the De Jong function 1.%Syntax:ObjVal=objfun1(Chrom,switch1)%Input parameters:%Chrom-Matrix containing the chromosomes of the current%population.Each row corresponds to one individuals%string representation.%if Chrom=,then special values will be returned%switch-if Chrom=and%switch=1(or)return boundaries%switch=2 return title%switch=3 return value of global minimum%Output parameters:%ObjVal-Column vector containing the objective values of the%individuals in the current population.%if called with Chrom=,then ObjVal contains%switch=1,matrix with the boundaries of the function%switch=2,text for the title of the graphic output%switch=3,value of global minimum%Author:Hartmut Pohlheim%History:26.11.93 file created%27.11.93 text of title and switch added%30.11.93 show Dim in figure title%16.12.93 switch=3,return value of global minimum%01.03.94 name changed in obj*function ObjVal=objfun1(Chrom,switch1);%Dimension of objective function Dim=20;名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 3 页 -%Compute population parameters Nind,Nvar=size(Chrom);%Check size of Chrom and do the appropriate thing%if Chrom is,then define size of boundary-matrix and values if Nind=0%return text of title for graphic output if switch1=2 ObjVal=DE JONG function 1-int2str(Dim);%return value of global minimum elseif switch1=3 ObjVal=0;%define size of boundary-matrix and values else%lower and upper bound,identical for all n variables ObjVal=100*-5.12;5.12;ObjVal=ObjV al(1:2,ones(Dim,1);end%if Dim variables,compute values of function elseif Nvar=Dim%function 1,sum of xi2 for i=1:Dim(Dim=30)%n=Dim,-5.12=xi=5.12%global minimum at(xi)=(0);fmin=0 ObjVal=sum(Chrom.*Chrom);%ObjVal=diag(Chrom*Chrom);%both lines produce the same%otherwise error,wrong format of Chrom else error(size of matrix Chrom is not correct for function evaluation);end%End of function 注意:在原工具箱所带的目标函数文件里,用switch 做为标志,和Matlab 内部函数冲突,所以该为switch1 6,工作框下输入的代码NIND=40;MAXGEN=500;NVAR=20;PRECI=20;GGAP=0.9;trace=zeros(MAXGEN,2);名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 3 页 -FieldD=rep(PRECI,1,NVAR);rep(-512;512,1,NVAR);rep(1;0;1;1,1,NVAR);Chrom=CRTBP(NIND,NV AR*PRECI);gen=0;ObjV=OBJFUN1(BS2RV(Chrom,FieldD);while genMAXGEN,FitnV=ranking(ObjV);SelCh=select(sus,Chrom,FitnV,GGAP);SelCh=recombin(xovsp,SelCh,0.7);SelCh=mut(SelCh);ObjVSel=objfun1(BS2RV(SelCh,FieldD);Chrom ObjV=reins(Chrom,SelCh,1,1,ObjV,ObjVSel);gen=gen+1;trace(gen,1)=min(ObjV);trace(gen,2)=sum(ObjV)/length(ObjV);end plot(trace(:,1);hold on;plot(trace(:,2),-.);grid;legend(mean fitness,solution)注意:大家最好把所要用到的函数名称(比如交叉变异选择等)和工具箱中的文件名称大小写一致,这样在运行的时候就不会出现警告提示6,结果图0501001502002503003504004505000246810121416x 105mean fitness solution名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 3 页 -