1、%人工鱼数量for i=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;figurex=;figurey=;figurez=;figurex(numel(figurex)+1)=maxX(1);figurey(numel(figurey)+1)=maxX(2);figurez(numel(figurez)+1)=maxY;kkk=0;while(km)XX=X(i,1),X(i,2);%人工鱼当前状态Xinf=0;Xc=0;for j=1:N %聚群行为开始XXX=X(j
2、,1),X(j,2);if(norm(XXX-XX)maxf(XX)XXnext1=XX+rand*Step*(Xc-XX)/norm(Xc-XX); if(XXnext1(1)b1)XXnext1(1)=b1;if(XXnext1(1)b2)XXnext1(2)=b2;if(XXnext1(2)maxY) maxX=XXX;maxY=maxf(XXX);if(maxY)XXnext2=XX+rand*Step*(maxX-XX)/norm(maxX-XX); if(XXnext2(1)XXnext2(1)=b1;if(XXnext2(1)XXnext2(2)=b2;if(XXnext2(2)
3、maxf(XXnext2)X(i,1)=XXnext1(1);X(i,2)=XXnext1(2);X(i,1)=XXnext2(1);X(i,2)=XXnext2(2);end %一次迭代结束XXXX=X(i,1),X(i,2);if maxf(XXXX)maxYmaxY=maxf(XXXX);maxX=XXXX; figurey(numel(figurey)+1)=maxX(2); figurez(numel(figurez)+1)=maxY;k=k+1; %进入下一次迭代maxXplot3(figurex,figurey,figurez)function XXnext = gmjprey(
4、XX,Try_number,Visual,Step) pp=0;Try_numberXXj=XX+rand*Step*Visual;if(maxf(XX)maxf(XXj)XXnext=XX+rand*Step*(XXj-XX)/norm(XXj-XX); pp=1;breakif(pp)XXnext=XX+rand*Step;function y = maxf(QQ)y=(sin(QQ(1)/QQ(1)*(sin(QQ(2)/QQ(2);人工鱼群算法的仿真程序-matlab23tic;figure(1);holdon;ezplot(x*sin(10*pi*;%参数设置;fishnum=50;
5、%生成50只人工鱼;MAXGEN=50;%最多迭代次数;try_number=100;%最多试探次数;visual=1;%感知距离;delta=0.618;%拥挤度因子;step=0.1;%步长;%初始化鱼群;lb_ub=-1,tichold onx*sin(10*pi*x)+2,-1,2);% 参数设置 %生成50只人工鱼 %最多迭代次数%最多试探次数 %感知距离 %拥挤度因子 %步长% 初始化鱼群lb_ub=-1,2,1;X=AF_init(fishnum,lb_ub);LBUB=;fori=1:size(lb_ub,1)LBUB=LBUB;repmat(lb_ub(i,1:2),lb_u
6、b(i,3),1);gen=1;BestY=-1*ones(1,MAXGEN); %每步中最优的函数值BestX=-1*ones(1,MAXGEN); %每步中最优的自变量besty=-100; %最优函数值Y=AF_foodconsistence(X);while genYi2X(:,i)=Xi1;Y(1,i)=Yi1;,i)=Xi2;Y(1,i)=Yi2;Ymax,index=max(Y);plot(X(1,index),Ymax,.,color,gen/MAXGEN,0,0)ifYmaxbestybesty=Ymax;bestx=X(:,index);BestY(gen)=Ymax;BestX(:,gen)=X(: elseBestY(gen)=BestY(gen-1);,gen)=BestX(:,gen-1); endgen=gen+1;plot(bestx(1),besty,roMarkerSize,100) xlabel(x)ylabel(ytitle(鱼群算法迭代过程中最优坐标移动% 优化过程图figureplot(1:MAXGEN,BestY)xlabel(迭代次数优化值鱼群算法迭代过程disp(最优解X:,num2str(bestx,%1.5f) disp(最优解Y:,num2str(besty,) toc