【问题标题】:I have a problem understanding the A* Algorithm (Python)我在理解 A* 算法 (Python) 时遇到问题
【发布时间】:2020-11-25 13:22:05
【问题描述】:

我正在尝试研究 A* 算法,但我很难理解特定部分。所以带有示例的 A* 算法 Python 代码是这样的:

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, 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, 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, 1, 0, 0, 0, 0, 0]]

    start = (4, 3)
    end = (4, 5)

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


if __name__ == '__main__':
    main()

for index, item in enumerate(open_list):
    if item.f < current_node.f:
        current_node = item
        current_index = index

我不明白如何将 current_node 定义为我上面给出的迷宫中的项目。在我上面给出的示例中,start = (4,3) 和 end = (4,5),给出唯一可能的最短距离如下所示:

maze = [[0, 0, 0, 0, *, 0, 0, 0, 0, 0],
        [0, 0, 0, *, 1, *, 0, 0, 0, 0],
        [0, 0, 0, *, 1, *, 0, 0, 0, 0],
        [0, 0, 0, *, 1, *, 0, 0, 0, 0],
        [0, 0, 0, s, 1, e, 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, 1, 0, 0, 0, 0, 0]]

s 是 start_node,e 是 end_node。

但是,在 A* 算法的代码中,只有当 item.f 小于 current_node.f 时,current_node 才会变为 item。在我在这里给出的示例中,我看不到第一个 * 的 f 值小于 start_node 的 f 值 - 我的意思是,在代码中,我们已经描述了 @987654330 @ = 0 不是吗?我们将第一个current_node 定义为start_node...所以open_list 中没有item 的item.f 值小于零..

这怎么可能??还是我在这里遗漏了什么??

【问题讨论】:

    标签: python algorithm a-star


    【解决方案1】:

    我认为线索是您还必须考虑此 for 循环上方的两行:

    # 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
    

    会发生什么:

    • 在 while 循环的第一次迭代中:
      • open_list 中只有一项,即确实 f=0 的 start_node
      • 所以经过上面的代码块,这个起始节点就变成了current_node
      • 在上述循环之后start_node从open_list中删除open_list.pop(current_index)
      • open_list 然后由有效的相邻位置填充(通过步行其子级)
    • 在 while 循环的 第二次迭代 中:
      • 上述代码块在 open_list 中查找 f 值最低的项目
      • 由于第一行 current_node = open_list[0],您将确定新的 current_node 始终是 open_list 中的一个
      • 由于 start_node 已经从 open_list 中删除,所以这里肯定会被替换

    【讨论】:

      猜你喜欢
      • 2011-03-17
      • 1970-01-01
      • 2019-11-16
      • 2021-04-07
      • 1970-01-01
      • 2012-12-20
      • 2011-03-16
      • 1970-01-01
      • 2023-03-20
      相关资源
      最近更新 更多