CSE 4355 W3 Β· Class 2 Hill climbing, simulated annealing and beam search
Sakeef Sani β†— ↓ PDF

CSE 4355Week 3Class 2Sunday Β· 9:30 AM – 10:50 AM80 min

Hill climbing, simulated annealing and beam search

When only the final state matters, forget the search tree: keep one state, improve it, and learn when to accept a step backwards. Class Test 1 in the last 20 minutes.

S present Β· L lecturer notes Β· β†’ next step Β· F full screen Β· P presenter window
01 Cold open 0–3 min

Climbing Keokradong in thick fog

You are on a hill in Bandarban, in fog so thick you can only feel the slope under your feet. Goal: the highest point.

Rule: always take the step that goes up the most. Stop when every step goes down.

No map, no memory of where you have been, one position at a time. That is local search.

Pop culture

Squid Game's glass bridge is the opposite problem: one wrong step and you are out, so there is no "try and step back". Local search lives on landscapes where you can always take another step.

02 Concept 3–7 min

When the path doesn't matter

In 8-queens the answer is the final board, not the moves that led to it. Same for timetables, chip layouts and delivery routes. So:

  • Complete-state formulation: every state is a full candidate solution (all 8 queens on the board).
  • Objective function: a number to maximise (or a cost such as h = attacking pairs to minimise).
  • Neighbours: states one small change away (move one queen in its column).
  • Keep one current state: O(1) memory, works on spaces far too big to search systematically.

Landscape vocabulary

Global maximum: the best state anywhere.

Local maximum: better than all its neighbours, but not the best.

Plateau: a flat area; a shoulder is a plateau that still leads up.

Ridge: a sequence of local maxima that is easy to walk along in one direction but every single move goes down.

03 Live lab 7–12 min

Live lab: a climber that only sees its feet

State-space landscape Click the ground to drop the climber (hill climbing and annealing), then Run. The climber only sees its two neighbours.
local maximumshoulderglobal maximumflat local max (plateau)state x β†’objective f(x) ↑
Height now30
Best seen (max 96)30
Steps0
04 Concept 12–16 min

Hill climbing and its variants

function HILL-CLIMBING(problem)
  current ← problem.INITIAL
  loop
    neighbour ← highest-valued successor of current
    if VALUE(neighbour) ≀ VALUE(current):
      return current           # a peak (maybe local)
    current ← neighbour

Steepest ascent, adapted from Russell & Norvig. Greedy: it never looks more than one step ahead.

VariantChange
Sideways movesallow equal-valued moves (with a limit) to cross shoulders
Stochasticpick a random uphill move, more likely the steeper ones
First-choicetry random neighbours, take the first that is better: good when there are thousands of neighbours
Random restartif stuck, start again from a random state; keep the best result
Pro tip

Random-restart hill climbing is complete with probability approaching 1: eventually a start lands in the global hill's basin.

05 Worked example 16–21 min

Worked example: h for an 8-queens board

Given

An 8-queens board in the complete-state formulation, one queen per column. Columns 1–8 from left to right, rows 1–8 from bottom to top. The queens are on rows (5, 6, 7, 4, 5, 6, 7, 6).

Find

h (number of attacking pairs, counted once) and the best move for steepest-ascent hill climbing.

Method: Russell & Norvig's 8-queens hill-climbing example

Solution

Predict the answer first. Then reveal one step at a time.

06 Live lab 21–27 min

Live lab: 8-queens, one move at a time

8-queens hill climbing h = attacking pairs. Click any square to move that column’s queen; or let steepest ascent pick the lowest number.
18141415171814121612141414141413181417161313151315181517131215131515121214121614141414121413161512141614161616181122334455667788β™›β™›β™›β™›β™›β™›β™›β™›
h17attacking pairs
Steps0
Best move givesh = 12

The textbook board: h = 17. Every number is the h you would get by moving that column’s queen there.

07 Your turn 27–31 min

Your turn Β· 1: how many restarts?

03:00
1 Exam CQ

Steepest-ascent hill climbing solves a random 8-queens board with probability p = 0.14, taking 4 steps when it succeeds and 3 when it gets stuck.

(a) With random restarts, how many runs do you expect? (b) How many steps in total? (c) Redo (b) with sideways moves: p = 0.94, 21 steps when it succeeds, 64 when it fails.

Work it out first.
Press Solve when the class has an answer.

08 Concept 31–35 min

Simulated annealing: sometimes step down

function SIMULATED-ANNEALING(problem, schedule)
  current ← problem.INITIAL
  for t = 1, 2, 3, …
    T ← schedule(t)
    if T = 0: return current
    next ← a random successor of current
    Ξ”E ← VALUE(next) βˆ’ VALUE(current)
    if Ξ”E > 0: current ← next
    else current ← next with probability eΞ”E/T

The idea

Early on (T high) accept many bad moves: explore, jump out of small hills. Later (T low) accept almost none: settle on a peak.

If T is lowered slowly enough, SA finds the global optimum with probability approaching 1.

Pop culture

Named after annealing in metallurgy: heat a metal, then cool it slowly so its atoms settle into a low-energy crystal. A village kamar (blacksmith) quenching a da does the opposite: fast cooling makes it hard but brittle.

09 Worked example 35–38 min

Worked example: how likely is a step down?

Given

Simulated annealing, acceptance probability eΞ”E/T for a worse move (Ξ”E < 0).

Find

The acceptance probability for Ξ”E = βˆ’2 and Ξ”E = βˆ’10 at T = 10, 2 and 0.5. What do the numbers say?

Method: Metropolis acceptance rule, as in Russell & Norvig's simulated annealing

Solution

Predict the answer first. Then reveal one step at a time.

10 Live lab 38–43 min

Live lab: annealing on the landscape

State-space landscape Click the ground to drop the climber (hill climbing and annealing), then Run. The climber only sees its two neighbours.
local maximumshoulderglobal maximumflat local max (plateau)state x β†’objective f(x) ↑
Height now30
Best seen (max 96)30
Steps0
Temperature T30.00
Last downhill moveβ€”
11 Think deeper 43–46 min

Critical thinking Β· tuning the temperature

β˜… Trap

A classmate runs SA with Tβ‚€ = 0.1 and Ξ± = 0.5 (T halves every step) on the landscape and says: "Annealing is no better than hill climbing, it got stuck on the first hill."

What happened? What does SA become when T is very large, and when T is very small? How would you choose Tβ‚€?

Work it out first.
Press Solve when the class has an answer.

12 Concept 46–50 min

Local beam search: k climbers that talk

  • Start with k random states.
  • Each step: generate all successors of all k; if one is a goal, stop.
  • Otherwise keep the best k from the whole pool and repeat.

Unlike k independent restarts, information flows: if one state finds a great hill, its successors take over the beam and the climbers on poor hills are dropped.

Gotcha

The same feature is the weakness: all k can crowd onto one hill and lose diversity. Stochastic beam search picks the k successors at random, weighted by value. Add "parents combine" and you get genetic algorithms (Week 5).

Try it

In the landscape lab, choose Local beam (k = 4) and press New random start a few times. Four climbers start; within a few steps the beam usually sits on one hill.

MemoryShares information?
Hill climbing1 stateβ€”
k random restarts1 at a timeNo
Local beam (k)k statesYes
13 Real world 50–55 min

Real world: routes, timetables and chips

Annealing a delivery-drone route 18 drop-off points. A move reverses one stretch of the route (2-opt). Hill climbing keeps only improvements; annealing sometimes accepts a longer route.
Route length189.3 km
Best this sessionβ€”
Moves tried0
Temperature8.00
route length over time
14 Pop quiz 55–58 min

Pop quiz

Score 0

Question 1 / 5

Local search keeps in memory…

15 Recap 58–60 min

Three things to remember

01

Local search keeps one complete state and moves to neighbours: O(1) memory, no path, no guarantee. Problems: local maxima, plateaus, ridges.

02

Hill climbing takes the best neighbour until none is better. Fixes: sideways moves, random restarts (8-queens: 14 % β†’ 94 % β†’ β‰ˆ 100 %).

03

Simulated annealing accepts bad moves with eΞ”E/T, cooling T slowly. Local beam keeps the best k of all successors.

Pro tip

Before next class: Assignment 1 is due Week 4, Class 1. Next week: informed search. Heuristics, greedy best-first and A*.

16 Recap 60–80 min

Class Test 1

Class Test 1 Β· 20 minutes
20:00

Covers

Problem formulation Β· BFS and DFS Β· UCS Β· DLS and IDS (traces and the comparison table) Β· local search basics (hill climbing, annealing acceptance, beam search).

Rules

Closed book, written, answer all questions on the paper. Calculators allowed, phones away. Write your tie-breaking and goal-test convention at the top of every trace.

Questions are handed out on paper. The practice set is on the course page under Assessments.