A*搜尋演算法
![]() A*搜索算法(英語:A* search algorithm)是一種在圖形平面上,有多個節點的路徑,求出最低通過成本的演算法。常用於遊戲中的NPC的移動計算,或网络游戏的BOT的移動計算上。 该算法综合了最良優先搜索和戴克斯特拉算法的优点:在进行启发式搜索提高算法效率的同时,可以保证找到一条最优路径(需要评估函数满足单调性)。 在此算法中,如果以表示从起点到任意顶点的实际距离,表示任意顶点到目标顶点的估算距离(根据所采用的评估函数的不同而变化),那么A*算法的估算函数为: 这个公式遵循以下特性:
虛擬碼//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
相關連結外部連結 |
Index:
pl ar de en es fr it arz nl ja pt ceb sv uk vi war zh ru af ast az bg zh-min-nan bn be ca cs cy da et el eo eu fa gl ko hi hr id he ka la lv lt hu mk ms min no nn ce uz kk ro simple sk sl sr sh fi ta tt th tg azb tr ur zh-yue hy my ace als am an hyw ban bjn map-bms ba be-tarask bcl bpy bar bs br cv nv eml hif fo fy ga gd gu hak ha hsb io ig ilo ia ie os is jv kn ht ku ckb ky mrj lb lij li lmo mai mg ml zh-classical mr xmf mzn cdo mn nap new ne frr oc mhr or as pa pnb ps pms nds crh qu sa sah sco sq scn si sd szl su sw tl shn te bug vec vo wa wuu yi yo diq bat-smg zu lad kbd ang smn ab roa-rup frp arc gn av ay bh bi bo bxr cbk-zam co za dag ary se pdc dv dsb myv ext fur gv gag inh ki glk gan guw xal haw rw kbp pam csb kw km kv koi kg gom ks gcr lo lbe ltg lez nia ln jbo lg mt mi tw mwl mdf mnw nqo fj nah na nds-nl nrm nov om pi pag pap pfl pcd krc kaa ksh rm rue sm sat sc trv stq nso sn cu so srn kab roa-tara tet tpi to chr tum tk tyv udm ug vep fiu-vro vls wo xh zea ty ak bm ch ny ee ff got iu ik kl mad cr pih ami pwn pnt dz rmy rn sg st tn ss ti din chy ts kcg ve
Portal di Ensiklopedia Dunia