diff --git a/Search_Algorithms/A_Star.py b/Search_Algorithms/A_Star.py deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/Search_Algorithms/Heuristics.py b/Search_Algorithms/Heuristics.py new file mode 100644 index 0000000000000000000000000000000000000000..ac0bef28d7ea16a43364a17d8042a25fe4511453 --- /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 bfdeb01001944fcea1979a20289d692356c93be0..6558e36bc21821219080d2e28ef2e19f9af02bee 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 305ea05c30e7585cfedbf9d4c9d245409ff07bb4..a087bbe034b27d072a1986f4cdb1aec53a5ae239 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 30d97a3b5873004498447aa569d6a86e080e50da..d34c13505f48ef42031e591b23861a05497c1c51 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")