1. Background
Take the 2017 Huawei Cup Graduate Mathematical Modeling Competition Question A (optimized application of drones in emergency rescue and disaster relief) as an example to explain the application of ant colony algorithm in solving the optimal path problem. We simplified the problem and described it as follows : The
drone starts from a certain base, passes through all rescue points, and then returns to the base (each point passes only once) to find the best travel path.
The scatter diagram of the base and all rescue points is shown in Figure 1.
The ant colony algorithm is an algorithm that seeks to optimize the path. The idea of this algorithm originates from the path of the ants in the process of searching for things. This algorithm is essentially a heuristic global optimization algorithm with positive information feedback and distributed computing. And the characteristics of heuristic search. The rules adopted by the ant colony algorithm are mainly environmental information, obstacle avoidance rules, movement rules, and pheromone distribution rules. Ant colony algorithm is mainly used in combinatorial optimization problems, and its application in network routing is becoming more and more extensive.
The rescue route finally obtained by using the ant colony algorithm is shown in Figure 2:
Second, the source code
我们提供了可运行的代码,并添加了注释,大家可自行去了解该算法的精髓。代码包含三个部分,主函数mainfun.m调用Antcolonyalgorithm.m和flightRoute.m. Antcolonyalgorithm.m用于实现蚁群算法,flightRoute.m用于画飞行路线。
function mainfun()
% 位点坐标
position=[ 91.2000 94.3000
83.4000 90.4000
76.7000 88.8000
57.8000 91.4000
75.6000 91.4000
57.8000 88.0000
51.1000 88.9000
30.0000 90.6000
30.0000 99.4000
42.2000 78.1000
58.9000 92.7000
62.3000 93.2000
50.0000 78.9000
44.5000 86.7000
55.6000 71.2000
52.3000 67.3000
72.3000 64.7000
82.3000 66.0000
85.6000 71.2000
82.3000 67.3000
85.6000 58.2000
85.6000 60.8000
83.4000 59.5000
50.0000 71.7000
52.3000 78.4000
44.5000 75.8000
66.0000 93.6000
62.2000 82.9000
78.9000 93.2000
67.8000 116.5000
52.2000 120.4000
55.6000 80.2000
91.2000 124.3000
53.3000 65.9000
110.0000 55.0000];
% 所有点的散点图
figure
scatter(position(:,1),position(:,2),'ro');
hold on
axis([10,120,20,140])
plot(110,55,'ro','MarkerFaceColor','r')
text(100,60,'基地J','Color','r')
set(gca,'FontSize',16)
% 蚁群算法
[~,L_best,L_ave,Shortest_Route,Shortest_Length]=Antcolonyalgor
figure
plot(L_best,'b','LineWidth',2)
hold on
plot(L_ave,'b','LineWidth',2)
xlabel('迭代次数')
ylabel('平均距离和最短距离')
set(gca,'FontSize',16)
end
Ant colony algorithm function——
function [R_best,L_best,L_ave,Shortest_Route,Shortest_Length]=Antcolonyalgorithm(C,NC_max,m,Alpha,Beta,Rho,Q)
% 输入输出说明
% C城市的坐标
% NC_max 最大迭代次数
% m 蚂蚁个数
% Rho 信息素蒸发系数
% Q 信息素增加强度系数
% R_best 最佳路线
% L_best 最佳路线的长度
% Alpha 信息素重要程度
% Beta 启发式因子重要程度
%变量初始化
n=size(C,1);
D=zeros(n,n);
for i=1:n
for j=1:n
if i~=j
D(i,j)=((C(i,1)-C(j,1))^2+(C(i,2)-C(j,2))^2)^0.5;
else
D(i,j)=eps;
end
D(j,i)=D(i,j);
end
end
Eta=1./D;
Tau=ones(n,n);
Tabu=zeros(m,n);
NC=1;
R_best=zeros(NC_max,n);
L_best=inf.*ones(NC_max,1);
L_ave=zeros(NC_max,1);
while NC<=NC_max
Randpos=[];
for i=1:(ceil(m/n))
Randpos=[Randpos,randperm(n)];
end
Tabu(:,1)=(Randpos(1,1: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 isempty(find(visited==k, 1))
J(Jc)=k;
Jc=Jc+1;
end
end
if NC>=2
Tabu(1,:)=R_best(NC-1,:);
end
L=zeros(m,1);
for i=1:m
R=Tabu(i,:);
for j=1:(n-1)
L(i)=L(i)+D(R(j),R(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;
%更新信息素
Delta_Tau=zeros(n,n);
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);
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;
%禁忌表清零
Tabu=zeros(m,n);
end
%输出结果
Pos=find(L_best==min(L_best));
Shortest_Route=R_best(Pos(1),:);
Shortest_Length=L_best(Pos(1));
end
The function of drawing a route map, the input is the location coordinates and path——
function flightRoute(C,Rou)
Num=length(Rou);
scatter(C(:,1),C(:,2));
hold on
plot([C(Rou(1),1),C(Rou(Num),1)],[C(Rou(1),2),C(Rou(Num),2)],'r','LineWidth',3)
hold on
for ii=2:Num
plot([C(Rou(ii-1),1),C(Rou(ii),1)],[C(Rou(ii-1),2),C(Rou(ii),2)],'r','LineWidth',3)
hold on
end
end
Four, remarks
Add QQ912100926 for complete code or writing.
Review >>>>>>
[Path Planning] Particle Swarm Optimization Algorithm for 3D UAV Path Planning [Matlab 012]
[Path Planning] Genetic Algorithm for Open Vehicle Path Planning for Multiple Logistics Centers [Matlab 013]
[Path planning] Robot grid path planning based on particle swarm algorithm [Matlab 014]
[Path planning] Ant colony algorithm to solve the shortest path [Matlab 015]
[Path planning] Immune algorithm for logistics center location selection [Matlab 016]
[Path planning] Three-dimensional path planning for drones
based on artificial bee colony [Matlab 017] [Path planning] Genetic algorithm for robot path planning based on grid map [Matlab 018]
[Path planning] Ant colony algorithm for multiple drones Attack scheduling [Matlab 019]
[Path planning] Genetic algorithm based on grid map robot optimal path planning [Matlab 020]
[Path planning] Genetic algorithm for multi-UAV cooperative target allocation modeling considering the allocation order [ Matlab 021 issue]
[path planning] ant colony algorithm multi-center vrp problem [Matlab 022]
[path planning] ant colony algorithm to solve multi-center VRP with time window [Matlab 023 issue]
[path planning] many genetic algorithms Central VRP solution [Matlab 024]
[Path planning] Simulated annealing to solve the VRP problem [Matlab 025]
[Path planning] A star grid path planning [Matlab 026]
[Path planning] Based on a two-way cross factor Optimizing particle swarm grid map path planning [Matlab 027]
[Path Planning] [TSP] Ant Colony Algorithm for Solving TSP Problem with GUI [Matlab 028]
[Path Planning] Ant Colony Algorithm for Raster Map Path Planning [Matlab 029]
[Path Planning] Genetic Algorithm for Traveling Salesman TSP [ Matlab Issue 030]
[Path Planning] Traveling Salesman TSP Problem of Simulated Annealing Algorithm [Matlab 031]
[Path Planning] Intelligent Car Path Planning of Ant Colony Algorithm [Matlab 032]