Learning Outcomes
SLO | After this lecture you will be able to... | CLO | Bloom |
|---|---|---|---|
SLO 16.1 | Explain simulated annealing and calculate the probability of accepting a worse move, e^(-drop / T) | CLO3 | C3 Apply |
SLO 16.2 | Apply simulated annealing to a landscape, 8-queens and a touring problem, and compare it with hill climbing | CLO3 | C3 Apply |
SLO 16.3 | Explain parallel search: independent runs, splitting the tree, speed-up, efficiency and load balancing | CLO3 | C2 Understand |
1. Simulated Annealing
§1.1 The idea
Slide L16-S06
Hill climbing never steps downhill, so it stops on the first peak (Lecture 15). A random walk goes anywhere but never settles. Simulated annealing (SA) takes a random neighbour: a better move is always accepted; a worse move is accepted with a probability that falls as the temperature T is lowered. The name comes from annealing metal: a blacksmith heats iron and cools it slowly so it settles into a strong state. High T = adventurous (explores); low T = careful (almost hill climbing).
§1.2 The algorithm and the acceptance probability
Slide L16-S07
drop = value(current) - value(next). If drop <= 0 move. Otherwise move with probability p = e^(-drop / T): draw a random number r between 0 and 1 and move if r < p. After each step T = cooling x T (for example 0.995).
def anneal_1d(start, rng, T0=200.0, cooling=0.995, steps=1500, track=TRACK): """Simulated annealing on the track: random step left/right; accept downhill with probability e^(delta/T).""" pos, T, best = start, T0, start for _ in range(steps): nxt = pos + rng.choice((-1, 1)) if not 0 <= nxt < len(track): continue delta = track[nxt] - track[pos] if delta > 0 or rng.random() < math.exp(delta / T): pos = nxt if track[pos] > track[best]: best = pos T = max(T * cooling, 0.1) return pos, best |
drop (m) T = 200 T = 100 T = 50 T = 10 T = 1 10 0.951 0.905 0.819 0.368 0.000 50 0.779 0.607 0.368 0.007 0.000 100 0.607 0.368 0.135 0.000 0.000 |
Worked example: drop 50 m at T = 100: p = e^(-0.5) = 0.607. Random number 0.4 < 0.607 - accept; 0.8 - reject.
2. Annealing in Action
§2.1 The foggy track
Slide L16-S09
runs (out of 100) that end on the 340 m top start hill climbing SA slow (x0.995) SA fast (x0.9) 1 stuck at 120 m 76 / 100 0 / 100 5 stuck at 120 m 71 / 100 0 / 100 12 stuck at 250 m 78 / 100 24 / 100 |
Hill climbing is always stuck on the nearest hill. SA with slow cooling ends on the 340 m top in about 3 runs out of 4. With fast cooling (x 0.9) T falls almost to zero within a few dozen steps and SA freezes like hill climbing.
§2.2 8-queens
Slide L16-S10
hill climbing (steepest ascent): 18 / 100 solved simulated annealing (T0 = 2, x0.99 per step): 96 / 100 solved, average 763 random moves tried |
SA checks one random neighbour per step (hill climbing checks all 56) and sometimes accepts worse states, so it escapes the local minima that trap hill climbing.
§2.3 A 10-town sales tour (travelling salesman problem)
Slide L16-S11
Start at Burewala, visit the other 9 towns once and return. Neighbour = 2-opt move: reverse one section of the tour. With 10 towns all 9! = 362,880 orders can be checked; with 50 towns there are about 10^62 orders, so local search is used.
Exact (checks all 362,880 orders): 724 km Burewala - Vehari - Multan - Khanewal - Mian Channu - Sahiwal - Faisalabad - Lahore - Pakpattan - Arifwala - Burewala Hill climbing (2-opt) 50 random starts: best tour found 29 times, average 726.7 km, worst 730 km Simulated annealing 50 random starts: best tour found 44 times, average 724.9 km, worst 730 km |
§2.4 Properties and tuning
Slide L16-S12
Setting / property | Meaning |
|---|---|
Starting temperature T0 | High enough to accept most moves at first |
Cooling factor | T = factor x T after each step (0.95-0.999) |
Stopping rule | T almost 0, or no improvement for long |
Optimal? | Yes in theory if cooled slowly enough; in practice very often, not always |
Memory | Current state + best state seen: O(1) |
3. Parallel Search
§3.1 Three ways
Slide L16-S14
Method | Idea | Communication |
|---|---|---|
Independent runs | Each core runs its own random restarts or SA; keep the best | Almost none |
Split the tree | Each core searches a different subtree (e.g. different first moves) | Little: combine results |
Parallel expansion | Expand many frontier nodes at once from a shared frontier | A lot: shared frontier, duplicates |
Examples: IBM Deep Blue (1997) used 30 processors and 480 chess chips; AlphaGo (2016) used about 1,200 CPUs and 176 GPUs. Local beam search (L15) is like k runs that share their best states.
§3.2 Splitting the 11-queens tree
Slide L16-S15
def count(n, cols=()): """Depth-first count of all n-queens solutions that extend the queens already placed in cols.""" c = len(cols) if c == n: return 1 return sum(count(n, cols + (r,)) for r in range(n) if all(r != q and abs(r - q) != c - i for i, q in enumerate(cols))) def subtree(args): n, r = args return count(n, (r,)) if __name__ == "__main__": n = 11 t = time.perf_counter(); total = count(n); serial = time.perf_counter() - t t = time.perf_counter() with Pool(2) as pool: parts = pool.map(subtree, [(n, r) for r in range(n)]) parallel = time.perf_counter() - t print(f"{n}-queens: {total} solutions (serial) = {sum(parts)} (parallel)") print("solutions in each subtree (first queen in row 0 ... 10):", parts) print(f"1 process: {serial:.2f} s | 2 processes: {parallel:.2f} s | speed-up {serial / parallel:.1f}x on this {os.cpu_count()}-core computer") |
11-queens: 2680 solutions (serial) = 2680 (parallel) solutions in each subtree (first queen in row 0 ... 10): [96, 219, 209, 295, 346, 350, 346, 295, 209, 219, 96] 1 process: 1.37 s | 2 processes: 0.79 s | speed-up 1.7x on this 2-core computer |
Times differ from run to run and from computer to computer. Speed-up = T(1) / T(p); efficiency = speed-up / p. Example: 12 s on 1 core and 4 s on 4 cores: speed-up 3, efficiency 75%.
§3.3 Problems in parallel search
Slide L16-S16
Problem | What goes wrong | Fix |
|---|---|---|
Load balancing | Subtrees differ (96 to 350 solutions); some cores sit idle | Many small pieces; work stealing |
Communication | Sending states and results takes time | Large independent pieces |
Duplicate work | Two cores expand the same state | Shared visited set or non-overlapping split |
Search overhead | Nodes a serial search would prune are explored | Share the best-so-far bound |
4. Common Mistakes
Wrong idea | Correct idea |
|---|---|
SA accepts every worse move. | Only with probability e^(-drop/T). |
High T = careful. | High T = adventurous; low T = careful. |
Fast cooling is better. | It freezes early (0 of 100 from start 1). |
p cores give p times speed-up. | Overheads and uneven work reduce it. |
SA returns its final state. | Keep the best state seen. |
Word Bank
Word | Simple meaning | Example |
|---|---|---|
Annealing | Heating then slowly cooling metal | Blacksmith hardening iron |
Temperature T | Controls how often worse moves are accepted | T = 200 at the start |
Cooling schedule | How T is lowered | T = 0.995 x T |
2-opt move | Reverse one section of a tour | Removes crossing roads |
Speed-up | How many times faster with p processors | 12 s / 4 s = 3 |
Load balancing | Sharing work evenly among processors | Subtrees of 96 to 350 solutions |
Self-Check Questions
- How is SA different from hill climbing? (see §1.1)
- Find p for a 30 m drop at T = 100 and T = 10. (see §1.2)
- Why does fast cooling fail on the track? (see §2.1)
- What is a 2-opt move? (see §2.3)
- Name three ways to search in parallel. (see §3.1)
- Define speed-up and efficiency with an example. (see §3.2)
Reading: Russell & Norvig (4e) 4.1.2 (simulated annealing), 4.1.3 (local beam); Govers (2018) Ch 9.