Escolar Documentos
Profissional Documentos
Cultura Documentos
PROBLEM
ARAVIND SESHADRI
Search meaning random routes are made and their corresponding distances calculated.
1
ARAVIND SESHADRI
3. 20 Cities TSP
A generic simulated annealing for 20 cities traveling salesman problem from
TSPLIB2 archive. The best solution found is given in figure 1 and the total optimal
distance to travel 20 cities is 4.030656 units.
The roundtrip for minimal total length for 20 cities is 4.030656 units
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
SIMULATED ANNEALING
[0.6606,0.9695,0.5906,0.2124,0.0398,0.1367,0.9536,0.6091,0.8767,...
0.8148,0.3876,0.7041,0.0213,0.3429,0.7471,0.5449,0.9464,0.1247,0.1636,...
0.8668,;0.9500,0.6740,0.5029,0.8274,0.9697,0.5979,0.2184,0.7148,0.2395,...
0.2867,0.8200,0.3296,0.1649,0.3025,0.8192,0.9392,0.8191,0.4351,0.8646,...
0.6768];
save cities.mat cities -V6;
When datafile function is called an array containing the coordinates of the cities
are loaded into a file called cities.mat. The file is stored in structure format. You
can write your own datafile function by visiting
www.iwr.uni-heidelberg.de/groups/comopt/software/TSPLIB95/tsp
and you can use any data file in the web site. An example is shown below.
function c = loadeil101()
% LOADEIL101 Loads the data file.
% COMMENT : 101-city problem (Christofides/Eilon)
% TYPE : TSP
% DIMENSION : 101
% Source: www.iwr.uni-heidelberg.de/groups/comopt/software/TSPLIB95/tsp
clear all; temp_x = [1 41 49 2 35 17 3 55 45 . . . . . . 98 19 21 99
20 26 100 18 18 101 35 35]; cities = [temp_x(:,2);temp_x(:,3)];
save cities.mat cities -V6;
The first column of the data file from TSPLIB is usually the city number and
the next couple of columns represent the coordinates for the cities. My function
looks for file named as cities.mat in a structure format which when loaded to
the workspace has the variable name cities. I sure there is an elegant way to make
everything completely independent but I am lazy to modify anything. Hence make
sure the data file has the last couple of lines the same unless you know what you
are doing.
3.1.2. Function for calculating the total distance.
function d = distance(inputcities)
% DISTANCE
% d = DISTANCE(inputcities) calculates the distance between n cities as
% required in a Traveling Salesman Problem. The input argument has two rows
% and n columns, where n is the number of cities and each column represent
% the coordinate of the corresponding city.
d = 0;
for n = 1 :
if n ==
d =
else
d =
end
end
length(inputcities)
length(inputcities)
d + norm(inputcities(:,n) - inputcities(:,1));
d + norm(inputcities(:,n) - inputcities(:,n+1));
The cost function for a symmetric traveling salesman problem is the distance between the cities. This function calculates the distance been n cities when distance(n)
function is called.
ARAVIND SESHADRI
SIMULATED ANNEALING
end
3.1.5. Function to perform Simulated Annealing.
function s = simulatedannealing(inputcities,initial_temperature,...
cooling_rate,threshold,numberofcitiestoswap)
% SIMULATEDANNEALING
% S = SIMULATEDANNEALING(inputcities,initial_temperature,cooling_rate)
% returns the new configuration of cities with an optimal solution for the
% traveling salesman problem for n cities.
%
%The input arguments are
% INPUTCITIES
- The cordinates for n cities are represented as 2
%
rows and n columns and is passed as an argument for
%
SIMULATEDANNEALING.
% INITIAL_TEMPERATURE - The initial temperature to start the
%
simulatedannealing process.
% COOLING_RATE
- Cooling rate for the simulatedannealing process.
%
Cooling rate should always be less than one.
% THRESHOLD
- Threshold is the stopping criteria and it is the
%
acceptable distance for n cities.
% NUMBEROFCITIESTOSWAP- Specify the maximum number of pair of cities to
%
swap. As temperature decreases the number of cities
%
to be swapped decreases and eventually reaches one
%
pair of cities.
global iterations;
temperature = initial_temperature;
initial_cities_to_swap = numberofcitiestoswap;
iterations = 1;
complete_temperature_iterations = 0;
while iterations < threshold
previous_distance = distance(inputcities);
temp_cities = swapcities(inputcities,numberofcitiestoswap);
current_distance = distance(temp_cities);
diff = abs(current_distance - previous_distance);
if current_distance < previous_distance
inputcities = temp_cities;
plotcities(inputcities);
if complete_temperature_iterations >= 10
temperature = cooling_rate*temperature;
complete_temperature_iterations = 0;
end
numberofcitiestoswap = round(numberofcitiestoswap...
*exp(-diff/(iterations*temperature)));
if numberofcitiestoswap == 0
numberofcitiestoswap = 1;
end
iterations = iterations + 1;
complete_temperature_iterations = complete_temperature_iterations + 1;
ARAVIND SESHADRI
else
if rand(1) < exp(-diff/(temperature))
inputcities = temp_cities;
plotcities(inputcities);
numberofcitiestoswap = round(numberofcitiestoswap...
*exp(-diff/(iterations*temperature)));
if numberofcitiestoswap == 0
numberofcitiestoswap = 1;
end
complete_temperature_iterations = complete_temperature_iterations + 1;
iterations = iterations + 1;
end
end
clc
fprintf(\t\t\tIterations = %d\n,iterations);
fprintf(\t\t\tTemperature = %3.8f\n,temperature);
fprintf(\t\t\tIn current temperature for %d times\n,...
complete_temperature_iterations);
end
previous_distance
3.1.6. The GUI to execute Simulated Annealing. A GUI is created for experimenting with different data set, with different operating conditions. Figure 2 shows the
GUI. rate as 0.9 and the initial temperature as 20 units. In the GUI play around
SIMULATED ANNEALING
with different initial temperatures, cooling rates and cities to swap. There is no best
recipe which can solve every problem hence you might have to play around a little
bit. A detailed explanation of simulatedannealing function will be presented in
the next section.
4. Simulated Annealing Algorithm
A sample optimal solution for 101 cities is shown in figure 3. Figures 4 - 7 show
The roundtrip for minimal total length for 101 cities is 698.169285 units
80
70
60
50
40
30
20
10
10
20
30
40
50
60
70
ARAVIND SESHADRI
Figure 4
Figure 5
SIMULATED ANNEALING
Figure 6
Figure 7
10
(4.1)
ARAVIND SESHADRI
finally only one pair of cities are swapped. The rate at which this number
is reduced is given by (4.1).
c = round x e T
where, c is the new number of pair of cities to be swept, x is the current number of pair of cities swapped, E is the difference in current and
previous cost function (total distance) and T is the current temperature.
This function is run as long as the threshold is not met. Also the temperature
is not decreased after each iteration as proposed by [5], where the decrease in
temperature is very slow as given in (4.2).
(4.2)
t=
t
1 + t
where, is sufficiently small value and is determined based on the problem. The
cooling schedule is carried out geometrically as given by (4.3)
(4.3)
t = t
where < 1. To make the algorithm realistic, search is carried out at each temperature for a given number of iterations before reducing the temperature. Also
at low temperatures the search is carried out for a longer time before reducing the
temperature. This can be done by simply making the temperature decrease only
when E < 0, while allowing uphill moves without temperature change.
Another key feature is the probability of acceptance when a new configuration
has higher cost function value than the current configuration. Instead of using
(4.4), (4.5) is employed.
0
1
E A
(4.4)
P (A) = e
kT
0
1
E A
(4.5)
P (A) = e
where, k is the number of iterations. This improved the performance of the simulated annealing algorithm when a large TSP problem is considered.
5. Potential Problematic Issues
5.1. Computation Time. One of the main drawbacks for simulated annealing is
the computation time. For better results the cooling has to be carried out very
slowly and this significantly increases the computation time. Clearly when moving
from 20 cities to 101 cities a significant rise in computation time is observed. Various
computations like, computation of the cost function, computation of probability of
acceptance etc. increases the computation time when the dimension of the problem
grows.
SIMULATED ANNEALING
11
12
ARAVIND SESHADRI
SIMULATED ANNEALING
13
14
ARAVIND SESHADRI
References
1. R. E. Burkard and F. Rendl, A thermodynamically motivated simulation procedure for combinatorial optimization problems, European Journal of Operational Research 17 (1984), no. 2,
169 174.
2. David T. Connolly, An improved annealing scheme for the qap, European Journal of Operational Research 46 (1990), no. 1, 93 100.
3. Karl Heinz Hoffmann and Peter Salamon, The optimal simulated annealing schedule for a
simple model, J. Phys. A: Math. Gen. 23 (1990), 3511 3523.
4. Graham Kendall, Simulated annealing, www.cs.nott.ac.uk/ gxk/aim/notes/simulatedannealing.doc,
2005.
5. M Lundy and A Mees, Convergence of an annealing algorithm, Mathematical Programming:
Series A and B 34 (1986), no. 1, 111 124.
6. Lyle A McGeoch, David S Johnson, Cecilia R Aragon, and Catherine Schevon, Optimization
by simulated annealing: An experimental evaluation part ii, graph coloring and number partitioning, Operations Research 39 (1991), 378 406.
E-mail address: aravind.seshadri@okstate.edu