ACO colony algorithm (algorithm process, TSP parsing example)

Algorithm  computer  Supercomputing  high-performance  scientific exploration

1.  algorithm background - self-organizing behavior characteristics of ant colony
Highly structured organization - although individual behavior of the ants is extremely simple, but ant colony composed of individuals but constitute a highly structured society organizations, members of the ant social division of labor, there is mutual communication and information transfer.
Natural optimization - the process of foraging ant colony, always find the shortest path between the nest to a food source without any prompting; when an obstacle appears on the routes, but also to quickly find a new optimal path.
Information Positive feedback - ants in search of food, pheromone (pheromone) in the path of their passing. Substantially no visual ants, but we can detect the same pheromone emitted within a small range of tracks, thereby to decide where to go, and tend to move toward the direction of the high intensity of pheromone.
Autocatalytic behavior - more on a path through the ants, leaving the more pheromone (evaporation part time), then the probability of selecting the path of ants higher.
 
2. The basic idea of ​​the algorithm:
(1) arranged according to specific problems plurality ants, separately parallel search.
(2) Each ant complete a traveled on the road traveling release pheromones, pheromone quantity proportional to the mass of the solution.
(3) Selection ant path according pheromone intensity level (equal to the initial amount of pheromone), taking into account the distance between two points, local random search strategy. This makes the distance shorter side, on which the amount of pheromone is large, then the probability of the edge is large ants choice.
(4) Each ant can only take the legal route (after each city once and only once), this setting taboo table to control.
(5) All the ants have been searched once is one iteration, each iteration time to do a pheromone update on all sides, the original dead ants, ant new new round of search.
(6) Update pheromone pheromone comprises increasing the original path through the evaporator and pheromone.
(7) reaches a predetermined iteration steps, or stagnation (all ants have chosen the same path, the solution does not change), the algorithm end to the current optimal solution as the optimal solution.
 
3. The information elements and transition probabilities calculated:

Step 4. Algorithm

Algorithm flow chart is as follows:

Example 5. Analysis
我们假设5个城市的TSP问题,然由于某种原因,城市道路均是单行道,即A->B和B->A的距离不相同,也就是说这是一个不对称的TSP问题。现在城市距离信息如下表:

设置参数:
m=5,α=1,β=1,ρ=0.5,τ_ij(0)=2。
第一次迭代第一只蚂蚁:

第一次迭代第二只蚂蚁

第一次迭代第三只蚂蚁:

第一次迭代第四只蚂蚁:

第一次迭代第五只蚂蚁:

第一次迭代完成,更新信息素矩阵,信息素挥发系数为0.5。

第一代蚂蚁全部累死,重新随机生成第二代蚂蚁进行迭代。
第二次迭代第一只蚂蚁:

第二次迭代第二只蚂蚁:

第二次迭代第三只蚂蚁:

第二次迭代第四只蚂蚁:

第二次迭代第五只蚂蚁:

至此,我们已经发现在第二次迭代的时候,五只蚂蚁走的是同一条路,所以算法收敛结束。    最优路径A->E->D->C->B->A, 最有路径的距离为9.
 
6. 算法特点:
是一种基于多主体的智能算法,不是单个蚂蚁行动,而是多个蚂蚁同时搜索,具有分布式的协同优化机制。
本质上属于随机搜索算法(概率算法),具有概率搜索的特征。
是一种全局搜索算法,能够有效地避免局部最优。

 

%--------------------------------------------------------------------------
%% 数据准备
% 清空环境变量
clear all
clc

% 程序运行计时开始
t0 = clock;
%导入数据
citys=xlsread('Chap9_citys_data.xlsx', 'B2:C53');
%--------------------------------------------------------------------------
%% 计算城市间相互距离
n = size(citys,1);
D = zeros(n,n);
for i = 1:n
    for j = 1:n
        if i ~= j
            D(i,j) = sqrt(sum((citys(i,:) - citys(j,:)).^2));
        else
            D(i,j) = 1e-4;      %设定的对角矩阵修正值
        end
    end    
end
%--------------------------------------------------------------------------
%% 初始化参数
m = 75;                              % 蚂蚁数量
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 = 100;                      % 最大迭代次数 
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; 
      % 构建解空间
      citys_index = 1:n;
      % 逐个蚂蚁路径选择
      for i = 1:m
          % 逐个城市路径选择
         for j = 2:n
             has_visited = Table(i,1:(j - 1));           % 已访问的城市集合(禁忌表)
             allow_index = ~ismember(citys_index,has_visited);    % 参加说明1(程序底部)
             allow = citys_index(allow_index);  % 待访问的城市集合
             P = allow;
             % 计算城市间转移概率
             for k = 1:length(allow)
                 P(k) = Tau(has_visited(end),allow(k))^alpha * Heu_F(has_visited(end),allow(k))^beta;
             end
             P = P/sum(P);
             % 轮盘赌法选择下一个访问城市
            Pc = cumsum(P);     %参加说明2(程序底部)
            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) '']);
%--------------------------------------------------------------------------
%% 绘图
figure(1)
plot([citys(Shortest_Route,1);citys(Shortest_Route(1),1)],...  %三点省略符为Matlab续行符
     [citys(Shortest_Route,2);citys(Shortest_Route(1),2)],'o-');
grid on
for i = 1:size(citys,1)
    text(citys(i,1),citys(i,2),['   ' num2str(i)]);
end
text(citys(Shortest_Route(1),1),citys(Shortest_Route(1),2),'       起点');
text(citys(Shortest_Route(end),1),citys(Shortest_Route(end),2),'       终点');
xlabel('城市位置横坐标')
ylabel('城市位置纵坐标')
title(['ACA最优化路径(最短距离:' num2str(Shortest_Length) ')'])
figure(2)
plot(1:iter_max,Length_best,'b')
legend('最短距离')
xlabel('迭代次数')
ylabel('距离')
title('算法收敛轨迹')
%--------------------------------------------------------------------------
%% 程序解释或说明
% 1. ismember函数判断一个变量中的元素是否在另一个变量中出现,返回0-1矩阵;
% 2. cumsum函数用于求变量中累加元素的和,如A=[1, 2, 3, 4, 5], 那么cumsum(A)=[1, 3, 6, 10, 15]。

 

Guess you like

Origin www.cnblogs.com/caiyishuai/p/11140587.html