Motivation, Basic Concepts, Basic Methods, Travelling Salesperson Problem (TSP), Algorithms 1
Next Step Application of simulated annealing to the Traveling Salesman problem. 2
Choosing a temperature schedule. Initially, obtain a random list of cities (initial route). Can do by permuting a given list. Perturbing a city list. Many ways to do this, but will show a simple way. Computing the cost function I.e., computing the distance of a route. Presents two different methods. Basic Simulated Annealing flow chart. MatLab code for the flow chart. Results for the 29 City data set. 3
Choosing a Temperature Schedule There are many different possible temperature reduction methods that can be used in SA, aka, cooling strategies or cooling schedules (i.e., temperature cooling equation(s) that implement how the temperature is to be changed at each iteration of the outer loop). They may be categorized as non-adaptive, and adaptive: Non-adaptive temperature reduction schedule The temperature reduction strategy is fixed at the initialization stage and is not changed at any time during the simulated annealing iterative process. I.e., the temperature is decreased at each iteration of the outer loop in accordance with the initial strategy, which is determined and fixed at initialization time. Adaptive cooling strategy The cooling strategy is set at the initialization stage of the simulated annealing process and then, thereafter, the cooling strategy may be changed. The cooling strategy is influenced by the performance of the SA process. The control parameters of the temperature reduction equations or the equations themselves that govern how the temperature is to be changed may change, depending on the performance of the SA process. 4
Let T represent the temperature is the time step (algorithm s iteration number) and are user selectable constants ( is usually set to one) ( is usually close to but less than 1) Exponential: Linear:, or, or Logarithmic:, or 5
The temperature for the next iteration is computed by multiplying the current temperature, found by any fixed schedule, by an adaptive factor, which is based on the difference between the cost of the current solution and the cost of the best solution achieved so far. The new temperature is proportional to the change in cost of the current and best solutions. If the current solution has a larger cost than the best solution, then the new temperature change will be greater. If the current solution has a better cost than that of the best solution, the temperature change is smaller. Other adaptive schemes are possible. 6
First, need to establish the initial temperature, which should be very high, and the final temperature of the cooling strategy, which should be very low. Solve for the initial and final temperatures from Boltzmann s probability function: Set the probability that a worse design could be accepted at the beginning of the optimization process. Set to a high number. Set the probability, that a worse design could be accepted at the end of the optimization process. Set to a low number. Then, if we assume, (which is clearly true at the start of the optimization), then: 7
Choosing How to Decrease Select the total number of outer loop iterations (i.e., cooling cycles). For each Cooling Loop (outer loop) the temperature can be decreased as follows: is the temperature for the next cycle and is the current temperature. 8
Accordingly, 9
Generate Initial City Route List Requirements Write MatLab code to generate a list of cities, where Cities are chosen according to a uniform random distribution Options No city appears more than once All cities are included in the list. Write custom routine to generate a list of cities Use Matlab s library routine randperm. 10
Generate Initial City Route (Using Matlab Library randperm) P = randperm(n) returns a vector containing a random permutation of the integers 1:N. For example, randperm(6) might be [2 4 5 6 1 3]. Therefore, to get an initial city route of 10 cities: cityroute = randperm(10); g=sprintf('%d ', cityroute); fprintf('city route before = %s\n', g); cityroute = randperm(10); g=sprintf('%d ', cityroute); fprintf('city route after = %s\n', g); Example Output: City route before = 1 9 7 5 4 10 8 6 3 2 City route after = 1 6 2 3 5 9 4 8 10 7 11
Generate Initial City Route (Using Custom Function) The city list is stored in a vector of -components, where is the number of cities. One potential way to permute the list: Starting from the first component in the list, and continuing on to the end of the list, for every component of the list, choose a component randomly. Swap the positions of these two components in the list. If, no swap will be done, but this is not a problem since this process is random. for i=1:numcities j = randi(numcities); temp = cityroute(i); cityroute (i) = cityroute(j); cityroute (j) = temp; end This will create the same random permutation each time it is run. A different seed is required to create different permutation. 12
Generate Initial City Route (Using Custom Function) cityroute = randperm(10); g=sprintf('%d ', cityroute); fprintf('city route before = %s\n', g); numcities = size(cityroute',1); for i=1:numcities j = randi(numcities); temp = cityroute(i); cityroute (i) = cityroute(j); cityroute (j) = temp; end Example Output: City route before = 4 2 6 7 10 3 8 1 5 9 City route after = 7 3 4 1 10 2 6 9 5 8 g=sprintf('%d ', cityroute); fprintf('city route after = %s\n', g); 13
Perturbing the City Route List For each iteration of the Equilibrium loop, the SA algorithm needs to change the city route list (cityroute). There are many ways in which the cityroute may be changed (perturbed). One potential way Choose two indices of the list at random, using a uniform distribution Swap the positions of these two cities in the list. For example: If indices 1 and 3 were chosen at random, then city 3 would swap position with city 4. Index City Before 1 2 3 4 5 3 2 4 1 5 Index City After 1 2 3 4 5 4 2 3 1 5 14
Perturbing the City Route List cityroute = randperm(5); randindex1 = randi(5); Example Output: alreadychosen = true; while alreadychosen == true Random index 1 = 4 randindex2 = randi(5); Random index 2 = 5 if randindex2 ~= randindex1 City route before = 2 3 5 1 4 alreadychosen = false; City route after = 2 3 5 4 1 end end fprintf('random index 1 = %d\n', randindex1); fprintf('random index 2 = %d\n', randindex2); g=sprintf('%d ', cityroute); fprintf('city route before = %s\n', g); temp = cityroute(randindex1); cityroute(randindex1) = cityroute(randindex2); cityroute(randindex2) = temp; g=sprintf('%d ', cityroute); fprintf('city route after = %s\n', g); 15
A city-distance data set may be represented in many ways. Examples: 1. Two dimensional array, where each element denotes the distance between city and city. d(1,1) d(1,2) d(1,3) d(1,4) d(1,5) d(2,1) d(2,2) d(2,3) d(2,4) d(2,5) d(3,1) d(3,2) d(3,3) d(3,4) d(3,5) d(4,1) d(4,2) d(4,3) d(4,4) d(4,5) d(5,1) d(5,2) d(5,3) d(5,4) d(5,5) 2. Each entry in a list represents the Euclidean coordinates of the city. So, you need to compute the distances. 16
Assume the data set is a two dimensional array, where each element denotes the distance between city and city. Let represent the city list route, i.e., is a list of city indices, that represents the route to take by the TS person. The cost function is the round trip distance ( is the number of cities): 17
Computing Route Cost Example The cost function is the round trip distance : d(1,1) d(1,2) d(1,3) d(1,4) d(1,5) d(2,1) d(2,2) d(2,3) d(2,4) d(2,5) d(3,1) d(3,2) d(3,3) d(3,4) d(3,5) d(4,1) d(4,2) d(4,3) d(4,4) d(4,5) d(5,1) d(5,2) d(5,3) d(5,4) d(5,5) Index 1 2 3 4 5 3 2 4 1 5 18
Computing Route Cost Example Now assume the distances are stored in a one dimensional vector (i.e., in a File), and the distances are symmetric. For example: d(1,1) d(1,2) d(1,3) d(1,4) d(1,5) d(2,1) d(2,2) d(2,3) d(2,4) d(2,5) d(3,1) d(3,2) d(3,3) d(3,4) d(3,5) d(4,1) d(4,2) d(4,3) d(4,4) d(4,5) d(5,1) d(5,2) d(5,3) d(5,4) d(5,5) File d(1,1) = 0.00 d(1,2) = d(2,1) d(1,3) = d(3,1) d(1,4) = d(4,1) d(1,5) = d(5,1) d(2,1) = d(1,2) d(2,2) = 0.00 d(2,3) = d(3,2) d(2,4) = d(4,2) d(2,5) = d(5,2) d(3,1) = d(1,3) d(3,2) = d(2,3) d(3,3) = 0.00 d(3,4) = d(4,3) d(3,5) = d(5,3) d(4,1) = d(1,4) d(4,2) = d(2,4) d(4,3) = d(3,4) d(4,4) = 0.00 d(4,5) = d(5,4) d(5,1) = d(1,5) d(5,2) = d(2,5) d(5,3) = d(3,5) d(5,4) = d(4,5) d(5,5) = 0.00 19
Computing Route Cost Example Index City 1 2 3 4 5 3 2 4 1 5 d(1,1) = 0.00 d(2,1) = d(1,2) d(3,1) = d(1,3) d(4,1) = d(1,4) d(5,1) = d(1,5) d(1,2) = d(2,1) d(2,2) = 0.00 d(3,2) = d(2,3) d(4,2) = d(2,4) d(5,2) = d(2,5) d(1,3) = d(3,1) d(2,3) = d(3,2) d(3,3) = 0.00 d(4,3) = d(3,4) d(5,3) = d(3,5) d(1,4) = d(4,1) d(2,4) = d(4,2) d(3,4) = d(4,3) d(4,4) = 0.00 d(5,4) = d(4,5) d(1,5) = d(5,1) d(2,5) = d(5,2) d(3,5) = d(5,3) d(4,5) = d(5,4) d(5,5) = 0.00 20
Row in File 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 File 00.00 12.00 13.00 14.00 15.00 12.00 00.00 23.00 24.00 25.00 13.00 23.00 00.00 34.00 35.00 14.00 24.00 34.00 00.00 45.00 15.00 25.00 35.00 45.00 00.00 d(1,1) = 0.00 d(2,1) = d(1,2) d(3,1) = d(1,3) d(4,1) = d(1,4) d(5,1) = d(1,5) Computing Route Cost d(1,2) = d(2,1) d(2,2) = 0.00 d(3,2) = d(2,3) d(4,2) = d(2,4) d(5,2) = d(2,5) Example Index City 1 2 3 4 5 3 2 4 1 5 d(1,3) = d(3,1) d(2,3) = d(3,2) d(3,3) = 0.00 d(4,3) = d(3,4) d(5,3) = d(3,5) d(1,4) = d(4,1) d(2,4) = d(4,2) d(3,4) = d(4,3) d(4,4) = 0.00 d(5,4) = d(4,5) d(1,5) = d(5,1) d(2,5) = d(5,2) d(3,5) = d(5,3) d(4,5) = d(5,4) d(5,5) = 0.00 21
Computing Route Cost MatLab Code with Example cityroute = [3 2 4 1 5]; Index 1 2 3 4 5 Distances = load('5x5symmetric.txt'); D=0; n=5; for i=1:n-1 cityroute 3 2 4 1 5 D = D + Distances((cityRoute(i)-1)*n+cityRoute(i+1)); end D = D + Distances((cityRoute(n)-1)*n+cityRoute(1)); Output: D = 111 Distances 00.00 12.00 13.00 14.00 15.00 12.00 00.00 23.00 24.00 25.00 13.00 23.00 00.00 34.00 35.00 14.00 24.00 34.00 00.00 45.00 15.00 25.00 35.00 45.00 00.00 22
Computing Route Cost Euclidean Distance Format (TSPLIB) The TSPLIB is a standard format for representing cities for the travelling salesperson problem (TSP). Each entry in the file denotes the Euclidean coordinates of the city. To determine the distance of a TSP route, the Euclidean metric is used. For example: Example: a 10x2 citycoords (cc) array, that holds the coordinates for each city to be visited. City 1 2 3 4 5 6 7 8 9 10 x-coordinate y-coordinate 20900.0000 17066.6667 21300.0000 13016.6667 21600.0000 14150.0000 21600.0000 14966.6667 21600.0000 16500.0000 22183.3333 13133.3333 22583.3333 14300.0000 22683.3333 12716.6667 23616.6667 15866.6667 23700.0000 15933.3333 cc cc cc cc cc cc cc cc 23
EUC_2D_29.txt Computing Route Cost Matlab Code with Example 20833.3333 17100.0000 20900.0000 17066.6667 21300.0000 13016.6667 21600.0000 14150.0000 21600.0000 14966.6667 21600.0000 16500.0000 22183.3333 13133.3333 22583.3333 14300.0000 22683.3333 12716.6667 23616.6667 15866.6667 23700.0000 15933.3333 23883.3333 14533.3333 24166.6667 13250.0000 25149.1667 12365.8333 26133.3333 14500.0000 26150.0000 10550.0000 26283.3333 12766.6667 26433.3333 13433.3333 26550.0000 13850.0000 26733.3333 11683.3333 27026.1111 13051.9444 27096.1111 13415.8333 27153.6111 13203.3333 27166.6667 9833.3333 27233.3333 10450.0000 27233.3333 11783.3333 27266.6667 10383.3333 27433.3333 12400.0000 27462.5000 12992.2222 cc = load('euc_2d_29.txt'); D=0; n=29; for i=1:n-1 D = D + sqrt((cc(i,1) - cc(i+1,1))^2 + (cc(i,2) - cc(i+1,2))^2); end D = D + sqrt((cc(n,1) - cc(1,1))^2 + (cc(n,2)- cc(1,2))^2); D Matlab output: D = 5.2284e+04 24
EUC_2D_29.txt 20833.3333 17100.0000 20900.0000 17066.6667 21300.0000 13016.6667 21600.0000 14150.0000 21600.0000 14966.6667 21600.0000 16500.0000 22183.3333 13133.3333 22583.3333 14300.0000 22683.3333 12716.6667 23616.6667 15866.6667 23700.0000 15933.3333 23883.3333 14533.3333 24166.6667 13250.0000 25149.1667 12365.8333 26133.3333 14500.0000 26150.0000 10550.0000 26283.3333 12766.6667 26433.3333 13433.3333 26550.0000 13850.0000 26733.3333 11683.3333 27026.1111 13051.9444 27096.1111 13415.8333 27153.6111 13203.3333 27166.6667 9833.3333 27233.3333 10450.0000 27233.3333 11783.3333 27266.6667 10383.3333 27433.3333 12400.0000 27462.5000 12992.2222 Usually in TSP problems, the city route is stored (represented) in city index format, rather than coordinate format. For example, cityroute (cr): 9 18 16 7 12 4 10 15 24 27 17 11 25 6 20 22 14 21 28 29 13 5 1 23 19 3 2 26 8 An entry in the route denotes the index of the city. The index references the input coordinates file. To determine the distance of the route stored in the index format, we must use the index in the route array to extract the coordinates from the input file. For example, let the file array be called cc and the route array be called cr: cc cc cc cc cc cc cc cc 25
Set #ncl, itemp. Done Get Initial Route Y #ncl s==0? Dec # ncl N Compute Distance of Initial Route Set #nel perturbroute N Equilibrium Loop: The temperature is held constant, while the system reaches equilibrium, i.e., until the best route if found for the given temperature. Compute Distance of Route Reduce Temperature Y #nel ==0? Dec # nel N? Worse Route? ncl=numcoolingloops nel=numequilibriumloops N? Y genrand # Y Find Prob of Acceptance, 26
clc; clear; close all; cc = load('euc_2d_29.txt'); numcities = size(cc,1); x=cc(1:numcities, 1); y=cc(1:numcities, 2); x(numcities+1)=cc(1,1); y(numcities+1)=cc(1,2); figure hold on plot(x',y','.k','markersize',14) labels = cellstr( num2str([1:numcities]') ); %' # labels text(x(1:numcities)', y(1:numcities)', labels,... 'VerticalAlignment','bottom',... 'HorizontalAlignment','center'); ylabel('y Coordinate', 'fontsize', 18, 'fontname', 'Arial'); xlabel('x Coordinate', 'fontsize', 18, 'fontname', 'Arial'); title('city Coordinates', 'fontsize', 20, 'fontname', 'Arial'); 27
28
numcoolingloops = 1100; numequilbriumloops = 100; pstart = 0.6; % Probability of accepting worse solution at the start pend = 0.001; % Probability of accepting worse solution at the end tstart = -1.0/log(pStart); % Initial temperature tend = -1.0/log(pEnd); % Final temperature frac = (tend/tstart)^(1.0/(numcoolingloops-1.0));% Fract temp reduction cityroute_i = randperm(numcities); % Get initial route cityroute_b = cityroute_i; % Best route cityroute_j = cityroute_i; % Current route cityroute_o = cityroute_i; % Optimal route % Initial distances D_j = computeeucdistance(numcities, cc, cityroute_i); D_o = D_j; D_b = D_j ; D(1) = D_o; numacceptedsolutions = 1.0; tcurrent = tstart; % Current temperature = initial temperature DeltaE_avg = 0.0; % DeltaE Average 29
for i=1:numcoolingloops disp(['cycle: ',num2str(i),' starting temp: ',num2str(tcurrent)]) for j=1:numequilbriumloops cityroute_j = perturbroute(numcities, cityroute_b); D_j = computeeucdistance(numcities, cc, cityroute_j); DeltaE = abs(d_j-d_b); if (D_j > D_b) % if cost is higher, then: if (i==1 && j==1) DeltaE_avg = DeltaE; end p = exp(-deltae/(deltae_avg * tcurrent)); if (p > rand()) accept = true; else accept = false; end else accept = true; % cost is lower, keep solution end 30
if (accept==true) cityroute_b = cityroute_j; D_b = D_j; numacceptedsolutions = numacceptedsolutions + 1.0; DeltaE_avg = (DeltaE_avg * (numacceptedsolutions-1.0) +... DeltaE) / numacceptedsolutions; end end // j=1:numequilbriumloops tcurrent = frac * tcurrent; % Lower temp for next cooling cycle cityroute_o = cityroute_b; % Record the best route D(i+1) = D_b; % Record each route distance D_o = D_b; end 31
Best Route Distance Found: 29,702.3 m 29,702.3 32
33
[1] J. D. Hedengren, "Optimization Techniques in Engineering," 5 April 2015. [Online]. Available: http://apmonitor.com/me575/index.php/main/homepage. [Accessed 27 April 2015]. [2] A. R. Parkinson, R. J. Balling and J. D. Heden, "Optimization Methods for Engineering Design Applications and Theory," Brigham Young University, 2013. 34