Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Example of Hill climbing algorithm

Hill climbing is a basic optimization algorithm in artificial intelligence. Let's consider a


simple example of using hill climbing to find the maximum value of a one-dimensional
function.

Suppose we want to find the maximum value of the function f(x) = -x^2 + 4x. We'll use hill
climbing to find the peak of this function.

1. Start with an initial guess for x, let's say x = 2.

2. Calculate the value of the function at this point: f(2) = -2^2 + 4*2 = 4.

3. Choose a small step size, for example, Δx = 0.1.

4. Calculate the value of the function at the neighboring point to the right: f(2 + 0.1) =
-1.69.

5. Compare the values of f(x) and f(x + Δx).

6. If f(x + Δx) is greater, move to the right by setting x = x + Δx.

7. Repeat steps 4-6 until you reach a point where f(x + Δx) is not greater than f(x). This
means you've found a local maximum.

In this case, hill climbing will converge to the local maximum of the function at x = 2. The
maximum value of the function f(x) is 4 at this point. Keep in mind that hill climbing is not
guaranteed to find the global maximum, and its effectiveness depends on the choice of
initial values and step size.Hill climbing algorithm vs simulated annealing algorithm

Difference between Hill Climbing Algorithm and Simulated annealing.

Characteristic Hill climbing algorithm Simulated annealing


algorithm

Acceptance of worse No Yes


solutions

Ability to escape local Limited Good


optima

Speed Fast Slow

Guarantee of finding global No No, but more likely than hill


optimum climbing

● Hill climbing algorithm is a greedy algorithm, meaning that it always moves towards a
better solution at each step.
● Simulated annealing algorithm is a probabilistic algorithm, meaning that it may move
towards a worse solution with a certain probability.
● Hill climbing algorithm is usually implemented using a simple iterative loop.
● Simulated annealing algorithm is usually implemented using a cooling schedule, which
gradually reduces the probability of moving towards a worse solution over time.

Overall, simulated annealing algorithm is a more powerful algorithm than hill climbing
algorithm, but it is also slower. Hill climbing algorithm is a good choice for simple
problems, but simulated annealing algorithm is a better choice for complex problems
with many local optima.

You might also like