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)
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) #Testing A* distance: Callable[[MazeLocation], float] = euclidean_distance(m.goal) solution_astar_euclidean: Optional[Node[MazeLocation]] = astar( m.start, m.goal_test, m.successors, distance) if solution_astar_euclidean is None: print("No solution found using A*") else:
ydist = ml.row - goal.row return sqrt(xdist**2 + ydist**2) return distance def manhattan_distance(goal: MazeLocation): '''曼哈顿距离,是只能横走竖走的距离''' def distance(ml: MazeLocation): xdist = abs(ml.column - goal.column) ydist = abs(ml.row - goal.row) return xdist + ydist return distance if __name__ == '__main__': m = Maze() distance = manhattan_distance(m.goal) solution3 = astar(m.start, m.goal_test, m.successors, distance) solution2 = bfs(m.start, m.goal_test, m.successors) if solution3 is None: print('No solution by A*') else: print(solution3[1]) if solution2 is None: print('No solution by BFS') else: print(solution2[1])
# 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) # Test BFS 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) # Test A* 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("No solution found using A*!") else:
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 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("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 search on city_graph import os import sys sys.path.append(f"{os.getcwd()}/search") from generic_search import bfs, Node, node_to_path bfs_result: Optional[Node[V]] = bfs("Seattle", 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)
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)
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]): """ Clears the maze by setting all the cells to be empty spaces """ print("Clearing the path from the start to the goal...") 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) solution1: Optional[Node[MazeLocation]] = bfs( initial=maze.start, goal_test=maze.goal_test, successors=maze.successors ) if solution1: path1: List[MazeLocation] = node_to_path(node=solution1) maze.mark(path1) print(maze) maze.clear(path=path1) print(maze) else: print("No solution found using depth-first-search")
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)
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}") if path: m.mark(path) print(m) else: print(m) print("Unsolvable") m.clear() print("A* search solution") start = time.time() node = astar( initial=m.start,
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 = bfs('Boston', lambda x: x == 'Miami', city_graph.neighbors_for_vertex) if bfs_result is None: print('No solution found using BFS!') else: path = node_to_path(bfs_result) print('Path from Boston to Miami:') print(path)
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) start = "Los Angeles" end = "Boston" node = bfs( initial=start, goal_test=lambda x: x == end, successors=city_graph.neighbors_for_vertex, ) if node is None: print(f"No path from {start} to {end}") path = node_to_path(node) print(f"Path from {start} to {end}") print(path)