Presentation is loading. Please wait.

Presentation is loading. Please wait.

Path Finding 靜宜大學資工系 蔡奇偉 副教授.

Similar presentations


Presentation on theme: "Path Finding 靜宜大學資工系 蔡奇偉 副教授."— Presentation transcript:

1 Path Finding 靜宜大學資工系 蔡奇偉 副教授

2 大綱 Introduction Trial and Error 法 Contour Tracing 法
Collision Avoidance Tracks 法 Waypoint Pathfinding 法 Graph Search Algorithms Map Search Structures 美化路徑

3 Trial and Error 法 物體往目標方向移動
若碰到障礙物,則後退轉 45 度或 90 度,往前走一定距離,然後再轉往目標方向前進。

4 優點:演算法簡單。 缺點:找到的路徑可能顯得愚笨或不自然(如下圖所示)。

5 Contour Tracing 法 物體往目標方向移動。若碰到障礙物,則沿輪廓繞過障礙物,繼續往目標方向前進。 缺點:移動顯得笨拙。

6 Collision Avoidance Tracks 法
先在障礙物周圍(用程式或人工)規劃出避免碰撞的路徑。 物體接近障礙物時,沿著上述的路徑繞過障礙物,再朝目標前進。

7 Waypoint Pathfinding 法
在遊戲地圖上定義出若干的接駁點(waypoints)與它們之間的連接圖。 兩個接駁點之間的路徑就可以用圖論(Graph Theory)路徑搜尋演算法找出來(見後面的討論)。

8 Graph Search Algorithms
Breadth-first Search Depth-first Search Bidirectional Search Greedy Best-first Search A* Search

9 Breadth-first Search(BFS)
以起點為中心,由近而遠地全面往「外」搜尋,直至碰到目標點為止。 優點:保證可找到最短路徑。 缺點:需要用大量的記憶體來儲存已展開但尚未檢視的節點。

10 Depth-first Search(DFS)
從起點開始,單線地往「前」搜尋。若無法再前進,則後退換另一條路線前進。搜尋直至碰到目標點為止。 優點:記憶體的需求較 BFS 低。 缺點:1. 若圖具有無限多個節點,可能無法走到目標。2. 找到的路徑也許不是最短的,

11 Bidirectional Search 從起點與終點用同樣的方法搜尋,直到發現交會點為止。找到的兩條路徑合併起來即為起點到終點的路徑。
優點:記憶體的需求較低。 缺點:程式比較複雜。

12 前述的 BFS 和 DFS 方法完全以圖的結構(即端點間的連接關係)來選擇下一個拜訪點。
另有一類搜尋演算法運用外在的資訊來選擇「可能更好」的下一個拜訪點,期望能更快地發現到達目標的路徑。這一類的演算法把已知和外在資訊轉化成一個函式 f(n) 來計算端點 n 目前的預估成本(estimated cost)。如果演算法永遠把具有最小函式值的已知但尚未拜訪的端點選擇為下一個拜訪點,則稱為 Best-first Search。 我們將用下一頁的 Romania 簡化地圖為例來介紹兩種 Best-first Search: Greedy Best-first Search 和 A* Search。

13 start goal 簡化的 Romania 地圖

14 Greedy Best-first Search
heuristic function h(n) 的定義如下: h(n) = 從端點 n 到目標點路徑的最低成本預估值 此演算法把 h(n) 選為前述的 f(n),即 f(n) = h(n)。 以 Romania 地圖為例來說,我們可以定義 h(n) = hSLD(n) = 都市 n 和目標都市 Bucharest 的直線距離 下一頁表列出 hSLD (n) 的值。

15 Romania 各都市與 Bucharest 市之間的直線距離

16

17 前述演算法實作時必須考慮 backtracking 與排除迴路情況(如 Iasi 與 Neamt 之間)
start goal solution

18 A* Search A*(讀作 A-star)是最著名的 best-first 搜尋法。它的函式 f(n) 定義如下:
f(n) = g(n) + h(n) 其中,h(n) 如前所述,g(n) 則是從起點到端點 n 路徑的真實成本。因此, f(n) = 經過端點 n 所有路徑的最低預估成本 譬如:下兩頁顯示 A* 搜尋 Romania 地圖的過程。

19

20

21 start goal

22 A* Tree Search Algorithm
function Search (start, goal) pool ← {start}; loop do if pool is empty return failure; node ← 從 pool 集合中取出 f 值最小的端點; if node is goal then return the solution path; 把與 node 相鄰的端點加入集合 pool 中; ※ 端點可重複展開 註:1. 集合 pool 可以用資料結構 priority queue 來製作。 2. 可以在 node 中記錄 parent 端點,藉以反求出 起點到終點的路徑。

23 A* Graph Search Algorithm
function Search (start, goal) closed ← empty set; // 存放已展開過的端點 open ← {start}; loop do if open is empty return failure; node ← 從 open 中取出 f 值最小的端點; if node is goal then return the solution path; if node 不在集合 closed 中 把 node 加入集合 closed 中; 把與 node 相鄰的端點加入集合 open 中; ※ 端點不可重複展開

24 A heuristic h(n) is admissible if it never overestimates the cost to reach the goal.
A heuristic h(n) is consistent if, for every node n and every successor node n' of n generated by any action a, the following inequality holds: h(n)  c(n, a, n') + h(n') where c(n, a, n') is the cost from n to n' due to action a. c(n, a, n') h(n) h(n') n n' goal

25 若 h(n) 是 admissible,則 A* Tree Search 演算法找出的路徑一定是最佳路徑。
f(G') = g(G') + h(G') = g(G') > C* 若 n 是最佳路徑上的一個節點,則 f(n) = g(n) + h(n)  C* 所以 f(n)  C* < f(G') 因此演算法會選擇節點 n 展開,繼續尋找最佳路徑。 = 0 start G' G n optimal path 不在最佳路徑上的目標點

26 若 h(n) 是 consistent,則 A* Graph Search 演算法找出的路徑一定是最佳路徑。
假定 m 是 n 在某路徑中的後繼節點。由 consistent 的定義,我們得到: f(m) = g(m) + h(m) = g(n) + c(n, a, m) + h(m)  g(n) + h(n) 所以 f 沿著路徑, 是一個非遞減函式。 因此,當演算法碰到第一個可展開的目標節點時,即找到了最佳的路徑。

27

28 Map Search Structures Retangular grid Quadtree Convex polygons
Points of visibility

29 goal start

30 Retangular grid

31 Quadtree

32 Convex polygons

33 Points of visibility

34 美化路徑

35

36

37 References Books Websites:
Andre LaMothe. Tricks of the Windows Game Programming Gurus. SAMS S. Russell and P. Norvig. Artifucial Intelligence: A Modern Approach 2nd Ed. Prentice Hall Mark DeLoura. Game Programming Gems. Charles River Media, Inc. 2000 Websites: Amit's Thoughts on Path-Finding and A-Star (


Download ppt "Path Finding 靜宜大學資工系 蔡奇偉 副教授."

Similar presentations


Ads by Google