蚁群算法的原理及其代码

%%来自别人的研究生数学建模的一片论文,侵权即删
clear
clc
t0 = clock;
% P01=xlsread('data.xlsx','E70:F72');
% P01=[72.3 82.3 85.6 82.3 85.6 85.6 83.4 85.6;
% 64.7 66 71.2 67.3 58.2 60.8 59.5 55.6]';
P01=[1 1 1 2 2 3 4 4 5 6 7 7 9 9 10 11 13 13;
4 10 16 2 14 7 4 10 16 13 11 8 15 5 11 16 14 11]';
%P01是点的(x,y)的坐标信息,找出了最短路径,还是TSP问题
%-------------------------------------------------------------------------%
n=size(P01,1);
D=zeros(n,n);
for i=1:n
for j=1:n
if i~=j
D(i,j)=sqrt( sum( ( P01(i,:)-P01(j,:) ).^2 ) );
else
D(i,j)=1e-4;
end
end
end
%-------------------------------------------------------------------------%
%%初始化参数
m = 100; % 蚂蚁数量
alpha = 1; % 信息素重要程度因子
beta = 5; % 启发函数重要程度因子
vol = 0.2; % 信息素挥发(volatilization)因子
Q = 10; % 常系数
Heu_F = 1./D; % 启发函数(heuristic function)
Tau = ones(n,n); % 信息素矩阵
Table = zeros(m,n); % 路径记录表
iter = 1; % 迭代次数初值
iter_max = 200; % 最大迭代次数
Route_best = zeros(iter_max,n); % 各代最佳路径
Length_best = zeros(iter_max,1); % 各代最佳路径长度
Length_ave = zeros(iter_max,1); % 各代路径的平均长度
Limit_iter = 0; % 程序收敛时迭代次数
%-------------------------------------------------------------------------
%% 迭代寻找最佳路径
while iter <= iter_max
% 随机产生各个蚂蚁的起点目标
start = zeros(m,1);
for i = 1:m
temp = randperm(n);
start(i) = temp(1);
end
Table(:,1) = start;
%构建解空间
PO1_index = 1:n;
%逐个蚂蚁路径选择
for i = 1:m
% 逐个城市路径选择
for j = 2:n
tabu = Table(i,1:(j - 1)); % 已访问的目标集合
allow_index = ~ismember(PO1_index,tabu);
allow = PO1_index(allow_index); % 待访问的城市集合
P = allow;
% 计算城市间转移概率
for k = 1:length(allow)
P(k) = Tau(tabu(end),allow(k))^alpha *Heu_F(tabu(end),allow(k))^beta;
end
P = P/sum(P);
% 轮盘赌法选择下一个访问目标
Pc = cumsum(P);
target_index = find(Pc >= rand);
target = allow(target_index(1));
Table(i,j) = target;
end
end
% 计算各个蚂蚁的路径距离
Length = zeros(m,1);
for i = 1:m
Route = Table(i,:);
for j = 1:(n - 1)
Length(i) = Length(i) + D(Route(j),Route(j + 1));
end
Length(i) = Length(i) + D(Route(n),Route(1));
end
%计算最短路径距离及平均距离
if iter == 1
[min_Length,min_index] = min(Length);
Length_best(iter) = min_Length;
Length_ave(iter) = mean(Length);
Route_best(iter,:) = Table(min_index,:);
Limit_iter = 1;
else
[min_Length,min_index] = min(Length);
Length_best(iter) = min(Length_best(iter - 1),min_Length);
Length_ave(iter) = mean(Length);
if Length_best(iter) == min_Length
Route_best(iter,:) = Table(min_index,:);
Limit_iter = iter;
else
Route_best(iter,:) = Route_best((iter-1),:);
end
end
% 更新信息素
Delta_Tau = zeros(n,n);
% 逐个目标计算
for i = 1:m
% Öð¸öÄ¿±ê¼ÆËã
for j = 1:(n - 1)
Delta_Tau(Table(i,j),Table(i,j+1)) =Delta_Tau(Table(i,j),Table(i,j+1)) + Q/Length(i);
end
Delta_Tau(Table(i,n),Table(i,1)) = Delta_Tau(Table(i,n),Table(i,1))+ Q/Length(i);
end
Tau = (1-vol) * Tau + Delta_Tau;
% 迭代次数加 1,情况路径记录
iter = iter + 1;
Table = zeros(m,n);
end
%--
%%结果显示
[Shortest_Length,index] = min(Length_best);
Shortest_Route = Route_best(index,:);
Time_Cost=etime(clock,t0);
disp(['最短距离:' num2str(Shortest_Length)]);
disp(['最短路径:' num2str([Shortest_Route Shortest_Route(1)])]);
disp(['收敛时迭代次数:' num2str(Limit_iter)]);
disp(['程序执行时间:' num2str(Time_Cost) 'Ãë']);
%--------------------------------------------------------------------------
plot([P01(Shortest_Route,1);P01(Shortest_Route(1),1)],...
[P01(Shortest_Route,2);P01(Shortest_Route(1),2)],'o-');
grid on
for i = 1:size(P01,1)
text(P01(i,1),P01(i,2),[' ' num2str(i)]);
end
text(P01(Shortest_Route(1),1),P01(Shortest_Route(1),2),' 起点');
text(P01(Shortest_Route(end),1),P01(Shortest_Route(end),2),' 终点');
xlabel('通信基站横坐标')
ylabel('通信基站纵坐标')
title(['遍历所有点的最优化路径(最短距离):' num2str(Shortest_Length) 'km)'])
figure(2)
plot(1:iter_max,Length_best,'b')
legend('最短距离')
xlabel('迭代次数')
ylabel('距离')
title('算法收敛轨迹')

《蚁群算法的原理及其代码》

蚁群算法是一种逐步完善的智能算法

    原文作者:蚁群算法
    原文地址: https://blog.csdn.net/bufengzj/article/details/82669433
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞