From 9f43b559a4629fb5c125cd857928a1101d576f9c Mon Sep 17 00:00:00 2001 From: Michael Mutote <130656746+mr1Michael@users.noreply.github.com> Date: Sat, 4 Nov 2023 13:35:09 +0100 Subject: [PATCH] 22202956 Create A* search algorithm and added to search algorithms. Heuristics file with maze heuristics. Moved puzzle goal to the top Created test case for mazes --- Search_Algorithms/A_Star.py | 0 Search_Algorithms/Heuristics.py | 24 ++++++++++++++++++++++++ Search_Algorithms/Search_Algorithms.py | 17 +++++++++++++++++ Search_Algorithms/is_goal.py | 14 +++++++------- Search_Algorithms/solution testing.py | 25 +++++++++++++++++-------- 5 files changed, 65 insertions(+), 15 deletions(-) delete mode 100644 Search_Algorithms/A_Star.py create mode 100644 Search_Algorithms/Heuristics.py diff --git a/Search_Algorithms/A_Star.py b/Search_Algorithms/A_Star.py deleted file mode 100644 index e69de29..0000000 diff --git a/Search_Algorithms/Heuristics.py b/Search_Algorithms/Heuristics.py new file mode 100644 index 0000000..ac0bef2 --- /dev/null +++ b/Search_Algorithms/Heuristics.py @@ -0,0 +1,24 @@ +import is_goal + + + + + + +def maze_opt(path): + """maze search heuristic going to have to use the euclidian distance, so it works for any maze""" + state = path[-1] + return (is_goal.MAZE_GOAL[0] - state[0])**2 + (is_goal.MAZE_GOAL[1] - state[1])**2 + + +def puzzle_opt(paths): + pass + + +def sudoku_opt(paths): + pass + + +def queens_opt(paths): + pass + diff --git a/Search_Algorithms/Search_Algorithms.py b/Search_Algorithms/Search_Algorithms.py index bfdeb01..6558e36 100644 --- a/Search_Algorithms/Search_Algorithms.py +++ b/Search_Algorithms/Search_Algorithms.py @@ -38,3 +38,20 @@ def DepthFirstSearch(state, successor, isgoal): toDo.append(path2) return "Error Path not found" + +def A_StarSearch(state, successor, isgoal, h): + """accepts start state, Please do not change this function""" + toDo = [[state]] + explored = {state} + while toDo: + toDo.sort(key=h) + path = toDo.pop(0) + current = path[-1] + if isgoal(current): + return path + for next_state in successor(current): + if next_state not in explored: + explored.add(next_state) + path2 = path + [next_state] + toDo.append(path2) + return "Error Path not found" diff --git a/Search_Algorithms/is_goal.py b/Search_Algorithms/is_goal.py index 305ea05..a087bbe 100644 --- a/Search_Algorithms/is_goal.py +++ b/Search_Algorithms/is_goal.py @@ -1,6 +1,13 @@ MAZE_GOAL = (0, 4) +PUZZLE_GOAL = ( + (0, 1, 2), + (3, 4, 5), + (6, 7, 8) +) + + def is_goal_maze(state): return MAZE_GOAL == state @@ -18,12 +25,5 @@ def is_goal_queens(state): return sum(map(sum, state)) == len(state) -PUZZLE_GOAL = ( - (0, 1, 2), - (3, 4, 5), - (6, 7, 8) -) - - def is_goal_puzzle(state): return PUZZLE_GOAL == state diff --git a/Search_Algorithms/solution testing.py b/Search_Algorithms/solution testing.py index 30d97a3..d34c135 100644 --- a/Search_Algorithms/solution testing.py +++ b/Search_Algorithms/solution testing.py @@ -1,9 +1,10 @@ import Search_Algorithms import Sucessors import is_goal +import Heuristics -# Maze Problem, note, the maze if fixed in the allowed state. +# Maze Problem, note, the maze is fixed in the allowed state. # print(Search_Algorithms.BreadthFirstSearch((4, 0), Sucessors.maze_successor, is_goal.is_goal_maze)) # Sudoku @@ -49,7 +50,6 @@ board = ((False, False, False, False, False, False, False, False, False), # Sliding Puzzle - initial_puzzle = ( (7, 2, 4), (5, 0, 6), @@ -64,11 +64,20 @@ initial_puzzle = ( # else: # print("No solution found") +# +# sln = (Search_Algorithms.DepthFirstSearch(initial_puzzle, Sucessors.puzzle_successor, is_goal.is_goal_puzzle)[-1]) +# +# if sln: +# for rows in sln: +# print(rows) +# else: +# print("No solution found") + +print(Search_Algorithms.A_StarSearch((4, 0), Sucessors.maze_successor, is_goal.is_goal_maze, Heuristics.maze_opt)) + + + + + -sln = (Search_Algorithms.DepthFirstSearch(initial_puzzle, Sucessors.puzzle_successor, is_goal.is_goal_puzzle)[-1]) -if sln: - for rows in sln: - print(rows) -else: - print("No solution found") -- GitLab