Reading 2: Hill Climbing

Always move uphill. Simple, fast, and not always right.

The Core Idea

Hill climbing is one of the oldest and simplest optimization strategies in computer science. Its logic mirrors what a person would do if they were literally trying to reach the top of a hill in thick fog: you cannot see the whole landscape, but you can feel the ground beneath your feet. So you take a step in every direction, notice which step goes uphill, and take it. Then you repeat. You keep climbing as long as any step takes you higher. When every step around you goes down or stays level, you stop — you have reached a peak.

Translated into algorithmic terms:

  1. Start with a candidate solution — any solution, often random.
  2. Evaluate its fitness score.
  3. Generate all neighboring solutions (those reachable by one small change).
  4. If any neighbor has a higher fitness score, move to the best one.
  5. Repeat from step 2 until no neighbor is better than the current solution.
  6. Report the current solution as the result.

That is the entire algorithm. Its simplicity is both its strength and its weakness.

A Worked Example: Classroom Scheduling

Suppose a middle school needs to schedule four subjects — Math, Science, English, and History — into four consecutive periods for a single class of students. The goal is to maximize student focus and performance based on research about attention and learning: demanding subjects benefit from morning periods, and variety helps prevent fatigue.

The school has developed a simple fitness scoring rubric for any given schedule:

Scoring rule Points
Math or Science in Period 1 or 2 (peak attention)+3 each
No two high-demand subjects back-to-back+2
English or History in Period 3 or 4+1 each
Math immediately before or after History−2 (poor transition)

There are 24 possible orderings of four subjects. A hill climbing agent starts with a random arrangement and improves it by swapping pairs of subjects one at a time. A "neighbor" of any arrangement is any arrangement reachable by swapping exactly two subjects.

Step-by-Step Trace

Starting arrangement: [History, English, Math, Science]

Period 1Period 2Period 3Period 4Fitness
HistoryEnglishMathScience Math in P3: 0. Science in P4: 0. English in P3/P4: +1. History in P3/P4: 0 (P1). Math after English, Science after Math: back-to-back high-demand: 0 bonus. History-Math not adjacent. Total: 1

The agent now evaluates all neighbors — every arrangement reachable by one swap — and selects the best:

SwapNew arrangementFitness
Swap History ↔ English[English, History, Math, Science]English P3/P4: 0(P1). History P3/P4: 0(P2). Math P3: 0. Science P4: 0.  0
Swap History ↔ Math[Math, English, History, Science]Math P1: +3. No back-to-back high-demand: +2. English P3: +1. History P3: +1. 7 ↑ best
Swap History ↔ Science[Science, English, Math, History]Science P1: +3. Math P3: 0. English P2: 0. History P4: +1. Math-History adjacent: −2. 2
Swap English ↔ Math[History, Math, English, Science]Math P2: +3. No back-to-back: +2. English P3: +1. History P1: 0. Math-History adjacent: −2. 4
Swap English ↔ Science[History, Science, Math, English]Science P2: +3. Math P3: 0. English P4: +1. Back-to-back Science-Math: 0. History P1: 0. 4
Swap Math ↔ Science[History, English, Science, Math]Science P3: 0. Math P4: 0. English P2: 0. History P1: 0. 1

The best neighbor is [Math, English, History, Science] with fitness 7. The agent moves there.

Pass 2: From [Math, English, History, Science] (fitness 7), the agent evaluates all neighbors again. Every swap either keeps the score at 7 or reduces it. The agent stops.

Result: Hill climbing found [Math, English, History, Science] with fitness 7. Is this the best possible schedule? Let us check one other arrangement: [Math, Science, English, History] scores Math P1 (+3), Science P2 (+3), no back-to-back high-demand (+2), English P3 (+1), History P4 (+1) = 10. Hill climbing missed it entirely — because it got stuck on a local optimum.

The agent stopped at fitness 7 because every single swap from that arrangement looked worse. But a solution two swaps away — [Math, Science, English, History] — was significantly better. To reach it, the agent would have had to accept a worse solution as an intermediate step, and hill climbing never does that.

Three Ways Hill Climbing Gets Stuck

The scheduling example illustrates the fundamental limitation of hill climbing. Three specific landscape features cause it to fail:

Local Optima

A local optimum is a solution that is better than all of its immediate neighbors, but not the best solution overall. It is the highest point within a small region of the landscape — a foothill rather than the mountain peak. Hill climbing always stops at a local optimum because it will not take a downward step to reach higher ground elsewhere.

This is the most common and most serious failure mode. In complex problems with many local optima, hill climbing frequently gets trapped far from the global optimum.

Plateaus

A plateau is a flat region of the landscape where many neighboring solutions have exactly the same fitness score. Hill climbing has no signal to follow on a plateau — all neighbors look equally good (or equally mediocre). The agent can wander randomly, make no progress, or stop entirely depending on how it handles ties.

Think of a scheduling plateau as a situation where swapping any two subjects leaves the score unchanged. You are stuck in the middle of a flat mesa with no idea which direction leads to higher ground.

Ridges

A ridge is a narrow elevated region of the landscape that runs in a direction that is not aligned with any single swap. To traverse a ridge and reach the peak at its end, you would need to take a diagonal step — changing two things at once. But hill climbing only considers one-step neighbors. Every one-step move from a ridge goes downhill, so the agent stops, even though the peak is tantalizingly close.

In scheduling terms: the best solution might require simultaneously moving Math to Period 1 and Science to Period 2. Either change alone makes things worse. Both changes together make things much better. Hill climbing, restricted to one swap at a time, can never make both changes simultaneously.

A Simple Countermeasure: Random Restarts

The most common practical fix for local optima is random restarts: run hill climbing multiple times from different random starting points and keep the best result. This does not guarantee finding the global optimum, but it dramatically reduces the chance of being stuck on a poor local optimum. The more restarts, the better the expected result — at the cost of more computation.

Random restarts are a pragmatic engineering solution, not a theoretical one. They acknowledge that hill climbing is imperfect and compensate with brute repetition. For many practical problems, this is good enough. For problems with deeply complex landscapes, something more powerful is needed — which is where genetic algorithms come in.

When Hill Climbing Is the Right Tool

Despite its limitations, hill climbing is widely used because it has genuine strengths for the right problems:

Network routing, parameter tuning, game AI for simple games, and logistics optimization in constrained environments all use hill climbing or close variants. The key engineering judgment is whether the landscape is smooth enough that local optima are not a serious concern — or whether you need the broader exploration that genetic algorithms provide.