Department of Computer Science and Engineering AISC Lab[CS3131]
Program 3
Date:3rd September 2024
AIM:
Minimize the function f(x) = x^2 with the bounds [-5, 5] using the Hill Climbing technique.
DESCRIPTION:
Hill Climbing is a simple optimization algorithm in AI used to find the best solution for a
problem. It starts with an initial solution and iteratively makes small changes to improve it,
guided by a heuristic function. The process continues until a local maximum is reached,
where no further improvements are possible.
Advantages:
- Simple, intuitive, and easy to implement.
- Works for a wide range of optimization problems.
- Efficient at finding local optima.
Disadvantages:
- Can get stuck in local optima.
- Dependent on the initial solution.
- Limited search space exploration.
ALGORITHM:
1. Initialization:
- Choose a random starting point ( x_0 ) within the bounds ([-5, 5]).
2. Evaluate Objective Function:
- Calculate the value of ( f(x) = x^2 ) at the starting point ( x_0 ), i.e., ( f(x_0) ).
3. Generate a Neighbour:
- Select a neighbouring point ( x_{neighbour} = x_0 + Delta x ), where ( Delta x ) is a
small random step (positive or negative). Ensure the new point stays within the bounds ([-5,
5]).
4. Evaluate Neighbour:
- Compute ( f(x_{neighbour}) ). If ( f(x_{neighbour}) < f(x_0) ), move to
( x_{neighbour} ) (i.e., ( x_0 = x_{neighbour} )).
5. Repeat or Stop:
Suhani Talreja 229301425
Department of Computer Science and Engineering AISC Lab[CS3131]
- Repeat steps 3 and 4 for a set number of iterations or until no improvement is observed
(i.e., ( f(x_{neighbour}) geq f(x_0) )).
This process helps find the minimum of ( f(x) = x^2 ) within the bounds. For this particular
function, the minimum should converge near ( x = 0 ), where ( f(x) = 0 ).
SOURCE CODE:
import random
def hill_climb(obj_func, bnds, step=0.1, max_iter=1000):
best_sol = None
best_val = float('inf')
for _ in range(max_iter):
cur_sol = random.uniform(bnds[0], bnds[1])
cur_val = obj_func(cur_sol)
while True:
next_sol = cur_sol + random.uniform(-step, step)
if next_sol < bnds[0] or next_sol > bnds[1]:
continue
next_val = obj_func(next_sol)
if next_val < cur_val:
cur_sol = next_sol
cur_val = next_val
else:
break
if cur_val < best_val:
best_val = cur_val
best_sol = cur_sol
return best_sol, best_val
def obj_func(x):
return x**2
Suhani Talreja 229301425
Department of Computer Science and Engineering AISC Lab[CS3131]
bnds = [-5, 5]
sol, val = hill_climb(obj_func, bnds, max_iter=100)
print(f"Optimal solution: x = {sol}, f(x) = {val}")
OUTPUT:
Suhani Talreja 229301425