Matlab simulation for solving urban TSP problem based on ACO ant colony optimization algorithm

Table of contents

1. Overview of ACO ant colony optimization algorithm

2. Solving urban TSP problem based on ACO ant colony optimization algorithm

3.MATLAB core program

4.aco-tsp simulation results


1. Overview of ACO ant colony optimization algorithm

       The ACO ant colony optimization algorithm is a bionic algorithm that solves optimization problems by simulating the foraging behavior of ants in nature. This algorithm was originally proposed by Italian scholar M.Dorigo et al. in 1992, and was successfully applied to solve the Traveling Salesman Problem (TSP). As ants search for food, they leave behind a substance called a pheromone on their path. When subsequent ants choose paths, they will tend to choose paths with high pheromone concentrations. At the same time, ants will also adjust the concentration of pheromone according to the length of the path. The shorter the path, the higher the pheromone concentration will be and the more ants will choose that path. Through continuous iteration and updating, the optimal solution can eventually be found.

       In the ACO algorithm, artificial ants are used to simulate the behavior of real ants. Each artificial ant will independently search for feasible solutions in the solution space and leave pheromones on the solutions. The better the solution, the more pheromones are left behind. As the algorithm progresses, the pheromone concentration on the optimal solution path will gradually increase, and more and more ants will choose this path. Eventually, the entire ant colony will converge on the optimal or near-optimal solution.
       In the ACO algorithm, the probability of each ant choosing the next node can be expressed by the following formula:

p_ij = [tau_ij^alpha] / [sum_{k in allowed_k} tau_ik^alpha]

       Among them, p_ij represents the probability of ants from node i to node j, tau_ij represents the pheromone concentration between node i and node j, alpha represents the importance of pheromone concentration, and allowed_k represents the set of nodes that ants have not visited.

After each iteration, the pheromone concentration on each node is updated according to the following formula:

tau_ij = (1 - rho) * tau_ij + sum_{k=1}^m delta_tau_ij^k

       Among them, rho represents the volatilization rate of pheromone, m represents the number of ants, and delta_tau_ij^k represents the increment of pheromone concentration left by the k-th ant between node i and node j. delta_tau_ij^k can be calculated using the following formula:

delta_tau_ij^k = Q / L_k

Among them, Q represents a constant, and L_k represents the path length of the k-th ant.

       In addition, there are some other parameters and formulas used to control the convergence speed and search range of the algorithm, such as the number of ants, the number of iterations, the initial concentration of pheromone, etc. The choice of these parameters needs to be tailored to the characteristics and scale of the specific problem.

2. Solving urban TSP problem based on ACO ant colony optimization algorithm

      The TSP problem is a classic combinatorial optimization problem that requires finding a shortest path that visits each city exactly once in a given set of cities and minimizes the total length of the path. The TSP problem is an NP-hard problem, that is, the optimal solution cannot be found in polynomial time.

       In solving the urban TSP problem based on the ACO ant colony optimization algorithm, we regard each city as a node and the distance between cities as the weight of the edge. Each ant starts from the starting point, visits each city in turn, and finally returns to the starting point. When visiting each city, the ants will choose the next city to visit based on the pheromone concentration and distance between the current city and the unvisited city. Specifically, the probability of an ant choosing the next city can be expressed by the following formula:

p_ij = [tau_ij^alpha] / [sum_{k in allowed_k} tau_ik^alpha]

       Among them, p_ij represents the probability of ants from city i to city j, tau_ij represents the pheromone concentration between city i and city j, alpha represents the importance of pheromone concentration, and allowed_k represents the set of cities that ants have not visited.

      In each iteration, each ant will choose the next city to visit based on the above formula until it visits all cities and returns to the starting point. We then calculate the increment in pheromone concentration between each city based on the path length of each ant. Specifically, the increment of pheromone concentration can be expressed by the following formula:

delta_tau_ij = Q / L_k

      Among them, delta_tau_ij represents the increment of pheromone concentration from city i to city j, Q represents a constant, and L_k represents the path length of the k-th ant.

       Finally, we will update the pheromone concentration between each city for use in the next iteration. Specifically, the update of pheromone concentration can be expressed by the following formula:

tau_ij = (1 - rho) * tau_ij + sum_{k=1}^m delta_tau_ij^k

       Among them, rho represents the volatilization rate of pheromone, m represents the number of ants, and delta_tau_ij^k represents the increment of pheromone concentration left by the k-th ant between city i and city j.

      Through continuous iteration and updating of the above steps, the optimal solution can finally be found. It should be noted that in practical applications, the algorithm may need to be optimized and improved to adapt to different problem sizes and complexities. In addition, the ant colony optimization algorithm also has some limitations, such as it is easy to fall into the local optimal solution. Therefore, parameters and settings need to be carefully selected in practical applications to ensure the effectiveness and reliability of the algorithm.

      It should be noted that although the ACO algorithm has achieved good results in solving TSP problems, it also has some limitations. For example, for large-scale and complex problems, the algorithm may require a long time and computing resources to find the optimal solution; at the same time, the algorithm is also prone to falling into local optimal solutions. Therefore, parameters and settings need to be carefully selected in practical applications to ensure the effectiveness and reliability of the algorithm.

3.MATLAB core program

.......................................................................              
while NC<=NC_max        %停止条件之一:达到最大迭代次数,停止
    %%2.将m只蚂蚁放到n个城市上
    Randpos=[];   %随即存取
    for i=1:(ceil(m/n))
        Randpos=[Randpos,randperm(n)];
    end
    Tabu(:,1)=(Randpos(1,1:m))';   
    %%3.m只蚂蚁按概率函数选择下一座城市,完成各自的周游
    for j=2:n     %所在城市不计算
        for i=1:m
            visited=Tabu(i,1:(j-1)); %记录已访问的城市,避免重复访问
            J=zeros(1,(n-j+1));       %待访问的城市
            P=J;                      %待访问城市的选择概率分布
            Jc=1;
            for k=1:n
                if length(find(visited==k))==0   %开始时置0
                    J(Jc)=k;
                    Jc=Jc+1;                         %访问的城市个数自加1
                end
            end
            %下面计算待选城市的概率分布
            for k=1:length(J)
                P(k)=(Tau(visited(end),J(k))^Alpha)*(Eta(visited(end),J(k))^Beta);
            end
            P=P/(sum(P));
            %按概率原则选取下一个城市
            Pcum=cumsum(P);     %cumsum,元素累加即求和
            Select=find(Pcum>=rand); %若计算的概率大于原来的就选择这条路线
            to_visit=J(Select(1));
            Tabu(i,j)=to_visit;
        end
    end
    if NC>=2
        Tabu(1,:)=R_best(NC-1,:);
    end
    %%4.记录本次迭代最佳路线
    L=zeros(m,1);     %开始距离为0,m*1的列向量
    for i=1:m
        R=Tabu(i,:);
        for j=1:(n-1)
            L(i)=L(i)+D(R(j),R(j+1));    %原距离加上第j个城市到第j+1个城市的距离
        end
        L(i)=L(i)+D(R(1),R(n));      %一轮下来后走过的距离
    end
    L_best(NC)=min(L);           %最佳距离取最小
    pos=find(L==L_best(NC));
    R_best(NC,:)=Tabu(pos(1),:); %此轮迭代后的最佳路线
    L_ave(NC)=mean(L);           %此轮迭代后的平均距离
    NC=NC+1                      %迭代继续
 
 
    %%5.更新信息素
    Delta_Tau=zeros(n,n);        %开始时信息素为n*n的0矩阵
    for i=1:m
        for j=1:(n-1)
            Delta_Tau(Tabu(i,j),Tabu(i,j+1))=Delta_Tau(Tabu(i,j),Tabu(i,j+1))+Q/L(i);
            %此次循环在路径(i,j)上的信息素增量
        end
        Delta_Tau(Tabu(i,n),Tabu(i,1))=Delta_Tau(Tabu(i,n),Tabu(i,1))+Q/L(i);
        %此次循环在整个路径上的信息素增量
    end
    Tau=(1-Rho).*Tau+Delta_Tau; %考虑信息素挥发,更新后的信息素
    %%6.禁忌表清零
    Tabu=zeros(m,n);             %直到最大迭代次数
end
%%7.输出结果及绘图
Pos=find(L_best==min(L_best));   %找到最佳路径(非0为真)
Shortest_Route=R_best(Pos(1),:)  %最大迭代次数后最佳路径
Shortest_Length=L_best(Pos(1))   %最大迭代次数后最短距离 
toc                   %计时结束
N=length(R);
scatter(C(:,1),C(:,2));
 hold on
 plot([C(R(1),1),C(R(N),1)],[C(R(1),2),C(R(N),2)],'r')
 hold on
for ii=2:N
    plot([C(R(ii-1),1),C(R(ii),1)],[C(R(ii-1),2),C(R(ii),2)],'r')
     hold on
end
xlabel('城市位置横坐标')
ylabel('城市位置纵坐标')
title(['蚁群算法(最短距离):' num2str(Shortest_Length) ''])
up2224

4.aco-tsp simulation results

Guess you like

Origin blog.csdn.net/ccsss22/article/details/133446521