为什么A*算法不会卡在两个节点之间
Why does A* algorithm not get stuck between two nodes
我是路径查找方面的初学者,虽然我理解 A* 的基本思想,但我仍然不明白为什么在回溯时,实现不会陷入循环在最近访问的两个节点之间。
为了更清楚,我一直在查看 here 中的代码(我将复制并粘贴它,以防 link 死掉):
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" 是这样的:
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)
时,有一个死胡同,所以算法应该"backtrack"到节点(3, 5)
,然后从那里往下走。
我的问题是我真的不明白这是怎么发生的。
返回一个节点并检查邻居意味着再次检查所有内容,它只会返回到 (5, 5)
.
这不会发生,因为实现工作得很好,但我似乎真的无法掌握如何实现。有什么提示吗?
重点是运行Dijkstra或A*时每个节点最多访问一次。
这是因为每次访问一个节点时(在我们将其从队列中弹出之后),我们 'mark' 这个节点已经被访问过。在您提供的实现中,标记是通过将节点添加到 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)
)。
我是路径查找方面的初学者,虽然我理解 A* 的基本思想,但我仍然不明白为什么在回溯时,实现不会陷入循环在最近访问的两个节点之间。
为了更清楚,我一直在查看 here 中的代码(我将复制并粘贴它,以防 link 死掉):
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" 是这样的:
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)
时,有一个死胡同,所以算法应该"backtrack"到节点(3, 5)
,然后从那里往下走。
我的问题是我真的不明白这是怎么发生的。
返回一个节点并检查邻居意味着再次检查所有内容,它只会返回到 (5, 5)
.
这不会发生,因为实现工作得很好,但我似乎真的无法掌握如何实现。有什么提示吗?
重点是运行Dijkstra或A*时每个节点最多访问一次。
这是因为每次访问一个节点时(在我们将其从队列中弹出之后),我们 'mark' 这个节点已经被访问过。在您提供的实现中,标记是通过将节点添加到 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)
)。