蟻の群のアルゴリズムは最短の経路のコードを求めます


%%     
%       
clear all
clc

%         
t0 = clock;

%    
%      :B,C             
citys=xlsread('Chap9_citys_data.xlsx', 'B2:C53');

%%          
n = size(citys,1);
D = zeros(n,n);
for i = 1:n
    for j = 1:n
        if i ~= j
        %           ,         
            D(i,j) = sqrt(sum((citys(i,:) - citys(j,:)).^2));
        else
            D(i,j) = 1e-4;      %          (   0      0)
        end
    end    
end

%%      
m = 75;                              %     
alpha = 1;                           %          
beta = 5;                            %           
vol = 0.2;                           %      (volatilization)  
Q = 10;                               %    
Heu_F = 1./D;                        %     (heuristic function)
Tau = ones(n,n);                     %      
Table = zeros(m,n);                  %      
iter = 1;                            %       
iter_max = 100;                      %        
Route_best = zeros(iter_max,n);      %              
Length_best = zeros(iter_max,1);     %            
Length_ave = zeros(iter_max,1);      %            
Limit_iter = 0;                      %          

%%         
while iter <= iter_max
    %              
      start = zeros(m,1);
      for i = 1:m
          temp = randperm(n);
          start(i) = temp(1);
      end
      Table(:,1) = start; 
      %      
      citys_index = 1:n;
      %         
      for i = 1:m
          %         
         for j = 2:n
             tabu = Table(i,1:(j - 1));           %         (   )
             allow_index = ~ismember(citys_index,tabu);    %     1(    )
             allow = citys_index(allow_index);  %         
             P = allow;
             %          
             for k = 1:length(allow)
                 P(k) = Tau(tabu(end),allow(k))^alpha * Heu_F(tabu(end),allow(k))^beta;
             end
             P = P/sum(P);
             %              
            Pc = cumsum(P);     %    2(    )
            target_index = find(Pc >= rand); 
            target = allow(target_index(1));
            Table(i,j) = target;
         end
      end
      %            
      Length = zeros(m,1);
      for i = 1:m
          Route = Table(i,:);
          for j = 1:(n - 1)
              Length(i) = Length(i) + D(Route(j),Route(j + 1));
          end
          Length(i) = Length(i) + D(Route(n),Route(1));
      end
      %              
      if iter == 1
          [min_Length,min_index] = min(Length);
          Length_best(iter) = min_Length;  
          Length_ave(iter) = mean(Length);
          Route_best(iter,:) = Table(min_index,:);
          Limit_iter = 1; 
          
      else
          [min_Length,min_index] = min(Length);
          Length_best(iter) = min(Length_best(iter - 1),min_Length);
          Length_ave(iter) = mean(Length);
          if Length_best(iter) == min_Length
              Route_best(iter,:) = Table(min_index,:);
              Limit_iter = iter; 
          else
              Route_best(iter,:) = Route_best((iter-1),:);
          end
      end
      %      
      Delta_Tau = zeros(n,n);
      %       
      for i = 1:m
          %       
          for j = 1:(n - 1)
              Delta_Tau(Table(i,j),Table(i,j+1)) = Delta_Tau(Table(i,j),Table(i,j+1)) + Q/Length(i);
          end
          Delta_Tau(Table(i,n),Table(i,1)) = Delta_Tau(Table(i,n),Table(i,1)) + Q/Length(i);
      end
      Tau = (1-vol) * Tau + Delta_Tau;
    %      1,       
    iter = iter + 1;
    Table = zeros(m,n);
end

%%     
[Shortest_Length,index] = min(Length_best);
Shortest_Route = Route_best(index,:);
Time_Cost=etime(clock,t0);
disp(['    :' num2str(Shortest_Length)]);
disp(['    :' num2str([Shortest_Route Shortest_Route(1)])]);
disp(['      :' num2str(Limit_iter)]);
disp(['      :' num2str(Time_Cost) ' ']);

%%   
figure(1) %           
plot([citys(Shortest_Route,1);citys(Shortest_Route(1),1)],...  %      Matlab   
     [citys(Shortest_Route,2);citys(Shortest_Route(1),2)],'o-');
grid on
for i = 1:size(citys,1)
    text(citys(i,1),citys(i,2),['   ' num2str(i)]);
end
text(citys(Shortest_Route(1),1),citys(Shortest_Route(1),2),'         ');
text(citys(Shortest_Route(end),1),citys(Shortest_Route(end),2),'         ');
xlabel('       ')
ylabel('       ')
title(['ACA     (    :' num2str(Shortest_Length) ')'])
figure(2)
plot(1:iter_max,Length_best,'b')
legend('    ')
xlabel('    ')
ylabel('  ')
title('      ')
%--------------------------------------------------------------------------
%%        
% 1. ismember                       ,  0-1  ;
% 2. cumsum              , A=[1, 2, 3, 4, 5],   cumsum(A)=[1, 3, 6, 10, 15]。

『matlab数学モデリング方法と実践』から