I'm trying to solve the TSP problem using ACO.

4 visualizzazioni (ultimi 30 giorni)
a
a il 11 Giu 2017
Modificato: Walter Roberson il 11 Giu 2017
Hello? I'm studying ACO (Ant colony optimization).
This time I'm trying to solve the TSP problem with ACO.
I apply the acutal data in this code.
but i cant set starting city.
How can I set a starting city?
And one more thing
It originally departed from the departure city in one direction only.
But What should I do to make it start in two directions?
I attach the code i have.
please help me
thank you
%**************************************************************************
% Solving TSP Problem Using ACO
% -------------------------------------------------------------------------
% By : Sutrisno
% Contact: sutrisno_link@yahoo.com Last update: May 2, 2011
% -------------------------------------------------------------------------
% This program is developed to find shortest path (minimum cost)between
% some cities.
%
% There are 4 parts in this program:
% 1.Main program of ACO (myaco.m)
% 2.Function to generate solution (ant_tour.m)
% 3.Function to calculate the cost (distance) (calculate_cost.m)
% 4.Function to update the traces (update_the_trace.m)
%**************************************************************************
%**************************************************************************
% The Program Start Here
%*------------------------------------------------------------------------
% function myaco(num_of_nodes,num_of_ants, max_iteration)
function ex11()
% inputs
miter=10;
m=10;
n=20;
% parameters
e=.15; % evaporation coefficient.
alpha=1; % effect of ants' sight.
beta=4; % trace's effect.
t=0.0001*ones(n); % primary tracing.
el=.97; % common cost elimination.
% -------------------------------------------------------------------------
% Generate coordinates of cities and plot
%for i=1:n
% x(i)=rand*20;
% y(i)=rand*20;
%end
x=[0.375 0.16 0.19 0.21 0.32 0.375 0.435 0.56 0.645 0.545 0.705 0.745 0.79 0.845 0.35 0.349 0.26 0.215 0.275 0.19];
y=[0.945 0.25 0.185 0.21 0.24 0.09 0.085 0.19 0.235 0.84 0.225 0.25 0.21 0.19 0.75 0.75 0.69 0.565 0.5 0.51];
figure(1);
subplot(1,2,1);
plot(x,y,'o','MarkerFaceColor','k','MarkerEdgeColor','b','MarkerSize',10);
title('Coordinates of Cities');
xlabel('x (km)');
ylabel('y (km)');
% generating distace between cities matrix.
for i=1:n
for j=1:n
d(i,j)=sqrt((x(i)-x(j))^2+(y(i)-y(j))^2);
end
end
d=load('factorya.csv');
% generating sight matrix.
for i=1:n
for j=1:n
if d(i,j)==0
h(i,j)=0;
else
h(i,j)=1/d(i,j);
end
end
end
h=h
% ------------------------------------------------------------------------
% Main Algorithm: ACO Meta heuristic procedure
% a. Probabilistic solution construction biased by
% pheromone trails, without forward pheromone
% updating
% b. Deterministic backward path with loop elimination
% and with pheromone updating--> update_the_trace
% c. Evaluation of the quality of the solutions
% generated and use of the solution quality in
% determining the quantity of pheromone to deposit-->calculate_cost
% -------------------------------------------------------------------------
for i=1:miter
% Step 1: Forward ants and solution construction
% Generate places for each ant
for j=1:m
start_places(j,1)=fix(1+rand*(n-1));
end
% Step 2:probabilistic solution contruction
[tour]=ant_tour(start_places,m,n,h,t,alpha,beta);
tour=horzcat(tour,tour(:,1));
% Step 3: Calculate the cost --> total distace
[cost,f]=calculate_cost(m,n,d,tour,el);
[t]=update_the_trace(m,n,t,tour,f,e);
average_cost(i)=mean(cost);
% Step 4: Determine the best route
[min_cost(i),best_index]=min(cost);
besttour(i,:)=tour(best_index,:);
iteration(i)=i;
end
% -------------------------------------------------------------------------
% Plot Average of tour distance vs Number of Iterations
figure(2);plot(iteration,average_cost);
%subplot(2,3,3);
title('Average of tour distance vs Number of iterations');
xlabel('iteration');
ylabel('distance (km)');
% Plot the best route
[k,l]=min(min_cost);
for i=1:n+1
X(i)=x(besttour(l,i));
Y(i)=y(besttour(l,i));
end
figure(1);
subplot(1,2,2);plot(X,Y,'--o',...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10)
xlabel('x (km)');ylabel('y (km)');
title(['minimum cost (total length)= ',num2str(k)]);
end
%**************************************************************************
% Ending of Program
%**************************************************************************

Risposte (0)

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!