数学建模专题之遗传算法的MATLAB实现ContentsIMatlab编程实现GA1Matlab函数调用实现GA2Matlab工具箱实现GA3Matlab编程实现GA例1:计算目标函数值函数()210sin(5)7cos(4)[0,10]max()?fxxxxxfx主程序%遗传算法主程序functionMy_GAglobalCmin;Cmin=-10^6;popsize=50;%群体大小Gene=20;chromlength=20;%字符串长度(个体长度)pc=0.8;%交叉概率pm=0.01;%变异概率Xmax=10;Xmin=0;Matlab编程实现GApop=initpop(popsize,chromlength);%随机产生初始群体fori=1:Gene%20为迭代次数[objvalue]=calobjvalue(pop,chromlength,Xmax,Xmin);%计算目标函数fitvalue=calfitvalue(objvalue);%计算群体中每个个体的适应度[newpop]=selection(pop,fitvalue);%复制[newpop]=crossover(newpop,pc);%交叉[newpop]=mutation(newpop,pm);%变异[bestindividual,bestfit]=best(pop,fitvalue);%求出群体中适应值最大的个体及其适应值x(i)=decodechrom(bestindividual,1,chromlength)*10/(2^chromlength-1);%最佳个体解码y(i)=bestfit+Cmin;%最佳个体适应度y_mean(i)=mean(fitvalue+Cmin);%第i代平均适应度pop=newpop;endfplot('2*x+10*sin(5*x)+7*cos(4*x)',[010])holdonplot(x,y,'r*')holdoff初始化(编码)%initpop.m函数的功能是实现群体的初始化,popsize表示群体的大小,chromlength表示染色体的长度(二值数的长度),%长度大小取决于变量的二进制编码的长度(在本例中取20位)。%Name:initpop.mfunctionpop=initpop(popsize,chromlength)pop=round(rand(popsize,chromlength));%rand随机产生每个单元为{0,1}行数为popsize,列数为chromlength的矩阵,%round对矩阵的每个单元进行取整。这样产生的初始种群。Matlab编程实现GA将二进制数转化为十进制数将二进制数转化为十进制数%产生[2^n2^(n-1)...1]的行向量,然后求和,将二进制转化为十进制functionpop2=decodebinary(pop)[px,py]=size(pop);%求pop行和列数fori=1:pypop1(:,i)=2.^(py-i).*pop(:,i);endpop2=sum(pop1,2);%求pop1的每行之和functionpop2=decodechrom(pop,spoint,length)pop1=pop(:,spoint:spoint+length-1);pop2=decodebinary(pop1);Matlab编程实现GA计算目标函数值计算目标函数值%calobjvalue.m函数的功能是实现目标函数的计算,其公式采用本文示例仿真,可根据不同优化问题予以修改。%遗传算法子程序%Name:calobjvalue.m%实现目标函数的计算function[objvalue]=calobjvalue(pop,chromlength,Xmax,Xmin)temp1=decodechrom(pop,1,chromlength);%将pop每行转化成十进制数x=temp1*(Xmax-Xmin)/(2^chromlength-1);%将十进制域中的数转化为变量域的数objvalue=2*x+10*sin(5*x)+7*cos(4*x);%计算目标函数值Matlab编程实现GA计算个体的适应值functionfitvalue=calfitvalue(objvalue)globalCmin;fitvalue=objvalue-Cmin;Matlab编程实现GA选择复制function[newpop]=selection(pop,fitvalue)%程序中采用赌轮盘选择法选择实现totalfit=sum(fitvalue);%求适应值之和fitvalue=fitvalue/totalfit;%单个个体被选择的概率fitvalue=cumsum(fitvalue);%如fitvalue=[1234],则cumsum(fitvalue)=[13610][px,py]=size(pop);ms=sort(rand(px,1));%从小到大排列fitin=1;newin=1;whilenewin=px%蒙特卡洛方法抽样if(ms(newin))fitvalue(fitin)newpop(newin,:)=pop(fitin,:);newin=newin+1;elsefitin=fitin+1;endendMatlab编程实现GA交叉function[newpop]=crossover(pop,pc)[px,py]=size(pop);newpop=ones(size(pop));fori=1:2:px-1if(randpc)cpoint=round(rand*py);newpop(i,:)=[pop(i,1:cpoint),pop(i+1,cpoint+1:py)];newpop(i+1,:)=[pop(i+1,1:cpoint),pop(i,cpoint+1:py)];elsenewpop(i,:)=pop(i);newpop(i+1,:)=pop(i+1);endendMatlab编程实现GA变异function[newpop]=mutation(pop,pm)[px,py]=size(pop);newpop=ones(size(pop));fori=1:pxif(randpm)mpoint=round(rand*py);ifmpoint=0mpoint=1;endnewpop(i,:)=pop(i,:);Matlab编程实现GAifany(newpop(i,mpoint))==0newpop(i,mpoint)=1;elsenewpop(i,mpoint)=0;endelsenewpop(i,:)=pop(i,:);endend求出群体中最大的适应值及其个体function[bestindividual,bestfit]…=best(pop,fitvalue)[px,py]=size(pop);bestindividual=pop(1,:);bestfit=fitvalue(1);fori=2:pxiffitvalue(i)bestfitbestindividual=pop(i,:);bestfit=fitvalue(i);endendMatlab编程实现GA或function[bestindividual,bestfit]=…best(pop,fitvalue)[bestfit,m_indx]=max(fitvalue);bestindividual=pop(m_indx,:);结果见My_GA.mMatlab编程实现GAMatlab函数调用实现GA•Matlab的GA函数[xfval]=ga(@fitnessfun,nvars,[],[],[],[],[],[],[],options);fitnessfcn—Fitnessfunctionnvars—NumberofvariablesfortheproblemAineq—MatrixforinequalityconstraintsBineq—VectorforinequalityconstraintsAeq—MatrixforequalityconstraintsBeq—VectorforequalityconstraintsLB—LowerboundonxUB—Upperboundonxnonlcon—NonlinearconstraintFunctionoptions—Optionsstructure2min()?sin()lnineqineqeqeqxfxAxBAxBaxbexxcMatlab函数调用实现GA•Matlab的GA函数x=ga(fitnessfcn,nvars)x=ga(fitnessfcn,nvars,A,b)x=ga(fitnessfcn,nvars,A,b,Aeq,beq)x=ga(fitnessfcn,nvars,A,b,Aeq,beq,LB,UB)x=ga(fitnessfcn,nvars,A,b,Aeq,beq,LB,UB,nonlcon)x=ga(fitnessfcn,nvars,A,b,Aeq,beq,LB,UB,nonlcon,options)x=ga(problem)[x,fval]=ga(...)[x,fval,exitflag]=ga(...)Matlab函数调用实现GA•GA函数调用步骤第一步:编写适应度函数;第二步:对GA参数进行设置;options=gaoptimset(‘参数名’,参数值,…,‘参数名’,参数值)例:options=gaoptimset('PopulationSize',100)第三步:调用GA函数;[xfval]=ga(@fitnessfun,nvars)[xfvalexitflagoutputpopulationscores]=ga(@fitnessfcn,nvars)[xfval]=ga(@fitnessfun,nvars,[],[],[],[],[],[],[],options);specifyanylinearequality,linearinequality,ornonlinearconstraintsMatlab函数调用实现GA•调用GA函数求例1的最大值(见ga_eg1.m)第一步:编写适应度函数;functiony=fit1(x)y=-(2*x+10*sin(5*x)+7*cos(4*x));第二步:设置参数options=gaoptimset('PopulationSize',100,'PopulationType','doubleVector','PlotFcns',{@gaplotbestf,@gaplotbestindiv,@gaplotexpectation,@gaplotstopping})第三步:调用GA函数[xfval]=ga(@fit1,1,[],[],[],[],0,10,[],options);最后还原y=-1*fit1(x)%最小值还原为最大值figure;fplot('2*x+10*sin(5*x)+7*cos(4*x)',[010])holdonplot(x,y,'*')遗传算法的GA工具箱实现:•Matlab的GA工具箱(R2007)•Matlab的GA工具箱(R2010b)遗传算法的GA工具箱实现:例二•求下列优化问题22121212121212(,)0.5*26min()?1121222130,0fxxxxxxxxfxxxxxMatlab函数调用实现•调用GA函数求解(见ga_eg2.m)functionga_eg2A=[11;-12;21];b=[2;2;3];lb=zeros(2,1);[x,fval,exitflag]=ga(@lincontest6,2,A,b,[],[],lb);mesh_lincontest6;holdon;