Hill climbing and simulated annealing
Hill climbing is a greedy local search that moves to improving neighbours; simulated annealing adds probabilistic uphill moves controlled by a temperature and cooling schedule to escape local optima.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Hill climbing and simulated annealing are two fundamental local-search algorithms used by engineers to solve optimisation problems when exact global methods are too slow or unavailable. Hill climbing is simple and fast for finding local optima; simulated annealing adds controlled randomness so you can escape local optima and (under conditions) converge to a global optimum.
Key ideas
Neighborhood: For each state s define a set N(s) ⊆ S of states considered one step away (neighbours). The neighbourhood defines allowed local moves.
Local optimum: A state s is a local maximum (for maximization) if f(s) ≥ f(t) for every t ∈ N(s). For minimization use ≤. A local minimum is f(s) ≤ f(t) for every t ∈ N(s) (or, if using an energy/cost E to minimize, E(s) ≤ E(t)).
Global optimum: A state s* is a global maximum if f(s*) ≥ f(t) for every t ∈ S. (Or global minimum for E.)
Greedy algorithm / Hill climbing: At each step, move from the current state s to a neighbour t ∈ N(s) that improves the objective (for maximization, f(t) > f(s)). Variants:
- Simple hill climbing (first-choice): pick the first improving neighbour and move.
- Steepest-ascent / steepest-descent: evaluate all neighbours and move to the best-improving neighbour.
- Stochastic hill climbing: choose among improving neighbours randomly (possibly weighted).
- Random-restart hill climbing: run hill climbing from several random initial states and take the best result.
Strengths/weaknesses: Hill climbing is fast and easy but can get trapped at local optima. Random restarts help but may still miss the global optimum if basins of attraction are small.
Simulated annealing (SA): A stochastic algorithm inspired by physical annealing. Define an energy function E(s) to minimize (or use −f to convert maximize→minimize). SA performs moves to neighbours but sometimes accepts worse moves with probability depending on a temperature T. Key elements:
- Temperature (T): a nonnegative parameter (traditionally in kelvin if modelling physics) that controls acceptance of uphill moves. High T → more uphill moves allowed; low T → mostly greedy moves.
- Acceptance probability (Metropolis criterion): When proposing t from current s compute ΔE = E(t) − E(s). If ΔE ≤ 0 accept (improvement). If ΔE > 0 accept with probability exp(−ΔE / T). For asymmetric proposal distributions q, use Metropolis–Hastings acceptance A = min(1, exp(−ΔE / T) · q(t→s)/q(s→t)).
- Cooling schedule: A rule for decreasing T over time (iteration k). Common schedules: exponential T_k = T_0 · α^k (0 < α < 1); inverse-logarithmic T_k = c / ln(k + k0); linear T_k = T_0 − βk (ensure T_k > 0; stop or clamp as needed).
Why SA works (intuitive): At high T the algorithm explores widely; as T decreases it focuses toward low-energy states. Under certain conditions (finite state space, irreducible and aperiodic proposals, and sufficiently slow cooling such as logarithmic), the probability that SA is in the set of global minima tends to 1 as iterations increase.
Formal condition (sketch): For finite S and a Markov chain that allows reaching any state from any other (irreducible and aperiodic), if the cooling schedule decreases slowly enough (e.g., T_k ≥ c / ln(k + k0) with an appropriate constant c derived from energy barriers), then the probability that SA ends in the set of global minima tends to 1 as k → ∞. (See verification notes below.)
Detailed balance and stationary distribution (brief): With symmetric proposals q(s→t)=q(t→s), the Metropolis acceptance rule yields a Markov chain with stationary distribution π(s) ∝ exp(−E(s)/T). As T → 0 this concentrates probability mass on global minima.
When to use which: Use hill climbing when the landscape is unimodal or when quick local improvement is sufficient. Use simulated annealing for larger, multimodal problems where escaping local optima is important. Refer to other search/metaheuristic methods (e.g., Genetic algorithms, Particle swarm optimisation) for alternative strategies.
Diagram instructions (draw by hand for exams):
- Draw a 2-D plot with x-axis labelled "solution index / parameter" and y-axis labelled "objective f(x)". Sketch a curve with at least two peaks: a higher peak (global maximum) at x≈3 and a smaller peak (local maximum) at x≈1. Mark a point at the local peak and label it "current (stuck)". Draw arrows showing small neighbour steps that keep the algorithm on the local peak; draw a dashed arrow representing a larger jump to the global peak that hill climbing would not take, and annotate "requires escape / stochastic move". For SA, show temperature schedule as a separate small plot: y-axis T, x-axis iterations k, draw a decreasing curve labelled "cooling schedule".
Formulas
ΔE = E(t) − E(s) — difference in cost/energy between proposed neighbour t and current state s. (E in arbitrary energy units; ΔE same units.)
If ΔE ≤ 0 then accept move — deterministic acceptance of improvements.
If ΔE > 0 then P_accept = exp(−ΔE / T) — Metropolis acceptance probability (algorithmic convention; T shares units with E so the exponent is dimensionless). In physical units use P_accept = exp(−ΔE / (k_B T)) with T in kelvin and k_B the Boltzmann constant; the exponent must be dimensionless in either case.
T_k = T_0 · α^k — exponential cooling (0 < α < 1). T in kelvin if physical; dimensionless otherwise.
T_k = c / ln(k + k0) — logarithmic cooling; c and k0 are constants chosen based on energy barriers. Units: same as T.
π(s) ∝ exp(−E(s) / (k_B T)) — Boltzmann distribution at physical temperature T (kelvin); k_B is Boltzmann’s constant so the exponent is dimensionless. In algorithmic SA we usually absorb k_B into T by defining T in the same units as E, yielding π(s) ∝ exp(−E(s)/T).
Notes on symbols: E(s) — energy / cost of state s, units depend on problem (Joule if physical energy); T — temperature (K) in physics analogy, or a dimensionless control parameter in algorithm; k — iteration number (unitless); α — cooling factor (unitless, 0<α<1); c, k0 — positive constants (unitless if T scaled appropriately).
Worked examples
Example 1 — Hill climbing on a simple continuous function (maximization) Problem: Maximize f(x) = −(x − 3)^2 + 10 over real x using discrete-neighbour hill climbing with step size 0.5. Start at x0 = 0. Stop when no neighbour improves. Step 1 (define neighbours): For current x, neighbours are x ± 0.5. Step 2 (compute): f(x) = −(x − 3)^2 + 10. x0 = 0 → f(0) = −(0 − 3)^2 + 10 = −9 + 10 = 1. Neighbours: −0.5 and 0.5. f(0.5) = −(0.5 − 3)^2 + 10 = −6.25 + 10 = 3.75 (improves). Move to x1 = 0.5. Step 3: x1 = 0.5 → neighbours 0.0 and 1.0; f(1.0) = −4 + 10 = 6 (improves) → x2 = 1.0. Step 4: x2 = 1.0 → neighbours 0.5 and 1.5; f(1.5) = −2.25 + 10 = 7.75 → x3 = 1.5. Continue: x3=1.5 → f(2.0)=−1+10=9 → x4=2.0. x4=2.0 → f(2.5)=−0.25+10=9.75 → x5=2.5. x5=2.5 → f(3.0)=10 → x6=3.0. x6=3.0 → neighbours 2.5 and 3.5; f(3.5)=−0.25+10=9.75 (worse). No improving neighbour. Stop. Final answer: x* = 3.0, f(x*) = 10.
Example 2 — Simulated annealing on a tiny discrete energy landscape Problem: State set S = {A,B,C}. Energies: E(A)=10, E(B)=8, E(C)=9. Start at A. Use one-step SA with current T=1.0 (dimensionless). Proposal distribution: uniform among other states. Step 1: Current A (E=10). Propose B (E=8). ΔE = 8 − 10 = −2 ≤ 0 ⇒ accept. Now at B (E=8). Step 2: Current B (E=8). Propose A (E=10). ΔE = 2 > 0. Acceptance probability P = exp(−2 / 1) = 0.1353352832. Simulate a random draw r ∈ [0,1]. If r ≤ 0.1353 accept; else reject. Example: r = 0.2 ⇒ reject, remain at B. Step 3: With cooling, reduce T: suppose exponential α=0.5 so new T=0.5. Now propose C from B: ΔE = 9 − 8 = 1 > 0 → P = exp(−1 / 0.5) = exp(−2) = 0.1353. Draw r = 0.05 ⇒ accept move to C. Step 4: From C (E=9) propose B (E=8): ΔE = −1 ⇒ accept. Conclusion: At higher T uphill moves happen sometimes; lowering T reduces acceptance and eventually the chain concentrates on the best states (here B with E=8). Final answer depends on randomness; multiple runs and slow cooling increase chance to find global minimum.
Example 3 — Short derivation (stationary distribution) Given a fixed temperature T and symmetric proposal probabilities q(s→t)=q(t→s), choose Metropolis acceptance A(s→t) = min(1, exp(−(E(t)−E(s))/T)). Then detailed balance holds: π(s)·q(s→t)·A(s→t) = π(t)·q(t→s)·A(t→s). If π(s) ∝ exp(−E(s)/T), substitute and verify equality holds. Thus π is a stationary distribution.
Common mistakes
- Forgetting that acceptance probability applies only to uphill moves; improvements are accepted deterministically.
- Mixing physical units: when you quote temperature T in kelvin, energy ΔE must be in joules so ΔE/T is dimensionless only if Boltzmann constant k_B is included. In algorithms we normally treat ΔE/T as dimensionless parameter — be explicit in exams.
- Cooling too fast (e.g., α too small, close to 0) destroys SA's ability to escape local minima.
- Not ensuring the proposal distribution is irreducible (some moves never allowed) — then SA cannot reach all states and convergence claims fail.
For your exam
Types of questions:
- Definitions: define neighbourhood, local/global optimum, hill climbing, simulated annealing, temperature, cooling schedule.
- Short notes: compare hill climbing vs simulated annealing; variants of hill climbing; cooling schedules.
- Derivations: show detailed balance for Metropolis acceptance; show why π(s) ∝ exp(−E(s)/T) is stationary at fixed T.
- Numericals: step-by-step hill-climbing or simulated-annealing steps with given energies and random draws.
- Programs/pseudocode: write SA pseudocode and explain parameters.
- Diagrams: draw a multimodal landscape and illustrate being trapped and escaping.
What full-marks answers include:
- Clear definitions with neighbourhood specified.
- Correct formulas (ΔE, acceptance probability) and explanation when each applies.
- For derivations, list assumptions (finite state space, symmetric proposal or include acceptance adjusted for asymmetric q), show algebra (detailed balance), and conclude.
- For numericals, show each step with numbers, state random draws explicitly and compute acceptance probabilities correctly.
Quick check
- What acceptance probability is used in SA when the new state has higher energy by ΔE and the current temperature is T?
- Give one advantage and one disadvantage of hill climbing.
- Name two common cooling schedules. Answers: 1) P = exp(−ΔE / T). 2) Advantage: fast and simple; Disadvantage: gets stuck in local optima. 3) Exponential (T_k = T_0·α^k) and logarithmic (T_k = c / log(k + k0)).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?