fbpx
维基百科

A*搜尋演算法

A*搜索算法(A* search algorithm)是一種在圖形平面上,有多個節點路徑,求出最低通過成本演算法。常用於遊戲中的NPC的移動計算,或网络游戏的BOT的移動計算上。

A*搜索算法的演示图

该算法综合了最良優先搜索英语Best-first searchDijkstra算法的优点:在进行启发式搜索提高算法效率的同时,可以保证找到一条最优路径(需要评估函数满足单调性)。

在此算法中,如果以表示从起点到任意顶点的实际距离,表示任意顶点到目标顶点的估算距离(根据所采用的评估函数的不同而变化),那么A*算法的估算函数为:

这个公式遵循以下特性:

  • 如果为0,即只计算任意顶点到目标的评估函数,而不计算起点到顶点的距离,则算法转化为使用贪心策略的最良優先搜索英语Best-first search,速度最快,但可能得不出最优解;
  • 如果不大于顶点到目標頂點的實際距離,则一定可以求出最优解,而且越小,需要计算的节点越多,算法效率越低,常见的评估函数有——欧几里得距离曼哈顿距离切比雪夫距离
  • 如果为0,即只需求出起点到任意顶点的最短路径,而不计算任何评估函数,则转化为最短路问题问题,即Dijkstra算法,此时需要计算最多的顶点;

虛擬碼

//Matlab語言  function A*(start,goal)  closedset := the empty set //已经被估算的節點集合  openset := set containing the initial node //將要被估算的節點集合,初始只包含start  came_from := empty map  g_score[start] := 0 //g(n)  h_score[start] := heuristic_estimate_of_distance(start, goal) //通過估計函數 估計h(start)  f_score[start] := h_score[start] //f(n)=h(n)+g(n),由於g(n)=0,所以省略  while openset is not empty //當將被估算的節點存在時,執行循環  x := the node in openset having the lowest f_score[] value //在將被估計的集合中找到f(x)最小的節點  if x = goal //x為終點,執行  return reconstruct_path(came_from,goal) //返回到x的最佳路徑  remove x from openset //x節點從將被估算的節點中刪除  add x to closedset //x節點插入已經被估算的節點  for each y in neighbor_nodes(x) //循環遍歷與x相鄰節點  if y in closedset //y已被估值,跳過  continue  tentative_g_score := g_score[x] + dist_between(x,y) //從起點到節點y的距離  if y not in openset //y不是將被估算的節點  tentative_is_better := true //暫時判斷為更好  elseif tentative_g_score < g_score[y] //如果起點到y的距離小於y的實際距離  tentative_is_better := true //暫時判斷為更好  else  tentative_is_better := false //否則判斷為更差  if tentative_is_better = true //如果判斷為更好  came_from[y] := x //y設為x的子節點  g_score[y] := tentative_g_score //更新y到原點的距離  h_score[y] := heuristic_estimate_of_distance(y, goal) //估計y到終點的距離  f_score[y] := g_score[y] + h_score[y]  add y to openset //y插入將被估算的節點中  return failure    function reconstruct_path(came_from,current_node)  if came_from[current_node] is set  p = reconstruct_path(came_from,came_from[current_node])  return (p + current_node)  else  return current_node 

相關連結

外部連結

  • A* 演算法簡介 (A* Algorithm Brief)(页面存档备份,存于互联网档案馆

搜尋演算法, 此條目需要补充更多来源, 2015年6月30日, 请协助補充多方面可靠来源以改善这篇条目, 无法查证的内容可能會因為异议提出而移除, 致使用者, 请搜索一下条目的标题, 来源搜索, 网页, 新闻, 书籍, 学术, 图像, 以检查网络上是否存在该主题的更多可靠来源, 判定指引, 此條目需要擴充, 2018年3月5日, 请協助改善这篇條目, 更進一步的信息可能會在討論頁或扩充请求中找到, 请在擴充條目後將此模板移除, 搜索算法, search, algorithm, 是一種在圖形平面上, 有多個節點的路徑. 此條目需要补充更多来源 2015年6月30日 请协助補充多方面可靠来源以改善这篇条目 无法查证的内容可能會因為异议提出而移除 致使用者 请搜索一下条目的标题 来源搜索 A 搜尋演算法 网页 新闻 书籍 学术 图像 以检查网络上是否存在该主题的更多可靠来源 判定指引 此條目需要擴充 2018年3月5日 请協助改善这篇條目 更進一步的信息可能會在討論頁或扩充请求中找到 请在擴充條目後將此模板移除 A 搜索算法 A search algorithm 是一種在圖形平面上 有多個節點的路徑 求出最低通過成本的演算法 常用於遊戲中的NPC的移動計算 或网络游戏的BOT的移動計算上 A 搜索算法的演示图 该算法综合了最良優先搜索 英语 Best first search 和Dijkstra算法的优点 在进行启发式搜索提高算法效率的同时 可以保证找到一条最优路径 需要评估函数满足单调性 在此算法中 如果以g n displaystyle g n 表示从起点到任意顶点n displaystyle n 的实际距离 h n displaystyle h n 表示任意顶点n displaystyle n 到目标顶点的估算距离 根据所采用的评估函数的不同而变化 那么A 算法的估算函数为 f n g n h n displaystyle f n g n h n 这个公式遵循以下特性 如果g n displaystyle g n 为0 即只计算任意顶点n displaystyle n 到目标的评估函数h n displaystyle h n 而不计算起点到顶点n displaystyle n 的距离 则算法转化为使用贪心策略的最良優先搜索 英语 Best first search 速度最快 但可能得不出最优解 如果h n displaystyle h n 不大于顶点n displaystyle n 到目標頂點的實際距離 则一定可以求出最优解 而且h n displaystyle h n 越小 需要计算的节点越多 算法效率越低 常见的评估函数有 欧几里得距离 曼哈顿距离 切比雪夫距离 如果h n displaystyle h n 为0 即只需求出起点到任意顶点n displaystyle n 的最短路径g n displaystyle g n 而不计算任何评估函数h n displaystyle h n 则转化为最短路问题问题 即Dijkstra算法 此时需要计算最多的顶点 虛擬碼 编辑 Matlab語言 function A start goal closedset the empty set 已经被估算的節點集合 openset set containing the initial node 將要被估算的節點集合 初始只包含start came from empty map g score start 0 g n h score start heuristic estimate of distance start goal 通過估計函數 估計h start f score start h score start f n h n g n 由於g n 0 所以省略 while openset is not empty 當將被估算的節點存在時 執行循環 x the node in openset having the lowest f score value 在將被估計的集合中找到f x 最小的節點 if x goal 若x為終點 執行 return reconstruct path came from goal 返回到x的最佳路徑 remove x from openset 將x節點從將被估算的節點中刪除 add x to closedset 將x節點插入已經被估算的節點 for each y in neighbor nodes x 循環遍歷與x相鄰節點 if y in closedset 若y已被估值 跳過 continue tentative g score g score x dist between x y 從起點到節點y的距離 if y not in openset 若y不是將被估算的節點 tentative is better true 暫時判斷為更好 elseif tentative g score lt g score y 如果起點到y的距離小於y的實際距離 tentative is better true 暫時判斷為更好 else tentative is better false 否則判斷為更差 if tentative is better true 如果判斷為更好 came from y x 將y設為x的子節點 g score y tentative g score 更新y到原點的距離 h score y heuristic estimate of distance y goal 估計y到終點的距離 f score y g score y h score y add y to openset 將y插入將被估算的節點中 return failure function reconstruct path came from current node if came from current node is set p reconstruct path came from came from current node return p current node else return current node相關連結 编辑寻路 广度优先搜索 深度优先搜索外部連結 编辑A 演算法簡介 A Algorithm Brief 页面存档备份 存于互联网档案馆 取自 https zh wikipedia org w index php title A 搜尋演算法 amp oldid 72064716, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。