第七章遗传算法应用举例首先要感谢雷英杰老师,为我们提供了学习遗传算法的好教材,下面的程序全部是雷老师第七章的例子,本人对书中的一些问题作了修正,保证可以运行,不足之处请大家谅解,仅供大家交流学习!7.1简单一元函数优化实例figure(1);fplot('variable.*sin(10*pi*variable)+2.0',[-1,2]);NIND=40;MAXGEN=25;PRECI=20;GGAP=0.9;trace=zeros(2,MAXGEN);FieldD=[20;-1;2;1;0;1;1];Chrom=crtbp(NIND,PRECI);gen=0;variable=bs2rv(Chrom,FieldD);ObjV=variable.*sin(10*pi*variable)+2.0;whilegenMAXGEN,FitnV=ranking(-ObjV);SelCh=select('sus',Chrom,FitnV,GGAP);SelCh=recombin('xovsp',SelCh,0.7);SelCh=mut(SelCh);variable=bs2rv(SelCh,FieldD);ObjVSel=variable.*sin(10*pi*variable)+2.0;[ChromObjV]=reins(Chrom,SelCh,1,1,ObjV,ObjVSel);variable=bs2rv(Chrom,FieldD);gen=gen+1;[Y,I]=max(ObjV),holdon;plot(variable(I),Y,'bo');trace(1,gen)=max(ObjV);trace(2,gen)=sum(ObjV)/length(ObjV);endvariable=bs2rv(Chrom,FieldD);holdon,gridplot(variable',ObjV','b*');figure(2);plot(trace(1,:)');holdon;plot(trace(2,:)','-.');grid;legend('½âµÄ±ä»¯','ÖÖȺ¾ùÖµµÄ±ä»¯')7.2多元单峰函数优化实例%¶¨ÒåÒÅ´«Ëã·¨²ÎÊýNIND=40;MAXGEN=500;NVAR=20;PRECI=20;GGAP=0.9;trace=zeros(MAXGEN,2);%½¨Á¢ÇøÓòÃèÊöÆ÷FieldD=[rep([PRECI],[1NVAR]);rep([-512;512],[1NVAR]);rep([1;0;1;1],[1NVAR])];Chrom=crtbp(NIND,NVAR*PRECI);gen=0;ObjV=objfun1(bs2rv(Chrom,FieldD));whilegenMAXGEN,FitnV=ranking(ObjV);SelCh=select('sus',Chrom,FitnV,GGAP);SelCh=recombin('xovsp',SelCh,0.7);SelCh=mut(SelCh);ObjVSel=objfun1(bs2rv(SelCh,FieldD));[ChromObjV]=reins(Chrom,SelCh,1,1,ObjV,ObjVSel);gen=gen+1;%Êä³ö×îÓŽ⼰Æä¶ÔÓ¦µÄ20¸ö×Ô±äÁ¿µÄÊ®½øÖÆÖµ£¬YΪ×îÓŽ⣬IΪÖÖȺµÄÐòºÅtrace(gen,1)=min(ObjV);trace(gen,2)=sum(ObjV)/length(ObjV);endplot(trace(:,1));holdon;plot(trace(:,2),'-.');grid;legend('ÖÖȺ¾ùÖµµÄ±ä»¯','½âµÄ±ä»¯')7.3多元多峰函数优化实例functionz=Shubert(x,y)z=((1*cos((1+1)*x+1))+(2*cos((2+1)*x+2))+(3*cos((3+1)*x+3))...+(4*cos((4+1)*x+4))+(5*cos((5+1)*x+5)))*((1*cos((1+1)*y+1))...+(2*cos((2+1)*y+2))+(3*cos((3+1)*y+3))+(4*cos((4+1)*y+4))...+(5*cos((5+1)*y+5)));%[x1,x2]=meshgrid(-10:0.1:10);%figure(1);%Shubert(x1,x2))NIND=40;MAXGEN=50;NVAR=2;PRECI=25;GGAP=0.9;FieldD=[rep([PRECI],[1NVAR]);rep([-3;3],[1NVAR]);rep([1;0;1;1],[1NVAR])];Chrom=crtbp(NIND,NVAR*PRECI);gen=0;trace=zeros(MAXGEN,2);x=bs2rv(Chrom,FieldD);A=rep(x,[120]);B=rep(x(:,2),[140]);C=Shubert(A,B)ObjV=C(:,1)whilegenMAXGEN,FitnV=ranking(ObjV);SelCh=select('sus',Chrom,FitnV,GGAP);SelCh=recombin('xovsp',SelCh,0.7);SelCh=mut(SelCh);x=bs2rv(SelCh,FieldD);A=rep(x,[118]);B=rep(x(:,2),[118]);C=Shubert(A,B)ObjVSel=C(:,1)[ChromObjV]=reins(Chrom,SelCh,1,1,ObjV,ObjVSel);gen=gen+1;[YI]=min(ObjVSel);if(gen==50)figure(2);plot(ObjV);holdon;plot(ObjV,'b*');gridendendfigure(3);clfplot(trace(:,1));holdon;plot(trace(:,2),'-.');grid;legend('½âµÄ±ä»¯','ÖÖȺ¾ùÖµµÄ±ä»¯')7.4收获系统最优控制NVAR=20;RANGE=[0;200];GGAP=0.8;XOVR=1;MUTR=1/NVAR;MAXGEN=500;INSR=0.9;SUBPOP=8;MIGR=0.2;MIGGEN=20;NIND=20;SEL_F='sus';XOV_F='recdis';MUT_F='mutbga';OBJ_F='objharv';FieldDD=rep(RANGE,[1,NVAR]);gen=0;trace=zeros(MAXGEN,2);Chrom=crtbp(SUBPOP*NIND,FieldDD);ObjV=objharv(Chrom);whilegenMAXGEN,FitnV=ranking(ObjV,[21],SUBPOP);SelCh=select(SEL_F,Chrom,FitnV,GGAP,SUBPOP);SelCh=recombin(XOV_F,SelCh,XOVR,SUBPOP);SelCh=mutate(MUT_F,SelCh,FieldDD,[MUTR],SUBPOP);ObjVOff=feval(OBJ_F,SelCh);[Chrom,ObjV]=reins(Chrom,SelCh,SUBPOP,[1INSR],ObjV,ObjVOff);gen=gen+1;[trace(gen,1),I]=min(ObjV);trace(gen,2)=mean(ObjV);if(rem(gen,MIGGEN)==0)[Chrom,ObjV]=migrate(Chrom,SUBPOP,[MIGR,1,1],ObjV);endend[YI]=min(ObjV);figure(1);plot(Chrom(I,:));holdon;grid;figure(2);plot(-trace(:,1));holdon;plot(-trace(:,2),'-');legend('½âµÄ±ä»¯','ÖÖȺ¾ùÖµµÄ±ä»¯');xlabel('µü´ú´ÎÊý')%×¢ÊÍ£º1¡¢º¬ÓÐ×ÓÖÖȺµÄÔÚ¼ÆËãChromʱҪ°ÑSUBPOP³ËÉÏ%2¡¢¡®¡¯²»ÊǶԱßÓõģ¬¼ÓÓë²»¼Ó²î±ð¾Þ´ó£¬Èç¡®XOV_F¡¯,»áÒÔºì×ÖÏÔʾ¡£7.5装载系统最优问题NVAR=20;RANGE=[0;10];GGAP=0.8;XOVR=1;MUTR=1/NVAR;MAXGEN=200;INSR=0.9;SUBPOP=12;MIGR=0.2;MIGGEN=20;NIND=20;SEL_F='sus';XOV_F='recdis';MUT_F='mutbga';OBJ_F='objpush';FieldDD=rep(RANGE,[1,NVAR]);gen=0;trace=zeros(MAXGEN,2);Chrom=crtbp(SUBPOP*NIND,FieldDD);ObjV=feval(OBJ_F,Chrom);whilegenMAXGEN,FitnV=ranking(ObjV,[20],SUBPOP);SelCh=select(SEL_F,Chrom,FitnV,GGAP,SUBPOP);SelCh=recombin(XOV_F,SelCh,XOVR,SUBPOP);SelCh=mutate(MUT_F,SelCh,FieldDD,[MUTR],SUBPOP);ObjVOff=feval(OBJ_F,SelCh);[Chrom,ObjV]=reins(Chrom,SelCh,SUBPOP,[1INSR],ObjV,ObjVOff);gen=gen+1;[trace(gen,1),I]=min(ObjV);trace(gen,2)=mean(ObjV);end[YI]=min(ObjV);subplot(211);plot(Chrom(I,:));holdon;plot(Chrom(I,:),'.');gridsubplot(212);plot(trace(:,1));holdon;plot(trace(:,2),'-');grid;legend('½âµÄ±ä»¯','ÖÖȺ¾ùÖµµÄ±ä»¯');%×¢Ò⣺1¡¢¾ùֵһֱΪ0£¬ÉÙÌõ¼þ»òÕß²ÎÊý¶¨Òå´íÎó%2¡¢subplotº¯ÊýµÄÓ÷¨£¬²»Óüӿոñ7.6离散二次线性系统最优控制问题NVAR=45;%RANGE=[0;10];GGAP=0.8;XOVR=1;MUTR=1/NVAR;MAXGEN=2000;INSR=0.9;SUBPOP=12;MIGR=0.2;MIGGEN=20;NIND=20;SEL_F='sus';XOV_F='recdis';MUT_F='mutbga';OBJ_F='objlinq';FieldDR=feval(OBJ_F,[],1);%FieldDD=rep(RANGE,[1,NVAR]);gen=0;trace=zeros(MAXGEN,2);Chrom=crtrp(SUBPOP*NIND,FieldDR);ObjV=feval(OBJ_F,Chrom);whilegenMAXGEN,trace(gen+1,1)=min(ObjV);trace(gen+1,2)=mean(ObjV);FitnV=ranking(ObjV,[20],SUBPOP);SelCh=select(SEL_F,Chrom,FitnV,GGAP,SUBPOP);SelCh=recombin(XOV_F,SelCh,XOVR,SUBPOP);SelCh=mutate(MUT_F,SelCh,FieldDR,[MUTR],SUBPOP