clear;
clc;
%% 参数设置
N = 20; %人工鱼的数量
Visual = 2.5; %人工鱼的感知距离
Step = 0.3; %人工鱼的移动最大步长
delta=0.618; %拥挤度因子
Try_number = 50; %最大试探次数
Max_iteration = 100; %最大迭代次数
dim =2;
%% 目标函数
f = @(x1,x2)sin(x1)./x1 .*sin(x2)./x2;
ub=10; %边界上限
lb=-10; %边界下限
%% 初始化人工鱼种群
X=lb+rand(N,dim).*(ub-lb);
for i = 1:N
fitness_fish(i) = f(X(i,1),X(i,2)); %计算每个人工鱼初始状态的适应度值;
end
[best_fitness,I] = max(fitness_fish); % 求出初始状态下的最优适应度;
best_x = X(I,:); % 最优人工鱼;
%% 行为选择
Iteration = 1;
while Iteration < Max_iteration
for i = 1:N
%% 聚群行为
nf_swarm=0;
Xc=0;
label_swarm =0; %群聚行为发生标志
%确定视野范围内的伙伴数目与中心位置
for j = 1:N
if norm(X(j,:)-X(i,:))<Visual
nf_swarm = nf_swarm+1; %统计在感知范围内的鱼数量
Xc = Xc+X(j,:); %将感知范围内的鱼进行累加
end
end
Xc=Xc-X(i,:); %需要去除本身;
nf_swarm=nf_swarm-1; %感知范围内的鱼数量
Xc = Xc/nf_swarm; %Xc表示视野范围其他伙伴的中心位置;
%判断中心位置是否拥挤
if (f(Xc(1,1),Xc(1,2))/nf_swarm > delta*f(X(i,1),X(i,2))) && (f(Xc(1,1),Xc(1,2))>f(X(i,1),X(i,2)))
X_swarm=X(i,:)+rand*Step.*(Xc-X(i,:))./norm(Xc-X(i,:));
%边界处理
ub_flag=X_swarm>ub;
lb_flag=X_swarm<lb;
X_swarm=(X_swarm.*(~(ub_flag+lb_flag)))+ub.*ub_flag+lb.*lb_flag;
X_swarm_fitness=f(X_swarm(1,1),X_swarm(1,2));
else
%觅食行为
label_prey =0; %判断觅食行为是否找到优于当前的状态
for j = 1:Try_number
%随机搜索一个状态
X_prey_rand = X(i,:)+Visual.*(-1+2.*rand(1,dim));
ub_flag2=X_prey_rand>ub;
lb_flag2=X_prey_rand<lb;
X_prey_rand=(X_prey_rand.*(~(ub_flag2+lb_flag2)))+ub.*ub_flag2+lb.*lb_flag2;
%判断搜索到的状态是否比原来的好
if f(X(i,1),X(i,2))<f(X_prey_rand(1,1),X_prey_rand(1,2))
X_swarm = X(i,:)+rand*Step.*(X_prey_rand-X(i,:))./norm(X_prey_rand-X(i,:));
ub_flag2=X_swarm>ub;
lb_flag2=X_swarm<lb;
X_swarm=(X_swarm.*(~(ub_flag2+lb_flag2)))+ub.*ub_flag2+lb.*lb_flag2;
X_swarm_fitness=f(X_swarm(1,1),X_swarm(1,2));
label_prey =1;
break;
end
end
%随机行为
if label_prey==0
X_swarm = X(i,:)+Step*(-1+2*rand(1,dim));
ub_flag2=X_swarm>ub;
lb_flag2=X_swarm<lb;
X_swarm=(X_swarm.*(~(ub_flag2+lb_flag2)))+ub.*ub_flag2+lb.*lb_flag2;
X_swarm_fitness=f(X_swarm(1,1),X_swarm(1,2));
end
end
%% 追尾行为
fitness_follow = -Inf;
label_follow =0;%追尾行为发生标记
%搜索人工鱼Xi视野范围内的最高适应度个体Xj
for j = 1:N
if (norm(X(j,:)-X(i,:))<Visual) && (f(X(j,1),X(j,2)) > fitness_follow)
X_best = X(j,:);
fitness_follow = f(X(j,1),X(j,2));
end
end
%搜索人工鱼Xj视野范围内的伙伴数量
nf_follow=0;
for j = 1:N
if norm(X(j,:) - X_best) < Visual
nf_follow=nf_follow+1;
end
end
nf_follow=nf_follow-1;%去掉本身
%判断人工鱼Xj位置是否拥挤
if (fitness_follow/nf_follow) > delta*f(X(i,1),X(i,2)) && (fitness_follow > f(X(i,1),X(i,2)))
X_follow = X(i,:)+rand*Step.*(X_best-X(i,:))./norm(X_best-X(i,:));
%边界判定
ub_flag2=X_follow>ub;
lb_flag2=X_follow<lb;
X_follow=(X_follow.*(~(ub_flag2+lb_flag2)))+ub.*ub_flag2+lb.*lb_flag2;
label_follow =1;
X_follow_fitness=f(X_follow(1,1),X_follow(1,2));
else
%觅食行为
label_prey =0; %判断觅食行为是否找到优于当前的状态
for j = 1:Try_number
%随机搜索一个状态
X_prey_rand = X(i,:)+Visual.*(-1+2.*rand(1,dim));
ub_flag2=X_prey_rand>ub;
lb_flag2=X_prey_rand<lb;
X_prey_rand=(X_prey_rand.*(~(ub_flag2+lb_flag2)))+ub.*ub_flag2+lb.*lb_flag2;
%判断搜索到的状态是否比原来的好
if f(X(i,1),X(i,2)) < f(X_prey_rand(1,1),X_prey_rand(1,2))
X_follow = X(i,:)+rand*Step.*(X_prey_rand-X(i,:))./norm(X_prey_rand-X(i,:));
ub_flag2=X_follow>ub;
lb_flag2=X_follow<lb;
X_follow=(X_follow.*(~(ub_flag2+lb_flag2)))+ub.*ub_flag2+lb.*lb_flag2;
X_follow_fitness=f(X_follow(1,1),X_follow(1,2));
label_prey =1;
break;
end
end
%随机行为
if label_prey==0
X_follow = X(i,:)+Step*(-1+2*rand(1,dim));
ub_flag2=X_follow>ub;
lb_flag2=X_follow<lb;
X_follow=(X_follow.*(~(ub_flag2+lb_flag2)))+ub.*ub_flag2+lb.*lb_flag2;
X_follow_fitness=f(X_follow(1,1),X_follow(1,2));
end
end
% 两种行为找最优
if X_follow_fitness > X_swarm_fitness
X(i,:)=X_follow;
else
X(i,:)=X_swarm;
end
end
%% 更新信息
for i = 1:N
if (f(X(i,1),X(i,2)) > best_fitness)
best_fitness = f(X(i,1),X(i,2));
best_x = X(i,:);
end
end
Convergence_curve(Iteration)=best_fitness;
Iteration = Iteration+1;
if mod(Iteration,1)==0
display(['迭代次数:',num2str(Iteration),'最优适应度:',num2str(best_fitness)]);
display(['最优人工鱼:',num2str(best_x)]);
end
end
%% 目标函数图
subplot(1,2,1);
[X,Y] = meshgrid(-10:0.1:10);
Z = (sin(X)./X).*(sin(Y)./Y);
mesh(X,Y,Z);
xlabel('第一维度');
ylabel('第二维度');
zlabel('函数值');
title('目标函数状态图');
%% 迭代收敛曲线
subplot(1,2,2);
semilogy(Convergence_curve,'Color','b')
title('Convergence curve')
xlabel('Iteration');
ylabel('Best fitness');
axis tight
grid off
box on