资源描述:
《遗传蚁群作业》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、二.遗传算法:%GAmainprogram%EditedbyTan%Revision:1.1%Date:2013/07/07%n----ÖÖȺ¹æÄ£%ger----µü´ú´ÎÊý%pc----½»²æ¸ÅÂÊ%pm----±äÒì¸ÅÂÊ%v----³õʼÖÖȺ£¨¹æģΪn£©%f----Ä¿±êº¯ÊýÖµ%fit----ÊÊÓ¦¶ÈÏòÁ¿%vx----×îÓÅÊÊÓ¦¶ÈÖµÏòÁ¿%vmfit----ƽ¾ùÊÊÓ¦¶ÈÖµÏòÁ¿clearall;closeall;clc;tic;n=30;ger=200;pc=0.65;pm=0.05;%É
2、ú³É³õʼÖÖȺv=init_population(n,50);[N,L]=size(v);disp(sprintf('Numberofgenerations:%d',ger));disp(sprintf('Populationsize:%d',N));disp(sprintf('Crossoverprobability:%.3f',pc));disp(sprintf('Mutationprobability:%.3f',pm));%´ýÓÅ»¯ÎÊÌâxmin=-5;xmax=5;ymin=-5;ymax=5;f='-(2-exp(-(x.^2+y.^2)))
3、';[x,y]=meshgrid(xmin:0.1:xmax,ymin:0.1:ymax);vxp=x;vyp=y;vzp=eval(f);figure(1);mesh(vxp,vyp,-vzp);holdon;gridon;%¼ÆËãÊÊÓ¦¶È£¬²¢»³ö³õʼÖÖȺͼÐÎx=decode(v(:,1:25),xmin,xmax);y=decode(v(:,26:50),ymin,ymax);fit=eval(f);plot3(x,y,-fit,'k*');title('(a)ȾɫÌåµÄ³õʼλÖÃ');xlabel('x');ylabel('y
4、');zlabel('f(x,y)');%µü´úÇ°µÄ³õʼ»¯vmfit=[];vx=[];it=1;%µü´ú¼ÆÊýÆ÷%¿ªÊ¼½ø»¯whileit<=ger%Reproduction(Bi-classistSelection)vtemp=roulette(v,fit);%Crossoverv=crossover(vtemp,pc);%MutationM=rand(N,L)<=pm;%M(1,:)=zeros(1,L);v=v-2.*(v.*M)+M;%Resultsx=decode(v(:,1:25),xmin,xmax);y=decode(v(:,
5、26:50),ymin,ymax);fit=eval(f);[sol,indb]=max(fit);%ÿ´Îµü´úÖÐ×îÓÅÄ¿±êº¯ÊýÖµv(1,:)=v(indb,:);fit_mean=mean(fit);%ÿ´Îµü´úÖÐÄ¿±êº¯ÊýÖµµÄƽ¾ùÖµvx=[vxsol];vmfit=[vmfitfit_mean];it=it+1;end%%%%×îºó½á¹ûdisp(sprintf(''));%¿ÕÒ»ÐÐ%ÏÔʾ×îÓŽ⼰×îÓÅÖµdisp(sprintf('Maximumfound[x,f(x)]:[%.4f,%.4f
6、,%.4f]',x(indb),y(indb),-sol));%ͼÐÎÏÔʾ×îÓŽá¹ûfigure(2);mesh(vxp,vyp,-vzp);holdon;gridon;plot3(x,y,-fit,'r*');title('ȾɫÌåµÄ×îÖÕλÖÃ');xlabel('x');ylabel('y');zlabel('f(x,y)');%ͼÐÎÏÔʾ×îÓż°Æ½¾ùº¯ÊýÖµ±ä»¯Ç÷ÊÆfigure(3);plot(-vx);%title('×îÓÅ,ƽ¾ùº¯ÊýÖµ±ä»¯Ç÷ÊÆ');xlabel('Generations')
7、;ylabel('f(x)');holdon;plot(-vmfit,'r');holdoff;runtime=toc蚁群算法%Antmainprogramclearall;closeall;clc;tic;Ant=200;Ger=50;xmin=-5;xmax=5;ymin=-5;ymax=5;tcl=0.1;f='2-exp(-(x.^2+y.^2))';%´ýÓÅ»¯µÄÄ¿±êº¯Êý[x,y]=meshgrid(xmin:tcl:xmax,ymin:tcl:ymax);vxp=x;vyp=y;vzp=eval(f);figure(1);me