重新访问A *算法:我是否必须检查打开列表或关闭列表?

时间:2017-03-20 10:48:03

标签: algorithm a-star

我正在研究A *算法,并且对于重新访问存在一些困惑。

当我的教授解释A *时,他说如果我重新访问已经在关闭列表中的节点,

我必须检查重新访问的费用与原始费用。

如果重新访问更便宜,我应该放弃关闭列表中的节点并在其上添加重新访问的节点。

所以伪代码是这样的:

GeneralGraphSearch( v )
Prepare two empty lists: OPEN, CLOSED
Insert v with Coste(v) into OPEN
While forever
    If OPEN is empty, return failure
    while forever
       v = the node with the lowest cost in OPEN
       remove v from OPEN
       if v is not in CLOSED // v is not visited
        break 
       else if the new cost is cheaper than the cost of v in CLOSED
        remove v in CLOSED
        break
       end if
    end while
    If v is a goal, return success
    Insert v into CLOSED
    Expand v
    Insert all children of v with their costs into OPEN
end while
End

然而,当我查找维基百科时,看起来他们只是忽略一个节点,如果它已经在关闭列表中。

相反,它们处理已在打开列表中的节点。

他们的伪代码版本是这样的:

function A*(start, goal)
    // The set of nodes already evaluated.
    closedSet := {}
    // The set of currently discovered nodes that are not evaluated yet.
    // Initially, only the start node is known.
    openSet := {start}
    // For each node, which node it can most efficiently be reached from.
    // If a node can be reached from many nodes, cameFrom will eventually contain the
    // most efficient previous step.
    cameFrom := the empty map

    // For each node, the cost of getting from the start node to that node.
    gScore := map with default value of Infinity
    // The cost of going from start to start is zero.
    gScore[start] := 0 
    // For each node, the total cost of getting from the start node to the goal
    // by passing by that node. That value is partly known, partly heuristic.
    fScore := map with default value of Infinity
    // For the first node, that value is completely heuristic.
    fScore[start] := heuristic_cost_estimate(start, goal)

    while openSet is not empty
        current := the node in openSet having the lowest fScore[] value
        if current = goal
            return reconstruct_path(cameFrom, current)

        openSet.Remove(current)
        closedSet.Add(current)
        for each neighbor of current
            if neighbor in closedSet
                continue        // Ignore the neighbor which is already evaluated.
            // The distance from start to a neighbor
            tentative_gScore := gScore[current] + dist_between(current, neighbor)
            if neighbor not in openSet  // Discover a new node
                openSet.Add(neighbor)
            else if tentative_gScore >= gScore[neighbor]
                continue        // This is not a better path.

            // This path is the best until now. Record it!
            cameFrom[neighbor] := current
            gScore[neighbor] := tentative_gScore
            fScore[neighbor] := gScore[neighbor] + heuristic_cost_estimate(neighbor, goal)

    return failure

function reconstruct_path(cameFrom, current)
    total_path := [current]
    while current in cameFrom.Keys:
        current := cameFrom[current]
        total_path.append(current)
    return total_path

那么哪种方式是正确的?

或者两者都相同?

1 个答案:

答案 0 :(得分:5)

编辑:实际上两者都是对的,Correct formulation of the A* algorithm

但是,如果你的教义只是可以接受但不一致,那么你只会使用你的教授算法,取自namin's回答:

  

[维基百科的方法是最佳的]如果任何重复状态的最佳路径始终是首先遵循的。如果启发式函数具有一致性(也称为单性),则此属性成立。如果对于每个节点n和每个后继者n',则启发式函数是一致的。 n,从n到达目标的估计成本不大于到达n'从n加上从n。到达目标的估计成本。

  如果启发函数仅是可接受的,那么

[你的proffessors版本]是最佳的,也就是说,它永远不会过高估计达到目标的成本。

鉴于你的语言是欧几里德空间中的欧几里德距离(因此是一致的和可接受的),你不应该在闭合集中添加任何不是该节点的最小成本值的东西。以这种方式考虑,如果任何节点的任何成本都可以无效,那么闭合集中的成本会导致所有其他相关路径步骤必须重新评估,并且会破坏运行时复杂性对于这个算法。

维基百科在这些情况下是正确的,并且它甚至首先使开放设置优先级队列的全部原因。理想情况下,您将开放式设置为PQ,将闭合式设置为一个列表,该列表按某种顺序与您必须先采取的步骤相关。

您需要修改开放集中路径成本的想法实际上意味着您需要一个带删除的优先级队列,这可以在日志时间内使用hash-table-index-into二进制堆结构/配对堆/ Fibonacci堆等(如何实现这一点的答案可以在其他地方找到)。这实际上成为IMO算法实现中最难的部分,因为仅仅遵循算法实际上并没有告诉你如何实现这一点,并且会让你停下来搞清楚伪实现中缺少哪些部分。

您会注意到其他实现将讨论删除键/更新键操作,这些在闭合集中没有意义,只有基于优先级队列的开放集。