clear;
tic %开始记时
FishNum=10;%生成10只人工鱼
Max_gen=50;%最多迭代次数
trynumber=100;%最多试探次数
Visual=6;%感知距离
deta=0.8;% 拥挤度因子
CityPosition=[16.47,96.10;%从上到下,依次是14个城市的坐标,每一行一个城市。
16.47,94.44;
20.09,92.54;
22.39,93.37;
25.23,97.24;
22.00,96.05;
20.47,97.02;
17.20,96.29;
16.30,97.38;
14.05,98.12;
16.53,97.38;
21.52,95.59;
19.41,97.13;
20.09,92.55];
CityNum=length(CityPosition);%城市个数
DNAN=100; %同一个城市之间的距离定义为无穷大
for i=1:CityNum %算出每条边的长度
edge(i,i)=DNAN;
for j=i+1:CityNum
edge(i,j)=sqrt(sum((CityPosition(i,:)-CityPosition(j,:)).^2));
edge(j,i)=edge(i,j);
end
end
for i=1:FishNum %对所有人工鱼进行初始化
X(i,:)=Inital(CityNum);
end
Best=1000;
for NC=1:Max_gen
Besty(NC)=1000;
for i=1:FishNum
[Xi,flag1]=follow(X,FishNum,Visual,deta,i,edge);%首先偿试追尾
if(flag1==0) %追尾失败
Visual2=floor(Visual*(1-NC/Max_gen));
[Xi,flag2]=prey(X,CityNum,i,Visual,trynumber,edge);%再偿试觅食
if(flag2==0) %觅食失败
[Xi,flag3]=swarm(X,FishNum,CityNum,Visual,deta,i,edge);%再偿试聚群
if(flag3==0)%聚群失败
Xi=X(i,:);%无奈,静止不动,这是对算法的一个改进
end
end
end
X(i,:)=Xi;
Yi=evaluate(Xi,edge);
if(Yi<Besty(NC))
Besty(NC)=Yi;%每次最优解
end
if(Yi<Best)
Best=Yi; %全局最优解
Bestpath=Xi;
end
end
disp(['第',num2str(NC),'次迭代,得出的最优值:',num2str(Besty(NC))]);
end
t_train=toc;%结束记时
s=num2str(Bestpath(1));
for i=2:CityNum
s=strcat(s,'->');
s=strcat(s,num2str(Bestpath(i)));
end
s=strcat(s,'->');
s=strcat(s,num2str(Bestpath(1)));
disp(['得出的最优路径:',s,',最优值:',num2str(Best)]);
plot(Besty);
title(['本次运行得到的最优值为',num2str(Best),',共耗时:',num2str(t_train),'秒']);%x,
text(floor(Max_gen/2),44,'最优值随迭代次数变化曲线');
xlabel('迭代次数');
ylabel('最优值');
XC=[1,10,9,11,8,13,7,12,6,5,4,3,14,2];%你给的最优解
y=evaluate(XC,edge);
disp(['你给的最优解:',num2str(XC),' 1,最优值:',num2str(y)]);
disp('最优路径可能不只一条,我给的解可能和你的解刚好逆转,很显然也是对的。不知道你的30.8785是怎么来的(C++么?)');
bghnq.rar_belongbzn_人工鱼群_旅行商 鱼群_系统/网络安全_鱼群算法
版权申诉
40 浏览量
2022-07-15
13:53:53
上传
评论
收藏 3KB RAR 举报
weixin_42651887
- 粉丝: 79
- 资源: 1万+
最新资源
- 驱动代码驱动代码驱动代码驱动代码
- SVID_20240523_141155_1.mp4
- Code for the complete guide to tkinter tutorial
- 关于百货中心供应链管理系统.zip
- SimpleFolderIcon-master 修改Unity的Project下的文件夹图标
- A python Tkinter widget to display tile based maps
- A pure Python library for adding tables to a Tkinter application
- Vector资源文件.zip
- MobaXterm-Installer
- MicroMsg.xlsx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0