def manhattan_distance(goal: MazeLocation) -> Callable[[MazeLocation], float]:
    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__":
    # 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)
コード例 #2
0
                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)
コード例 #3
0
ファイル: maze.py プロジェクト: Karuka3/PythonComputerScience
    return distance


def manhattan_distance(goal: MazeLocation) -> Callable[[MazeLocation], float]:
    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)
コード例 #4
0
        return math.sqrt((xdist*xdist)+(ydist*ydist))
    return distance


def manhattan_distance(goal: MazeLocation) -> typing.Callable[[MazeLocation], float]:
    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)
コード例 #5
0
        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())
コード例 #6
0
def manhattan_distance(goal: MazeLocation) -> Callable[[MazeLocation], float]:
    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()
コード例 #7
0
        elif (ml.column - 1 >= 0) and (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]) -> None:
        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]) -> None:
        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]] = dfs(maze.start, maze.goal_test,
                                                  maze.successors)
    if solution1 is None:
        print('No Solutions found using depth-first search!')
    else:
        path1: List[Mazelocation] = node_to_path(solution1)
        maze.mark(path1)
        print(maze)
        maze.clear(path1)