def __init__(self): self.mazeDFS = Maze() self.free = False self.goal = [] self.mazeDFS.findEnd(self.goal) self.start = [] self.mazeDFS.findStart(self.start)
def __init__(self): self.mazeM = Maze() self.goal = [] self.mazeM.findEnd(self.goal) self.start = [] self.mazeM.findStart(self.start)
class GreedySolver: # initial value that should always be greater than heuristic LARGE_NUM = 65234 # Constructor of GreedySolver # duplicates the maze and initializes goal and start def __init__(self): self.mazeM = Maze() self.goal = [] self.mazeM.findEnd(self.goal) self.start = [] self.mazeM.findStart(self.start) # Finds the heuristic value from the current position to the goal # Returns the heuristic def heuristic(self, posX, posY): return abs(posX - self.goal[0]) + abs(posY - self.goal[1]) # The main body (search method) for the GreedySolver # Uses an entered maze and start position # Searches through the maze by following the best heuristic path found # It also prints out the location on the map as it traverses toward the exit. def search(self, maze, start, goal): opened = [] closed = [] opened.append(MazeInfo(start[0], start[1], 0, self.heuristic(start[0], start[1]))) while len(opened): h = self.LARGE_NUM tocheck = -1 for x in range(len(opened)): if opened[x].h <= h: h = opened[x].h tocheck = x checking = opened[tocheck] closed.append(checking) if maze.checkExit(checking.x, checking.y): print(self.goal) print("Exit Found!\n") return # go left tempX = checking.x tempY = checking.y maze.maze[checking.y][checking.x] = 'X' tempX = tempX - 1 tempChecking = MazeInfo(tempX, tempY, checking.g + 1, self.heuristic(tempX, tempY)) if maze.moveable(tempX, tempY) and tempChecking not in closed: if tempChecking in opened: for j in range(len(opened)): if opened[j] == tempChecking: opened[j] = tempChecking else: opened.append(tempChecking) # go down tempX = checking.x tempY = checking.y tempY = tempY + 1 tempChecking = MazeInfo(tempX, tempY, checking.g + 1, self.heuristic(tempX, tempY)) if maze.moveable(tempX, tempY) and tempChecking not in closed: if tempChecking in opened: for j in range(len(opened)): if opened[j] == tempChecking: opened[j] = tempChecking else: opened.append(tempChecking) # go right tempX = checking.x tempY = checking.y tempX = tempX + 1 tempChecking = MazeInfo(tempX, tempY, checking.g + 1, self.heuristic(tempX, tempY)) if maze.moveable(tempX, tempY) and tempChecking not in closed: if tempChecking in opened: for j in range(len(opened)): if opened[j] == tempChecking: opened[j] = tempChecking else: opened.append(tempChecking) # go up tempX = checking.x tempY = checking.y tempY = tempY - 1 tempChecking = MazeInfo(tempX, tempY, checking.g + 1, self.heuristic(tempX, tempY)) if maze.moveable(tempX, tempY) and tempChecking not in closed: if tempChecking in opened: for j in range(len(opened)): if opened[j] == tempChecking: opened[j] = tempChecking else: opened.append(tempChecking) opened.remove(checking) print("[" + str(checking.x) + ", " + str(checking.y) + "]")
class DfsSolver: # Constructor # initializes a maze to use # A variable to know if it is free # and where the goal and start are def __init__(self): self.mazeDFS = Maze() self.free = False self.goal = [] self.mazeDFS.findEnd(self.goal) self.start = [] self.mazeDFS.findStart(self.start) # The main body for the DFS search # Uses entered coordinates being the start and solves for the end # uses recursion def solve(self, y, x): if self.mazeDFS.maze[y][x] != Maze.EXIT and self.mazeDFS.maze[y][ x] != Maze.START: self.mazeDFS.maze[y][x] = Maze.VISITED if self.mazeDFS.maze[y][x] == Maze.EXIT: self.free = True return if self.mazeDFS.maze[y][x - 1] == Maze.OPEN or self.mazeDFS.maze[y][ x - 1] == Maze.EXIT: # Logic for moving left if self.free: return if self.mazeDFS.maze[y][x] != Maze.START: self.mazeDFS.maze[y][x] = Maze.VISITED print("[" + str(x) + ", " + str(y) + "]") self.solve(y, x - 1) if self.mazeDFS.maze[y + 1][x] == Maze.OPEN or self.mazeDFS.maze[ y + 1][x] == Maze.EXIT: # Logic for moving down if self.free: return if self.mazeDFS.maze[y][x] != Maze.START: self.mazeDFS.maze[y][x] = Maze.VISITED print("[" + str(x) + ", " + str(y) + "]") self.solve(y + 1, x) if self.mazeDFS.maze[y - 1][x] == Maze.OPEN or self.mazeDFS.maze[ y - 1][x] == Maze.EXIT: # Logic for moving up if self.free: return if self.mazeDFS.maze[y][x] != Maze.START: self.mazeDFS.maze[y][x] = Maze.VISITED print("[" + str(x) + ", " + str(y) + "]") self.solve(y - 1, x) if self.mazeDFS.maze[y][x + 1] == Maze.OPEN or self.mazeDFS.maze[y][ x + 1] == Maze.EXIT: # Logic for moving right if self.free: return if self.mazeDFS.maze[y][x] != Maze.START: self.mazeDFS.maze[y][x] = Maze.VISITED print("[" + str(x) + ", " + str(y) + "]") self.solve(y, x + 1) # prints the maze that the dfs solver initialized def printDfsMaze(self): for i in range(len(self.mazeDFS.maze)): for j in range(len(self.mazeDFS.maze[i])): print(self.mazeDFS.maze[i][j], end='') print()
# Project 1 # Contributors: Logan Garza, Timothy Kempster, Aidan Zastrow # The problem we addressed in our project was the solving a linear maze in an [x,y] plane using python. # When traversing in a maze, there are four basic actions you can proceed with at any given point, # you can check for a wall and attempt to move: right, up, down, or left. # Our project aimed to automate this decision making based on different algorithms and heuristics. # We implemented a depth first search algorithm, # A star heuristic, and greedy best-first heuristic to address different ways to traverse and # solve a maze given that it has one specific entry point and one specific exit/goal point. from bin.Maze import Maze from bin.AStarSolver import AStarSolver from bin.DfsSolver import DfsSolver from bin.GreedySolver import GreedySolver test = Maze() test2 = AStarSolver() test3 = DfsSolver() test4 = GreedySolver() print("\n") # Main Body. Runs through all the algorithms. print("Start of DFS Solver") print("Original Maze: 1 = wall, 0 = free, 2 = start, 3 = exit, X = visited") test3.printDfsMaze() print() print("Printing locations visited by the solver in form of [x, y]") test3.solve(test3.start[1], test3.start[0]) print("Finished Maze:") test3.printDfsMaze() print("End of DFS Solver\n") print("Start of Greedy Solver")
class AStarSolver: # used to determine the object in opened with the lowest path cost function LARGE_NUM = 65234 # Constructor of A* # initializes goal and start and a maze to use def __init__(self): self.mazeM = Maze() self.goal = [] self.mazeM.findEnd(self.goal) self.start = [] self.mazeM.findStart(self.start) # Finds the heuristic value from the current position to the goal # Returns the heuristic def heuristic(self, posX, posY): return abs(posX - self.goal[0]) + abs(posY - self.goal[1]) # The main body for the A* search # Uses an entered maze and start position # Searches through the maze using A* until it finds the goal # Will print out the path taken as it goes def solve(self, maze, start): opened = [] closed = [] opened.append( MazeInfo(start[0], start[1], 0, self.heuristic(start[0], start[1]))) while len(opened): f = self.LARGE_NUM tocheck = -1 for x in range(len(opened)): if (opened[x].f < f): f = opened[x].f tocheck = x checking = opened[tocheck] print("[", checking.x, ",", checking.y, "]") closed.append(checking) ## go left tempX = checking.x tempY = checking.y if maze.checkExit(checking.x, checking.y): return maze.visited(checking.y, checking.x) tempX = tempX - 1 tempChecking = MazeInfo(tempX, tempY, checking.g + 1, self.heuristic(tempX, tempY)) if maze.moveable(tempX, tempY) and tempChecking not in closed: if tempChecking in opened: if tempChecking.g >= checking.g + 1: for j in range(len(opened)): if opened[j] == tempChecking: opened[j] = tempChecking else: opened.append(tempChecking) ## go down tempX = checking.x tempY = checking.y tempY = tempY + 1 tempChecking = MazeInfo(tempX, tempY, checking.g + 1, self.heuristic(tempX, tempY)) if maze.moveable(tempX, tempY) and tempChecking not in closed: if tempChecking in opened: if tempChecking.g >= checking.g + 1: for j in range(len(opened)): if opened[j] == tempChecking: opened[j] = tempChecking else: opened.append(tempChecking) ##right tempX = checking.x tempY = checking.y tempX = tempX + 1 tempChecking = MazeInfo(tempX, tempY, checking.g + 1, self.heuristic(tempX, tempY)) if maze.moveable(tempX, tempY) and tempChecking not in closed: if tempChecking in opened: if tempChecking.g >= checking.g + 1: for j in range(len(opened)): if opened[j] == tempChecking: opened[j] = tempChecking else: opened.append(tempChecking) ## up tempX = checking.x tempY = checking.y tempY = tempY - 1 tempChecking = MazeInfo(tempX, tempY, checking.g + 1, self.heuristic(tempX, tempY)) if maze.moveable(tempX, tempY) and tempChecking not in closed: if tempChecking in opened: if tempChecking.g >= checking.g + 1: for j in range(len(opened)): if opened[j] == tempChecking: opened[j] = tempChecking else: opened.append(tempChecking) opened.remove(checking)