Search/Local Search and Optimization

Lesson 2.52,399 words

Local Search and Optimization

When the path to a goal is irrelevant and only the final state matters, we can discard the search tree entirely and keep just the current state, moving to a better neighbor at each step. This lesson builds the state-space landscape metaphor, works through hill climbing and the three obstacles that defeat it (local maxima, ridges, plateaus), then develops the first escapes: random restarts and simulated annealing with its temperature schedule.

╌╌╌╌

The uninformed and informed search algorithms keep one or more paths in memory and record, at every node, which alternatives have already been tried. When a goal turns up, the path that reached it is the solution — the order of moves is the answer. For a large class of problems that bookkeeping is wasted effort. In the 8-queens problem the answer is a board configuration with no two queens attacking; the sequence in which the queens were placed is irrelevant. The same holds for integrated-circuit layout, factory-floor scheduling, job-shop scheduling, telecommunications-network routing, vehicle routing, and portfolio management: what you want is a good final state, not a route to it.1

When the path does not matter, keep only the current state and try to improve it. Local search algorithms operate on a single current node and move only to neighbors, discarding the paths behind them. They are not systematic, so they give up the completeness guarantees of tree search, but they buy two things in return: they use a constant amount of memory, and they can find reasonable solutions in state spaces far too large — or continuous, hence infinite — for any systematic method to enumerate.

Local search also solves problems that the standard framing of Chapter 3 cannot express at all: optimization problems, where there is no explicit goal test, only an objective function to be made as large (or small) as possible. Nature runs one such problem — Darwinian evolution maximizes reproductive fitness with no goal and no path cost — and much of engineering runs the rest.

This lesson develops the single-state methods: the landscape metaphor, hill climbing and its failure modes, and the first two ways to escape those failures — random restarts and simulated annealing. The methods that keep a population of states (beam search and genetic algorithms) and the calculus for continuous spaces follow in the companion lesson, Population and Continuous Search.

The state-space landscape

The right mental picture for local search is a terrain. Give each state two attributes: a location, fixed by the state itself, and an elevation, given by the value of the objective function (or of a heuristic cost, if we are minimizing). Plot elevation against location and the state space becomes a surface with peaks and valleys. If elevation is value, we want the highest peak, the global maximum; if elevation is cost, we want the lowest valley, the global minimum. The two are interchangeable — negate the function and a maximization problem becomes a minimization problem — so we fix ideas on maximization and speak of climbing.

A one-dimensional state-space landscape. Elevation is the objective function; a hill-climber (current state, at the arrow) moves uphill toward the nearest peak. Only the global maximum is the true optimum; local maxima, shoulders, and flat local maxima are the traps defined in the text.

A complete local search always finds a goal if one exists; an optimal one always finds the global maximum. The features of this one-dimensional landscape — shoulders, local maxima, plateaus — are what make those two guarantees hard to achieve, and every algorithm below is a strategy for coping with one of them.

Hill climbing

The simplest local search is hill climbing, in its steepest-ascent form: a loop that repeatedly moves to the neighboring state of highest value, and halts when no neighbor is higher. It keeps no search tree — the only state it records is the current node and its objective value — and it never looks past its immediate neighbors. Russell and Norvig liken it to finding the top of Mount Everest in a thick fog while suffering from amnesia.2

Algorithm:Hill-Climbing(problem)\textsc{Hill-Climbing}(problem) — returns a state that is a local maximum
  1. 1
    currentMake-Node(problem.Initial-State)current \gets \textsc{Make-Node}(problem.\textsc{Initial-State})
  2. 2
    loop
  3. 3
    neighborneighbor \gets a highest-valued successor of currentcurrent
  4. 4
    if Value(neighbor)Value(current)\textsc{Value}(neighbor) \le \textsc{Value}(current) then
  5. 5
    return current.Statecurrent.\textsc{State}
  6. 6
    currentneighborcurrent \gets neighbor

Hill climbing is sometimes called greedy local search: it grabs the best neighbor without any thought for where to go next. Greed usually pays. Because a bad state is typically easy to improve, hill climbing makes rapid progress — from a random 8-queens position it reaches a near-solution in only a handful of steps. When it works, it works fast.

The 8-queens formulation

To see the algorithm at work, use the complete-state formulation of 8-queens: every state has all eight queens on the board, one per column, and the successors of a state are the states reached by moving a single queen to another square in its own column. The heuristic is the number of pairs of queens attacking each other, directly or indirectly; the global minimum of is , attained only by a genuine solution.

The 8-queens complete-state formulation. Each state fixes one queen per column; a move slides one queen within its column, giving 56 successors. The objective to minimize is h, the count of attacking queen pairs, and h = 0 marks a solution.

From a randomly generated 8-queens state, steepest-ascent hill climbing gets stuck 86% of the time, solving only 14% of instances. But it is fast where it does not stall: about four steps to reach a solution, three to reach a dead end — respectable for a space of million states.

A short trace shows the greedy loop at work. Start from a state with attacking pairs. Of its successors, the best drops to (there may be several tied at ; pick one), so the climber takes it. From the new state the best successor reaches , then , then , then . At every one of the successors has — no single queen move removes the last conflict without creating another — so the best neighbor ties or worsens the current value, the halting test fires, and hill climbing returns a state with one attacking pair still on the board.

Step (attacking pairs)Best successor Action
move (improves)
move
move
move
move
halt (no improvement)

Five steps of steep, cheap progress, then a wall: the state with is a local minimum of the cost (equivalently a local maximum of the nonattacking pairs objective), one conflict short of a solution and unable to take the last step. This is the case in miniature — fast to a near-solution, then stuck.

Why hill climbing gets stuck

The 86% failure rate comes from three features of the landscape, each visible in the one-dimensional figure above.

Ridges are the subtler trap. A ridge is a sequence of local maxima aligned along a direction the available moves cannot follow: the grid of states is laid over a slope that rises diagonally, so from each state every single-step move points downhill even though a diagonal combination would climb. Greedy search cannot see the diagonal and stalls, oscillating along the crest.

Why ridges defeat hill climbing. The available moves lie on a grid (dots) laid over a slope that rises from lower-left to upper-right. Each dot is a local maximum relative to its grid neighbors, so every single move points downhill; the true ascent runs diagonally, which no single move reaches.

The plateau case exposes a design choice. The algorithm above halts when the best neighbor merely ties the current value, which surrenders on every shoulder. Allowing sideways moves — stepping to an equal-valued neighbor in the hope the plateau is a shoulder — fixes shoulders but risks an infinite loop on a flat local maximum. The standard compromise caps the number of consecutive sideways moves (say, 100 for 8-queens). That single change lifts the 8-queens success rate from 14% to 94%, at the cost of longer runs: about 21 steps per success and 64 per failure.

Variants of hill climbing

Because the failure modes are landscape-shaped, several cheap modifications of the greedy rule help on landscapes of different shape.

  • Stochastic hill climbing chooses at random among the uphill moves, weighting the choice by steepness. It usually converges more slowly than steepest ascent but finds better solutions on some landscapes, because it is less prone to march straight into the nearest local maximum.
  • First-choice hill climbing implements stochastic hill climbing without enumerating all successors: it generates successors at random and takes the first one that improves on the current state. This is the right choice when a state has thousands of successors and computing all of them is wasteful.
  • Random-restart hill climbing attacks incompleteness head-on. It runs a series of hill climbs from randomly generated initial states, keeping the best result, until a goal is found. It is complete with probability approaching 1, because eventually a starting state lands in the global maximum's basin — or is the goal.
Random-restart hill climbing. Each restart drops the climber at a random location; it then walks uphill to whatever local maximum owns that basin. Most restarts land in a suboptimal basin (short peaks), but with enough tries one lands in the global maximum's basin. The success probability per restart is p; expected restarts to a goal is 1/p.

Random restart has a clean cost analysis. If each hill climb succeeds with probability , the expected number of restarts to reach a goal is . For 8-queens without sideways moves, , so roughly restarts suffice — six failures and one success. The expected total work is the cost of one success plus times the cost of a failure, about 22 steps in all. With sideways moves allowed, , so restarts and about steps. Even three-million-queens instances fall in under a minute.

Whether random restart succeeds depends entirely on the shape of the landscape. A landscape with few local maxima and shallow plateaus surrenders after a handful of restarts; an NP-hard problem, whose surface resembles a widely scattered family of balding porcupines on a flat floor — a spike of local optima on every needle — may have exponentially many traps. Even then, a reasonably good local maximum is often reachable in a few restarts.

Simulated annealing

Hill climbing never steps downhill, so it is doomed to stop at the first local maximum it climbs into. A pure random walk — move to a uniformly random neighbor, ignoring value — is complete (it eventually visits every state, the global maximum included) but hopelessly slow. Simulated annealing combines the two: mostly climb, but occasionally allow a downhill move, and let the willingness to go downhill shrink over time.

The physical metaphor makes the schedule intuitive. Switch to minimizing cost (gradient descent) and picture rolling a ping-pong ball into the deepest crevice of a bumpy surface. Let it roll and it settles in the first dent — a local minimum. Shake the surface and the ball hops out; shake too hard and it never settles anywhere. The trick is to shake hard at first, enough to bounce the ball out of shallow local minima, then gradually shake more gently so that once the ball finds the deep global minimum it stays. Shaking hard is a high temperature ; cooling is lowering on a fixed schedule.

The acceptance rule is where the temperature enters. Let be the change in value a proposed move would produce (positive if the move improves things). Improving moves are always accepted. A worsening move, , is accepted with probability

Two properties make this the right rule. It decreases exponentially with the badness of the move: a slightly worse neighbor is often accepted, a much worse one rarely. And it decreases as falls: bad moves are common early, when is high, and progressively forbidden as the schedule cools. If is lowered slowly enough, simulated annealing finds a global optimum with probability approaching 1.3

Put numbers to it. Suppose the objective is a cost being minimized (so is negative for a worsening move) and a proposed move worsens the cost by . The acceptance probability then depends entirely on where the schedule stands:

Read down a column and the cooling story is plain: at a move that worsens cost by is accepted of the time — the search wanders almost freely — while at the same move is accepted under of the time, and at it is effectively forbidden. Read across a row and the badness story appears: at , a worsening of is accepted of the time but a worsening of only . High temperature early lets the ball bounce out of shallow traps; low temperature late pins it in the deepest basin it has found.

Algorithm:Simulated-Annealing(problem,schedule)\textsc{Simulated-Annealing}(problem, schedule) — returns a solution state
  1. 1
    input: problemproblem; scheduleschedule, a mapping from time to temperature
  2. 2
    currentMake-Node(problem.Initial-State)current \gets \textsc{Make-Node}(problem.\textsc{Initial-State})
  3. 3
    for t=1,2,3,t = 1, 2, 3, \ldots do
  4. 4
    Tschedule(t)T \gets schedule(t)
  5. 5
    if T=0T = 0 then
  6. 6
    return currentcurrent
  7. 7
    nextnext \gets a randomly selected successor of currentcurrent
  8. 8
    ΔEValue(next)Value(current)\Delta E \gets \textsc{Value}(next) - \textsc{Value}(current)
  9. 9
    if ΔE>0\Delta E > 0 then
  10. 10
    currentnextcurrent \gets next
  11. 11
    else
  12. 12
    currentnextcurrent \gets next with probability eΔE/Te^{\Delta E / T}
Left: a cooling schedule, temperature T falling with time t (here geometric, T_t = T_0 * 0.95^t). Right: the acceptance probability for a worsening move, e^{dE/T}, plotted against the size of the worsening at three temperatures -- high T accepts almost anything, low T accepts almost nothing.

The inner loop is nearly identical to hill climbing; the two differences are that it picks a random successor rather than the best, and that a worsening successor is not rejected outright but accepted with probability . Simulated annealing was first used for VLSI layout in the early 1980s and is now a staple of factory scheduling and other large-scale optimization.

Random restart and simulated annealing both work with a single current state. Another way to escape a local optimum is to keep several states at once and let them share what they learn — and once you allow states to combine, you have the door to genetic algorithms and, past the discrete world, to the calculus of continuous optimization. This continues in Population and Continuous Search.

Footnotes

  1. Russell & Norvig, Artificial Intelligence: A Modern Approach (3rd ed.), §4.1 — Local Search Algorithms and Optimization Problems: local search keeps a single current node, uses constant memory, and suits problems where only the solution state matters, including pure optimization with an objective function and no goal test.
  2. Russell & Norvig, AIMA (3rd ed.), §4.1.1 — Hill-climbing search: the steepest-ascent loop, the 8-queens complete-state formulation ( successors, = attacking pairs), the local-maximum / ridge / plateau failure modes, sideways moves, and the stochastic, first-choice, and random-restart variants with their 8-queens statistics.
  3. Russell & Norvig, AIMA (3rd ed.), §4.1.2 — Simulated annealing: the annealing metaphor, the random-neighbor proposal with the acceptance rule for worsening moves, the cooling schedule, and the guarantee of a global optimum as slowly.

╌╌ END ╌╌