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

def objective_function(x):

return -(x**2) + 4*x

def hill_climbing(initial_x, step_size, max_iterations):


current_x = initial_x

for _ in range(max_iterations):
current_value = objective_function(current_x)

# Try a small increment


new_x = current_x + step_size
new_value = objective_function(new_x)

# If the new value is better, move to the new solution


if new_value > current_value:
current_x = new_x
else:
break # Stop if no improvement

return current_x, objective_function(current_x)

# Example usage
initial_solution = 2
step_size = 0.1
max_iterations = 500

final_solution, max_value = hill_climbing(initial_solution, step_size, max_iterations)

print(f"Final Solution: x = {final_solution}, Max Value: {max_value}")

output Final Solution: x = 2, Max Value: 4

import matplotlib.pyplot as plt


import numpy as np

def objective_function(x):
return -(x**2) + 4*x

def hill_climbing(initial_x, step_size, max_iterations):


history = []

current_x = initial_x
for _ in range(max_iterations):
current_value = objective_function(current_x)
history.append((current_x, current_value))

# Try a small increment


new_x = current_x + step_size
new_value = objective_function(new_x)

# If the new value is better, move to the new solution


if new_value > current_value:
current_x = new_x
else:
break # Stop if no improvement

return history

# Example usage
initial_solution = 2.0
step_size = 0.1
max_iterations = 50

history = hill_climbing(initial_solution, step_size, max_iterations)

# Plot the function


x_values = np.linspace(0, 4, 100)
y_values = objective_function(x_values)

plt.plot(x_values, y_values, label='Objective Function')

# Plot the progression of the algorithm


hill_x, hill_y = zip(*history)
plt.scatter(hill_x, hill_y, color='red', label='Hill Climbing Progression')

plt.title('Hill Climbing on f(x) = -x^2 + 4x')


plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.show()

import matplotlib.pyplot as plt


import numpy as np

def objective_function(x):
return -(x**2) + 4*x

def hill_climbing(initial_x, step_size, max_iterations):


history = []
history = []

current_x = initial_x
for _ in range(max_iterations):
current_value = objective_function(current_x)
history.append((current_x, current_value))

# Try a small increment


new_x = current_x + step_size
new_value = objective_function(new_x)

# If the new value is better, move to the new solution


if new_value > current_value:
current_x = new_x
else:
break # Stop if no improvement

return history

# Example usage
initial_solutions = [0.5, 2.0, 3.5]
step_size = 0.1
max_iterations = 50

plt.figure(figsize=(12, 6))

for initial_solution in initial_solutions:


history = hill_climbing(initial_solution, step_size, max_iterations)

# Plot the function


x_values = np.linspace(0, 4, 100)
y_values = objective_function(x_values)
plt.plot(x_values, y_values, label='Objective Function')

# Plot the progression of the algorithm


hill_x, hill_y = zip(*history)
plt.scatter(hill_x, hill_y, label=f'Initial Solution = {initial_solution}')

plt.title('Hill Climbing on f(x) = -x^2 + 4x with Different Initial Solutions')


plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.show()

You might also like