2 %> Algorithm 27.7: simulated annealing
for the TSP. Implementation of algorithm 27.7 of \cite Bier15-book
4 %> @note Tested with \ref run2703simAnn.m
8 %> @author Michel Bierlaire
9 %> @date Sun Apr 19 13:30:57 2015
10 %> @ingroup Algorithms
13 %> Simulated annealing
for the TSP
14 %> @param tour initial solution
15 %> @param dist the distance matrix
16 %> @
return bestTour best tour found
17 %> @
return bestTourLength length of the best tour found
18 %> @
return The file \c tspSimAnnIters.dat is also created, containing the details of the iterations.
20 [fff,msg] = fopen(
"tspSimAnnIters.dat",
"w") ;
21 fprintf(fff,
"Iter\tTemp\tCurrent\tBest\n") ;
22 printf(
"Iter\tTemp\tCurrent\tBest\n") ;
29 bestTourLength = currentLength ;
30 typicalIncrease = 5.0 ;
35 a = (lastTau - firstTau) / nbrcooling ;
36 temp = -typicalIncrease/log(firstTau + a * tt) ;
39 while (iter <= maxiter)
41 fprintf(fff,
"%d\t%f\t%f\t%f\n",n,temp,currentLength,bestTourLength)
42 printf(
"%d\t%f\t%f\t%f\n",n,temp,currentLength,bestTourLength)
44 if (mod(iter,10) == 0 )
48 delta = l - currentLength ;
50 current = chosenNeighborhood ;
52 if (l < bestTourLength)
54 bestTourLength = currentLength ;
58 if r < exp(-delta/temp)
59 current = chosenNeighborhood ;
67 fprintf(fff,
"%d\t%f\t%f\t%f\n",n,temp,currentLength,bestTourLength)
68 printf(
"%d\t%f\t%f\t%f\n",n,temp,currentLength,bestTourLength)
function tspSimulatedAnnealing(in tour, in dist)
function tspTourLength(in tour, in dist)
Calculate the length of a tour for the TSP.
function twoOptRandomNeighbor(in cities)
Generate one random 2-opt neighbor for the TSP.