-3

一段时间以来,我一直在尝试优化我自己的 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))

我知道使用堆队列,但这不是这个问题的重点。

4

1 回答 1

2

原件有一个开集和一个闭集。它将检查邻居是否在封闭集中,如果该暂定分数较高,则将跳过它。如果它不在打开的集合中,或者暂定分数较低,它将使用它作为更好的路径。

相反,您有一个打开的集合和一个可见的集合。您检查它是否不在可见集中,在这种情况下,您会将其添加到可见集中并使用它。

这是非常不同的,并且可能会给出不正确的结果。据我所知,您的算法不会产生最短路径,它只会始终使用最后一个邻居作为路径。

于 2013-04-22T09:50:22.787 回答