一段时间以来,我一直在尝试优化我自己的 A* 搜索算法的实现,最后稍微改变了实际的算法部分。
我一直想知道这种方法是否会比常规 A* 更快。为什么或者为什么不?如果是这样,有什么理由在这种略有不同的方法上使用常规 A*?
def find_path(a, b):
seen = set()
opened = set()
parent = {}
distance = {a: path_distance(a, b)}
while opened:
node = min(opened, key=lambda x: distance[x])
if node == end:
path = []
while node in parent:
path.append(node)
node = parent[node]
return path
opened.remove(node)
for neighbor in node.neighbors:
if neighbor not in seen:
seen.add(neighbor)
opened.add(neighbor)
parent[neighbor] = node
distance[neighbor] = pathDistance(neighbor, b)
def path_distance(a, b):
return sum(y - x for x, y in zip(a.position, b.position))
我知道使用堆队列,但这不是这个问题的重点。