From 938a7c913f10fb2f6970a41e8c35c9533091fd39 Mon Sep 17 00:00:00 2001 From: Prathamesh Rokade <144042848+Prathamesh-1011@users.noreply.github.com> Date: Sun, 18 May 2025 13:45:03 +0530 Subject: [PATCH 1/2] added 8 puzzle problem using A star algorithm --- other/8PuzzleProblem_Astar.py | 77 +++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 other/8PuzzleProblem_Astar.py diff --git a/other/8PuzzleProblem_Astar.py b/other/8PuzzleProblem_Astar.py new file mode 100644 index 000000000000..9453520e2dd8 --- /dev/null +++ b/other/8PuzzleProblem_Astar.py @@ -0,0 +1,77 @@ +import copy +import heapq + +class Puzzle: + + def __init__(self, initial_state): + self.goal_state = [[1, 2, 3], [4, 5, 6], [7, 8, 0]] + self.initial_state = initial_state + self.open_list = [] + self.closed_list = [] + + def get_heuristic(self, state): + heuristic = 0 + for i in range(3): + for j in range(3): + if state[i][j] != 0: + x_goal, y_goal = divmod(state[i][j] - 1, 3) + heuristic += abs(i - x_goal) + abs(j - y_goal) + return heuristic + + def get_blank_position(self, state): + for i in range(3): + for j in range(3): + if state[i][j] == 0: + return (i, j) + + def get_successors(self, state): + successors = [] + i_blank, j_blank = self.get_blank_position(state) + for x, y in [(0, 1), (1, 0), (0, -1), (-1, 0)]: + if 0 <= i_blank + x < 3 and 0 <= j_blank + y < 3: + new_state = copy.deepcopy(state) + new_state[i_blank][j_blank], new_state[i_blank + x][j_blank + y] = new_state[i_blank + x][j_blank + y], new_state[i_blank][j_blank] + successors.append(new_state) + return successors + + def solve(self): + start_node = (self.get_heuristic(self.initial_state), self.initial_state, 0, None) + heapq.heappush(self.open_list, start_node) + + while self.open_list: + current_node = heapq.heappop(self.open_list) + current_cost = current_node[2] + current_state = current_node[1] + + if current_state == self.goal_state: + path = [] + while current_node: + path.append(current_node[1]) + current_node = current_node[3] + return reversed(path) + + self.closed_list.append(current_state) + successors = self.get_successors(current_state) + for successor in successors: + if successor not in self.closed_list: + g_cost = current_cost + 1 + h_cost = self.get_heuristic(successor) + f_cost = g_cost + h_cost + new_node = (f_cost, successor, g_cost, current_node) + heapq.heappush(self.open_list, new_node) + + return None + +# Run the Solver +initial_state = [[1,2, 3], [4,5,6], [0,7,8]] +solver = Puzzle(initial_state) +solution = solver.solve() + +if solution: + for idx, state in enumerate(solution): + print(f"Move {idx + 1}:") + for row in state: + print(row) + print() +else: + print("No solution found.") \ No newline at end of file From 37ec8b76e5ce6a5360af54dd645802d0d187b511 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 18 May 2025 08:16:40 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/8PuzzleProblem_Astar.py | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/other/8PuzzleProblem_Astar.py b/other/8PuzzleProblem_Astar.py index 9453520e2dd8..6c5f5da9449d 100644 --- a/other/8PuzzleProblem_Astar.py +++ b/other/8PuzzleProblem_Astar.py @@ -1,14 +1,14 @@ import copy import heapq + class Puzzle: - def __init__(self, initial_state): self.goal_state = [[1, 2, 3], [4, 5, 6], [7, 8, 0]] self.initial_state = initial_state self.open_list = [] self.closed_list = [] - + def get_heuristic(self, state): heuristic = 0 for i in range(3): @@ -17,39 +17,47 @@ def get_heuristic(self, state): x_goal, y_goal = divmod(state[i][j] - 1, 3) heuristic += abs(i - x_goal) + abs(j - y_goal) return heuristic - + def get_blank_position(self, state): for i in range(3): for j in range(3): if state[i][j] == 0: return (i, j) - + def get_successors(self, state): successors = [] i_blank, j_blank = self.get_blank_position(state) for x, y in [(0, 1), (1, 0), (0, -1), (-1, 0)]: if 0 <= i_blank + x < 3 and 0 <= j_blank + y < 3: new_state = copy.deepcopy(state) - new_state[i_blank][j_blank], new_state[i_blank + x][j_blank + y] = new_state[i_blank + x][j_blank + y], new_state[i_blank][j_blank] + new_state[i_blank][j_blank], new_state[i_blank + x][j_blank + y] = ( + new_state[i_blank + x][j_blank + y], + new_state[i_blank][j_blank], + ) successors.append(new_state) return successors - + def solve(self): - start_node = (self.get_heuristic(self.initial_state), self.initial_state, 0, None) + start_node = ( + self.get_heuristic(self.initial_state), + self.initial_state, + 0, + None, + ) heapq.heappush(self.open_list, start_node) - + while self.open_list: current_node = heapq.heappop(self.open_list) current_cost = current_node[2] current_state = current_node[1] - + if current_state == self.goal_state: path = [] while current_node: path.append(current_node[1]) current_node = current_node[3] return reversed(path) - + self.closed_list.append(current_state) successors = self.get_successors(current_state) for successor in successors: @@ -59,11 +67,12 @@ def solve(self): f_cost = g_cost + h_cost new_node = (f_cost, successor, g_cost, current_node) heapq.heappush(self.open_list, new_node) - + return None + # Run the Solver -initial_state = [[1,2, 3], [4,5,6], [0,7,8]] +initial_state = [[1, 2, 3], [4, 5, 6], [0, 7, 8]] solver = Puzzle(initial_state) solution = solver.solve() @@ -74,4 +83,4 @@ def solve(self): print(row) print() else: - print("No solution found.") \ No newline at end of file + print("No solution found.")