【问题标题】:A* algorithm pseudocodeA*算法伪代码
【发布时间】:2016-05-02 04:05:16
【问题描述】:

我从维基百科找到了伪代码

function A*(start, goal)
    // The set of nodes already evaluated.
    closedSet := {}
    // The set of currently discovered nodes still to be evaluated.
    // 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)
    ....

但我还是不明白,heuristic_cost_estimate()是什么?
伪代码没有显示函数是什么。

在我看来,它是像 dijkstra 这样的其他算法,对吗?

【问题讨论】:

    标签: algorithm a-star


    【解决方案1】:

    该函数将返回一个用于做出决定的启发式值。在 A* 中,它通常是当前节点和最后一个节点之间的最短直线距离,因此函数似乎只是计算两个给定节点之间的距离(直线,不使用路径)。

    【讨论】:

    • 所以和dist_between()一样吗?
    • 是的。正如亨利所说,它应该是低于实际值的任何值(使用路径)但为了获得良好的性能,您应该使用节点之间的最短距离
    • 这不是一个“错误”的结果,因为您不知道最短路径,您可以使用此估计来做出决策。如果没有估计(或亨利所说的返回 0),您将是盲目的并尝试随机节点。
    【解决方案2】:

    启发式必须给出实际成本的下限。重要的是返回值小于或等于实际最小成本,否则算法无法正常工作。

    满足此要求的任何估计都将起作用。即使是最简单的选择始终返回 0 也有效。但是,估计越好,算法的性能就越好。

    【讨论】:

      猜你喜欢
      • 2019-01-07
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      • 2021-12-21
      • 1970-01-01
      • 2021-03-23
      • 2013-04-07
      • 2010-12-14
      相关资源
      最近更新 更多