WEEK 3
CHAPTER 3

Local Search & CSP

Sometimes the path doesn't matter at all. Only the final configuration does.

Manga page: Lake Balkhash. / Place 3 boat docks so every village is close to one. / I'll start anywhere and keep IMPROVING! / Uphill... uphill... is this the top? / WHAT?!

When the path is irrelevant

A* answers "which path?". Many problems instead ask "which configuration?": placing 8 queens with no attacks, ordering cities in a delivery tour (the Travelling Salesperson Problem, TSP), building a timetable, siting facilities. Local search keeps one (or a few) complete candidate solutions and tries to improve them by small changes. It uses very little memory: no big frontier, no explored set.

ConceptMeaning8-queens exampleTSP example
StateOne complete configuration8 queens, one per columnA full tour A→C→B→D→A
NeighbourA small changeMove one queen within its columnSwap two cities / reverse a segment (2-opt)
Objective functionOne number: how good is it?Number of attacking pairs (minimise)Total tour length (minimise)

Hill climbing

The simplest iterative improvement: move to the best neighbour while it is better than the current state; stop when no neighbour improves. It's like climbing a hill in thick fog by always stepping uphill.

def hill_climbing(values, start):
    current = start
    while True:
        neighbors = [i for i in (current-1, current+1) if 0 <= i < len(values)]
        best = max(neighbors, key=lambda i: values[i])
        if values[best] <= values[current]:
            return current               # no uphill move: a (local) peak
        current = best

Trace (lecture example): values = [1, 3, 5, 8, 6, 4, 2], start at index 0. Then 1 → 3 → 5 → 8. At 8 the neighbours are 5 and 6, neither better, so it returns index 3 (value 8).

Three ways hill climbing gets stuck

local maxplateauridgeGLOBAL MAX
  • Local maximum: higher than all neighbours, lower than another peak.
  • Plateau: a flat area. No neighbour is better, so there's no direction to follow.
  • Ridge: the good direction needs a sequence of moves that no single neighbour offers.

Escaping: restarts, annealing, beams, genes

Random-restart hill climbing runs hill climbing from several random starts and keeps the best result. It costs more computation but depends less on one unlucky start.

Simulated annealing sometimes accepts a worse move, with a probability controlled by a temperature T that slowly decreases (like cooling metal):

ΔE = value(next) − value(current) if ΔE > 0: accept (better: always) else: accept with probability e^(ΔE / T) (worse: sometimes) T ← 0.95 · T (cooling schedule)

High T means the search explores and can cross valleys. Low T means it becomes greedy and refines. Cool too fast and it freezes in a poor spot; too slow and it wastes time. With slow enough cooling, theory guarantees convergence to the global optimum.

Local beam search keeps K states, generates all their successors, and keeps the best K overall. The K searches share information, so good regions attract the whole beam. Genetic algorithms keep a population, select by fitness, combine parents by crossover and add random mutation, generation after generation. In continuous spaces we can use gradient descent: step opposite the slope, x ← x − α∇f(x), where α is the step size (learning rate). This same idea trains the neural networks of Chapter 9.

TRY IT! Climb the landscape: find the highest peak (maximisation)

Pick a method and press Run.

EXPERIMENT

Start hill climbing at x = 8: it gets stuck on the first small peak. Random restarts usually find a higher peak. Annealing with a high start T often crosses valleys; with T = 1 it behaves almost like hill climbing. Annealing is random, so run it several times and compare.

Constraint Satisfaction Problems (CSP)

A CSP describes a problem by:

  • Variables X₁…Xₙ (e.g. regions on a map),
  • Domains: the allowed values for each variable ({red, green, blue}),
  • Constraints: rules on combinations (two neighbouring regions must differ).

A solution assigns every variable a value with no constraint violated. Timetables, exam scheduling, Sudoku and map colouring are all CSPs. The textbook example colours the 7 regions of Australia with 3 colours.

Backtracking search assigns variables one at a time, checks constraints immediately, and backs up when a variable has no legal value left. Two simple tricks make it much faster:

  • MRV (minimum remaining values): pick the variable with the fewest legal values left. "Fail first."
  • Forward checking: after each assignment, remove now-illegal values from the neighbours' domains. If a domain becomes empty, backtrack at once.

Local search solves CSPs too. Min-conflicts starts with a full (possibly bad) assignment and repeatedly changes one conflicted variable to the value with the fewest conflicts. This works astonishingly well on n-queens, even with a million queens. The lecture's scheduling example turns constraints into a cost, Cost = 100T + 100R + 20S + 5W, and minimises it with local search.

TRY IT! Colour the map (CSP). Click a region to change its colour.
R G B

Every region needs a colour that differs from all its neighbours. Conflicting edges turn red and dashed.

REAL WORLD

University timetabling, airline crew scheduling and delivery-route optimisation are solved with CSP and local-search tools such as Google OR-Tools, the same toolkit the lecture used for its TSP and scheduling examples.

DID YOU KNOW?

Simulated annealing (Kirkpatrick, Gelatt & Vecchi, 1983) borrowed its maths from physics: slowly cooled metal settles into a low-energy crystal structure, while quenched metal stays brittle.

MINI-QUIZ

1. Why can hill climbing return a solution that is not the best?

Hill climbing is greedy: no improving neighbour, no move. Peaks elsewhere stay invisible.

2. In simulated annealing at high temperature, worse moves are…

A worse move is accepted with probability e^(ΔE/T). ΔE < 0, so this is close to 1 when T is large and close to 0 when T is small.

3. In map colouring, what are the domains?

Regions are variables, colours are domain values, and "neighbours differ" are the constraints.
Manga page: Docks placed! / Hey... who's that guy?
3