Sub topic 13
Worked Example: Simulated Annealing
Assume you understand basic optimization, can read simple Python, and know that an analogy compares relationships rather than just appearances. This example shows how an idea from metallurgy becomes a search method for difficult optimization problems.
The source problem: cooling metal
In metallurgy, annealing means heating a metal and then cooling it slowly. Heating gives atoms enough energy to move. Slow cooling lets them settle into a low-energy arrangement instead of becoming trapped in a poor arrangement.
The target problem is optimization. An optimization problem asks us to find the best choice from many possible choices. A solution may be a schedule, route, set of parameters, or design. The algorithm assigns each candidate a cost. Lower cost means a better candidate.
The analogy maps the relationships between the two domains:
| Metallurgy | Optimization algorithm |
|---|---|
| Arrangement of atoms | Candidate solution |
| Energy of the metal | Cost of the solution |
| Heat | Willingness to explore |
| Slow cooling | Gradually reducing exploration |
The algorithmic mapping
At each step, simulated annealing creates a nearby candidate. If the candidate is better, the algorithm accepts it. If it is worse, the algorithm may still accept it.
The probability of accepting a worse candidate is:
exp(-cost_increase / temperature)A high temperature makes exploration likely. A low temperature makes the algorithm more selective. This lets the search temporarily move downhill in quality so it can escape a local minimum, which is a solution that looks best nearby but is not best overall.
import math
import random
def anneal(cost, neighbor, x, temp=1.0, cooling=0.995, steps=5000):
"""Minimize cost(x) using a cooling search process."""
best = x
for _ in range(steps):
candidate = neighbor(x)
delta = cost(candidate) - cost(x)
# Keep improvements. Sometimes keep worse moves while hot.
if delta <= 0 or random.random() < math.exp(-delta / temp):
x = candidate
if cost(x) < cost(best):
best = x
temp *= cooling
return best
random.seed(7)
result = anneal(
cost=lambda x: (x - 3) ** 2,
neighbor=lambda x: x + random.uniform(-0.5, 0.5),
x=0.0,
)
print(round(result, 2))The target function has its minimum at x = 3. The neighbor function proposes a nearby number. The algorithm may first accept moves away from 3, but cooling eventually reduces that behavior. The printed result should be close to 3.0.
Where the analogy holds
Both processes use controlled randomness to escape poor arrangements. Both begin with greater movement and become less flexible over time. Both aim to settle into a low-energy or low-cost state.
The analogy breaks in important ways. Metal atoms follow physical laws, while the algorithm uses a chosen probability rule. The algorithm's temperature is not physical heat. Its cooling schedule, neighbor function, and cost function are design choices.
Adapting and evaluating the borrowed idea
For a real problem, adapt three parts:
- Define a cost function that measures the target's constraints.
- Define a neighbor function that makes meaningful small changes.
- Choose a starting temperature, cooling rate, and stopping rule.
Check whether the borrowed relationships still hold. If every neighbor is equally unlikely to be useful, or if cooling happens too quickly, the algorithm may behave like a random search or become stuck early. Compare its results with simpler methods and run it several times because randomness affects the outcome.
Exercise
Why can accepting a worse candidate improve an optimization search?
Show answer
It can move the search out of a local minimum and make a better region reachable later.