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
Note: Add QQ1564658423 for complete code or writing.
Past review>>>>>>
[Matlab 015] [Path planning 1] 3D UAV path planning based on particle swarm matlab source code
[Matlab 017] [Path planning 2] Using genetic algorithms to prepare the opening of multiple logistics centers -Style vehicle routing problem Matlab program
[Matlab 018] [Path planning 3] Robot grid path planning based on particle swarms
[Matlab 019] [Path planning 4] Ant colony algorithm to solve the shortest path matlab
[Matlab 020] [Path planning 5] Matlab immune algorithm for logistics center location problem
[Matlab 021] [Path planning 6] Three-dimensional path planning of drones based on artificial bee colony
[Matlab 027] [Path planning 7] Based on grid map-genetic algorithm Robot optimal path planning
[Matlab 034 period] [Path planning 8] Ant colony-based multi-UAV attack scheduling
based on grid map-genetic algorithm for robot optimal path planning [Matlab 022 period] [Path planning 9]
Multi-UAV cooperative target allocation modeling and genetic algorithm solution considering allocation order [Matlab 110] [Path planning 10]
Ant colony algorithm for multi-center vrp problem matlab [Matlab 111] [Path planning 11]
Based on ant colony algorithm Solving multi-center VRP problem with time windows matlab [Matlab 112] [Path planning 12]
Based on ant colony algorithm to solve multi-center VRP problems with time window matlab [Matlab 113] [Path planning 13]
Multi-center based on genetic algorithm VRP solution matlab【Matlab 114】【Path planning 14】
Simulated annealing to solve the VRP problem matlab [Matlab 115] [Path planning 15]
A star-based grid path planning [Matlab 116] [Path planning 16]
A bidirectional optimization particle swarm grid map path planning with cross factors [Matlab 117] [Path planning 17]
[TSP] Ant colony algorithm for solving TSP problems matlab with GUI [Matlab 118] [Path planning 18]
Based on ant colony algorithm grid map path planning matlab [Matlab 119] [Path planning 19]
TSP problem of traveling salesman based on genetic algorithm [Matlab 135] [Path planning 20]
TSP problem of traveling salesman based on simulated annealing algorithm [Matlab 136] [Route planning 21]
Smart car path planning based on ant colony algorithm [Matlab Issue 137]【Path Planning 22】