The traveling salesman problem (TSP) asks for the shortest closed route visiting each of $N$ cities exactly once. A route is a Hamiltonian cycle $\sigma$, i.e. a cyclic ordering of the cities, and its cost is the total length

$$ C(\sigma) = \sum_{i=1}^{N} d\left(\sigma_i, \sigma_{i+1}\right), \qquad \sigma_{N+1} \equiv \sigma_1. $$

There are $(N-1)!/2$ distinct cycles, and the problem is NP-hard: no known algorithm finds the optimum in polynomial time. Simulated annealing is a heuristic borrowed from statistical physics [1, 2]. It treats the cost as an energy $H = C$ and samples cycles from the Gibbs measure $\pi_T(\sigma) \propto e^{-C(\sigma)/T}$ at a slowly decreasing temperature $T$, the way a slowly cooled metal settles into a low-energy crystal.

Metropolis sampling

Sampling uses a Markov chain. At each iteration a move proposes a neighboring cycle $\sigma^{\prime}$, and the Metropolis prescription [3] accepts it with probability

$$ A(\sigma \to \sigma^{\prime}) = \min\left(1,\; e^{-\left(C(\sigma^{\prime}) - C(\sigma)\right)/T}\right). $$

Downhill moves are always taken. Uphill moves are taken with a probability that shrinks with $T$, which lets the chain climb out of local minima while $T$ is still high, unlike a greedy local search.

Geometric cooling

The annealing loop samples $n_{\text{iter}}$ iterations at each temperature and then cools:

$$ T_{t+1} = \alpha\, T_t \quad\Longrightarrow\quad T_t = \alpha^t\, T_0, \qquad \alpha \in (0, 1), $$

until the final temperature $T_f$ is reached after $n_{\text{steps}} = 1 + \log(T_f/T_0)/\log\alpha$ steps, for a total of $n_{\text{steps}} \times n_{\text{iter}}$ iterations. Logarithmic schedules $T_t \propto 1/\log(t+1)$ provably reach the global minimum, but only in infinite time [4]. The geometric schedule has no such guarantee, yet in practice it gives very good cycles in reasonable time [5].

Moves

Both moves replace two edges of the cycle, so the cost difference $\Delta C$ of a candidate costs $O(1)$ to compute and no cycle ever has to be summed from scratch. Writing $h$ and $k$ for the cities just before and just after the affected stretch $i \dots j$,

$$ \Delta C = d(h, j) + d(i, k) - d(h, i) - d(j, k). $$

  • Swap exchanges two cities adjacent in the cycle ($j = i + 1$). The moves are tiny, so many of them are needed to change the cycle’s shape.
  • 2-opt [6] reverses the whole stretch between $i$ and $j$: it cuts two edges and reconnects the two resulting paths the other way around. One move can undo a crossing of two edges of any length, which is why 2-opt reaches far cheaper cycles than swap, and the gap grows with $N$.

Domains

Cities are drawn at random from one of the following two-dimensional domains:

  • Uniform: uniform in the unit square $[0, 1]^2$.
  • Correlated: coordinates with correlation $\rho$ [7, 8], mixed from two independent variables $z_1, z_2$ (uniform on $[-\tfrac12, \tfrac12]$ or normal with the same variance $\tfrac{1}{12}$):

$$ x = z_1 \sin\varphi + z_2 \cos\varphi, \qquad y = z_1 \cos\varphi + z_2 \sin\varphi, \qquad \varphi = \tfrac12 \arcsin\rho. $$

The mixing keeps the mean and variance of each coordinate, so $\rho$ is the only thing that changes. At $\rho \to 1$ the cities collapse onto the diagonal and the TSP turns into sorting points on a line, which is easy. Tuning $\rho$ moves the instance from a hard problem to one in P.

  • Power law: each coordinate independently drawn from the two-tailed power law

$$ p(x) = \frac{\gamma - 1}{2 x_0^{1-\gamma}} \, |x|^{-\gamma}, \qquad |x| \geq x_0, $$

with $x_0 = 0.1$ fixed here. For $\gamma \leq 3$ the variance is infinite and a few far outliers dominate the picture. The bulk of the cities then gets squeezed into the middle of the view, which is an honest rendering of the domain rather than a glitch. Note also that $T$ has units of distance: a power-law domain has a different scale than the unit square, so the same $T$ means something different on it.

Performance

The measure of performance is the ratio $C/\langle C_0\rangle$ between the cost reached and the expected cost of a uniformly random cycle on the same cities, which is $N$ times the mean distance between two cities:

$$ \langle C_0 \rangle = \frac{2}{N - 1} \sum_{i < j} d(i, j). $$

For the unit square this is known in closed form, $\langle C_0\rangle = \frac{N}{15}\left(2 + \sqrt2 + 5\ln(1 + \sqrt2)\right) \approx 0.52\\,N$. The shortest cycle only grows like $\sqrt N$, so good final ratios shrink as $N$ grows.

This is a live version of the simulations in my M.Sc. dissertation [8], whose research code is at eliseuv/tsp-sa. Every control acts on the running simulation. The schedule runs on its own, but you can grab the current $T$ at any time to reheat or quench the cycle, or hold it fixed to watch the chain at equilibrium.

P start/stop · A anneal · R randomize

N =

Domain

Move

T0 =

Tf =

α =

niter =

Moves/frame =

T =

t = 0 → C = ⟨C0⟩ = C/⟨C0⟩ = best = accepted =

Cycle

C/⟨C0⟩ vs T (log-log)

C/⟨C0⟩ / best

Acceptance rate

References

  1. S. Kirkpatrick, C. D. Gelatt, M. P. Vecchi, Optimization by Simulated Annealing, Science 220, 671–680 (1983). doi:10.1126/science.220.4598.671
  2. V. Černý, Thermodynamical approach to the traveling salesman problem: An efficient simulation algorithm, Journal of Optimization Theory and Applications 45, 41–51 (1985). doi:10.1007/BF00940812
  3. N. Metropolis, A. W. Rosenbluth, M. N. Rosenbluth, A. H. Teller, E. Teller, Equation of State Calculations by Fast Computing Machines, The Journal of Chemical Physics 21, 1087–1092 (1953). doi:10.1063/1.1699114
  4. S. Geman, D. Geman, Stochastic Relaxation, Gibbs Distributions, and the Bayesian Restoration of Images, IEEE Transactions on Pattern Analysis and Machine Intelligence PAMI-6, 721–741 (1984). doi:10.1109/TPAMI.1984.4767596
  5. Y. Nourani, B. Andresen, A comparison of simulated annealing cooling strategies, Journal of Physics A: Mathematical and General 31, 8373–8385 (1998). doi:10.1088/0305-4470/31/41/011
  6. G. A. Croes, A Method for Solving Traveling-Salesman Problems, Operations Research 6, 791–812 (1958). doi:10.1287/opre.6.6.791
  7. R. da Silva, S. D. Prado, A simple study of the correlation effects in the superposition of waves of electric fields: The emergence of extreme events, Physics Letters A 384, 126231 (2020). doi:10.1016/j.physleta.2019.126231
  8. R. da Silva, E. Venites Filho, A. Alves, A Thorough Study of the Performance of Simulated Annealing in the Traveling Salesman Problem under Correlated and Long Tailed Spatial Scenarios, Physica A: Statistical Mechanics and its Applications (2021). doi:10.1016/j.physa.2021.126067
  9. G. Dantzig, R. Fulkerson, S. Johnson, Solution of a Large-Scale Traveling-Salesman Problem, Journal of the Operations Research Society of America 2, 393–410 (1954). doi:10.1287/opre.2.4.393
  10. S. Lin, B. W. Kernighan, An Effective Heuristic Algorithm for the Traveling-Salesman Problem, Operations Research 21, 498–516 (1973). doi:10.1287/opre.21.2.498
  11. J. Beardwood, J. H. Halton, J. M. Hammersley, The shortest path through many points, Mathematical Proceedings of the Cambridge Philosophical Society 55, 299–327 (1959). doi:10.1017/S0305004100034095
  12. S. Kirkpatrick, Optimization by simulated annealing: Quantitative studies, Journal of Statistical Physics 34, 975–986 (1984). doi:10.1007/BF01009452
  13. P. J. M. van Laarhoven, E. H. L. Aarts, Simulated Annealing: Theory and Applications, (Springer, 1987). doi:10.1007/978-94-015-7744-1
  14. E. H. L. Aarts, J. Korst, Simulated Annealing and Boltzmann Machines: A Stochastic Approach to Combinatorial Optimization and Neural Computing, (Wiley, 1989).
  15. H. E. Stanley, S. V. Buldyrev, The salesman and the tourist, Nature 413, 373–374 (2001). doi:10.1038/35096668
  16. M. E. J. Newman, G. T. Barkema, Monte Carlo Methods in Statistical Physics, (Oxford University Press, 1999). doi:10.1093/oso/9780198517962.001.0001
  17. D. P. Landau, K. Binder, A Guide to Monte Carlo Simulations in Statistical Physics, (Cambridge University Press, 2014). doi:10.1017/CBO9781139696463