[Path planning] Solving the shortest path using ant colony algorithm [Matlab 015]

1. Introduction

The steps of the ant colony algorithm:
1 Initialize each parameter: including the distance of each point, the initial concentration of pheromone, the number of ants, the pheromone volatilization factor, the importance factor of the pheromone and heuristic function, heuristic function, the maximum number of iterations, path record Table and so on
2 iterations: randomly set initial values ​​for each ant, and then select the path of each ant according to the probability selection, determine the total path length of each ant, and the optimal path length and average length of the ant colony, and Update the pheromone.
3 Display: Show the best path and the change graph of the best path to iteration

Second, the source code

% Ant main program
 
clear all;
close all;
clc;
 
tic;
Ant=25;%蚂蚁数量
Ger=120;%迭代次数
first_address = [
    100,10
    150,10
    180,30
    200,10
    200,200
    200,220
    180,240
    180,270
    150,270
    100,240
    80,240
    50,270
    200,300
    10,300
    10,270
    10,240
    10,200
    10,10
    50,30
    100,10
    ];%first_address表示测试数据中的节点坐标
 
SumOfCity = size(first_address,1);%节点个数
length_address =10000.*ones(SumOfCity,SumOfCity);%length_address表示两两节点间的距离,初始设定10000,可以设定无穷大,表示不相连
length_address(1,2)=377;%表示节点1和节点2的距离
length_address(2,4)=190;
length_address(2,3)=100;
length_address(3,4)=101;
length_address(4,5)=240;
length_address(5,17)=1932;
length_address(5,6)=70;
length_address(6,13)=200;
length_address(6,7)=63.1;
 
length_address(7,10)=377;
length_address(7,8)=87.5;
length_address(8,9)=100;
length_address(10,11)=8;
length_address(9,10)=170.8;
length_address(9,12)=332.9;
length_address(11,12)=168.8;
length_address(11,16)=375.2;
length_address(12,15)=135.1;
 
length_address(13,14)=458;
length_address(14,15)=100;
length_address(15,16)=86.7;
length_address(16,17)=187.5;
length_address(17,18)=639.8;
 
length_address(18,20)=510.5;
length_address(18,19)=200.1;
length_address(19,20)=246.8;
for   n=1:size(first_address)
    for m=1:size(first_address)
        if length_address(n,m)~=10000
            length_address(m,n)=length_address(n,m);   %对称矩阵
        end
    end
end
 
power=length_address;%距离
[PM PN]=size(power);%距离矩阵大小,行列个数
% %% 画出节点分布图形
% figure(1);
% grid on;
% hold on;
% scatter(first_address(:,1),first_address(:,2));
% for i=1:PN
%     for j=1:PN
%         if(length_address(i,j)~=10000)
%             line([first_address(i,1),first_address(j,1)],[first_address(i,2),first_address(j,2)],'Color','g');%划线
%             text((first_address(i,1)+first_address(j,1))/2,(first_address(i,2)+first_address(j,2))/2,num2str(length_address(i,j)));%标注线段距离
%         end
%     end
% end
 
 
 
% 初始化蚂蚁位置
v=init_population(Ant,PN);
v(:,1)=1;%起点
v(:,PN)=1;%终点
 
fit=short_road_fun(v,power);%求每条路径的距离
T0 = max(fit)-fit;
 
 
% 初始化
vmfit=[];
vx=[];
P0=0.2;           % P0----全局转移选择因子
P=0.8;            % P ----信息素蒸发系数
%C=[];
% 开始寻优
for i_ger=1:Ger
    lamda=1/i_ger;    % 转移步长参数
    [T_Best(i_ger),BestIndex]=max(T0);
    for j_g=1:Ant    % 求取全局转移概率
        r=T0(BestIndex)-T0(j_g);
        Prob(i_ger,j_g)=r/T0(BestIndex);
    end
    %100只蚂蚁进行路径的转变
    for j_g_tr=1:Ant
        %路径进行改变,该路径存放到temp变量,1表示经过该列所在的节点数
        if Prob(i_ger,j_g_tr)<P0
            M=rand(1,PN)<lamda;
            temp=v(j_g_tr,:)-2.*(v(j_g_tr,:).*M)+M;
        else
            M=rand(1,PN)<P0;
            temp=v(j_g_tr,:)-2.*(v(j_g_tr,:).*M)+M;
        end
        temp(:,1)=1;%起点和终点必须有蚂蚁存在
        temp(:,end)=1;
        %判转变后的临时路径距离是否小于原来的路径,是的话就将该蚂蚁的路径进行转换成temp中存放的路径
        if short_road_fun(temp,power)<short_road_fun(v(j_g_tr,:),power)
            v(j_g_tr,:)=temp;
        end
    end
    
    %信息素更新
    
       [sol,indb]=min(fit);
    v(1,:)=v(indb,:);%第一只蚂蚁的路径保存最小路径
    media=mean(fit);
    vx=[vx sol];%存放每一代最小的距离
    %     vmfit=[vmfit media];
    
    
end
 
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
%%%% 最后结果
% 显示最优解及最优值
% v(indb,:)
disp(sprintf('Code of shortroad is: %s',num2str(v(indb,:))));
disp(sprintf('\n'));    %空一行
disp(sprintf('Shortroad is: %s',num2str(find(v(indb,:)))));
disp(sprintf('Mininum is: %d',sol));
route=find(v(indb,:));
% 画出节点分布图形
figure(2);
grid on;
hold on;
for i=1:PN-1
    plot(first_address(i,1),first_address(i,2),'bo','MarkerSize',10);
    str=num2str(i);
    text(first_address(i,1)-10,first_address(i,2)+10,str,'Color','red','FontSize',15);
end
m=length(route);
for i=1:m
    plot(first_address(route(i),1),first_address(route(i),2),'MarkerSize',10,'MarkerEdgeColor','k','MarkerFaceColor',[0.5,0.5,0.5])  ;
    hold on;
end
for i=1:PN
    for j=1:PN
        if(length_address(i,j)~=10000)
            line([first_address(i,1),first_address(j,1)],[first_address(i,2),first_address(j,2)],'Color','g','LineWidth',5);%划线
            text((first_address(i,1)+first_address(j,1))/2,(first_address(i,2)+first_address(j,2))/2,num2str(length_address(i,j)));%标注线段距离
        end
    end
end
%% 最短路径
for p=1:m-1
    if(route(p+1)~=20)
        line([first_address(route(p),1),first_address(route(p+1),1)],[first_address(route(p),2),first_address(route(p+1),2)],'Color','r','LineWidth',5);%划线
        text((first_address(route(p),1)+first_address(route(p+1),1))/2,(first_address(route(p),2)+first_address(route(p+1),2))/2,num2str(length_address(route(p),route(p+1))));%标注线段距离
    else
        line([first_address(route(p),1),first_address(1,1)],[first_address(route(p),2),first_address(1,2)],'Color','r','LineWidth',5);%划线
        text((first_address(route(p),1)+first_address(1,1))/2,(first_address(route(p),2)+first_address(1,2))/2,num2str(length_address(route(p),route(p+1))));%标注线段距离
    end
end
axis([0 250 0 400])
% 图形显示最优及平均函数值变化趋势
% figure(3);
% plot(vx);
% title('最优,平均函数值变化趋势');
% xlabel('Generations');
% ylabel('f(x)');
% hold on;
% plot(vmfit,'r');
% hold off;
 
runtime=toc

Three, running results

Insert picture description here

Four, remarks

Complete code or writing to add QQ912100926 past review
>>>>>>
[Path planning] Particle swarm optimization algorithm for three-dimensional UAV path planning [Matlab 012]
[Path planning] Genetic algorithm for open vehicles in multiple logistics centers Path planning [Matlab 013 issue]
[Path planning] Particle swarm algorithm robot grid path planning [Matlab 014 issue]

Guess you like

Origin blog.csdn.net/m0_54742769/article/details/112983441