Algorithm A*算法伪码
我从维基百科找到了伪代码Algorithm A*算法伪码,algorithm,a-star,Algorithm,A Star,我从维基百科找到了伪代码 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
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)
....
但我仍然不明白什么是启发式成本估算(?伪代码没有显示函数是什么。
在我看来,这是另一种类似dijkstra的算法,对吗?该函数将返回一个启发式值,用于做出决策。在A*中,它通常是当前节点和最终节点之间的最短直线距离,因此函数似乎只是简单地计算两个给定节点之间的距离(直线,不使用路径)。启发式必须给出实际成本的下限。返回值必须小于或等于实际最小成本,否则算法无法正常工作
满足此要求的任何估算都将有效。即使是始终返回0的最简单的选择也能起作用。但是,估计值越高,算法的性能就越好。所以dist_between()也是一样的?是的,是这样。正如Henry所说,它应该是低于实际值的任何值(使用路径),但是为了获得良好的性能,您应该使用节点之间的最短距离。这不是一个“错误”的结果,因为您不知道最短路径,所以您可以使用此估计来做出决策。如果没有估计(或者像Henry说的返回0),您将是盲目的,并尝试随机节点。