def a_star(initial_state, goal_state): OPEN = [(calculate_heuristic(initial_state, goal_state), 0, initial_state, 0)] CLOSED = set() while OPEN: f, g, current_state,iteration = min(OPEN) OPEN.remove((f, g, current_state, iteration)) CLOSED.add(tuple(map(tuple, current_state))) print_state(current_state) print() if current_state == goal_state: print(f"{hooray}Solution found!") print(f"Iteration:- {iteration}") return successors = [ (move_up(current_state), "UP"), (move_down(current_state), "DOWN"), (move_left(current_state), "LEFT"), (move_right(current_state), "RIGHT") ] successors = [(s, move) for s, move in successors if s is not None and tuple( map(tuple, s)) not in CLOSED] for successor, move in successors: h = calculate_heuristic(successor, goal_state) g_successor = g + 1 f_successor = g_successor + h i = iteration + 1 if (h, g_successor, successor, i) not in OPEN: OPEN.append((f_successor, g_successor, successor, i)) print(f"{hooray}No solution found.") initial_state = [] print("Enter the initial state (3x3 matrix):") for _ in range(3): row = list(map(int, input().split())) initial_state.append(row) goal_state = [] print("Enter the goal state (3x3 matrix):") for _ in range(3): row = list(map(int, input().split())) goal_state.append(row) a_star(initial_state, goal_state) ________________________________________________________________________________ PROGRAM 2 MAX, MIN = 1000, -1000 def minimax(depth, nodeIndex, maximizingPlayer, values, alpha, beta,d): if depth == d: return values[nodeIndex] if maximizingPlayer: best = MIN for i in range(0, 2): val = minimax(depth + 1, nodeIndex * 2 + i, False, values, alpha, beta,d) best = max(best, val) alpha = max(alpha, best) if beta <= alpha: break return best else: best = MAX for i in range(0, 2): val = minimax(depth + 1, nodeIndex * 2 + i, True, values, alpha, beta,d) best = min(best, val) beta = min(beta, best) if beta <= alpha: break return best if __name__ == "__main__": values = [int(x) for x in input("Enter values separated by spaces: ").split()] depth = int(input("Enter the depth: ")) print("The optimal value is :", minimax(0, 0, True, values, MIN, MAX,depth)) ____________________________________________________________________________________________________ Program 4 import random import numpy as np def hill_climbing_search(func, x0, delta=0.01, tol=1e-6, max_iter=1000): x = x0 i = 0 while True: i += 1 x_up = x + delta x_down = x - delta f_x_up = func(x_up) f_x_down = func(x_down) if i > max_iter: print("Max iterations reached.") break if f_x_up < f_x_down: x = x_down else: x = x_up if abs(x - x0) < tol: break x0 = x return x def func(x): return -0.2*x**4 + 1.5*x**3 - 3*x**2 + 2*x + 10*np.sin(x) x0 = 78 x_max = hill_climbing_search(func, x0) print(f"The value of x that maximises the function is {x_max}.")