Simulated Annealing — ELI5

Imagine you are blindfolded on a hilly landscape, trying to find the lowest valley. You can feel the ground around you, so you always know if a step goes downhill or uphill.

The obvious strategy: always go downhill. But here is the problem — you might walk into a small dip and get stuck there, never knowing there is a much deeper valley just over the next ridge.

Simulated annealing fixes this by doing something unusual: sometimes it takes a step uphill on purpose. Early on, it accepts bad moves frequently, which helps it explore widely and escape shallow dips. Over time, it becomes pickier — fewer uphill steps, more downhill steps — until it settles into the deepest valley it can find.

The name comes from metalworking. When blacksmiths heat metal until it glows and then cool it slowly, the atoms settle into a strong, ordered structure. Cool it too fast and you get a brittle, disordered mess. The slow cooling is the key.

The algorithm works the same way:

  1. Start hot — accept almost any move, good or bad. Explore everywhere.
  2. Cool down gradually — get pickier about accepting bad moves.
  3. Finish cold — only accept improvements. Settle into the best spot found.

People use it for:

  • Scheduling — finding the best arrangement of tasks, flights, or classes
  • Circuit design — placing components on a chip
  • Route planning — like the traveling salesman problem
  • Puzzle solving — Sudoku, crosswords, and other constraint problems

In Python, the algorithm is surprisingly short — just a loop, a random neighbor, and a temperature that cools down.

One thing to remember: Simulated annealing finds great solutions by being strategically imperfect — accepting bad moves early helps it escape traps and explore more of the landscape.

pythonalgorithmsoptimization

See Also

  • Python Bayesian Inference How updating your beliefs with new evidence works — and why it helps computers make smarter guesses.
  • Python Convolution Operations The sliding-window trick that lets computers sharpen photos, recognize faces, and hear words in noisy audio.
  • Python Fourier Transforms How breaking any sound, image, or signal into simple waves reveals hidden patterns invisible to the naked eye.
  • Python Genetic Algorithms How computers borrow evolution's playbook — survival of the fittest, mutation, and reproduction — to solve problems too complicated for brute force.
  • Python Linear Algebra Numpy Why solving puzzles with rows and columns of numbers is the secret engine behind search engines, video games, and AI.