Introduction to Hill Climbing | Artificial Intelligence
Last Updated :
20 Apr, 2023
Hill climbing is a simple optimization algorithm used in Artificial Intelligence (AI) to find the best possible solution for a given problem. It belongs to the family of local search algorithms and is often used in optimization problems where the goal is to find the best solution from a set of possible solutions.
- In Hill Climbing, the algorithm starts with an initial solution and then iteratively makes small changes to it in order to improve the solution. These changes are based on a heuristic function that evaluates the quality of the solution. The algorithm continues to make these small changes until it reaches a local maximum, meaning that no further improvement can be made with the current set of moves.
- There are several variations of Hill Climbing, including steepest ascent Hill Climbing, first-choice Hill Climbing, and simulated annealing. In steepest ascent Hill Climbing, the algorithm evaluates all the possible moves from the current solution and selects the one that leads to the best improvement. In first-choice Hill Climbing, the algorithm randomly selects a move and accepts it if it leads to an improvement, regardless of whether it is the best move. Simulated annealing is a probabilistic variation of Hill Climbing that allows the algorithm to occasionally accept worse moves in order to avoid getting stuck in local maxima.
Hill Climbing can be useful in a variety of optimization problems, such as scheduling, route planning, and resource allocation. However, it has some limitations, such as the tendency to get stuck in local maxima and the lack of diversity in the search space. Therefore, it is often combined with other optimization techniques, such as genetic algorithms or simulated annealing, to overcome these limitations and improve the search results.
Advantages of Hill Climbing algorithm:
- Hill Climbing is a simple and intuitive algorithm that is easy to understand and implement.
- It can be used in a wide variety of optimization problems, including those with a large search space and complex constraints.
- Hill Climbing is often very efficient in finding local optima, making it a good choice for problems where a good solution is needed quickly.
- The algorithm can be easily modified and extended to include additional heuristics or constraints.
Disadvantages of Hill Climbing algorithm:
- Hill Climbing can get stuck in local optima, meaning that it may not find the global optimum of the problem.
- The algorithm is sensitive to the choice of initial solution, and a poor initial solution may result in a poor final solution.
- Hill Climbing does not explore the search space very thoroughly, which can limit its ability to find better solutions.
- It may be less effective than other optimization algorithms, such as genetic algorithms or simulated annealing, for certain types of problems.
Hill Climbing is a heuristic search used for mathematical optimization problems in the field of Artificial Intelligence.
Given a large set of inputs and a good heuristic function, it tries to find a sufficiently good solution to the problem. This solution may not be the global optimal maximum.
- In the above definition, mathematical optimization problems imply that hill-climbing solves the problems where we need to maximize or minimize a given real function by choosing values from the given inputs. Example-Travelling salesman problem where we need to minimize the distance traveled by the salesman.
- ‘Heuristic search’ means that this search algorithm may not find the optimal solution to the problem. However, it will give a good solution in a reasonable time.
- A heuristic function is a function that will rank all the possible alternatives at any branching step in the search algorithm based on the available information. It helps the algorithm to select the best route out of possible routes.
Features of Hill Climbing
1. Variant of generating and test algorithm:
It is a variant of generating and testing algorithms. The generate and test algorithm is as follows :
- Generate possible solutions.
- Test to see if this is the expected solution.
- If the solution has been found quit else go to step 1.
Hence we call Hill climbing a variant of generating and test algorithm as it takes the feedback from the test procedure. Then this feedback is utilized by the generator in deciding the next move in the search space.
At any point in state space, the search moves in that direction only which optimizes the cost of function with the hope of finding the optimal solution at the end.
Types of Hill Climbing
1. Simple Hill climbing:
It examines the neighboring nodes one by one and selects the first neighboring node which optimizes the current cost as the next node.
Algorithm for Simple Hill climbing :
Evaluate the initial state. If it is a goal state then stop and return success. Otherwise, make the initial state as the current state.
Loop until the solution state is found or there are no new operators present which can be applied to the current state.
- Select a state that has not been yet applied to the current state and apply it to produce a new state.
- Perform these to evaluate the new state.
- If the current state is a goal state, then stop and return success.
- If it is better than the current state, then make it the current state and proceed further.
- If it is not better than the current state, then continue in the loop until a solution is found.
Exit from the function.
2. Steepest-Ascent Hill climbing:
It first examines all the neighboring nodes and then selects the node closest to the solution state as of the next node.
Algorithm for Steepest Ascent Hill climbing :
- Evaluate the initial state. If it is a goal state then stop and return success. Otherwise, make the initial state as the current state.
- Repeat these steps until a solution is found or the current state does not change
- Select a state that has not been yet applied to the current state.
- Initialize a new ‘best state’ equal to the current state and apply it to produce a new state.
- Perform these to evaluate the new state
- If the current state is a goal state, then stop and return success.
- If it is better than the best state, then make it the best state else continue the loop with another new state.
- Make the best state as the current state and go to Step 2 of the second point.
- Exit from the function.
3. Stochastic hill climbing:
It does not examine all the neighboring nodes before deciding which node to select. It just selects a neighboring node at random and decides (based on the amount of improvement in that neighbor) whether to move to that neighbor or to examine another.
- Evaluate the initial state. If it is a goal state then stop and return success. Otherwise, make the initial state the current state.
- Repeat these steps until a solution is found or the current state does not change.
- Select a state that has not been yet applied to the current state.
- Apply the successor function to the current state and generate all the neighbor states.
- Among the generated neighbor states which are better than the current state choose a state randomly (or based on some probability function).
- If the chosen state is the goal state, then return success, else make it the current state and repeat step 2 of the second point.
- Exit from the function.
State Space diagram for Hill Climbing
The state-space diagram is a graphical representation of the set of states our search algorithm can reach vs the value of our objective function(the function which we wish to maximize).
- X-axis: denotes the state space ie states or configuration our algorithm may reach.
- Y-axis: denotes the values of objective function corresponding to a particular state.
The best solution will be a state space where the objective function has a maximum value(global maximum).

Different regions in the State Space Diagram:
- Local maximum: It is a state which is better than its neighboring state however there exists a state which is better than it(global maximum). This state is better because here the value of the objective function is higher than its neighbors.
- Global maximum: It is the best possible state in the state space diagram. This is because, at this stage, the objective function has the highest value.
- Plateau/flat local maximum: It is a flat region of state space where neighboring states have the same value.
- Ridge: It is a region that is higher than its neighbors but itself has a slope. It is a special kind of local maximum.
- Current state: The region of the state space diagram where we are currently present during the search.
- Shoulder: It is a plateau that has an uphill edge.
Problems in different regions in Hill climbing
Hill climbing cannot reach the optimal/best state(global maximum) if it enters any of the following regions :
- Local maximum: At a local maximum all neighboring states have a value that is worse than the current state. Since hill-climbing uses a greedy approach, it will not move to the worse state and terminate itself. The process will end even though a better solution may exist.
To overcome the local maximum problem: Utilize the backtracking technique. Maintain a list of visited states. If the search reaches an undesirable state, it can backtrack to the previous configuration and explore a new path.
- Plateau: On the plateau, all neighbors have the same value. Hence, it is not possible to select the best direction.
To overcome plateaus: Make a big jump. Randomly select a state far away from the current state. Chances are that we will land in a non-plateau region.
- Ridge: Any point on a ridge can look like a peak because movement in all possible directions is downward. Hence the algorithm stops when it reaches this state.
To overcome Ridge: In this kind of obstacle, use two or more rules before testing. It implies moving in several directions at once.
Here is a simple example of hill climbing in Python:
C++
#include <algorithm>
#include <iostream>
#include <vector>
std::vector<int> generate_neighbors(int x)
{
}
int hill_climbing(int (*f)(int), int x0)
{
int x = x0;
while (true) {
std::vector<int> neighbors = generate_neighbors(
x);
int best_neighbor = *std::max_element(
neighbors.begin(), neighbors.end(),
[f](int a, int b) {
return f(a) < f(b);
});
if (f(best_neighbor)
<= f(x))
return x;
x = best_neighbor;
}
}
int main()
{
int x0 = 1;
int x = hill_climbing([](int x) { return x * x; }, x0);
std::cout << "Result: " << x << std::endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
public static List<Integer> generate_neighbors(int x)
{
return new ArrayList<>();
}
public static int
hill_climbing(Function<Integer, Integer> f, int x0)
{
int x = x0;
while (true) {
List<Integer> neighbors = generate_neighbors(
x);
int best_neighbor = Collections.max(
neighbors,
Comparator.comparingInt(
f::apply));
if (f.apply(best_neighbor)
<= f.apply(x))
return x;
x = best_neighbor;
}
}
public static void main(String[] args)
{
int x0 = 1;
int x = hill_climbing((Integer y) -> y * y, x0);
System.out.println("Result: " + x);
}
|
Python3
def hill_climbing(f, x0):
x = x0
while True:
neighbors = generate_neighbors(x)
best_neighbor = max(neighbors, key=f)
if f(best_neighbor) <= f(x):
return x
x = best_neighbor
|
Javascript
function hill_climbing(f, x0) {
let x = x0;
while (true) {
const neighbors = generate_neighbors(x);
const best_neighbor = neighbors.reduce((a, b) => f(a) > f(b) ? a : b);
if (f(best_neighbor) <= f(x)) {
return x;
}
x = best_neighbor;
}
}
|
Please Login to comment...
Similar Reads
Difference Between Hill Climbing and Simulated Annealing Algorithm
Pre-requisites: Hill Climbing, Hill Climbing is a heuristic optimization process that iteratively advances towards a better solution at each step in order to find the best solution in a given search space. It is a straightforward and quick technique that iteratively improves the initial solution by making little changes to it. Hill Climbing only ac
4 min read
Difference Between Greedy Best First Search and Hill Climbing Algorithm
AI algorithms, or Artificial Intelligence algorithms, are computational procedures or methods designed to solve problems and perform tasks that would typically require human intelligence. These algorithms are fundamental components of artificial intelligence systems and play a crucial role in various applications across different domains. Pre-requi
4 min read
Hill Climbing and Simulated Annealing for the Traveling Salesman Problem
The Traveling Salesman Problem (TSP) is a classic example where a salesman must visit a set of cities exactly once and return to the starting point while minimizing the total distance traveled. This article explores two popular optimization algorithms—Hill Climbing and Simulated Annealing—and demonstrates their application to the TSP using Python.
5 min read
Difference Between Artificial Intelligence and Business Intelligence
Artificial Intelligence: Artificial intelligence is the field of computer science associated with making machines that are programmed to be capable of thinking and solving problems like the human brain. These machines can perform human-like tasks and can also learn from past experiences like human beings. Artificial intelligence involves advanced a
3 min read
Super Intelligence vs Artificial Intelligence
Super Intelligence vs Artificial Intelligence: Artificial Super intelligence is like a computer program that can be smarter than people. It learns and thinks by itself. Artificial Intelligence (AI) has become a widely talked-about topic in today’s rapidly changing world. In this article, we are going to discuss super intelligence vs artificial inte
6 min read
Difference Between Artificial Intelligence and Human Intelligence
Artificial Intelligence: Artificial Intelligence is based on human insights that can be decided in a way that can machine can effortlessly actualize the tasks, from the basic to those that are indeed more complex. The reason for manufactured insights is learning, problem-solving, reasoning, and perception. This term may be connected to any machines
5 min read
Top 5 Programming Languages For Artificial Intelligence
In recent years, Artificial Intelligence has seen exponential growth and innovation in the field of technology. As the demand for Artificial intelligence among companies and developers is continuously increasing and several programming languages have emerged as popular choices for the Artificial Intelligence Development field, so, in that case, the
5 min read
Emergence Of Artificial Intelligence
Concept of AI goes back to the classical ages. Under greek mythology, the concept of machines and mechanical men were well thought of. In 1950, “ALAS TURING”, published landmark paper “Computing Machinery and Intelligence”, proposes the “Imitation Game”, known as the famous “TURING TEST” He speculated about the possibility of creating machines that
5 min read
Agents in Artificial Intelligence
In artificial intelligence, an agent is a computer program or system that is designed to perceive its environment, make decisions and take actions to achieve a specific goal or set of goals. The agent operates autonomously, meaning it is not directly controlled by a human operator. Agents can be classified into different types based on their charac
11 min read
Integration of Artificial Intelligence and BlockChain
Artificial Intelligence and Blockchain are proving to be quite a powerful combination, improving virtually every industry in which they're implemented. These technologies can be combined to upgrade everything from food supply chain logistics and healthcare record sharing to media royalties and financial security. The integration of AI and Blockchai
8 min read