Ant colony algorithm matlab for solving VRP problem

Source: Internet
Author: User

Excel Exp12_3_2.xls Content:

ANT_VRP function:

function [R_BEST,L_BEST,L_AVE,SHORTEST_ROUTE,SHORTEST_LENGTH]=ANT_VRP (D,DEMAND,CAP,ITER_MAX,M,ALPHA,BETA,RHO,Q) Percent R_best the best route of each generation l_best the length of the best route of each generation L_ave the average distance of each generation Shortest_route shortest path percent shortest_length shortest path length percent of the distance matrix between cities, for symmetric matrices percent D Emand customer demand% CAP vehicle maximum load Iter_max maximum iteration count percent m ant number percent Alpha characterization pheromone importance degree parameter percent Beta characterization heuristic factor importance degree parameter percent of Rho evaporation coefficient percent Q pheromone increase strength coefficient                 N=size (d,1);           T=zeros (M,2*n);          % load Distance eta=ones (m,2*n);            % heuristic factor tau=ones (n,n);          % pheromone Tabu=zeros (m,n);       % Taboo table Route=zeros (m,2*n);             % path L=zeros (m,1);   % Total distance L_best=zeros (iter_max,1); % of the best route length R_best=zeros (iter_max,2*n);       % of the best route nc=1;while nc<=iter_max% stop condition Eta=zeros (m,2*n);      T=zeros (M,2*n);    Tabu=zeros (M,n);    Route=zeros (M,2*n);        L=zeros (m,1);      %%%%%%============== Initialize the beginning of the City (taboo table) ==================== for i=1:m Cap_1=cap;        % maximum loading capacity j=1;        J_r=1;    While Tabu (i,n) ==0 T=zeros (m,2*n);             % Loading Load matrixTabu (i,1) = 1;      % Taboo table Start position is 1 Route (i,1) = 1;   % path start position is 1 visited=find (Tabu (i,:) >0);        % visited city Num_v=length (visited);         % visited city number J=zeros (1, (N-num_v));                          % to be visited city load table p=j;                         % to be visited city selection probability distribution jc=1; % to be visited city select pointer for k=1:n% city if Length (Find (Tabu (i,:) ==k) ==0% if K is not visited city code                     , K is added to J (Jc) =k in Matrix J;                 jc=jc+1;                          End end%%%%%%%============= Each ant traverses all cities according to the chosen probabilities ================== For k=1:n-num_v% cities to visit if Cap_1-demand (J (1,k), 1) >=0% if vehicle loading is greater than the city to visit                         City demand if Route (i,j_r) ==1% if each ant in the beginning City T (i,k) =d (1,j (1,k));  P (k) = (Tau (1,j (1,k)) ^alpha) * ((1/t (i,k)) ^beta);         % of molecule else in probability calculation formula                % if each ant is not in the beginning City T (i,k) =d (Tabu (I,j), J (1,k)); P (k) = (Tau (Tabu (i,visited (end)), J (1,k)) ^alpha) * ((1/t (i,k)) ^beta);                     % probability formula in the numerator end else if the vehicle load is less than the city needs to be visited                     T (i,k) = 0;                 P (k) = 0;                 End End If Length (Find (T (i,:) >0)) ==0%%% when the vehicle load is less than the city you want to visit, select the starting point of 1                 Cap_1=cap;                 j_r=j_r+1;                               Route (I,j_r) = 1;                L (i) =l (i) +d (1,tabu (i,visited (end)));                 else p=p/(sum (P));               % Select a city pcum=cumsum (P) according to the probability principle;       The cumulative probability and: Cumsum ([1 2 3]) =1 3 6, the purpose of which is to make the value of pcum always be greater than the number of Rand Select=find (Pcum>rand);       % Select the next city by probability: when the cumulative probability and is greater than the given random number, then the sum is added to the last city as the forthcoming city of O_visit=j (1,select (1));                 % to be visited city j=j+1;j_r=j_r+1;             Tabu (i,j) =o_visit;                 % to be visited City Route (I,j_r) =o_visit;  Cap_1=cap_1-demand (o_visit,1);       % Vehicle Load remaining L (i) =l (i) +t (I,select (1));               % path length end end L (i) =l (i) +d (Tabu (I,n), 1);             Percent Path length end l_best (NC) =min (L);           The optimal path is the shortest path Pos=find (l==min (L));  % find the location of the optimal path: which Ant r_best (NC,:) =route (POS (1),:);            % determine the optimal path corresponding to the city order L_ave (NC) =mean (L) ';            The average distance Delta_tau=zeros (n,n) for the K-iteration is obtained;    %delta_tau (I,J) indicates that all ants remain in the first city to the J City path on the pheromone increment l_zan=l_best (1:nc,1);    Post=find (L_zan==min (L_zan));    Cities=find (R_best (NC,:) >0);        Num_r=length (Cities); For k=1:num_r-1% establishes the full path after releasing pheromone Delta_tau (R_best (nc,k), R_best (nc,k+1)) =delta_tau (R_best (nc,k), R_best (nc,k+    1)) +q/l_best (NC);        End Delta_tau (R_best (Nc,num_r), 1) =delta_tau (R_best (Nc,num_r), 1) +q/l_best (NC);                Tau=rho*tau+delta_tau; Nc=nc+1;endshOrtest_route=zeros (1,2*n); % Extract Shortest path Shortest_route (1,:) =r_best (Iter_max,:); Shortest_route=shortest_route (shortest_route>0); Shortest_route=[shortest_route Shortest_route (+)];           Shortest_length=min (l_best); % Extract Shortest path length%l_ave=mean (l_best);

solver:

Clc;clear all%% ============== Extract Data ==============[xdata,textdata]=xlsread (' Exp12_3_2.xls '); % load data from 20 cities, data is saved in Excel file Exp12_3_1.xls x_label=xdata (:, 2) According to the table location; % second column horizontal y_label=xdata (:, 3);  % The third column is the Ordinate demand=xdata (:, 4);      % of the demand C=[x_label Y_label];        % coordinate matrix n=size (c,1);       %n indicates the number of nodes (customers) is ============== calculated distance matrix ==============d=zeros (n,n); %d represents the full graph of the weighted adjacency matrix, that is, the distance matrix D is initialized for the 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;   % calculates the distance between two cities else D (i,j) = 0;  %i=j, the distance is 0; end D (j,i) =d (I,J); The% distance matrix is symmetric matrix endendalpha=1; beta=5; rho=0.75;iter_max=100; q=10;  cap=1;m=20; %cap for maximum vehicle load [R_BEST,L_BEST,L_AVE,SHORTEST_ROUTE,SHORTEST_LENGTH]=ANT_VRP (D,demand,cap,iter_max,m,alpha,beta,rho , Q); The% ant colony algorithm solves the VRP problem general function, see the companion disc shortest_route_1=shortest_route-1% extract the best route shortest_length% extract shortest path length percent ====== ======== plot ==============figure (1)% as iterative convergence curve x=linspace (0,iter_max,iter_max); Y=l_best (:, 1);p lot (x, y); Xlabel (' Iteration count ‘); Ylabel (' Shortest path length '); figurE (2)% as the shortest path plot ([C (shortest_route,1)],[c (shortest_route,2)], ' O '), grid onfor I =1:size (c,1) text (c (i,1), C (i,2), [' ' Num2str (i-1)]); Endxlabel (' customer's horizontal axis '); Ylabel (' Customer ordinate ');

  

Ant colony algorithm matlab for solving VRP problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.