Simulated Annealing

GATE Relevance: Medium

Prerequisites

  • Hill Climbing — local search paradigm, local maxima problem
  • Basic probability — understanding of ex and random sampling

🎯 Learning Objectives

  • Understand simulated annealing as hill climbing that can go “downhill”
  • Explain the acceptance probability eΔE/T
  • Write the SA pseudocode
  • Compare cooling schedules: linear, geometric, logarithmic
  • Analyze convergence guarantees
  • Apply SA to an optimization example

1. Physical Analogy: Metallurgical Annealing

In metallurgy, annealing is the process of heating metal to a high temperature and then slowly cooling it. At high temperatures, atoms move freely and can escape unfavorable configurations. As the temperature decreases, atoms settle into a low-energy crystalline structure — ideally the global minimum energy state.

From Physics to Optimization

Simulated Annealing (SA) applies this idea to optimization. The “energy” is the objective function value. The “temperature” controls how willing the algorithm is to accept worse moves. At high temperature, SA explores broadly (accepting bad moves). As temperature decreases, SA becomes more selective, converging to a local (hopefully global) optimum.

SA was introduced by Kirkpatrick, Gelatt, and Vecchi (1983), inspired by the Metropolis algorithm from statistical mechanics (1953).

2. The SA Algorithm

SA is a modification of hill climbing. The key difference: when a random neighbor is worse than the current state, SA doesn’t always reject it. Instead, it accepts the worse move with a probability that depends on:

  • ΔE = VALUE(neighbor) − VALUE(current) — how much worse the move is
  • T = current temperature — how “hot” the system is

2.1 Acceptance Probability

For a maximization problem (we want to increase VALUE):

  • If ΔE ≥ 0 (neighbor is better or equal): always accept.
  • If ΔE < 0 (neighbor is worse): accept with probability P = eΔE / T.

Understanding P = eΔE / T

Since ΔE < 0 (a bad move), ΔE/T is negative, so P is between 0 and 1.

  • High T: ΔE/T ≈ 0, so P ≈ e0 = 1. Almost all moves accepted. (Random walk.)
  • Low T: ΔE/T is a large negative number, so P ≈ 0. Bad moves rarely accepted. (Hill climbing.)
  • Small |ΔE|: Slightly worse moves are accepted more easily than very bad moves.

2.2 Full Pseudocode

function SIMULATED-ANNEALING(problem, schedule):
    // schedule: a mapping from time t to temperature T
    current = problem.initial_state
    best = current                         // track overall best

    for t = 1 to ∞:
        T = schedule(t)
        if T == 0:
            return best                    // frozen — return best found

        neighbor = RANDOM-NEIGHBOR(current)
        ΔE = VALUE(neighbor) - VALUE(current)

        if ΔE > 0:                         // neighbor is better
            current = neighbor
        else:                              // neighbor is worse
            // Accept with probability e^(ΔE/T)
            if RANDOM(0,1) < e^(ΔE / T):
                current = neighbor

        if VALUE(current) > VALUE(best):
            best = current

    return best

📝 Minimization Version

For minimization problems (minimize cost), flip the sign: ΔE = COST(current) − COST(neighbor). If ΔE > 0, the move is an improvement. If ΔE < 0, accept with probability eΔE / T. Equivalently, use P = e−|ΔE| / T when the move is worse.

3. Cooling Schedules

The cooling schedule determines how temperature decreases over time. It critically affects SA’s performance.

ScheduleFormulaCooling RateProperties
Linear T(t) = T₀ − α·t Constant decrease per step Simple; may cool too fast. T reaches 0 at t = T₀/α.
Geometric T(t) = T₀ · αt, where 0 < α < 1 Multiplicative; typically α = 0.95–0.99 Most commonly used. Good balance of exploration and exploitation. T → 0 asymptotically.
Logarithmic T(t) = c / ln(1 + t) Very slow decrease Theoretically guaranteed to find global optimum (but impractically slow).
Geometric Cooling Example

T₀ = 100, α = 0.95:

t=0:  T = 100.0
t=10: T = 100 × 0.95^10  = 59.87
t=20: T = 100 × 0.95^20  = 35.85
t=50: T = 100 × 0.95^50  = 7.69
t=100: T = 100 × 0.95^100 = 0.59
t=150: T = 100 × 0.95^150 = 0.05  (nearly frozen)

4. Convergence Guarantee

A famous theoretical result states that SA converges to the global optimum with probability 1 if the cooling schedule is sufficiently slow. Specifically:

Convergence Theorem

If T(t) ≥ c / ln(1 + t) for some constant c ≥ ΔEmax (the largest energy barrier), then SA is guaranteed to reach the global optimum as t → ∞.

In practice, logarithmic cooling is too slow. The geometric schedule T(t) = T₀·αt does not theoretically guarantee convergence, but works well empirically. The art of SA is choosing the right T₀, α, and stopping criterion for a given problem.

4.1 SA vs Hill Climbing

PropertyHill ClimbingSimulated Annealing
Accepts worse moves?NeverYes, with probability eΔE/T
Escapes local optima?NoYes (at high T)
Deterministic?Yes (steepest ascent)No (stochastic)
Convergence guarantee?No (local optimum only)Yes (with log cooling; theoretical)
Parameters to tune?NoneT₀, cooling rate, stopping criterion
MemoryO(1)O(1)

5. Worked Example: Minimizing a Function

Example: Optimize f(x) = −(x−3)² + 10

Goal: maximize f(x). Global max at x = 3, f(3) = 10. Neighbor: x ± random step in [−2, 2].

Settings: T₀ = 10, geometric cooling α = 0.8.

Step 0: x = 7, f(7) = -(7-3)² + 10 = -6.   T = 10.
  Neighbor: x' = 5.5, f(5.5) = -(2.5)² + 10 = 3.75
  ΔE = 3.75 - (-6) = 9.75 > 0. Accept. x = 5.5

Step 1: T = 10 × 0.8 = 8.
  Neighbor: x' = 6.8, f(6.8) = -(3.8)² + 10 = -4.44
  ΔE = -4.44 - 3.75 = -8.19 < 0.
  P = e^(-8.19/8) = e^(-1.024) ≈ 0.359. Roll: 0.22 < 0.359. Accept! x = 6.8

Step 2: T = 8 × 0.8 = 6.4.
  Neighbor: x' = 4.9, f(4.9) = -(1.9)² + 10 = 6.39
  ΔE = 6.39 - (-4.44) = 10.83 > 0. Accept. x = 4.9

Step 3: T = 6.4 × 0.8 = 5.12.
  Neighbor: x' = 3.1, f(3.1) = -(0.1)² + 10 = 9.99
  ΔE = 9.99 - 6.39 = 3.6 > 0. Accept. x = 3.1

Step 4: T = 5.12 × 0.8 = 4.096.
  Neighbor: x' = 4.5, f(4.5) = -(1.5)² + 10 = 7.75
  ΔE = 7.75 - 9.99 = -2.24 < 0.
  P = e^(-2.24/4.096) = e^(-0.547) ≈ 0.578. Roll: 0.81 > 0.578. Reject.

Step 5: T = 4.096 × 0.8 = 3.277.
  Neighbor: x' = 2.8, f(2.8) = -(0.2)² + 10 = 9.96
  ΔE = 9.96 - 9.99 = -0.03 < 0.
  P = e^(-0.03/3.277) ≈ 0.991. Roll: 0.15 < 0.991. Accept. x = 2.8

...continues, converging around x ≈ 3.

Notice how at Step 1, SA accepted a worse move (from f=3.75 to f=−4.44). This is the key mechanism that lets SA escape local optima. As T decreases, such large downhill moves become exponentially less likely.

6. GATE-Relevant: Acceptance Probability Calculations

GATE questions often ask you to compute the acceptance probability given specific values.

Typical GATE-Style Question

Q: In simulated annealing (maximization), current state has value 50, neighbor has value 45, temperature T = 10. What is the probability of accepting the neighbor?

A: ΔE = 45 − 50 = −5. P = e−5/10 = e−0.5 ≈ 0.6065.

Another Example

Q: Same as above but T = 2. P = ?

A: ΔE = −5. P = e−5/2 = e−2.5 ≈ 0.0821.

Much lower! At lower temperature, bad moves are rarely accepted.

Key Insight for GATE

Q: At what temperature will a ΔE = −10 move be accepted with probability 0.5?

A: 0.5 = e−10/T ⇒ ln(0.5) = −10/T ⇒ −0.693 = −10/T ⇒ T = 10/0.693 ≈ 14.43.

7. Common Applications

  • Travelling Salesman Problem (TSP) — neighbor: swap two cities in the tour
  • VLSI circuit layout — the original application (Kirkpatrick et al.)
  • Job scheduling — minimize makespan or tardiness
  • Protein folding — minimize free energy
  • N-Queens — minimize attacking pairs
  • Graph partitioning — minimize edge cuts

Summary

  • SA = hill climbing + stochastic acceptance of worse moves (controlled by temperature).
  • Acceptance probability for bad moves: P = eΔE/T.
  • High T → random walk (exploration). Low T → hill climbing (exploitation).
  • Cooling schedules: linear (simple), geometric (most practical), logarithmic (theoretical guarantee).
  • Guaranteed to find global optimum with logarithmic cooling (impractically slow in general).
  • O(1) space. One of the most widely used metaheuristics.
  • GATE focus: compute P given ΔE and T; understand when/why SA outperforms hill climbing.

References

  • Kirkpatrick, S., Gelatt, C.D., Vecchi, M.P. (1983). “Optimization by Simulated Annealing.” Science, 220(4598), 671–680.
  • Russell & Norvig, Artificial Intelligence: A Modern Approach, 4th Ed. — Section 4.1.2.