《人工鱼群算法源代码(5页).doc》由会员分享,可在线阅读,更多相关《人工鱼群算法源代码(5页).doc(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-人工鱼群算法源代码-第 5 页%人工鱼群算法formatlongVisual=2.5;Step=0.3;N=50;Try_number=50;a1=-10;b1=10;a2=-10;b2=10;d=;h=1e-1;Friend_number=50;k=0;m=50;X1=rand(N,1)*(b1-a1)+a1;X2=rand(N,1)*(b2-a2)+a2;X=X1X2;%人工鱼数量fori=1:Nwwww=X(i,1),X(i,2);d(i)=maxf(wwww);endw,i=max(d);maxX=X(i,1),X(i,2);%初始公告板记录maxY=w;%初始公告板记录figure
2、x=;figurey=;figurez=;figurex(numel(figurex)+1)=maxX(1);figurey(numel(figurey)+1)=maxX(2);figurez(numel(figurez)+1)=maxY;kkk=0;while(km)fori=1:NXX=X(i,1),X(i,2);%人工鱼当前状态Xinf=0;Xc=0;forj=1:N%聚群行为开始XXX=X(j,1),X(j,2);if(norm(XXX-XX)maxf(XX)XXnext1=XX+rand*Step*(Xc-XX)/norm(Xc-XX);if(XXnext1(1)b1)XXnext1
3、(1)=b1;endif(XXnext1(1)b2)XXnext1(2)=b2;endif(XXnext1(2)b1)XXnext1(1)=b1;endif(XXnext1(1)b2)XXnext1(2)=b2;endif(XXnext1(2)a2)XXnext1(2)=a2;endend%聚群行为结束%maxX=XX;%追尾行为开始%maxY=maxf(XX);forj=1:Friend_numberXXX=X(j,1),X(j,2);if(norm(XXX-XX)maxY)maxX=XXX;maxY=maxf(XXX);endendif(maxY)maxf(XX)XXnext2=XX+ra
4、nd*Step*(maxX-XX)/norm(maxX-XX);if(XXnext2(1)b1)XXnext2(1)=b1;endif(XXnext2(1)b2)XXnext2(2)=b2;endif(XXnext2(2)b1)XXnext2(1)=b1;endif(XXnext2(1)b2)XXnext2(2)=b2;endif(XXnext2(2)maxf(XXnext2)X(i,1)=XXnext1(1);X(i,2)=XXnext1(2);elseX(i,1)=XXnext2(1);X(i,2)=XXnext2(2);endend%一次迭代结束fori=1:NXXXX=X(i,1),X
5、(i,2);ifmaxf(XXXX)maxYmaxY=maxf(XXXX);maxX=XXXX;figurex(numel(figurex)+1)=maxX(1);figurey(numel(figurey)+1)=maxX(2);figurez(numel(figurez)+1)=maxY;endendk=k+1;%进入下一次迭代endmaxXmaxYplot3(figurex,figurey,figurez)functionXXnext=gmjprey(XX,Try_number,Visual,Step)pp=0;forj=1:Try_numberXXj=XX+rand*Step*Visual;if(maxf(XX)maxf(XXj)XXnext=XX+rand*Step*(XXj-XX)/norm(XXj-XX);pp=1;breakendendif(pp)XXnext=XX+rand*Step;endfunctiony=maxf(QQ)y=(sin(QQ(1)/QQ(1)*(sin(QQ(2)/QQ(2);