Articles / AI · Part 22

Hill Climbing and Its Limitations in Artificial Intelligence

Learn how the Hill Climbing algorithm improves solutions through neighbouring states. Explore its variations, Python implementation, limitations, and ways to overcome local maxima, plateaus, and ridges.

Hill Climbing and Its Limitations in Artificial Intelligence

Imagine standing somewhere on a mountain, surrounded by hills and valleys. Your goal is to reach the highest point.

You cannot see the entire landscape. You can only examine the nearby ground and decide which direction takes you higher. So, you keep moving towards a better position until none of the nearby positions seems higher.

This is the basic idea behind Hill Climbing, a local search algorithm used in Artificial Intelligence.

It makes decisions based on the current state and its neighbours. It does not need to explore every possible path or remember the complete search history. However, this simplicity also creates some important limitations.

1. What Is Hill Climbing?

Hill Climbing is a local search algorithm that repeatedly moves from its current state to a neighbouring state with a better evaluation value.

The process continues until the algorithm reaches a state where no neighbouring state offers an improvement.

In simple terms:

  • Start with an initial solution.
  • Examine its neighbouring solutions.
  • Move to a better neighbour.
  • Repeat until no better neighbour is available.

The algorithm is called Hill Climbing because, when maximising a value, it behaves like someone climbing towards a higher point.

Hill Climbing can also be used for minimisation problems. In that case, it moves towards neighbouring states with lower values.

2. Understanding the Main Components

Before looking at the algorithm, let’s understand a few important terms.

Current State: The solution or position the algorithm is currently examining.

Neighbouring State: A solution that can be reached from the current state through a small change.

Objective Function: A function that evaluates how good a particular solution is. It may need to be maximised or minimised.

Evaluation Value: The score returned by the objective function for a state.

For example, suppose an AI system is trying to arrange tasks in the most efficient order. Each possible arrangement is a state, and the objective function measures the total time required to complete the tasks.

Hill Climbing examines nearby arrangements and moves towards one that reduces the total time.

3. How Does Hill Climbing Work?

Consider a simple problem where we want to find the highest value of a function.

The algorithm begins at an initial point. It checks nearby points and moves to one with a higher value. It repeats this process until it cannot find a better neighbour.

The basic process is:

  1. Choose an initial state.
  2. Calculate its evaluation value.
  3. Generate its neighbouring states.
  4. Evaluate the neighbours.
  5. Move to a better neighbour.
  6. Stop when no neighbouring state improves the solution.

The algorithm does not necessarily find the best solution in the entire search space. It only looks for improvement around its current position.

4. A Simple Example

Suppose we want to maximise the following function:

\[
f(x)=-(x-3)^2+9
\]

We begin at (x=0). At each step, the algorithm checks the neighbouring integer values, (x-1) and (x+1).

Position (x) Function value
0 0
1 5
2 8
3 9
4 8

Starting at \(x=0\), the algorithm finds that moving to \(x=1\) improves the value. It continues to \(x=2\), then \(x=3\).

At \(x=3\), neither neighbouring position offers a higher value. The algorithm stops.

The maximum value in this example is 9, reached at \(x=3\).

This example shows how Hill Climbing improves a solution step by step.

5. Python Implementation

Let’s implement this example in Python.

def objective(x):
    return -(x - 3) ** 2 + 9


def hill_climbing(start):
    current = start

    while True:
        neighbours = [current - 1, current + 1]

        best_neighbour = max(
            neighbours,
            key=objective
        )

        if objective(best_neighbour) <= objective(current):
            return current, objective(current)

        current = best_neighbour


position, value = hill_climbing(0)

print("Best position:", position)
print("Maximum value:", value)

Output:

Best position: 3
Maximum value: 9

The function objective() calculates the value of each position. The algorithm checks the two neighbouring positions and moves to the one with the higher value.

When neither neighbour improves the current value, it stops.

This is a simple implementation for a one-dimensional problem. More complex problems may have many possible neighbours and different ways of generating them.

6. Types of Hill Climbing

Hill Climbing has several variations. They differ mainly in how they select the next neighbouring state.

A. Simple Hill Climbing

Simple Hill Climbing examines neighbouring states one by one. As soon as it finds a better state, it moves there.

It does not necessarily examine every neighbour before moving.

Example: If the first better neighbour is found, the algorithm immediately moves to it.

B. Steepest-Ascent Hill Climbing

Steepest-Ascent Hill Climbing examines all available neighbouring states and selects the one with the greatest improvement.

It makes a more informed choice among the neighbours, but examining all of them can require more computation.

C. Stochastic Hill Climbing

Stochastic Hill Climbing selects a better neighbouring state randomly rather than always choosing the first or the best one.

This introduces some randomness into the search and can help explore different possible directions.

7. Limitations of Hill Climbing

Hill Climbing is simple and often efficient, but it does not always find the best possible solution.

Its main limitations come from the shape of the search space.

A. Local Maximum

A local maximum is a state that is better than all its immediate neighbours, but it is not the highest point in the entire search space.

The algorithm may reach this point and stop, even though a better solution exists somewhere else.

Example: Imagine climbing a small hill while a much taller mountain exists nearby. Once you reach the top of the smaller hill, the surrounding ground may all be lower. Hill Climbing has no immediate reason to move away from it.

B. Plateau

A plateau is a flat area where neighbouring states have the same evaluation value.

The algorithm may struggle to decide which direction to take because moving in different directions does not produce an improvement.

Depending on its implementation, it may stop or move around without making useful progress.

C. Ridge

A ridge is a narrow path of improvement where reaching a higher point may require a sequence of carefully chosen moves.

The available neighbouring moves may not point directly towards the higher region. As a result, Hill Climbing can struggle to follow the ridge and may stop before reaching the best solution.

These problems explain why Hill Climbing does not guarantee a global optimum.

8. How Can We Overcome These Limitations?

There are several ways to improve Hill Climbing.

Random-Restart Hill Climbing: Run the algorithm multiple times using different initial states. Even if one attempt gets stuck at a local maximum, another starting position may lead to a better solution.

Sideways Moves: Allow the algorithm to move to a neighbouring state with the same evaluation value. This can help it cross a plateau, although unlimited sideways moves may cause loops.

Stochastic Selection: Randomly select among improving neighbours instead of always following the same selection rule. This can introduce variety into the search.

Another approach is to use a different search algorithm, such as Simulated Annealing, which sometimes accepts a move to a worse state to escape local maxima.

No technique guarantees success in every problem, but these approaches can make the search more effective.

9. Applications of Hill Climbing

Hill Climbing is useful in problems where the aim is to improve a solution rather than find a specific path from a starting point to a destination.

Some applications include:

  • Scheduling: Improving the arrangement of tasks to reduce completion time.
  • Route Optimisation: Finding shorter or more efficient routes.
  • Resource Allocation: Improving how limited resources are distributed.
  • Machine Learning: Optimising model parameters in certain settings.
  • Engineering: Searching for designs that perform better according to a chosen objective.
  • Game AI: Selecting moves that improve an evaluation score.

Its usefulness depends on how the neighbouring states and objective function are designed.

10. Advantages and Disadvantages

Advantages Disadvantages
Simple to understand and implement Can get stuck at local maxima
Requires relatively little memory May struggle with plateaus
Often finds improvements quickly Does not guarantee the global optimum
Useful for optimisation problems Results depend on the starting state

Hill Climbing differs from algorithms such as Breadth-First Search and Depth-First Search.

Traditional search algorithms explore a search space by following paths from an initial state. Hill Climbing focuses on improving the current solution by examining neighbouring states.

It usually does not maintain a complete search tree or remember the path taken to reach the current state.

This makes it useful for optimisation problems where the quality of the final solution matters more than the path used to reach it.

Conclusion

Hill Climbing demonstrates an important idea in Artificial Intelligence: sometimes, a good solution can be found by repeatedly making small improvements.

It is simple, uses little memory, and can work well for many optimisation problems. However, its tendency to get stuck at local maxima, plateaus, and ridges means it cannot guarantee the best possible solution.

The central lesson is that moving towards a better immediate option does not always lead to the best overall result.

In the next article, we will explore Gradient Descent, an optimisation algorithm that uses the direction of a function’s gradient to find a minimum.

Quick quiz

5 questions
What is the main idea behind Hill Climbing?
What is an objective function used for?
What is a local maximum?
Which variation examines all neighbours and selects the best one?
What is the purpose of Random-Restart Hill Climbing?

Keep a copy

Read offline as a printout or PDF.

farhanpaal.com

Keep reading

AI · Part 5

Types of Artificial Intelligence

Learn how Artificial Intelligence is classified by its capabilities and functionality, from Narrow AI and Reactive Machines to the hypothetical concepts of…

9 min read

Ask Farhan

Ask anything about Farhan