Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

4/9/2019 Travelling salesman problem using Ant Colonization optimization - Google Docs

DATA STRUCTURES AND ALGORITHMS - EMBEDDED PROJECT

ADITYA RUHATIYA 18BCE0582


HIRTHIK MATHAVAN 18BCE2036
GANGAVARAPU PRANEETH 18BCE2042
VISHAL VEERAMANI 18BCE2360

Prof. Padma Priya


8 April 2019

https://docs.google.com/document/d/19Y9EYzc59DKpRzClZ7JFLILq7a9qHb26NAloZ6jJg0U/edit 1/13
4/9/2019 Travelling salesman problem using Ant Colonization optimization - Google Docs

Travelling salesman problem using Ant Colonization optimization

Abstract:

The traveling-salesman problem is one of the classical NP-Complete problems, as no current


algorithms are available which can solve these problems in polynomial time, meaning, the
number of steps grows as a polynomial according to the size of the input. The
traveling-salesman problem involves a salesman who must make a tour of a number of
cities using the shortest path available. For each number of cities n, the number of paths
which must be explored is n! causing this problem to grow exponentially rather than as a
polynomial.

The ant colony optimization algorithm (ACO) is a probabilistic technique for computing the
best path of a given problem using a weighted graph. In ACO, a set of software agents
called “Artificial ants” search for the optimal route for the given problem. The artificial ants
incrementally build solutions by moving on the graph. The solution construction process is
biased by a pheromone model, or a set of parameters associated with graph components,
either nodes or edges, whose values are modified at runtime by the ants.

The ACO algorithm is a member of swarm intelligence methods, and it constitutes some
metaheuristic optimizations. Swarm intelligence is the discipline that deals with natural and
artificial systems composed of many individuals that coordinate using decentralized control
and self-organization. Swarm intelligence focuses on the collective behaviors of all the
artificial ants that result from the local interactions with each other and with their
environment.

Introduction:

To apply ACO to the TSP, we consider the graph to be associated with the set of cities with
the set of vertices of the graph. In TSP, it is possible to move from any given city to any
other city, the graph is fully connected and the number of vertices is equal to the number of
cities. Now we can infer that the lengths of the edges between the vertices to be
proportional to the distances between the cities represented by these vertices by associating
the pheromone values and heuristic values with the edges or nodes of the graph.
Pheromone values are modified at runtime and represent the accumulated experience of the
ant colony, while heuristic values are problem dependent values, they are set to be the
inverse of the lengths of the edges.
The artificial ants construct the best path as follows. Each ant starts from a randomly
selected city, in any given vertex of the graph, and it moves at each construction step along
the edges of the graph. Each and keeps track the memory of its path, and in subsequent
steps, it chooses among the edges that do not lead to vertices that it has already visited. An
ant has constructed a solution once it has visited all the vertices of the graph. At each
construction step, an ant probabilistically chooses the edge to follow among those that lead
to yet unvisited vertices. The probabilistic rule is biased by pheromone values and heuristic

https://docs.google.com/document/d/19Y9EYzc59DKpRzClZ7JFLILq7a9qHb26NAloZ6jJg0U/edit 2/13
4/9/2019 Travelling salesman problem using Ant Colonization optimization - Google Docs

Information, also the higher the pheromone and the heuristic value associated with an edge,
the higher the probability an ant will choose that particular edge. Once all the ants have
completed their tour, the pheromone on the edges is updated. Each of the pheromone
values is initially decreased by a certain percentage. Each edge then receives an amount of
additional pheromone proportional to the quality of the solutions to which it belongs (there
is one solution per ant). This procedure is repeatedly applied until a termination criterion is
satisfied.

Algorithm:

1. Initialize:
Set time=0 {time is time counter}
For every edge (i, j) set an initial tij = c for trail density and tij = 0.

2. Set s=0 {s is travel step counter}


For k=1 to l do
Place ant k on a city randomly. Place the city in visitedk. Place the group of the
city in tabuk.

3. Repeat until s =m
Set s=s + 1
For k=1 to l do
Choose the next city to be visited according to the probability pijk was given by Eq.
(1).
Move the ant k to the selected city.
Insert the selected city in visitedk.
Insert the group of the selected city in tabuk.

4. For k=1 to l do
Move the ant k from visitedk(n) to visitedk(1).
Compute the tour length Lk traveled by ant k.
Update the shortest tour found.
For every edge (i, j) do
For k=1 to l do
Update the pheromone trail density tij according to Eqs.
time=time + 1

5. If (time <TIME_MAX) then


Empty all visitedk and tabuk
Goto Step 2.
Else
Print the shortest tour.
Stop

https://docs.google.com/document/d/19Y9EYzc59DKpRzClZ7JFLILq7a9qHb26NAloZ6jJg0U/edit 3/13
4/9/2019 Travelling salesman problem using Ant Colonization optimization - Google Docs

Implementation of Code:

Function: ant_tour

% Function to calculate ants tour matrix during one cycle


%--------------------------------------------------------------------------

function [new_places]=ant_tour(start_places,m,n,h,t,alpha,beta);
for i=1:m
mh=h;
for j=1:n-1
c=start_places(i,j);
mh(:,c)=0;
temp=(t(c,:).^beta).*(mh(c,:).^alpha);
s=(sum(temp));

https://docs.google.com/document/d/19Y9EYzc59DKpRzClZ7JFLILq7a9qHb26NAloZ6jJg0U/edit 4/13
4/9/2019 Travelling salesman problem using Ant Colonization optimization - Google Docs

p=(1/s).*temp;
r=rand;
s=0;
for k=1:n
s=s+p(k);
if r<=s
start_places(i,j+1)=k;
break
end
end
end
end
new_places=start_places;

% End of Function
%--------------------------------------------------------------------------

Function: calculate_cost

% Function to calculate cost (distance) of ants' touring


%--------------------------------------------------------------------------

function [cost,f]=calculate_cost(m,n,d,at,el);
for i=1:m
s=0;
for j=1:n
s=s+d(at(i,j),at(i,j+1));
end
f(i)=s;
end
cost=f;
f=f-el*min(f);

% End of Function
%--------------------------------------------------------------------------

Program: myAco

% This program is developed to find the shortest path between some cities.
%--------------------------------------------------------------------------

% There are 4 parts in this program:

https://docs.google.com/document/d/19Y9EYzc59DKpRzClZ7JFLILq7a9qHb26NAloZ6jJg0U/edit 5/13
4/9/2019 Travelling salesman problem using Ant Colonization optimization - Google Docs

% 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)

% function myaco(num_of_nodes,num_of_ants, max_iteration)


function myaco()
% inputs
miter=10;
m=10;
n=25;
% 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
subplot(3,1,1);
plot(x,y,'o','MarkerFaceColor','k','MarkerEdgeColor','b','MarkerSize',10);
title('Coordinates of Cities');
xlabel('x (km)');
ylabel('y (km)');

% generating distance 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

% 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
% ------------------------------------------------------------------------

https://docs.google.com/document/d/19Y9EYzc59DKpRzClZ7JFLILq7a9qHb26NAloZ6jJg0U/edit 6/13
4/9/2019 Travelling salesman problem using Ant Colonization optimization - Google Docs

% 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


subplot(3,1,2);plot(iteration,average_cost);
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
subplot(3,1,3);plot(X,Y,'--o',...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...

https://docs.google.com/document/d/19Y9EYzc59DKpRzClZ7JFLILq7a9qHb26NAloZ6jJg0U/edit 7/13
4/9/2019 Travelling salesman problem using Ant Colonization optimization - Google Docs

'MarkerSize',10)
xlabel('x (km)');ylabel('y (km)');
title(['minimum cost (total length)= ',num2str(k)]);
end
% End of Program
%--------------------------------------------------------------------------

Function: update_traces

% Function to update the traces


%--------------------------------------------------------------------------

function [t]=update_the_trace(m,n,t,tour,f,e);
for i=1:m
for j=1:n
dt=1/f(i);
t(tour(i,j),tour(i,j+1))=(1-e)*t(tour(i,j),tour(i,j+1))+dt;
end
end
% End of Function
%--------------------------------------------------------------------------

https://docs.google.com/document/d/19Y9EYzc59DKpRzClZ7JFLILq7a9qHb26NAloZ6jJg0U/edit 8/13
4/9/2019 Travelling salesman problem using Ant Colonization optimization - Google Docs

Output:

When m=10 and n=10:


Miter=10

https://docs.google.com/document/d/19Y9EYzc59DKpRzClZ7JFLILq7a9qHb26NAloZ6jJg0U/edit 9/13
4/9/2019 Travelling salesman problem using Ant Colonization optimization - Google Docs

When m=10 and n=20:


miter =10

https://docs.google.com/document/d/19Y9EYzc59DKpRzClZ7JFLILq7a9qHb26NAloZ6jJg0U/edit 10/13
4/9/2019 Travelling salesman problem using Ant Colonization optimization - Google Docs

10

When m=10 and n=30:


miter =20

https://docs.google.com/document/d/19Y9EYzc59DKpRzClZ7JFLILq7a9qHb26NAloZ6jJg0U/edit 11/13
4/9/2019 Travelling salesman problem using Ant Colonization optimization - Google Docs

11

https://docs.google.com/document/d/19Y9EYzc59DKpRzClZ7JFLILq7a9qHb26NAloZ6jJg0U/edit 12/13
4/9/2019 Travelling salesman problem using Ant Colonization optimization - Google Docs

12

Analysis and inferences using graph:

Conclusion:

In this ACO execution, landing at the best case scenario requires adjusting
exploitation-exploration tradeoff. Setting the evaporation coefficient low makes the
pheromones stay longer. Nonetheless, path bias of the colony extremely high, so they get
the opportunity to investigate more alternatives the route with the highest pheromone
concentration, rather than settling in it. ACO is a lot simpler to control, rather than using
any other swarm methods. There are not many parameters required and the investigation
capacity does not tend to leave control. It likewise helps that the ants are randomly placed
in different areas of the map and allowed to make a “guided” initial tour. This makes the
initial values much lower as compared in the case of Brute Force method where initial costs
are comparable to ACO but the cost of the matrix and the time to find the optimal route
increases.

References:

1. https://ljvmiranda921.github.io/notebook/2017/01/18/ant-colony-optimization-tsp/
2. https://www.codeproject.com/articles/644067/applying-ant-colony-optimization-algo
rithms-to-sol
3. https://ieeexplore.ieee.org/document/7970119
4. https://en.wikipedia.org/wiki/Ant_colony_optimization_algorithms
5. https://www.sciencedirect.com/science/article/pii/S1002007108002736

Implementation in Review I: Abstract, Introduction, and Algorithm


Implementation in Review II: myAco code implementation along with ant_tour

https://docs.google.com/document/d/19Y9EYzc59DKpRzClZ7JFLILq7a9qHb26NAloZ6jJg0U/edit 13/13

You might also like