self._grid[ml.row][ml.column - 1] != Cell.BLOCKED: locations.append(MazeLocation(ml.row, ml.column - 1)) return locations def mark(self, path: List[MazeLocation]): for maze_location in path: self._grid[maze_location.row][maze_location.column] = Cell.PATH self._grid[self.start.row][self.start.column] = Cell.START self._grid[self.goal.row][self.goal.column] = Cell.GOAL def clear(self, path: List[MazeLocation]): for maze_location in path: self._grid[maze_location.row][maze_location.column] = Cell.EMPTY self._grid[self.start.row][self.start.column] = Cell.START self._grid[self.goal.row][self.goal.column] = Cell.GOAL if __name__ == "__main__": # Test DFS m: Maze = Maze() print(m) solution1: Optional[Node[MazeLocation]] = dfs(m.start, m.goal_test, m.successors) if solution1 is None: print("No solution found using depth-first search!") else: path1: List[MazeLocation] = node_to_path(solution1) m.mark(path1) print(m) m.clear(path1)
return [x for x in sucs if x.is_legal] def display_solution(path: List[MCState]): if len(path) == 0: # sanity check return old_state: MCState = path[0] print(old_state) for current_state in path[1:]: if current_state.boat: print( f"{old_state.em - current_state.em} missionaries and {old_state.ec - current_state.ec} cannibals moved from the east bank to the west bank.\n" ) else: print( f"{old_state.wm - current_state.wm} missionaries and {old_state.wc - current_state.wc} cannibals moved from the west bank to the east bank.\n" ) print(current_state) old_state = current_state if __name__ == "__main__": start: MCState = MCState(MAX_NUM, MAX_NUM, True) solution: Optional[Node[MCState]] = bfs(start, MCState.goal_test, MCState.successors) if solution is None: print("No solution found!") else: path: List[MCState] = node_to_path(solution) display_solution(path)
y = abs(current_location.row - goal.row) return (x + y) return distance if __name__ == "__main__": m: Maze = Maze() print(m) #Testing depth first search solution_dfs: Optional[Node[MazeLocation]] = dfs(m.start, m.goal_test, m.successors) if solution_dfs == None: print("No solution using dfs") else: path_dfs: List[MazeLocation] = node_to_path(solution_dfs) m.mark(path_dfs) print(m) m.clear(path_dfs) #Testing breadth first search solution_bfs: Optional[Node[MazeLocation]] = bfs(m.start, m.goal_test, m.successors) if solution_bfs == None: print("No solution using bfs") else: path_bfs: List[MazeLocation] = node_to_path(solution_bfs) m.mark(path_bfs) print(m) m.clear(path_bfs)
m: Maze = Maze() # print(m) # solution1 : Optional[Node[MazeLocation]] = dfs(m.start, m.goal_test, m.successors) # if solution1 is None: # print("Cannot find the way using DFS") # else: # path1: List[MazeLocation] = node_to_path(solution1) # m.mark(path1) # print(m) # m.clear(path1) # solution2 : Optional[Node[MazeLocation]] = bfs(m.start, m.goal_test, m.successors) # if solution2 is None: # print("Cannot find the way using BFS") # else: # path2:List[MazeLocation] = node_to_path(solution2) # m.mark(path2) # print(m) # m.clear(path2) distance: Callable[[MazeLocation], float] = manhattan_distance(m.goal) solution3: Optional[Node[MazeLocation]] = astar(m.start, m.goal_test, m.successors, distance) if solution3 is None: print("Cannot find the way using A*") else: path3: List[MazeLocation] = node_to_path(solution3) m.mark(path3) print(m)
xdist: int = abs(ml.column - goal.column) ydist: int = abs(ml.row - goal.row) return xdist + ydist return distance if __name__ == "__main__": m: Maze = Maze() print(m) solution1: Optional[Node[MazeLocation]] = dfs(m.start, m.goal_test, m.successors) if solution1 is None: print("No solution found using depth-first search!") else: path1: List[MazeLocation] = node_to_path(solution1) m.mark(path1) print(m) m.clear(path1) solution2: Optional[Node[MazeLocation]] = bfs(m.start, m.goal_test, m.successors) if solution2 is None: print("No solution found using breadth-first search!") else: path2: List[MazeLocation] = node_to_path(solution2) m.mark(path2) print(m) m.clear(path2) distance: Callable[[MazeLocation], float] = manhattan_distance(m.goal) solution3: Optional[Node[MazeLocation]] = astar(m.start, m.goal_test, m.successors, distance)
def distance(ml: MazeLocation) -> float: xdist = abs(ml.column - goal.column) ydist = abs(ml.row - goal.row) return (xdist + ydist) return distance if __name__ == "__main__": m = Maze() print(m) solution1 = dfs(m.start, m.goal_test, m.successors) if solution1 is None: print("No solutions") else: path1 = node_to_path(solution1) m.mark(path1) print(m) m.clear(path1) solution2 = bfs(m.start, m.goal_test, m.successors) if solution2 is None: print("No solutions") else: path2 = node_to_path(solution2) m.mark(path2) print(m) m.clear(path2) distance = manhattan_distance(m.goal) solution3 = astar(m.start, m.goal_test, m.successors, distance)
def display_solution(path): if len(path) == 0: return None old_state = path[0] print(old_state) for state in path[1:]: if state.boat: # boat on the west bank print( "{} 👼 and {} 😈 moved from east to west bank.\n".format( old_state.em - state.em, old_state.ec - state.ec ) ) else: # boat on the east bank print( "{} 👼 and {} 😈 moved from west to east bank.\n".format( old_state.wm - state.wm, old_state.wc - state.wc ) ) print(state) old_state = state if __name__ == "__main__": start = MCState(missionaries=3, cannibals=3, boat=True) solution = bfs(start, MCState.goal_test, MCState.successors) if not solution: print("No solution found!") exit(1) else: path = node_to_path(solution)
city_graph.add_edge_by_vertices("Phoenix", "Houston") city_graph.add_edge_by_vertices("Dallas", "Chicago") city_graph.add_edge_by_vertices("Dallas", "Atlanta") city_graph.add_edge_by_vertices("Dallas", "Houston") city_graph.add_edge_by_vertices("Houston", "Atlanta") city_graph.add_edge_by_vertices("Houston", "Miami") city_graph.add_edge_by_vertices("Atlanta", "Chicago") city_graph.add_edge_by_vertices("Atlanta", "Washington") city_graph.add_edge_by_vertices("Atlanta", "Miami") city_graph.add_edge_by_vertices("Miami", "Washington") city_graph.add_edge_by_vertices("Chicago", "Detroit") city_graph.add_edge_by_vertices("Detroit", "Boston") city_graph.add_edge_by_vertices("Detroit", "Washington") city_graph.add_edge_by_vertices("Detroit", "New York") city_graph.add_edge_by_vertices("Boston", "New York") city_graph.add_edge_by_vertices("New York", "Philadelphia") city_graph.add_edge_by_vertices("Philadelphia", "Washington") print(city_graph) # Reuse BFS from Chapter 2 on city_graph from generic_search import bfs, node_to_path bfs_result = bfs("Boston", lambda x: x == "Miami", city_graph.neighbors_for_vertex) if bfs_result is None: print("No solution found using breadth-first search!") else: path = node_to_path(bfs_result) print("Path from Boston to Miami:") print(path)
def clear(self, path: List[MazeLocation]): for maze_location in path: self._grid[maze_location.row][maze_location.column] = Cell.EMPTY self._grid[self.start.row][self.start.column] = Cell.START self._grid[self.goal.row][self.goal.column] = Cell.GOAL if __name__ == "__main__": maze: Maze = Maze() print(maze) print("---------------------") # solution1: Optional[Node[MazeLocation]] = dfs(maze.start, maze.goal_test, maze.successors) # # if solution1 is None: # print("No solution found using depth-first search!") # else: # path1: List[MazeLocation] = node_to_path(solution1) # maze.mark(path1) # print(maze) # maze.clear(path1) # Test BFS solution2: Optional[Node[MazeLocation]] = bfs(maze.start, maze.goal_test, maze.successors) if solution2 is None: print("No solution found using breadth-first search!") else: path2: List[MazeLocation] = node_to_path(solution2) maze.mark(path2) print(maze) maze.clear(path2)
def distance(ml: MazeLocation) -> float: xdist: int = abs(ml.column-goal.column) ydist: int = abs(ml.row-goal.row) return xdist+ydist return distance if __name__ == "__main__": maze: Maze = Maze() print(maze) solution1: typing.Optional[Node[MazeLocation]] = dfs( maze.start, maze.goal_test, maze.successors) if solution1 is None: print("No solution found") else: path1: typing.List[MazeLocation] = node_to_path(solution1) maze.mark(path1) print(maze) maze.clear(path1) print('\n\n') solution2: typing.Optional[Node[MazeLocation]] = bfs( maze.start, maze.goal_test, maze.successors) if solution2 is None: print("No solution found") else: path2: typing.List[MazeLocation] = node_to_path(solution2) maze.mark(path2) print(maze) maze.clear(path2)
return x_dist + y_dist return distance if __name__ == '__main__': m = Maze() print(m) print() solution_dfs = dfs(m.start, m.goal_test, m.successors) if solution_dfs is None: print('No solution found using depth-first search!') else: path_dfs = node_to_path(solution_dfs) m.mark(path_dfs) print(m) print() m.clear(path_dfs) solution_bfs = bfs(m.start, m.goal_test, m.successors) if solution_bfs is None: print('No solution found using breadth-first search!') else: path_bfs = node_to_path(solution_bfs) m.mark(path_bfs) print(m) print() m.clear(path_bfs)
self._grid[self.start.row][self.start.column] = Cell.START self._grid[self.goal.row][self.goal.column] = Cell.GOAL def __str__(self) -> str: output: str = "" for row in self._grid: output += "".join([c.value for c in row]) + '\n' return output if __name__ == "__main__": coords = [(randint(0, 20), randint(0, 60)) for _ in range(2)] labyrinth: Labyrinth = Labyrinth(rows=20, columns=60, start=LabyrinthLocation(*coords[0]), goal=LabyrinthLocation(*coords[1]), rarity=0.2) # print(labyrinth) solution1: Optional[Node[LabyrinthLocation]] = dfs(labyrinth.start, labyrinth.goal_test, labyrinth.successors) if solution1 is None: print("\tNo solution found using depth-first search!\n".upper()) else: path1: List[LabyrinthLocation] = node_to_path(solution1) labyrinth.mark(path1) print(labyrinth) labyrinth.clear(path1) print("\tGreat, we have path to our goal!\n".upper())
city_graph.add_edge_by_vertices('Los Angeles', 'Riverside') city_graph.add_edge_by_vertices('Los Angeles', 'Phoenix') city_graph.add_edge_by_vertices('Riverside', 'Phoenix') city_graph.add_edge_by_vertices('Riverside', 'Chicago') city_graph.add_edge_by_vertices('Phoenix', 'Dallas') city_graph.add_edge_by_vertices('Phoenix', 'Houston') city_graph.add_edge_by_vertices('Dallas', 'Chicago') city_graph.add_edge_by_vertices('Dallas', 'Atlanta') city_graph.add_edge_by_vertices('Dallas', 'Houston') city_graph.add_edge_by_vertices('Houston', 'Atlanta') city_graph.add_edge_by_vertices('Houston', 'Miami') city_graph.add_edge_by_vertices('Atlanta', 'Chicago') city_graph.add_edge_by_vertices('Atlanta', 'Washington') city_graph.add_edge_by_vertices('Atlanta', 'Miami') city_graph.add_edge_by_vertices('Miami', 'Washington') city_graph.add_edge_by_vertices('Chicago', 'Detroit') city_graph.add_edge_by_vertices('Detroit', 'Boston') city_graph.add_edge_by_vertices('Detroit', 'Washington') city_graph.add_edge_by_vertices('Detroit', 'New York') city_graph.add_edge_by_vertices('Boston', 'New York') city_graph.add_edge_by_vertices('New York', 'Philadelphia') city_graph.add_edge_by_vertices('Philadelphia', 'Washington') bfs_result: Optional[Node[V]] = bfs('Boston', lambda x: x == 'Miami', city_graph.neighbors_for_vertex) if bfs_result is None: print('No solution found using beadth-first search!') else: path: List[V] = node_to_path(bfs_result) print('Path from Boston to Miami') print(path)
return [x for x in sucs if x.is_legal] def display_solution(path: typing.List[MCState]) -> None: if len(path) == 0: return old_state: MCState = path[0] print(old_state) for current_state in path[1:]: if current_state.boat: print( f"{old_state.em-current_state.em} missionaries and {old_state.ec-current_state.ec} cannibals moved from the east to the west bank.\n" ) else: print( f"{old_state.wm-current_state.wm} missionaries and {old_state.wc-current_state.wc} cannibals moved from the west to the east bank.\n" ) print(current_state) old_state = current_state if __name__ == "__main__": start: MCState = MCState(MAX_NUM, MAX_NUM, True) solution: typing.Optional[Node[MCState]] = bfs(start, MCState.goal_test, MCState.successors) if solution is None: print("No solution found") else: path: typing.List[MCState] = node_to_path(solution) display_solution(path)
def distance(ml: MazeLocation) -> float: xdist: int = abs(ml.col - goal.col) ydist: int = abs(ml.row - goal.row) return xdist + ydist return distance if __name__ == "__main__": m = Maze(num_rows=8, num_columns=30, sparseness=0.3) print("Depth first search solution") start = time.time() node = dfs(initial=m.start, goal_test=m.is_goal, successors=m.successors) end = time.time() path = node_to_path(node) print(f"Path length: {len(path)}, Time: {end - start}") if path: m.mark(path) print(m) else: print(m) print("Unsolvable") m.clear() print("Breadth first search solution") start = time.time() node = bfs(initial=m.start, goal_test=m.is_goal, successors=m.successors) end = time.time() path = node_to_path(node) print(f"Path length: {len(path)}, Time: {end - start}")
def distance(ml: MazeLocation): xdist = abs(goal.column - ml.column) ydist = abs(goal.row - ml.row) return (xdist + ydist) return distance if __name__ == '__main__': m = Maze() print(m) soluton1 = dfs(m.start, m.goal_test, m.successors) if soluton1 is None: print('No solution found using depth-first search!') else: path = node_to_path(soluton1) m.mark(path) print('DFS') print('-' * m._rows) print(m) m.clear(path) soluton2 = bfs(m.start, m.goal_test, m.successors) if soluton2 is None: print('No solution found using breadth-first search!') else: path = node_to_path(soluton2) m.mark(path) print('BFS') print('-' * m._rows) print(m) m.clear(path)