A list,

1. Ant colony algorithm is proposed

Ant Colony Optimization algorithm (ACO), also known as ant algorithm, is a probabilistic algorithm used to find optimal paths. It was proposed by Marco Dorigo in his PhD thesis in 1992 and was inspired by the behavior of ants in finding their way to food. Genetic algorithm has been applied in pattern recognition, neural network, machine learning, industrial optimal control, adaptive control, biological science, social science and so on.

2. Basic principles of the algorithm





Ii. Source code

% Ant main program
 
clear all;
close all;
clc;
 
tic;
Ant=25; % Number of ants Ger=120; % number of iterations 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]; SumOfCity = size(first_address,1); % Number of nodes length_address =10000.*ones(SumOfCity,SumOfCity); % length_ADDRESS Specifies the initial distance between two nodes10000, infinity can be set to indicate that the length_address(1.2) =377; % indicates node1And the node2The distance from the 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)~=10000length_address(m,n)=length_address(n,m); End end power=length_address; % distance [PM PN]=size(power); % distance matrix size, number of rows %% % draw the node distribution graph % 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'); % line % text((first_address(I,1)+first_address(j,1)) /2,(first_address(i,2)+first_address(j,2)) /2,num2str(length_address(i,j))); % Mark line segment distance % end % end % End % Initialize Ant location v=init_population(Ant,PN); v(:,1) =1; V (:, PN) = % starting point1; The finish fit = % short_road_fun (v, the power); % Find the distance between each path T0 = Max (fit)-fit; % Initialize vmFit =[]; vx=[]; P0=0.2; % P0---- Global transfer selection factor P=0.8; % P ---- pheromone evaporation coefficient %C=[]; % start searchfor i_ger=1:Ger
    lamda=1/i_ger; % transfer step parameter [T_Best(i_ger),BestIndex]= Max (T0);for j_g=1:Ant % to calculate the global transition probability r=T0(BestIndex) -t0 (j_g); Prob(i_ger,j_g)=r/T0(BestIndex); End % of100Only the ants make the switchfor j_g_tr=1The Ant % path is stored in the temp variable,1Represents the number of nodes that pass through the columnif 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; % Ants must exist temp(:,end)=1; % Determine whether the temporary path distance after transformation is smaller than the original path. If yes, convert the ant path to the path stored in tempif short_road_fun(temp,power)<short_road_fun(v(j_g_tr,:),power)
            v(j_g_tr,:)=temp; End End % Pheromone update [sol,indb]=min(fit); v(1,:)=v(indb,:); % First ant path save minimum path media=mean(fit); vx=[vx sol]; % vmfit=[vmfit media]; End % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % final result shows that the optimal solution and optimal value % v (indb, disp ()sprintf('Code of shortroad is: %s',num2str(v(indb,:))));
disp(sprintf('\n')); % Empty line disp(sprintf('Shortroad is: %s',num2str(find(v(indb,:)))));
disp(sprintf('Mininum is: %d',sol)); route=find(v(indb,:)); % Draw the node distribution 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; endfor 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); % underlined 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 %% Indicates the shortest pathfor 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); % underlined 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)))); % marks line segment distanceelse
        line([first_address(route(p),1),first_address(1.1)],[first_address(route(p),2),first_address(1.2)].'Color'.'r'.'LineWidth'.5); % underlined 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)))); % marks line segment distanceend
end
axis([0 250 0 400])% Graph shows the trend % of optimal and average function valuesfigure(3);
% plot(vx);
% title('Optimal, trend of average function value');
% xlabel('Generations');
% ylabel('f(x)');
% hold on;
% plot(vmfit,'r');
% hold off;
 
runtime=toc
Copy the code

3. Operation results

Fourth, note

Version: 2014 a