示例#1
0
def aStar(mazeObject, start, end, verbose=False):
    if verbose:
        pen = Pen()
        pen.color("yellow")
    q = PriorityQueue()
    g_cost = 0
    h_cost = manhattan_distance(start, end)
    startNode = Node(start, g_cost, h_cost)
    f_cost = startNode.g + startNode.h
    q.put((f_cost, startNode))

    closed = set()
    while not q.empty():
        checkNode = q.get()
        current_F = checkNode[0]
        node = checkNode[1]

        # g_cost = 1
        # h_cost = how far it is from the end node
        # f_cost = g_cost + h_cost
        if node.point == end:
            if verbose:
                pen2 = Pen()
                pen2.color("red")
                retracePath(node.parent, pen2)
            print("Retraced Path")
            return
        closed.add(node.point)

        if verbose and node.point != start:
            pen.draw(node.point[0], node.point[1], 300)
        for neighbor in mazeObject.getNeighbors(node.point[0], node.point[1]):
            if neighbor in closed:
                continue
            g_cost = node.g + 1
            h_cost = manhattan_distance(neighbor, end)
            f_cost = g_cost + h_cost
            # print(f"Test: {f_cost}")
            childNode = Node(neighbor, g_cost, h_cost, node)
            if f_cost < current_F or neighbor not in [
                    element[1].point for element in q.queue
            ]:
                if neighbor not in [element[1].point for element in q.queue]:
                    q.put((f_cost, childNode))

    return 0
示例#2
0
def dijkstra(mazeObject, destination_i, destination_j, verbose=False):
    if verbose:
        pen = Pen()
        pen.color("yellow")
    maze = np.asarray(mazeObject.maze)
    dmaze = [[None for _ in range(len(maze))] for __ in range(len(maze))]
    unvisited = set()
    # print(dmaze)
    for i in range(len(maze)):
        for j in range(len(maze)):
            if maze[i, j] == 0:
                if i == 0 and j == 0:
                    dmaze[i][j] = Node(i, j, 0)
                    unvisited.add((i, j))
                else:
                    cell = Node(i, j, math.inf)
                    dmaze[i][j] = cell
                    unvisited.add((i, j))
    current_spot = (0, 0)
    while not dmaze[destination_i][destination_j].visited:
        x = current_spot[0]
        y = current_spot[1]
        for neighbor in mazeObject.getNeighbors(x, y):
            cell = dmaze[neighbor[0]][neighbor[1]]
            if not cell.visited:
                if verbose and not (x == 0 and y == 0) and not (
                        x == destination_i and y == destination_j):
                    pen.draw(x, y, 300)
                distance = 1 + dmaze[x][y].distance
                if distance < cell.distance:
                    cell.distance = distance
        dmaze[x][y].visited = True
        unvisited.remove((x, y))
        min_distance = math.inf
        for nodes in list(unvisited):
            x = nodes[0]
            y = nodes[1]
            # print(f"Get Next Node{x}:{y}")
            if dmaze[x][y].distance < min_distance:
                current_spot = nodes
                min_distance = dmaze[x][y].distance
        # print(f"Next Neighbor{current_spot[0]}:{current_spot[1]}")
    # print(f"Done: {dmaze[destination_i][destination_j].distance}")
    return dmaze[destination_i][destination_j].distance, dmaze
示例#3
0

if __name__ == "__main__":
    maze = Maze(5, False)
    test_maze = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 1, 1, 1, 0],
                 [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

    maze.maze = np.asarray(test_maze)

    maze.printMaze()

    blackPen = Pen()
    greenPen = Pen()
    redPen = Pen()

    blackPen.color("black")
    greenPen.color("green")
    redPen.color("red")
    wn = turtle.Screen()
    wn.bgcolor("gray")
    wn.setup(800, 800)

    mazeArray = maze.drawMaze()
    drawMaze(mazeArray, blackPen, greenPen, maze.exit)
    print(maze.exit[0])
    distance, dmaze = d.dijkstra(maze,
                                 maze.exit[0],
                                 maze.exit[1],
                                 verbose=True)
    solveMaze(maze, dmaze, redPen, maze.exit[0], maze.exit[1], distance)
    while True: