人工鱼群算法源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
%人工鱼群算法
format long
Visual=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];%
人工鱼数量
for i=1:N
wwww=[X(i,1),X(i,2)];
d(i)=maxf(wwww);
end
[w,i]=max(d);
maxX=[X(i,1),X(i,2)];%
初始公告板记录
maxY=w;%
初始公告板记录
figurex=[];
figurey=[];
figurez=[];
figurex(numel(figurex)+1)=maxX(1); figurey(numel(figurey)+1)=maxX(2); figurez(numel(figurez)+1)=maxY; kkk=0;
while(k for i=1:N XX=[X(i,1),X(i,2)];%人工鱼当前状态Xi nf=0; Xc=0; for j=1:N%聚群行为开始 XXX=[X(j,1),X(j,2)]; if(norm(XXX-XX) nf=nf+1; Xc=Xc+XXX; end end Xc=Xc/nf; if((maxf(Xc))>maxf(XX)) XXnext1=XX+rand*Step*(Xc-XX)/norm(Xc-XX); if(XXnext1(1)>b1) XXnext1(1)=b1; end if(XXnext1(1) XXnext1(1)=a1; end if(XXnext1(2)>b2) XXnext1(2)=b2; end if(XXnext1(2) XXnext1(2)=a2; end else XXnext1=gmjprey(XX,Try_number,Visual,Step); if(XXnext1(1)>b1) XXnext1(1)=b1; end if(XXnext1(1) XXnext1(1)=a1; end if(XXnext1(2)>b2) XXnext1(2)=b2; end if(XXnext1(2) XXnext1(2)=a2; end end%聚群行为结束 %maxX=XX;%追尾行为开始 %maxY=maxf(XX); for j=1:Friend_number XXX=[X(j,1),X(j,2)]; if(norm(XXX-XX) maxY=maxf(XXX); end end if((maxY)>maxf(XX)) XXnext2=XX+rand*Step*(maxX-XX)/norm(maxX-XX); if(XXnext2(1)>b1) XXnext2(1)=b1; end if(XXnext2(1) XXnext2(1)=a1; end if(XXnext2(2)>b2) XXnext2(2)=b2; end if(XXnext2(2) XXnext2(2)=a2; end else XXnext2=gmjprey(XX,Try_number,Visual,Step); if(XXnext2(1)>b1) XXnext2(1)=b1; end if(XXnext2(1) XXnext2(1)=a1; end if(XXnext2(2)>b2) XXnext2(2)=b2; end if(XXnext2(2) XXnext2(2)=a2; end end%追尾行为结束 if(maxf(XXnext1)>maxf(XXnext2)) X(i,1)=XXnext1(1); X(i,2)=XXnext1(2); else X(i,1)=XXnext2(1); X(i,2)=XXnext2(2); end end%一次迭代结束 for i=1:N XXXX=[X(i,1),X(i,2)]; if maxf(XXXX)>maxY maxY=maxf(XXXX); maxX=XXXX; figurex(numel(figurex)+1)=maxX(1); figurey(numel(figurey)+1)=maxX(2); figurez(numel(figurez)+1)=maxY; end end k=k+1;%进入下一次迭代