310 likes | 620 Views
演算法報告. 報告者 : 陳俊至 教授 : 徐熊健. Contents. The Breadth-First Search Depth-First Search Hill Climbing Best-First Search Strategy. The Breadth-First Search. The Breadth-First Search. The Breadth-First Search. 寬度優先搜尋 (BFS) 演算法
E N D
演算法報告 報告者:陳俊至 教授:徐熊健
Contents • The Breadth-First Search • Depth-First Search • Hill Climbing • Best-First Search Strategy
The Breadth-First Search 寬度優先搜尋(BFS)演算法 • Breadth-first search (BFS) is a general technique for traversing a graph • BFS on a graph with n vertices and m edges takesO(n + m ) time • 依序走訪同一層的所有節點,走訪完畢後,才繼續走訪下一層的節點 • BFS 會使用到Queue (佇列--先進先出) 來紀錄過程中展開的節點
The Breadth-First Search A • Queue Output • A • B B D • C C • D • E • G E G • F F
Animation • http://www.cs.bme.hu/~gsala/alg_anims/3/graph2-e.html • http://www.cosc.canterbury.ac.nz/mukundan/dsal/GraphAppl.html • http://www.ee.ryerson.ca/~courses/coe428/graphs/breadth.html • http://www.cs.duke.edu/csed/jawaa2/examples/BFS.html • http://www.rci.rutgers.edu/~cfs/472_html/AI_SEARCH/SearchAnimations.html
Depth-First Search 深度優先搜尋(DFS)演算法 • Depth-first search (DFS) is a general techniquefor traversing a graph • DFS on a graph with n vertices and m edges takes O(n + m ) time • 先走訪越深的子節點, 直到該點沒有子節點後,回溯到最近尚有未走訪子節點的節點,再繼續下訪其他節點 • DFS 會使用到Stack (堆疊—後進先出) 來紀錄過程中展開的節點
The Breadth-First Search A • Stack • A • B • C • F • E • D • G B D C E G F
Animation • http://www.cs.bme.hu/~gsala/alg_anims/3/graph-e.html • http://www.cosc.canterbury.ac.nz/mukundan/dsal/GraphAppl.html • http://www.ee.ryerson.ca/~courses/coe428/graphs/depth.html • http://www.cs.duke.edu/csed/jawaa2/examples/DFS.html • http://www.rci.rutgers.edu/~cfs/472_html/AI_SEARCH/SearchAnimations.html
Hill Climbing • 基本的Hill Climbing 演算法 1. 從搜尋空間中亂數取一點a作為出發點 2. 考慮a點周圍可用的狀態點 3. 取a點周圍最好品質(錯位少)的一點b,並移往b點 4. 重複2~4,直到找不到更好的點 5. 則最後的狀態點就是用Hill Climbing找到的最佳解 6. 若有兩點以上是最好解,則亂數擇一 • Hill Climbing並不能保證得到最佳化 solution,但卻可以有近似 solution
Hill Climbing • 8-puzzle problem • Given a initial arrangement and the goal state, the problem is to determine whether there exists a sequence of movements from initial state to goal state, where each item can be moved only horizontally or vertically to the empty spot. Example:The initial arrangement and the goal state of the 8-puzzle problem.
Hill Climbing • Hill Climbing strategy for 8-puzzle problem • Evaluation function f(n) = w(n), where w(n) is # of misplaced tiles in node n. • The hill climbing strategy is to select the least f(n) to expand the present node Ex: (3) 1, 2, 8 are misplace, f(n)=3
Hill Climbing • An8-puzzle problem solved by a hill climbing method.
Animation • http://www.rci.rutgers.edu/~cfs/472_html/AI_SEARCH/SearchAnimations.html • http://www.kramer.me.uk/robin/NetLogo/hill-climbing%20algorithm.html • http://files.bookboon.com/ai/Hill-Climbing-Example-2.html
Best-First Search Strategy • Combine depth-first search and breadth-first search. • 這個搜尋法只是根據最佳化的評估函數來選擇下一個搜尋的節點 • 當評估函數的準確度愈高,則愈可能找到最佳的節點 • 反之,評估函數可能無作用,甚至可能導至錯誤的搜尋。
Best-First Search Strategy • 以井字遊戲為例 • 若電腦是“o”,對手是“x” • 且評估函數定義為: • F(x)=同一行及對角線的其他空白個數×1 +同一行及對角線的“o”個數×2+Y(X) • Y(X)= 0 若該一行及對角線的“x”個數為0 =–1 若該一行及對角線的“x”個數為 1 =2 若該一行及對角線的“x”個數為 2
Best-First Search Strategy • 假設目前的盤面狀況如下: • 一開始全部都是空格
Best-First Search Strategy • 若由電腦先下,則 • F(A1)=(2+2+2) ×1 + (0+0+0) ×2 + (0+0+0) = 6 • F(A2)=(2+2) ×1 + (0+0) ×2 + (0+0) = 4 • F(A3)=(2+2+2) ×1 + (0+0+0) ×2 + (0+0+0) = 6 • F(A4)=(2+2) ×1 + (0+0) ×2 + (0+0) ×2 = 4 • F(A5)=(2+2+2+2) ×1 + (0+0+0) ×2 + (0+0+0) = 8 • F(A6)=(2+2) ×1 + (0+0) ×2 + (0+0) = 4 • F(A7)=(2+2+2) ×1 + (0+0+0) ×2 + (0+0+0) = 6 • F(A8)=(2+2) ×1 + (0+0) ×2 + (0+0) ×2 = 4 • F(A9)=(2+2+2) ×1 + (0+0+0) ×2 + (0+0+0) ×2 = 6
Best-First Search Strategy • 因此電腦選擇A5;若對手選擇A1 • 則盤面狀況如下:
Best-First Search Strategy • 依同樣的評估函數,可算出 • F(A2)=(1+1) ×1 + (1+0) ×2 + (-1+0) = 3 • F(A3)=(1+1+2) ×1 + (0+1+0) ×2 + (-1+0+0) = 5 • F(A4)=(1+1) ×1 + (1+0) ×2 + (0-1) = 3 • F(A6)=(1+2) ×1 + (1+0) ×2 + (0+0) = 5 • F(A7)=(1+1+2) ×1 + (0+1+0) ×2 + (-1+0+0) = 5 • F(A8)=(1+2) ×1 + (1+0) ×2 + (0+0) = 5 • F(A9)=(0+2+2) ×1 + (0+1+0) ×2 + (0-1+0) = 5
由此可知,A3、A6、A7、A8 及A9都是不錯的選擇 • 假設電腦選擇A3;若對手選擇A7 • 則盤面狀況如下:
依同樣的評估函數,可算出 • F(A2)=(0+1) ×1 + (1+1) ×2 + (-1+0) = 4 • F(A4)=(0+1) ×1 + (0+1) ×2 + (4+0) = 7 • F(A6)=(1+1) ×1 + (1+1) ×2 + (0+0) = 6 • F(A8)=(1+1) ×1 + (0+1) ×2 + (-1+0) = 3 • F(A9)=(1+0+1) ×1 + (0+1+1) ×2 + (-1+0+0) = 5
因此電腦選擇A4;若對手選擇A6 • 則盤面狀況如下:
依同樣的評估函數,可算出 • F(A2)=(0+1) ×1 + (1+1) ×2 + (-1+0) = 4 • F(A8)=(1+1) ×1 + (0+1) ×2 + (-1+0) = 3 • F(A9)=(1+0+0) ×1 + (0+1+1) ×2 + (-1+0-1) = 3
因此電腦選擇A2;若對手選擇A8 • 則盤面狀況如下: