Question: # TODO: Hill-climbing search algorithm: a loop that continually moves in the direction of increasing value. # It terminates when it reaches a peak where
# TODO: Hill-climbing search algorithm: a loop that continually moves in the direction of increasing value.
# It terminates when it reaches a peak where no neighbor has a higher value.
# objective function: function to be maximized
# initial_state: initial (x, y) vector
# step_size: numerical interval by which to change the current (x,y) state to generate a new neighboring state
# print_iters: set to True to print out the current value at each iteration
# returns: best [x, y] solution found
def hill_climbing(objective_function, initial_state = np.array([0, 0]), step_size = 0.01, print_iters=True):
# set the starting point of the search algorithm
current_state = initial_state
# loop until a peak is found
i = 0
while True:
# Step 1: create a list of neighboring states to the current state
neighbors = [[current_state[0] + step_size, current_state[1]], [current_state[0] - step_size, current_state[1]], [current_state[0], current_state[1] + step_size], [current_state[0], current_state[1]]]
# Step 2: calculate the objective function at each of the neighboring states
neighborsObjVal = [objective_function(neighbors[0]), objective_function(neighbors[1]), objective_function(neighbors[2]), objective_function(neighbors[3])]
# Step 3: determine the highest-valued neighboring state
maxObjVal = neighborsObjVal[0]
maxNeighbor = np.array(neighbors[0])
k = 0
for x in neighborsObjVal:
if x > maxObjVal:
maxObjVal = x
maxNeighbor = neighbors[k]
k = k + 1
# Step 4: compare the highest value among neighboring states to the current value
# if the latter is higher, we have found a peak -> return the current state
# if the former is higher, assign current state to be the best neighbor state
current_value = objective_function(current_state)
if maxObjVal > current_value:
current_state = maxNeighbor
current_value = objective_function(current_state)
else:
return current_state
if print_iters:
print('iteration: {}, current_state: {}, current_value: {}'.format(i, current_state, current_value))
i += 1
hill_climbing_solution = hill_climbing(objective_function)
print('Hill climbing solution is:', hill_climbing_solution)
I am supposed to get this as the solution for the hill climbing function but my program is not working properly. Kindly help on this problem

iteration: 99 , current_state: [0.420.58], current_value: 3.7406959833985103 iteration: 100, current_state: [0.420.59], current_value: 3.7471648548982013 iteration: 101, current_state: [0.430.59], current_value: 3.753730545333419 iteration: 102, current_state: [0.430.6], current_value: 3.758597899100315 iteration: 103, current_state: [0.440.6], current_value: 3.7638298463272735 iteration: 104, current_state: [0.440.61], current_value: 3.76717859312926 iteration: 105, current_state: [0.450.61], current_value: 3.770996620728709 iteration: 106, current_state: [0.450.62], current_value: 3.772852644915758 iteration: 107, current_state: [0.460.62], current_value: 3.7752396172240177 iteration: 108, current_state: [0.460.63], current_value: 3.7756316536536967 Hill climbing solution is_[-8.46 0.63]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
