%蚁群算法寻找最短路径程序
%% 清空环境变量
clear
clc
%% 导入数据,导入方式,看个人习惯
zuobiao=[1304 2312
3639 1315
4177 2244
3712 1399
3488 1535
3326 1556
3238 1229
4196 1004
4312 790
4386 570
3007 1970
2562 1756
2788 1491
2381 1676
1332 695
3715 1678
3918 2179
4061 2370
3780 2212
3676 2578
4029 2838
4263 2931
3429 1908
3507 2367
3394 2643
3439 3201
2935 3240
3140 3550
2545 2357
2778 2826
2370 2975];
%% 计算城市间相互距离
n = size(zuobiao,1);%城市个数
jl = zeros(n,n);%首先求得各个坐标点的距离,这里是矩阵初始化
for i = 1:n
for j = 1:n
if i ~= j %~=是不等于的意思,zuobiao矩阵中每行都有个坐标,坐标相减用i和j区分不同的坐标点,然后求两点距离
jl(i,j) = sqrt(sum((zuobiao(i,:) - zuobiao(j,:)).^2));
%上式运算如a=[2,2;1,1]=>a(1,:)-a(2,:)=>ans =1 1,然后1?+1?=2,最后开根号
else
jl(i,j) = 1e-4;%相等的点相减准确说是等于0的,这里设置成了一个很小的数,是为了避免后面程序运算出错
end
end
end
%% 初始化参数
m = 50; % 蚂蚁数量,视情况而定,坐标点多的话可以适当增加蚂蚁数量
a= 1; % 信息素重要程度因子
b= 5; % 启发函数重要程度因子
r = 0.1; % 信息素挥发因子
Q = 1; % 常数
qfhs = 1./jl; % 启发函数,将jl矩阵中每个元素转化为倒数
xxsjz = ones(n,n); % 信息素矩阵初始化
ljjl = zeros(m,n); % 路径记录表矩阵初始化
ddcs = 1; % 迭代次数初值
ddcs_max = 200; % 最大迭代次数
Lujin_best = zeros(ddcs_max,n); % 各代最佳路径
L_best = zeros(ddcs_max,1); % 各代最佳路径的长度
L_ave = zeros(ddcs_max,1); % 各代路径的平均长度
%% 迭代寻找最佳路径
while ddcs <= ddcs_max%在ddcs小于ddcs_max前,一直循环
%% 随机产生各个蚂蚁的起点
start = zeros(m,1);
for i = 1:m
temp = randperm(n);%功能是随机打乱一个数字序列,也就是现将坐标点排号再打乱,相当于将蚂蚁随机分布在各个地点
start(i) = temp(1);
end
ljjl(:,1) = start;
%% 构建解空间
zuobiao_index = 1:n;
% 逐个蚂蚁路径选择
for i = 1:m
% 逐个地点路径选择
for j = 2:n
yfw = ljjl(i,1:(j - 1)); % 已访问的地点集合(禁忌表)
allow_index = ~ismember(zuobiao_index,yfw);%ismember用于判断矩阵某个元素是否存在,用法详见后文函数讲解
allow = zuobiao_index(allow_index); % 待访问的城市集合
P = allow;
% 计算城市间转移概率
for k = 1:length(allow)
P(k) = xxsjz(yfw(end),allow(k))^a * qfhs(yfw(end),allow(k))^b;%见文中公式
end
P = P/sum(P);
% 选择下一个访问城市
Plj = cumsum(P); %cumsum函数用于累加,具体用法详见后文函数讲解
yidong_index = find(Plj >= rand);
yidong = allow(yidong_index(1));
ljjl(i,j) = yidong;
end
end
% 计算各个蚂蚁的路径距离
L = zeros(m,1);
for i = 1:m
Lujin = ljjl(i,:);
for j = 1:(n - 1)
L(i) = L(i) + jl(Lujin(j),Lujin(j + 1));
end
L(i) = L(i) + jl(Lujin(n),Lujin(1));
end
% 计算最短路径距离及平均距离
if ddcs == 1
[min_L,min_index] = min(L);
L_best(ddcs) = min_L;
L_ave(ddcs) = mean(L);
Lujin_best(ddcs,:) = ljjl(min_index,:);
else
[min_L,min_index] = min(L);
L_best(ddcs) = min(L_best(ddcs - 1),min_L);
L_ave(ddcs) = mean(L);
if L_best(ddcs) == min_L
Lujin_best(ddcs,:) = ljjl(min_index,:);
else
Lujin_best(ddcs,:) = Lujin_best((ddcs-1),:);
end
end
%% 更新信息素
S = zeros(n,n);
% 逐个蚂蚁计算
for i = 1:m
% 逐个城市计算
for j = 1:(n - 1)
S(ljjl(i,j),ljjl(i,j+1)) = S(ljjl(i,j),ljjl(i,j+1)) + Q/L(i);
end
S(ljjl(i,n),ljjl(i,1)) = S(ljjl(i,n),ljjl(i,1)) + Q/L(i);
end
xxsjz = (1-r) * xxsjz + S;
% 迭代次数加1,清空路径记录表
ddcs = ddcs + 1;
ljjl = zeros(m,n);
end
%% 结果显示
[Shortest_L,index] = min(L_best);
Shortest_Lujin = Lujin_best(index,:);
disp(['最短距离:' num2str(Shortest_L)]);
disp(['最短路径:' num2str([Shortest_Lujin Shortest_Lujin(1)])]);
%% 绘图
figure(1)
plot([zuobiao(Shortest_Lujin,1);zuobiao(Shortest_Lujin(1),1)],...
[zuobiao(Shortest_Lujin,2);zuobiao(Shortest_Lujin(1),2)],'o-');
grid on
for i = 1:size(zuobiao,1)
text(zuobiao(i,1),zuobiao(i,2),[' ' num2str(i)]);
end
text(zuobiao(Shortest_Lujin(1),1),zuobiao(Shortest_Lujin(1),2),' 起点');
text(zuobiao(Shortest_Lujin(end),1),zuobiao(Shortest_Lujin(end),2),' 终点');
xlabel('城市位置横坐标')
ylabel('城市位置纵坐标')
title(['蚁群算法优化路径(最短距离:' num2str(Shortest_L) ')'])
figure(2)
plot(1:ddcs_max,L_best,'b',1:ddcs_max,L_ave,'r')
legend('最短距离','平均距离')
xlabel('迭代次数')
ylabel('距离')
title('各代最短距离与平均距离对比')
|