Question: Write in Python (Artificial Intelligence on Local Search) # TODO: Hill-climbing search algorithm: a loop that continually moves in the direction of increasing value. #
Write in Python (Artificial Intelligence on Local Search)
# 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
# Step 2: calculate the objective function at each of the neighboring states
# Step 3: determine the highest-valued neighboring state
# 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 print_iters:
print('iteration: {}, current_state: {}, current_value: {}'.format(i, current_state, current_value))
i += 1
break
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
