题目虽然起的很文艺,不过从我对该算法的理解,蚁群算法着实有这么点意思。接下来我将用”土话“帮助大家理解一下该算法。
蚁群算法是一种用来寻找优化路径的概率型算法。它由Marco Dorigo于1992年在他的博士论文中提出,其灵感来源于蚂蚁在寻找食物过程中发现路径的行为。(源自百度百科)
此算法运用了仿生学的原理。假如黄旗为蚂蚁群,红旗为食物。蚂蚁群体从黄旗到红旗有三条路,蚂蚁群的起始地选择是均等的。蚂蚁在走的时候都会留下自己的气味(学名:信息素),这个气味与路程的距离成反比,也就是就是距离越短,气味越重。这时蚂蚁的团结的精神就表现出来了,路程越短的路径,气味越重,这就达到众人拾柴火焰高的效果,吸引来越来越多的蚂蚁去这条最短的路上来,从而得到了最短路径(最优解)。看上图!!加深理解(看来要成为知名博主,ps的能力还需提高啊)
伟人发现以及发明该算法主要是为了解决旅行商问题(TSP,旅行商卖东西寻找路径问题),也可以看做蚂蚁觅食寻找最优路径的问题。
所以旅行商问题(蚂蚁整个觅食的过程)有以下的要素:
1、蚁群的数量
2、城市数量
3、不同城市之间的距离
4、信息素因子(前文说的气味)
5、信息素挥发因子
6、信息素常数
7、启发函数因子
8、最大迭代次数
每个参数的设置我就不再赘述,见此博主的详解智能算法---蚁群算法介绍 感谢
需要算法的直接跳到这!!接下来咱们上实战,讲算法
0.100000000000000,0.600000000000000;
0.200000000000000,0.300000000000000;
0.400000000000000,0.100000000000000;
0.500000000000000,0.500000000000000;
0.700000000000000,0.200000000000000;
0.800000000000000,0.400000000000000;
0.200000000000000,0.800000000000000;
0.500000000000000,0.900000000000000;
0.700000000000000,0.600000000000000;
0.900000000000000,0.800000000000000]
clear all
clc
city10=[0.100000000000000,0.600000000000000;
0.200000000000000,0.300000000000000;
0.400000000000000,0.100000000000000;
0.500000000000000,0.500000000000000;
0.700000000000000,0.200000000000000;
0.800000000000000,0.400000000000000;
0.200000000000000,0.800000000000000;
0.500000000000000,0.900000000000000;
0.700000000000000,0.600000000000000;
0.900000000000000,0.800000000000000]
%% 计算城市间相互距离
n = size(city10,1);
D = zeros(n,n);
for i = 1:n
for j = 1:n
if i ~= j
D(i,j) = sqrt(sum((city10(i,:) - city10(j,:)).^2));
else
D(i,j) = 0;
end
end
end
%% 初始化参数
m = 16; % 蚂蚁数量
alpha = 1; % 信息素重要程度因子
rho = 0.2; % 信息素挥发因子
Q = 1; % 信息素常系数
Eta = 1./D; % 启发函数
beta = 4; % 启发函数重要程度因子
Tau = ones(n,n); % 信息素矩阵,城市i和城市j连接路径上的信息素浓度
road_record = zeros(m,n); % 路径记录表
iter = 1; % 迭代次数初值
iter_max = 150; % 最大迭代次数
Route_best = zeros(iter_max,n); % 各代最佳路径
Length_best = zeros(iter_max,1); % 各代最佳路径的长度
Length_ave = zeros(iter_max,1); % 各代路径的平均长度
%% 迭代寻找最佳路径
while iter <= iter_max
%1.随机产生各个蚂蚁的起点城市
start = zeros(m,1);
for i = 1:m
temp = randperm(n);
start(i) = temp(1);
end
road_record(:,1) = start;
citys_index = 1:n;
%2. 逐个蚂蚁路径选择
for i = 1:m
%3. 逐个城市路径选择
for j = 2:n
recorded = road_record(i,1:(j - 1));
allow_index = ~ismember(citys_index,recorded);
allow = citys_index(allow_index);
P = allow;
% 计算城市间转移概率
for k = 1:length(allow)
P(k) = Tau(recorded(end),allow(k))^alpha * Eta(recorded(end),allow(k))^beta;
end
P = P/sum(P);
Pc = cumsum(P);
target_index = find(Pc >= rand);
target = allow(target_index(1));
road_record(i,j) = target;
end
end
% 4.计算各个蚂蚁的路径距离
Length = zeros(m,1);
for i = 1:m
Route = road_record(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
% 5.计算最短路径距离及平均距离
if iter == 1
[min_Length,min_index] = min(Length);
Length_best(iter) = min_Length;
Length_ave(iter) = mean(Length);
Route_best(iter,:) = road_record(min_index,:);
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,:) = road_record(min_index,:);
else
Route_best(iter,:) = Route_best((iter-1),:);
end
end
% 6.更新信息素
Delta_Tau = zeros(n,n);
% 7.逐个蚂蚁计算
for i = 1:m
%8. 逐个城市计算
for j = 1:(n - 1)
Delta_Tau(road_record(i,j),road_record(i,j+1)) = Delta_Tau(road_record(i,j),road_record(i,j+1)) + Q/Length(i);
end
Delta_Tau(road_record(i,n),road_record(i,1)) = Delta_Tau(road_record(i,n),road_record(i,1)) + Q/Length(i);
end
Tau = (1-rho) * Tau + Delta_Tau;
% 9.迭代次数加1,清空路径记录表
iter = iter + 1;
road_record = zeros(m,n);
end
%% 结果显示
[Shortest_Length,index] = min(Length_best);
Shortest_Route = Route_best(index,:);
disp(['最短距离:' num2str(Shortest_Length)]);
disp(['最短路径:' num2str([Shortest_Route Shortest_Route(1)])]);
%% 绘图
figure(1)
plot([city10(Shortest_Route,1);city10(Shortest_Route(1),1)],...
[city10(Shortest_Route,2);city10(Shortest_Route(1),2)],'o-');
grid on
for i = 1:size(city10,1)
text(city10(i,1),city10(i,2),[' ' num2str(i)]);
end
text(city10(Shortest_Route(1),1),city10(Shortest_Route(1),2),' 起点');
text(city10(Shortest_Route(end),1),city10(Shortest_Route(end),2),' 终点');
xlabel('城市位置横坐标')
ylabel('城市位置纵坐标')
title(['蚁群算法优化路径(最短距离:' num2str(Shortest_Length) ')'])
figure(2)
plot(1:iter_max,Length_best,'b',1:iter_max,Length_ave,'r:')
legend('最短距离','平均距离')
xlabel('迭代次数')
ylabel('距离')
title('各代最短距离与平均距离对比')