A*算法基本原理
Posted 智低对商象
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了A*算法基本原理相关的知识,希望对你有一定的参考价值。
广义上而言,A(A-Star)算法就是一种动态规划算法,只不过A算法在每步迭代计算时作出了更为严格的限制。关于动态规划,可以参考我的另一篇博客:《动态规划及其在Apollo项目Planning模块的应用.
A*算法基于代价函数f(n)=g(n)+h(n)计算最短路径,其中f(n)是结点n的总代价函数,g(n)是从初始结点到结点n的移动代价,h(n)是从结点n到目标结点的启发代价。
如果不考虑具体实现代码,A*算法是相当简单的。有两个集合,OPEN集和CLOSED集。其中OPEN集保存待考察的结点。开始时,OPEN集只包含一个元素:初始结点。CLOSED集保存已考查过的结点。开始时,CLOSED集是空的。如果绘成图,OPEN集就是被访问区域的边境(frontier),而CLOSED集则是被访问区域的内(interior)。每个结点同时保存其父结点的指针,以便反向溯源。
在主循环中重复地从OPEN集中取出最好的结点n(f值最小的结点)并检查之。如果n是目标结点,则我们的任务完成了。否则,从OPEN集中删除结点n并将其加入CLOSED集。然后检查它的邻居n’。如果邻居n’在CLOSED集中,表明该邻居已被检查过,不必再次考虑(若你确实需要检查结点n’的g值是否更小,可进行相关检查,若其g值更小,则将该结点从CLOSED集中删除);如果n’在OPEN集中,那么该结点今后肯定会被考察,现在不必考虑它。否则,把它加入OPEN集,把它的父结点设为n。到达n’的路径的代价g(n’),设定为g(n) + movementcost(n, n’)。
下面是算法伪代码:
OPEN = priority queue containing START
CLOSED = empty set
while lowest rank in OPEN is not the GOAL:
current = remove lowest rank item from OPEN
add current to CLOSED
for neighbors of current:
cost = g(current) + movementcost(current, neighbor)
if neighbor in OPEN and cost less than g(neighbor):
remove neighbor from OPEN, because new path is better
if neighbor in CLOSED and cost less than g(neighbor): **
remove neighbor from CLOSED
if neighbor not in OPEN and neighbor not in CLOSED:
set g(neighbor) to cost
add neighbor to OPEN
set priority queue rank to g(neighbor) + h(neighbor)
set neighbor\'s parent to current
reconstruct reverse path from goal to start by following parent pointers
(**) This should never happen if you have an admissible heuristic.
However in games we often have inadmissible heuristics.
以上是关于A*算法基本原理的主要内容,如果未能解决你的问题,请参考以下文章