为什么A *算法不会卡在两个节点之间

问题描述 投票:2回答:1

在路径查找时我是初学者,虽然我理解A *的基本思想,但我仍然不明白为什么,当回溯时,实现不会卡在两个最新节点之间的循环中参观。

为了更清楚,我一直在查看来自here的代码(我将要复制并粘贴,以防链接死亡):

class Node():
    """A node class for A* Pathfinding"""

    def __init__(self, parent=None, position=None):
        self.parent = parent
        self.position = position

        self.g = 0
        self.h = 0
        self.f = 0

    def __eq__(self, other):
        return self.position == other.position


def astar(maze, start, end):
    """Returns a list of tuples as a path from the given start to the given end in the given maze"""

    # Create start and end node
    start_node = Node(None, start)
    start_node.g = start_node.h = start_node.f = 0
    end_node = Node(None, end)
    end_node.g = end_node.h = end_node.f = 0

    # Initialize both open and closed list
    open_list = []
    closed_list = []

    # Add the start node
    open_list.append(start_node)

    # Loop until you find the end
    while len(open_list) > 0:

        # Get the current node
        current_node = open_list[0]
        current_index = 0
        for index, item in enumerate(open_list):
            if item.f < current_node.f:
                current_node = item
                current_index = index

        # Pop current off open list, add to closed list
        open_list.pop(current_index)
        closed_list.append(current_node)

        # Found the goal
        if current_node == end_node:
            path = []
            current = current_node
            while current is not None:
                path.append(current.position)
                current = current.parent
            return path[::-1] # Return reversed path

        # Generate children
        children = []
        for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]: # Adjacent squares

            # Get node position
            node_position = (current_node.position[0] + new_position[0], current_node.position[1] + new_position[1])

            # Make sure within range
            if node_position[0] > (len(maze) - 1) or node_position[0] < 0 or node_position[1] > (len(maze[len(maze)-1]) -1) or node_position[1] < 0:
                continue

            # Make sure walkable terrain
            if maze[node_position[0]][node_position[1]] != 0:
                continue

            # Create new node
            new_node = Node(current_node, node_position)

            # Append
            children.append(new_node)

        # Loop through children
        for child in children:

            # Child is on the closed list
            for closed_child in closed_list:
                if child == closed_child:
                    continue

            # Create the f, g, and h values
            child.g = current_node.g + 1
            child.h = ((child.position[0] - end_node.position[0]) ** 2) + ((child.position[1] - end_node.position[1]) ** 2)
            child.f = child.g + child.h

            # Child is already in the open list
            for open_node in open_list:
                if child == open_node and child.g > open_node.g:
                    continue

            # Add the child to the open list
            open_list.append(child)


def main():

    maze = [[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

    start = (0, 0)
    end = (7, 6)

    path = astar(maze, start, end)
    print(path)


if __name__ == '__main__':
    main()

在这种特殊情况下,这看起来很容易,但如果“迷宫”是这样的:

maze = 
   [[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
    [0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
    [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

当被检查的节点是(5, 5)时,有一个死胡同,所以算法应该“回溯”到节点(3, 5)并从那里下来。

我的问题是我真的不明白这是怎么回事。返回一个节点并检查邻居意味着再次检查所有内容,它将返回(5, 5)

这不会发生,因为实现工作得很好,但我似乎真的无法掌握如何。任何提示?

python algorithm a-star
1个回答
2
投票

关键是当运行Dijkstra或A *时,每个节点最多访问一次。 这是因为每次访问节点时(在我们从队列中弹出它之后),我们都会“标记”此节点已被访问。在您给出的实现中,通过将节点添加到closed_list来进行标记:

closed_list.append(current_node)

现在节点是closed_list的事实将允许在将节点推送到队列之前检查节点是否已经被访问过。这在代码中进一步完成(笨拙地):

            # Child is on the closed list
            for closed_child in closed_list:
                if child == closed_child:
                    continue

这种机制对于确保算法终止(Dijkstra或A *)以及其复杂性在O(n.log n)中是必不可少的。 但是在大多数实现中,你不会看到closed_list,而是一个与每个节点相关联的visited布尔值,或者一种颜色(如果没有访问则为white,如果已访问则为green)。它们在终止保证方面都是等价的(不一定是在性能方面,因为列表查找可能会花费O(n))。

© www.soinside.com 2019 - 2024. All rights reserved.