⛰️ Hill Climbing
999 words
5 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# ⛰️ Hill Climbing ## 1. 🎯 Learning Objectives By the end of this topic, you will be able to: - Trace Hill Climbing on a simple state landscape - Explain the difference between Hill Climbing and Best First Search (no OPEN list) - Identify local optima, plateaus, and ridges - Describe variants: Stochastic Hill Climb...

⛰️ Hill Climbing
1. 🎯 Learning Objectives
By the end of this topic, you will be able to:
- Trace Hill Climbing on a simple state landscape
- Explain the difference between Hill Climbing and Best First Search (no OPEN list)
- Identify local optima, plateaus, and ridges
- Describe variants: Stochastic Hill Climbing, Random Restart Hill Climbing
- Explain why Hill Climbing uses O(1) space
2. 📋 Prerequisites
| Prerequisite | Course | Why It Matters |
|---|---|---|
| Heuristics | W3 T1 | Hill Climbing uses heuristic evaluation |
| Best First Search | W3 T1 | Contrast with Hill Climbing |
| Local vs. global search | General | Understanding search strategy |
3. 📖 Core Content
3.1 Intuition: Always Go Uphill
Imagine you are blindfolded on a mountain and want to reach the peak. You feel the ground around you with your foot. If one direction goes up, you step that way. You repeat until every direction goes down (you are at a local peak).
Hill Climbing is a local search algorithm that keeps just one current state and repeatedly moves to the best neighboring state (the one with the lowest h-value, or highest "value"). It has no OPEN list and no CLOSED list — it just keeps moving.
3.2 The Hill Climbing Algorithm
textHillClimbing(initial_state, goal_test, move_gen, heuristic): current = initial_state while True: if goal_test(current): return current // Found goal neighbors = move_gen(current) if neighbors is empty: return current // Stuck at local optimum // Find the best neighbor (lowest h-value for minimization) best = neighbor with minimum heuristic value if h(best) >= h(current): return current // Stuck — no improvement current = best // Move to the best neighbor
3.3 Worked Example: Hill Climbing on 8-Puzzle
Consider 8-puzzle with Manhattan heuristic. Start state:
pseudo2 8 3 1 6 4 7 0 5
Goal:
pseudo1 2 3 8 0 4 7 6 5
Step 1: Current = start state. h(Current) = 5 (Manhattan). Step 2: Generate neighbors (move blank up, left, right):
- Move blank UP: [[2, 0, 3], [1, 6, 4], [7, 8, 5]] → Compute h = ? Let's trace carefully:
- State after blank up: [2, 0, 3], [1, 6, 4], [7, 8, 5] Tile positions: 1(1,0), 2(0,0), 3(0,2), 4(1,2), 5(2,2), 6(1,1), 7(2,0), 8(1,1? wait 8 is at (1,1)) Manhattan: 1→goal(0,0): |1-0|+|0-0|=1; 2→goal(0,1): |0-0|+|0-1|=1; 3→goal(0,2): 0; 4→goal(1,2): 0; 5→goal(2,2): 0; 6→goal(2,1): |1-2|+|1-1|=1; 7→goal(2,0): 0; 8→goal(1,0): |1-1|+|1-0|=1 Total = 4
- Move blank LEFT: [2, 8, 3], [1, 6, 4], [0, 7, 5] Manhattan ≈ 5 (similar to start)
- Move blank RIGHT: [2, 8, 3], [1, 6, 4], [7, 5, 0] Manhattan ≈ 6 Best neighbor: Blank UP (h=4) which is < h(current)=5. Move to it. Step 3: Current = [2, 0, 3], [1, 6, 4], [7, 8, 5]. h = 4. Continue until goal reached or stuck.
3.4 Hill Climbing vs. Best First Search
| Aspect | Hill Climbing | Best First Search |
|---|---|---|
| State kept | One (current) | Many (OPEN list) |
| Memory | O(1) | O(b^d) |
| OPEN list | No | Yes (priority queue) |
| CLOSED list | No | Yes |
| Backtracking | No | Yes (via OPEN) |
| Completeness | No | Yes |
| Local optima | Gets stuck | Can escape via other branches |
3.5 Local Optima and How Hill Climbing Gets Stuck
(Diagram)
Hill Climbing gets stuck at:
- Local optima: A state that is better than all its neighbors but not the global optimum
- Plateaus: All neighbors have the same heuristic value
- Ridges: A sequence of states where each is better than the previous, but no single move improves
3.6 Variants of Hill Climbing
Stochastic Hill Climbing: Instead of always picking the best neighbor, pick randomly among the better neighbors. This can sometimes escape local optima by chance.
Random Restart Hill Climbing: Run Hill Climbing multiple times from random initial states, keeping the best solution found. This is the most effective simple variant.
textRandomRestartHillClimbing(num_restarts, move_gen, heuristic): best_solution = null best_value = infinity for i = 1 to num_restarts: initial = random_state() solution = HillClimbing(initial, move_gen, heuristic) if value(solution) < best_value: best_value = value(solution) best_solution = solution return best_solution
First-Choice Hill Climbing: Pick the first neighbor that is better, rather than evaluating all neighbors. Useful when branching factor is large.
4. 📐 Key Formulas / Concepts
| Concept | Description |
|---|---|
| Hill Climbing space | O(1) — only current state |
| Local optimum | State with no better neighbor |
| Plateau | All neighbors have same value |
| Stochastic HC | Randomly pick among improving neighbors |
| Random Restart HC | Multiple runs from different starts |
5. ⚠️ Common Pitfalls
Pitfall 1: Confusing Hill Climbing with Best First
The mistake: Thinking Hill Climbing maintains an OPEN list. Correct approach: Hill Climbing has NO memory — it only knows the current state. It cannot backtrack.
Pitfall 2: Expecting Optimal Solutions
The mistake: Assuming Hill Climbing will find the global optimum. Correct approach: Hill Climbing finds a local optimum. Use Random Restart or Simulated Annealing for better results.
Pitfall 3: Applying Hill Climbing to Discontinuous Landscapes
The mistake: Using Hill Climbing where the heuristic function is not smoothly varying. Correct approach: Hill Climbing works best on smooth landscapes where small changes produce small heuristic changes.
6. 📝 Practice Questions
Q1: Trace Hill Climbing on a problem where start has h=10, neighbors have h=[12, 8, 15, 9].Answer: Hill Climbing picks the neighbor with h=8 (lowest). If that state's neighbors all have h ≥ 8, Hill Climbing stops there — it has found a local optimum (h=8) even though the global optimum might have h=0. Q2: Why doesn't Hill Climbing use an OPEN list?Answer: Hill Climbing is a local search — it only keeps the current state and tries to improve it. It has no need to store alternative paths because it never backtracks. This makes it memory-efficient (O(1)) but also means it can get stuck in local optima. Q3: How does Random Restart Hill Climbing improve over standard Hill Climbing?Answer: Random Restart runs Hill Climbing from multiple random starting positions. If one run gets stuck in a local optimum, another run from a different start might reach the global optimum. It provides probabilistic completeness — as the number of restarts increases, the probability of finding the global optimum approaches 1. Join Discord PreviousHeuristic FunctionsNextLocal Search Overview