人工鱼群算法源代码.docx
《人工鱼群算法源代码.docx》由会员分享,可在线阅读,更多相关《人工鱼群算法源代码.docx(7页珍藏版)》请在冰豆网上搜索。
![人工鱼群算法源代码.docx](https://file1.bdocx.com/fileroot1/2022-10/7/c8084920-b574-4763-8fca-c1e7ff2092f6/c8084920-b574-4763-8fca-c1e7ff2092f61.gif)
人工鱼群算法源代码
%人工鱼群算法;formatlong;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)
%人工鱼群算法
formatlong
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];%人工鱼数量
fori=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(kfori=1:
N
XX=[X(i,1),X(i,2)];%人工鱼当前状态Xi
nf=0;
Xc=0;
forj=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);
forj=1:
Friend_number
XXX=[X(j,1),X(j,2)];
if(norm(XXX-XX)maxY)maxX=XXX;
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%一次迭代结束
fori=1:
N
XXXX=[X(i,1),X(i,2)];
ifmaxf(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;%进入下一次迭代
end
maxX
maxY
plot3(figurex,figurey,figurez)
function[XXnext]=gmjprey(XX,Try_number,Visual,Step)pp=0;
forj=1:
Try_number
XXj=XX+rand*Step*Visual;
if(maxf(XX)XXnext=XX+rand*Step*(XXj-XX)/norm(XXj-XX);pp=1;
break
end
end
if(~pp)
XXnext=XX+rand*Step;
end
functiony=maxf(QQ)
y=(sin(QQ
(1))/QQ
(1))*(sin(QQ
(2))/QQ
(2));
人工鱼群算法的仿真程序-matlab23
tic;figure
(1);holdon;ezplot('x*sin(10*pi*;%%参数设置;fishnum=50;%生成50只人工鱼;MAXGEN=50;%最多迭代次数;try_number=100;%最多试探次数;visual=1;%感知距离;delta=0.618;%拥挤度因子;step=0.1;%步长;%%初始化鱼群;lb_ub=[-1,
tic
figure
(1);holdon
ezplot('x*sin(10*pi*x)+2',[-1,2]);
%%参数设置
fishnum=50;%生成50只人工鱼
MAXGEN=50;%最多迭代次数
try_number=100;%最多试探次数
visual=1;%感知距离
delta=0.618;%拥挤度因子
step=0.1;%步长
%%初始化鱼群
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_ub(i,3),1)];
end
gen=1;
BestY=-1*ones(1,MAXGEN);%每步中最优的函数值
BestX=-1*ones(1,MAXGEN);%每步中最优的自变量
besty=-100;%最优函数值
Y=AF_foodconsistence(X);
whilegen<=MAXGEN
fprintf(1,'%d\n',gen)
fori=1:
fishnum
%%聚群行为
[Xi1,Yi1]=AF_swarm(X,i,visual,step,delta,try_number,LBUB,Y);%%追尾行为
[Xi2,Yi2]=AF_follow(X,i,visual,step,delta,try_number,LBUB,Y);ifYi1>Yi2
X(:
i)=Xi1;
Y(1,i)=Yi1;
else
X(:
i)=Xi2;
Y(1,i)=Yi2;
end
end
[Ymax,index]=max(Y);
figure
(1);
plot(X(1,index),Ymax,'.','color',[gen/MAXGEN,0,0])
ifYmax>besty
besty=Ymax;
bestx=X(:
index);
BestY(gen)=Ymax;
[BestX(:
gen)]=X(:
index);else
BestY(gen)=BestY(gen-1);
[BestX(:
gen)]=BestX(:
gen-1);end
gen=gen+1;
end
plot(bestx
(1),besty,'ro','MarkerSize',100)xlabel('x')
ylabel('y')
title('鱼群算法迭代过程中最优坐标移动')
%%优化过程图
figure
plot(1:
MAXGEN,BestY)
xlabel('迭代次数')
ylabel('优化值')
title('鱼群算法迭代过程')
disp(['最优解X:
',num2str(bestx,'%1.5f')])disp(['最优解Y:
',num2str(besty,'%1.5f')])toc