A*算法伪代码
A* algorithm pseudocode
我从维基百科找到了伪代码
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,我说得对吗?
该函数将 return 一个用于做出决定的启发式值。在 A* 中,它通常是当前节点和最后一个节点之间的最短直线距离,因此函数似乎只是计算两个给定节点之间的距离(直线,不使用路径)。
启发式必须给出实际成本的下限。 returned 值小于或等于实际最小成本很重要,否则算法无法正常工作。
任何满足此要求的估算都可以。即使是 return 0 的最简单选择也始终有效。但是,估计得越好,算法的性能就越好。
我从维基百科找到了伪代码
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,我说得对吗?
该函数将 return 一个用于做出决定的启发式值。在 A* 中,它通常是当前节点和最后一个节点之间的最短直线距离,因此函数似乎只是计算两个给定节点之间的距离(直线,不使用路径)。
启发式必须给出实际成本的下限。 returned 值小于或等于实际最小成本很重要,否则算法无法正常工作。
任何满足此要求的估算都可以。即使是 return 0 的最简单选择也始终有效。但是,估计得越好,算法的性能就越好。