Hill-Climbing Search Algorithm

by Sajjad Ahmed Niloy

Hill-Climbing Search

How it Works

  1. Start Anywhere: Begin with a random solution on the "hill".
  2. Look Around: Examine all immediately neighboring solutions.
  3. Take the Best Step: Move to the neighbor that has the best value (the highest point).
  4. Repeat: Continue this process until you reach a "peak" where no neighbor has a better value.

Analogy: The Lost Hiker

A hiker lost in thick fog wants to get to the highest point. They can only see their immediate surroundings, so they keep taking steps in whichever direction goes up. They stop when all directions around them lead downhill.

Pros

  • Simple and easy to implement.
  • Memory efficient; only stores current state.

Cons

  • Easily gets stuck on local maxima.
  • Cannot navigate plateaus or ridges.

Simulation

A climber (blue dot) explores a landscape to find the highest point (global maximum, green marker). Watch how it always moves to the highest adjacent point and stops at the first peak it finds, which is often a smaller, local maximum (red marker).

Deconstructing the Simulation:

1. The Landscape: This represents all possible solutions. The height of a point is its "value" or "quality". The goal is to find the highest point.

2. The Climber: The blue dot is the algorithm's current solution. It starts at a random location.

3. The "Greedy" Move: In each step, the climber looks at its immediate left and right neighbors. It will *always* move to whichever one is higher. It cannot see the whole landscape.

4. Getting Stuck: When the climber reaches a point where both neighbors are lower, it stops. This simulation clearly shows how this simple logic causes the climber to get stuck on the first peak it finds (a local maximum), often failing to find the true highest peak (the global maximum).

Python Code Example

# --- Conceptual Example --- # Goal: Find the maximum of a function import random # A function with a peak at x=5 def objective_function(x): return -((x - 5) ** 2) current_solution = random.uniform(0, 10) step_size = 0.1 for _ in range(100): current_value = objective_function(current_solution) # Check neighbors neighbor_right = objective_function(current_solution + step_size) neighbor_left = objective_function(current_solution - step_size) # Move to the best neighbor if neighbor_right > current_value and neighbor_right >= neighbor_left: current_solution += step_size elif neighbor_left > current_value and neighbor_left > neighbor_right: current_solution -= step_size else: # No better neighbor found, this is a peak break print(f"Found maximum near x = {current_solution:.2f}")