Skip to content
Snippets Groups Projects
Commit 9f43b559 authored by Michael Mutote's avatar Michael Mutote
Browse files

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
parent 31689098
No related branches found
No related tags found
No related merge requests found
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
......@@ -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"
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
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")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment