Nexus Learn

Lecture 16: Simulated Annealing and Parallel Search

Lecture Slides CS-511 Artificial Intelligence 7 min read Updated 8 Nov 2026

↓ Download slides (PowerPoint)

Recap of Lecture 15 and Homework Check

LAST LECTURE · 5 MIN

Today: the hiker at 120 m must walk DOWN first to reach 340 m. Can we allow that - but not too often?

Today's Plan (50 minutes)

ROADMAP

0-5

Recap of Lecture 15 and homework check

S02

5-8

Plan and learning outcomes

S03-S04

8-17

Module 1: Simulated annealing - idea, algorithm, acceptance probability

S05-S07

17-26

Module 2: Annealing on the track, 8-queens and a sales tour

S08-S12

26-35

Module 3: Parallel search - methods, speed-up, load balancing

S13-S16

35-43

Class activity: accept or reject? speed-up and efficiency

S17

43-48

Common mistakes and recap quiz

S18-S19

48-50

Homework, mid-term and next lecture

S20

Learning Outcomes (SLOs)

BY THE END OF THIS LECTURE YOU WILL BE ABLE TO

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

  • Course Learning Outcome(s) served
  • CLO3: Apply various techniques and algorithms of AI used in general problem solving, optimization problems, constraint satisfaction problems, and game programming.

Slide 5

MODULE 1

Simulated Annealing

Shake hard at first, then gently

The Idea: Allow Some Downhill Steps

MODULE 1 · IDEA

Hill climbing

Random walk

Simulated annealing

Never steps down: stuck on the first peak.

Steps anywhere: finds the top by luck but never stays there.

Random steps; downhill allowed with a probability that falls as T cools.

Blacksmith: heat the iron red-hot, then cool it slowly - the metal settles into a strong state. Cool too fast and it cracks.

Temperature T: high T = adventurous (accepts bad moves); low T = careful (almost hill climbing).

The Algorithm and the Acceptance Probability

MODULE 1 · ALGORITHM

SIMULATED ANNEALING (LOCALSEARCH.PY)

REAL OUTPUT: L16/ACCEPT.PY - P = E^(-DROP/T)

  • T = T0
  • for step in range(max_steps):
  • nxt = random neighbour of current
  • drop = value(current) - value(nxt)
  • if drop <= 0 or random() < exp(-drop / T):
  • current = nxt # accept
  • T = T * cooling # e.g. 0.995
  • 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

Rule: better move: always accept. Worse move: accept if a random number r < e^(-drop / T).

Example: drop 50 m at T = 100: p = e^(-0.5) = 0.607. Random number 0.4 < 0.607 - accept; 0.8 - reject.

Slide 8

MODULE 2

Annealing in Action

The track, 8-queens and a sales tour

Annealing on the Foggy Track

MODULE 2 · TRACK

REAL OUTPUT: L16/TRACK_SA.PY (T0 = 200, 1,500 STEPS)

  • 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

Slow cooling

Fast cooling

Always stuck on the nearest hill: 120 m or 250 m.

About 3 runs in 4 reach the 340 m top.

Freezes too early - behaves like hill climbing (0 of 100 from start 1).

Annealing on 8-Queens

MODULE 2 · 8-QUEENS

REAL OUTPUT: L16/QUEENS_SA.PY (100 RANDOM STARTS EACH)

  • hill climbing (steepest ascent): 18 / 100 solved
  • simulated annealing (T0 = 2, x0.99 per step): 96 / 100 solved, average 763 random moves tried

Lesson: a little 'bad' movement early on helps the search escape local minima.

A Real Problem: A 10-Town Sales Tour

MODULE 2 · TOURING PROBLEM

REAL OUTPUT: L16/TOUR.PY

  • 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

Touring problem

2-opt neighbour

Used for

Visit every town once and return; shortest total distance.

Reverse one section of the tour - removes crossing roads.

Delivery routes, chip layout, timetables - too big for exact search.

Properties and Tuning of Simulated Annealing

MODULE 2 · PROPERTIES

Tip: always remember the best state seen - SA can walk away from it while T is still high.

Slide 13

MODULE 3

Parallel Search

Many processors, one search

Three Ways to Search in Parallel

MODULE 3 · METHODS

Independent runs

Split the tree

Parallel expansion

Each core runs its own random restarts or SA. Keep the best. No communication.

Each core searches a different subtree (e.g. first queen in different rows).

Expand many frontier nodes at once; share the frontier; remove duplicates.

Examples: Deep Blue (1997): 30 processors + 480 chess chips. AlphaGo (2016): about 1,200 CPUs and 176 GPUs.

Link: local beam search (L15) = k runs that share their best states.

Splitting the 11-Queens Tree

MODULE 3 · DEMO

REAL OUTPUT: L16/PARALLEL.PY (TIMES VARY FROM RUN TO RUN AND COMPUTER TO 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.30 s | 2 processes: 0.78 s | speed-up 1.7x on this 2-core computer

PYTHON: GIVE SUBTREES TO WORKER PROCESSES

  • from multiprocessing import Pool
  • with Pool(2) as pool:
  • parts = pool.map(subtree,
  • [(n, r) for r in range(n)])
  • total = sum(parts)

Never exactly p times: starting processes, sending work and results, and uneven parts all cost time.

Problems in Parallel Search

MODULE 3 · CHALLENGES

Subtree sizes: 96, 219, 209, 295, 346, 350, 346, 295, 209, 219, 96 - not equal, so the work must be shared out carefully.

Activity: Accept or Reject? Speed-Up and Efficiency

CLASS ACTIVITY · PAIRS · 8 MIN

Pairs, 6 min + 2 min. Use p = e^(-drop / T) (calculator allowed). Track heights: position 3 = 120 m, position 4 = 110 m.

A move drops 30 m. Find p at T = 100 and at T = 10.

Why did fast cooling (x 0.9) give 0 of 100 runs on the 340 m top from start 1?

SA is at position 3 (120 m) and tries position 4 (110 m), T = 50, random number 0.70. Accept or reject?

A search takes 12 s on 1 core and 4 s on 4 cores. Speed-up and efficiency?

Same move at T = 5, random number 0.70. Accept or reject?

Give the 11 subtrees (S16) to 4 workers in turn (1st to W1, 2nd to W2, ...). Work per worker? Is it balanced?

Common Mistakes

WATCH OUT

WRONG IDEA

CORRECT IDEA

✗ SA accepts every worse move.

✓ Only with probability e^(-drop/T) - rarer for big drops and low T.

✗ Higher temperature = more careful.

✓ High T = adventurous; low T = careful, almost hill climbing.

✗ Cooling fast is better - it is quicker.

✓ Fast cooling freezes early: 0 of 100 reached the top from start 1.

✗ p processors always give p times speed-up.

✓ Overheads and uneven work: under 2x on our 2 cores.

✗ SA returns its final state.

✓ Keep the best state seen - SA may walk away from it.

Recap Quiz

CHECK YOUR UNDERSTANDING · 3 MIN

  • Q1. SA accepts a worse move with probability:
  • (a) drop / T (b) e^(-drop / T) (c) T / drop (d) Always 0.5
  • Q2. Drop 50 m at T = 100: p is about:
  • (a) 0.37 (b) 0.50 (c) 0.61 (d) 0.95
  • Q3. As the temperature T falls, SA behaves more like:
  • (a) A random walk (b) Hill climbing (c) BFS (d) A*
  • Q4. 12 s on 1 core and 4 s on 4 cores. Efficiency is:
  • (a) 25% (b) 33% (c) 75% (d) 300%

Homework and Next Lecture

BEFORE THE NEXT CLASS

  • Task 1: Calculate p = e^(-drop / T) for a 20 m drop at T = 100, 20 and 2.
  • Task 2: SA on the track starts at position 3 (120 m). Positions 4 and 5 are 110 m and 80 m. Steps: try 4 (T = 100, r = 0.5); try 5 (T = 90, r = 0.9); try 5 (T = 81, r = 0.6). Where is SA after each step?
  • Task 3: A search takes 40 s on 1 core and 8 s on 8 cores. Find speed-up and efficiency; give two reasons it is not 8x.
  • Mid-term: Lectures 1-16. Practise the traces: BFS, DFS, UCS, greedy, A*, hill climbing, SA acceptance.

NEXT LECTURE

  • After the mid-term - Lecture 17:
  • Game Playing: Minimax

← All CS-511 material