3 AI 經典搜索算法知識點

文章目錄

3.1 Problem solving in AI 人工智能中的問題求解

  • The solution
    It is a sequence of actions to reach the goal.
    解是一個達到目標的動作序列。
  • search 搜索
    The process of looking for a sequence of actions that reaches the goal is called search. (The process of looking for a solution)
    爲達到目標,尋找這樣的行動序列的過程被稱爲搜索。
  • A search algorithm takes a problem as input and returns a solution in the form of an action sequence.
    搜索算法的輸入是問題,輸出是問題的解,以行動序列的形式返回問題的解。
  • State space 狀態空間
    The state space of the problem is formally defined by: initial state, actions and goal state.
    問題的狀態空間可以形式化地定義爲:初始狀態、動作(操作)和目標狀態。
  • Goal test is to determine whether a given state is a goal state.
    目標測試,確定給定的狀態是不是目標狀態.
  • Graph
    State space forms a graph, in which nodes are states, and links are actions.
    狀態空間形成一個圖,其中節點表示狀態、鏈接表示動作。
  • Path 路徑
    A path in the state space is a sequence of states connected by a sequence of actions. 狀態空間的一條路徑是通過行動連接起來的一個狀態序列。
  • Path cost 路徑代價
    Assign a numeric value to each path to denote the cost of taking the action. The
    path cost can be described as the sum of the costs of the individual actions along the path.爲每條路徑賦一個代價值,即邊加權。一條路徑的代價值爲該路徑上的每個行動(每條邊)的耗散值總和。
  • 人工智能研究的搜索技術問題:
    如何生成、存儲與問題有關的部分狀態空間?
  • 知識存儲方式:
    顯示存儲:與問題有關的全部知識(敘述知識、過程知識和控制知識)都存入知識庫。
    隱式存儲:只存儲與問題求解有關部分知識。
    在知識庫種存儲局部狀態空間圖
    從初始狀態出發,運用相應知識生成部分狀態空間圖,通過搜索逐步達到目標狀態。
  • 爲了節約存儲容量,提高存儲效率,採用隱式存儲,進行隱式圖搜索。

3.2 Example

Problem 1: 8-queens problem

The goal is to place 8 queens on a chessboard such that no queen attacks any other. (A queen attacks any piece in the same row, column or diagonal.)
八皇后問題的目標是將8個皇后擺放在國際象棋的棋盤上,使得皇后之間不發生攻擊一個皇后會攻擊同一行、同一列或同一斜線上的其他皇后)。
在這裏插入圖片描述
In figure ©, the queen in the rightmost column is attacked by the queen at the top left.最右下角的皇后與最左上角的皇后可能互相攻擊。

  • States: Any arrangement of 0 to 8 queens on the board is a state.
    棋盤上0 到8 個皇后的任一擺放都是一個狀態。
  • Initial state: No queens on the board.棋盤上沒有皇后。
  • Actions: Add a queen to any empty square. 在任一空格增加擺放1 個皇后。
  • Goal test: 8 queens are on the board, none attacked.
    8 個皇后都在棋盤上,並且不互相攻擊。

Problem 2: 8-puzzle 8數碼難題

  • 3×3 board with 8 numbered tiles and a blank space.
    3×3棋盤上有8個數字棋子和一個空位。
  • A tile adjacent to the blank space can slide into the space. The object is to reach a specified goal state, such as the one shown on the right of the figure (b).與空位相鄰的滑塊可以移向該空位。遊戲目標是達到一個指定的目標狀態。
    在這裏插入圖片描述
  • States: A state description specifies the location of each of the eight tiles and the blank in one of the nine squares.狀態描述指明8 個棋子以及空位在棋盤9 個方格上的分佈。
  • Initial state: Any state can be designated as the initial state.
    任何狀態都可能是初始狀態。
  • Actions: defines the actions as movements of the blank space Left, Right, Up, or Down. It is equivalent to move a numbered tile into the blank space.
    動作:就是將空位向Left、Right、Up或Down 方向移動,等價於將數字牌移動到空位。
  • Goal test: This checks whether the state matches the specified goal configuration
    目標測試:用來檢測狀態是否能匹配指定的目標佈局.
  • Path cost: Each step costs 1, so the path cost is the number of steps in the path.路徑代價:每一步的代價值爲1, 因此整個路徑的代價值是路徑中的步數。

Searching for Solutions 搜索解

  • How to find the solutions of the problems?
    There are several methods (deduction 演繹,induction 歸納,reasoning 推理,searching 搜索)to solve the problems, but we focus on search algorithms.
  • There are two types of searching:
Uninformed Search Strategies
無信息搜索策略(盲目搜索)
Informed Search Strategies
有信息搜索策略(啓發式heuristic)
The strategies use only the information available in the problem definition. 只使用問題定義中可用的信息。 The strategies use problem-specific knowledge beyond the definition of the problem itself, so that can find solutions more efficiently than can an uninformed strategy.這類策略採用超出問題本身定義的、問題特有的知識(啓發函數),因此能夠找到比無信息搜索更有效的解
  • Uninformed Search Strategies 無信息搜索策略(盲目搜索)
     Backtracking search 回溯搜索
     Graph Search 圖搜索

(1)General Graph Search 一般的圖搜索
(2)Breadth-first search 寬度優先搜索
(3)Depth-first search 深度優先搜索
(4)Uniform-cost search 一致代價搜索
(5)Depth-limited search 深度受限搜索
(6)Iterative deepening search 迭代加深的深度優先搜索
(7)Bidirectional Search 雙向搜索

  • Informed Search Strategies 有信息搜索策略( heuristic )
    Best-first Search 最佳優先搜索
    A Search
    A
    Search*
    Local search algorithms(局部搜索)

 Hill-climbing search(爬山搜索)
 Local beam search(局部束搜索)
 Simulated annealing search(模擬煺火搜索)
 Genetic algorithms(遺傳算法)

3.3 Uninformed Search Strategies 無信息搜索策略

  • The uninformed search is also called blind search.無信息搜索也被稱爲盲目搜索。
  • The term (uninformed, or blind) means that the search strategies have no additional information about states beyond that provided in the problem definition.該術語(無信息、盲目的)意味着該搜索策略沒有超出問題定義提供的狀態之外的附加信息。
  • Uninformed search strategies use only the information available in the problem definition. 無信息搜索策略只使用問題定義中可用的信息。
  • All they can do is to generate successors and distinguish a goal state from a non-goal state.搜索算法要做的就是生成後繼結點,並且區分目標狀態和非目標狀態。
  • All search strategies are distinguished by the order in which nodes are expanded.所有的搜索策略是由節點擴展的順序加以區分。

3.3.1 Backtracking search 回溯搜索

Backtracking search is as follows.

  • Starting from the initial state, search for the path continuously and tentatively until it reaches the goal state or “unsolvable node”, that is, “dead end”.
    從初始狀態出發,不停地、試探性地尋找路徑,直到它到達目的狀態或“不可解結
    點”,即“死衚衕”爲止。
  • If it encounters an unsolvable node, it goes back to the nearest parent node in
    the path to see if there are any other child nodes that have not been expanded.
    若它遇到不可解結點,就回溯到路徑中最近的父結點上,查看該結點是否還有其他的子結點未被擴展。
  • If yes, the search continues along these child nodes;
    若有未被擴展的子結點,則沿這些子結點繼續搜索;
  • If it finds a goal state, exit the search successfully and return the problem–solving path. 如果找到目標狀態,就成功退出搜索,返回解題路徑。
    在這裏插入圖片描述
  • The number indicates the order in which the search is made.數字表示被搜索的次序.
  • This procedure presents the property of recursive procedure.該過程呈現出遞歸過程的性質。

3.3.1 Backtracking Search Algorithm回溯搜索算法

Function BACKTRACK1(DATALIST
DATALIST: is the state sequence from the initial state to the current state (reverse)
輸入:從初始狀態到當前狀態的狀態序列表(逆向)
returned value:a path from the current state to the goal state (expressed as a rule table, i.e. an action sequence) or FAIL
返回值:從當前狀態到目標狀態的路徑(以規則表的形式表示)或FAIL。

1. DATA:=FIRST(DATALIST) 
2. // get the first state in the DATALIST
3. IF MENBER(DATA, TAIL(DATALIST)) // set element judgment 
	RETURN FAIL; // TAIL(DATALIST) remain the rest elements but the first
// if DATA is in TAIL(DATALIST), it means there is a loop. 出現迴路,陷入死循環
4. IF TERM(DATA) RETURN NIL; // judge whether DATA is the goal state(終點)
5. IF DEADEND(DATA) RETURN FAIL; // judge whether DATA is the legal
6. IF LENGTH(DATALIST)>BOUND // judge if the recursive depth is too large
		RETURN FAIL; // if too deep, stop searching. Otherwise can not go back.
7. RULES:=APPRULES(DATA); // RULES is the set including all legal actions of the current state (DATA),將DATA加入路徑集合
8. LOOP: IF NULL(RULES) RETURN FAIL; // 判斷動作集是否爲空
9. R:=FIRST(RULES); // get the first action in the RULES 
10. RULES:=TAIL (RULES);
11. RDATA:=GEN (R, DATA);// 將動作R作用於當前狀態,生成新狀態
12. RDATALIST:=CONS (RDATA, DATALIST);// 將新狀態加入到狀態表中
13. PATH:=BACKTRCK1 (RDATALIST) // recursive call, 遞歸調用
14. IF PATH=FAIL GO LOOP;
15. RETURN CONS (R, PATH); // 將動作R加入到PATH中,並返回路徑。

Example:4-Queens

在這裏插入圖片描述

Why need Graph Search

  • A feature of the backtracking search strategy is that only one path from the initial state to the current state is recorded. When backtracking occurs, the search at the backtracking point will be “forgotten” by the algorithm. 回溯搜索策略的一個特點就是:只保留了從初始狀態到當前狀態的一條路徑。當回溯出現時,回溯點處進行的搜索將被算法 “遺忘”。
  • The advantage is that the storage space is saved; 其好處是節省了存儲空間;
  • The disadvantage is that these searched parts that have been forgotten can not be reused later. 其壞處是這些被回溯掉(遺忘掉)的已被搜索過的部分,以後無法使用。
  • The graph search strategy records all the applied operations and their generated states in the search process. This search method is called “graph search” .圖搜索策略則將搜索過程中所有應用過的操作及其生成的狀態都記錄下來,這種搜索方法稱爲“圖搜索”
  • Graph search has two advantages: 其優點有二:
    (1) The searched paths can be reused; 搜索過的路徑可以被重複利用;
    (2) The knowledge related to the problem can be used more effectively to achieve the purpose of heuristic search. 可以更有效地利用與問題有關的知識,從而達到啓發式搜索的目的。

耗費空間,節省時間

3.3.2 Graph Search 圖搜索

(1)General Graph Search (GGS) 一般圖搜索
(2)Breadth-first search 寬度優先搜索
(3)Depth-first search 深度優先搜索

Some Basic Concepts

  • In a graph, a node denotes a state, an arc denotes an action.
  • Depth of a node (結點深度):
     Depth of the root (根結點深度)=0
     the Depth of the rest nodes = the Depth of its parent node+1
    其它結點深度=該結點的父結點深度+1
    在這裏插入圖片描述
  • Path (路徑)
    Let a sequence of nodes be (n0, n1,…,nk) , for i=1,…,k. If node ni-1 has a
    successor node ni, the sequence is called a path from n0 to nk. 設一節點序列爲(n0, n1,…,nk),對於i=1,…,k,若節點ni-1具有一個後繼節點ni,則該序列稱爲從n0到nk的路徑。
  • Path cost (路徑代價)
    The cost of a path is equal to the sum of all the costs connected to each node of
    the path. g (n) represents the cost of a path from initial state to Node n.
    一條路徑的代價等於連接這條路徑各節點間所有代價的總和。用g(n)表示從初始狀態到結點n的路徑代價。
  • Expand a node(擴展一個節點)
    All successor nodes of the node are generated and the cost of the connection arc is given. This process is called “Expand a node”.
    生成該結點的所有後繼節點,並給出連接弧的代價。這一過程稱爲“擴展一個節點”。

(1)General Graph Search Algorithm一般的圖搜索算法

在這裏插入圖片描述

  1. G=G0 (G0=s), OPEN:=(s);

// G is the generated search graph, Table OPEN is used for storing the nodes to be expanded, one node is taken out from Table OPEN to be expanded in each loop, and the newly generated nodes are added into Table OPEN ;
// G是生成的搜索圖,OPEN表用來存儲待擴展的結點,每次循環從OPEN表中取出一個結點加以擴展,並把新生成的結點加入OPEN表;

  1. CLOSED:=( );

// Table CLOSED is used to store expanded nodes and is used to check whether newly generated nodes have been expanded. CLOSED表用來存儲已擴展的結點,它的用途是檢查新生成的結點是否已被擴展過。

  1. LOOP: IF OPEN=( ) THEN EXIT(FAIL);
  2. n:=FIRST(OPEN), REMOVE(n, OPEN), ADD(n, CLOSED);

// 將n從OPEN中刪除,加入CLOSED中

  1. IF GOAL(n) THEN EXIT(SUCCESS);
  2. EXPAND(n)→{mi}, G:=ADD(mi, G);

// The set {mi} is established to include the successor nodes of n, but not the
ancestors of n, and these successor nodes are added to G.
// 擴展結點n,建立集合{mi}, 使其包含n的後繼結點,而不包含n的祖先,並將這些後繼結點加入G中。
Note:There are 3 types of n’s successor nodes : {mi} = {mj} ∪ {mk} ∪ {ml},
(1)n’s successor mj is included neither in OPEN,nor in CLOSED;
(2) n’s successor mk is included in OPEN;
(3) n’s successor ml is included in CLOSED;
注:n 的後繼結點有三類: {mi} = {mj} ∪ {mk} ∪ {ml},
(1)n的後繼結點mj 既不包含於OPEN,也不包含於CLOSED;
(2)n的後繼結點mk包含在OPEN中;
(3)n的後繼結點ml 包含在CLOSED中;

  1. For all nodes in {mi}, mark and modify their pointers.

//對{mi}中所有結點,標記和修改指針:

(1) ADD(mj, OPEN), and set a pointer from mj to n;

//並標記從mj到n的指針;

(2) judge whether the pointers of mk or ml should be set to point to n;

// 計算是否要修改 mk 和 ml 到n的指針,決定是否應當將它們的指針指向n(這些結點已有指針);

(3) judge whether the pointers of the successors of ml should be set to point to ml;

// 計算是否要修改ml後繼節點的指針,決定是否應當使ml的後繼的指針指向ml 本身;

  1. Reorder nodes in OPEN by some principle;

//對OPEN中的結點按某種原則重新排序;

  1. GO LOOP;

GGS:Example of step 7(2)—Node 6
在這裏插入圖片描述
(a) To expand Node 6 (n=6), first generate its successor set {4,7}, where Node 4 is already in OPEN(only considering step 7(2)), the cost of the original path {s➜3➜2➜4} is 5, the cost of the new path {S ➜6➜ 4} is 4, So the pointer of Node 4 should be changed from pointing to 2 to pointing to 6.
要擴展結點6,先生成其後繼結點集合{4,7},其中結點4已在OPEN中,只需考慮第7(2)步驟,原來路徑{s➜3➜2➜4}代價爲5,新路徑{s➜6➜4}代價爲4,故結點4的指針應由指向2改爲指向6。
Dash line points from the current node to its previous node in the path.
Solid circle denotes the node in CLOSED, which is expanded already.
Hollow circle denotes the node in OPEN, which is waiting for being expanded.
GGS: Example of step 7(2) —Node 1
在這裏插入圖片描述
To expand Node 1 (n=1), first generate its successor set {2}, where Node 2 is already in CLOSED, so need to consider both Step 7(2) and 7(3).
要擴展結點1,先生成其後繼結點集合{2},結點2已在CLOSED中,需同時考慮 Step7中的(2)和 (3) 步驟.

  • For Step 7(2), the cost of the original path {s➜3➜2} is 4, the cost of the new
    path {S ➜1➜ 2} is 2, So the pointer of Node 2 should be changed from pointing
    to 3 to pointing to 1.
    執行步驟7(2),原來路徑{s➜3➜2}代價爲4,新路徑{s➜1➜2}代價爲2,故結點2的指針應由指向3改爲指向1。
    GGS:Example of step 7(3) —Node 1
    在這裏插入圖片描述
    © To expand Node 1, it has only one successor, Node 2, which is already in CLOSED.
  • For Step 7(3), Node 2 has two successors (Node 4 and Node 5).
  • There are two paths from S to Node 4: {S➜3➜2 ➜4} and {s➜6➜4}. The original pointer of Node 4 is to point to 6, whose cost is 4.
  • Now we have a new new path {S ➜1➜ 2 ➜4}, its cost is 3, So the pointer of Node 4 should be changed from pointing to 6 to pointing to 2.
    執行步驟7(3),結點2還有2個後繼結點:4和5. 需要決定結點4和5的後繼是否應改變指針。初始結點S到結點4已有兩條路徑:{S ➜3➜2 ➜4}和路徑{s➜6➜4}, 代價分別爲5和4. 結點4原來的指針指向6,其代價爲4.現在從S到結點4又增加了一條新路徑{s➜1➜2 ➜4},其代價爲3,故© expand Node 1----condition (3) 結點4的指針應由指向6改爲指向2

(2)Breadth-first Search 寬度優先搜索—BFS

  • BFS is derived from a general graph search algorithm.
    BFS是從一般圖搜索算法變化來的.
  • BFS is to sort the nodes in Table OPEN in ascending order according to the node depth in the search tree. The nodes with the smallest depth are
    arranged at the front of Table OPEN, and the nodes with the same depth can be arranged in any arrangement. Such a search method is called “Breadth-first search”.
  • BFS就是把OPEN表中的結點按搜索樹中結點深度的增序排序,深度最小的結點排在最前面,深度相同的結點可以任意排列。這樣的搜索方式稱爲“寬度優先搜索”。

Search Strategy 搜索策略

  • It is a simple strategy in which the root node is expanded first, then all the successors of the root node are expanded next, then their successors, and so on. BFS是簡單搜索策略. 先擴展根結點,接着擴展根結點的所有後繼,然後再擴展它們的後繼,依此類推。
  • In BFS, the shallowest unexpanded node is chosen for expansion.
    BFS每次總是擴展深度最淺的結點。
  • In general, all the nodes are expanded at a given depth in the search tree before any nodes at the next level are expanded.
    一般地,在下一層的任何結點擴展之前,搜索樹上本層深度的所有結點都應該已經擴展過。

Implementation 實現方法

Use FIFO (First-In First-Out) queue to store Table OPEN.
使用FIFO隊列存儲OPEN表。
Thus, new nodes (which are always deeper than their parents) go to the tail of the queue, and old nodes, which are shallower than the new nodes, get expanded first.
新結點(結點比其父結點深)總是加入到隊列尾端,這意味着淺層的老結點
會在深層的新結點之前被擴展。

Breadth-first Search on a Simple Binary Tree 簡單二叉樹的寬度優先搜索

The searching order is {A, B, C, D, E, F, G}.
在這裏插入圖片描述

  • The black circles denote expanded nodes.
    塗黑的圓圈表示已被擴展過的結點;
  • The circle pointed by a triangle denote the node to be expanded next.
    三角指向的圓圈表示下一個將要被擴展的結點。

Breadth-First Search Algorithm

在這裏插入圖片描述

  1. G:=G0 (G0=s), OPEN:=(s), CLOSED:=( );

// Table OPEN is used to store
the nodes to be expanded. OPEN is stored in a queue (FIFO) .

  1. LOOP: IF OPEN=( ) THEN EXIT (FAIL);
  2. n:=FIRST(OPEN);

//get the first element from Table OPEN(the front of queue)

  1. IF GOAL(n) THEN EXIT (SUCCESS);
  2. REMOVE(n, OPEN), ADD(n, CLOSED);
  3. EXPAND(n) →{mi}, G:=ADD(mi, G);

// The set {mi} is established to include the successor nodes of n, but not the ancestors of n, and these successor nodes are added to G.
// 擴展結點n,建立集合{mi}, 使其包含n的後繼結點,而不包含n的祖先,並將這些後繼結點加入G中。

  1. IF Goal state is in{mi}, THEN EXIT(SUCCESS);
  2. ADD(mj,OPEN), and mark the pointer from mj to n;

// mj denotes the nodes that are neither in Table CLOSED nor in Table OPEN.
ADD is to place mj in the tail of Table OPEN, so that the shallowest nodes can
be expanded preferentially.
// mj表示不在CLOSED和OPEN中的結點,ADD用於把mj放進OPEN表尾端,使深度最淺的結點可優先擴展。

  1. GO LOOP;
    在這裏插入圖片描述

Features of BFS 寬度優先搜索的性質

  • When the problem has a solution, it must be found.
    當問題有解時,一定能找到解.
  • When the problem is unit cost and the problem has a solution, the optimal solution can be found. 當問題爲單位代價,且問題有解時,一定能找到最優解
  • BFS is a general problem-independent approach
    BFS是一個通用的與問題無關的方法.
  • Low efficiency(效率較低)

(3)Depth-first Search 深度優先搜索 —DFS

  • DFS is also derived from a general graph search algorithm.
    DFS也是從一般圖搜索算法變化來的.
  • DFS is to sort the nodes in Table OPEN in descending order according to the node depth in the search tree. The nodes with the biggest depth are arranged at the front of Table OPEN, and the nodes with the same depth can be
    arranged in any arrangement. Such a method is called “Depth-first search”.
    DFS就是把OPEN表中的結點按搜索樹中結點深度的降序排序,深度最大的結點排在最前面,深度相同的結點可以任意排列。這樣的搜索方式稱爲“深度優先搜索”。
  • DFS always expands the deepest unexpanded node first.
    DFS總是首先擴展最深的未擴展節點。
    BFS always expands the shallowest unexpanded node first.
    BFS總是首先擴展最淺的未擴展節點。

Search Strategy 搜索策略

  • DFS always expands the deepest node in the current Table OPEN of the
    search tree.
  • The search proceeds immediately to the deepest level of the search tree, where the nodes have no successors.
  • After those nodes are expanded, they are dropped from Table OPEN.
  • so then the search “backs up” to the next deepest node that still has unexplored successors.
    DFS總是擴展搜索樹的當前OPEN表中最深的結點。搜索很快推進到搜索樹的最深層,那裏的結點沒有後繼。當那些結點被擴展完之後,就從表OPEN中去掉,然後搜索算法回溯到下一個還有未擴展後繼的深度稍淺的結點。

Implementation of DFS (DFS 的實現方法)

  • Use LIFO (Last-In First-Out) stack to store Table OPEN, put successors at the top of the stack.使用 LIFO 棧存儲OPEN表,把後繼節點放在棧頂。
  • Note: breadth-first-search uses a FIFO queue
    注意:寬度優先搜索使用 FIFO 隊列。

Depth-first Search on a Simple Binary Tree

在這裏插入圖片描述

  • After a node is expanded, it is removed from the top of the stack (OPEN).
    一個結點被擴展後,就從OPEN表(棧)中將其刪除。
    The expanded order (out of stack) is {H, I, D, J, K, E, B, L, M, F, N, O,G, C, A}.
  • The black circles denote the explored and unexpanded nodes,which is pushed into the stack(OPEN). 塗黑的圓圈表示已被探索過但未被擴展過的結點,隨後被壓入棧中;
  • The circle pointed by a triangle denote the node to be explored next.
    三角指向的圓圈表示下一個將要被探索的結點。
    在這裏插入圖片描述

Depth-first Search Algorithm

有界深度優先搜索算法
在這裏插入圖片描述
在這裏插入圖片描述

  1. G:=G0(G0=s), OPEN:=(s), CLOSED:=( );
  2. LOOP: IF OPEN=( ) THEN EXIT (FAIL);
  3. n:=FIRST(OPEN);
  4. IF GOAL(n) THEN EXIT (SUCCESS);
  5. REMOVE(n, OPEN), ADD(n, CLOSED);
  6. IF DEPTH(n)≥Dm GO LOOP;

// Dm is a threshold. 控制深度,以免陷入“深淵”

  1. EXPAND(n) →{mi}, G:=ADD(mi, G);

// The set {mi} is established to include the successor nodes of n, but not the
ancestors of n, and these successor nodes are added to G.
// 擴展結點n,建立集合{mi}, 使其包含n的後繼結點,而不包含n的祖先,並將這些後繼結點加入G中。

  1. IF Goal state is in{mi}, THEN EXIT(SUCCESS);

//IF 目標在{mi}中,則成功退出;

  1. ADD(mj,OPEN), and mark the pointer from mj to n;

// mj denotes the nodes that are neither in Table CLOSED nor in Table OPEN.
ADD is to place mj in the tail of Table OPEN, so that the deepest nodes can be
extended preferentially.
// mj表示不在CLOSED和OPEN中的結點,ADD用於把mj放進OPEN表
後端,使深度最大的結點可優先擴展。

  1. GO LOOP;
    在這裏插入圖片描述
    在這裏插入圖片描述
    The expanded order (out of stack) is {H, I, D, J, K, E, B, L, M, F, N, O,G, C, A}.

Features of DFS 深度優先搜索的性質

  • There is no guarantee that the optimal solution can be found.
    一般不能保證找到最優解
  • When the depth limit is not reasonable, the solution may not be found.
    當深度限制不合理時,可能找不到解.
  • In the worst-case situation, search space is equivalent to exhaustion
    最壞情況時,搜索空間等同於窮舉
  • Compared with BFS, the advantage of DFS is that space complexity is low, because only one path from root to leaf is stored.
    與BFS相比,其優勢在於:空間複雜度低,因爲只存儲一條從根到葉子的路徑。
  • DFS is a general problem-independent approach
    DFS是一個通用的與問題無關的方法.

(3)等代價搜索

等代價搜索=寬度優先搜索+ 最小代價
在這裏插入圖片描述

3.4 Informed Search Strategy (啓發式搜索 Heuristic Search )

The strategies use problem-specific knowledge beyond the definition of the problem itself, so that can find solutions more efficiently than can an uninformed strategy.
這類策略採用超出問題本身定義的、問題特有的知識,因此能夠找到比無信息搜索更有效的解。
The general approaches use one or both of following functions:
一般方法使用如下函數中的一個或兩者:

  • An evaluation function, denoted f(n), used to select a node for expansion.
    評價函數,記作 f(n),用於選擇一個節點進行擴展。
  • A heuristic function, denoted h(n), as a component of f.
    啓發式函數,記作 h(n),作爲 f 的一個組成部分。

3.4.1 Best-First Search 最佳優先搜索

  • Best-first search is an instance of the general GRAPH-SEARCH algorithm.
    最佳優先搜索是一般圖搜索算法的一個實例。
  • Best-first search is to sort all the nodes in Table OPEN according to the heuristic information, and the best node is ranked first, so it is called the “best first search”.
    最佳優先搜索就是把OPEN表中所有結點按照啓發式信息排序,最佳結點排在最前面,因此稱爲“最佳優先搜索”

Search Strategy 搜索策略

  • A node is selected for expansion based on an evaluation function, f(n).
    搜索策略:根據評價函數 f(n) 選擇將要被擴展的節點。
  • The evaluation function is construed as a cost estimate, so the node with the lowest evaluation is expanded first.
    評估函數被看作是代價估計,因此評估值最低的結點被選擇首先進行擴展。

Heuristic Function (啓發式函數)

Most best-first algorithms also include a heuristic function, h(n) as a component of f . 大多數的最佳優先算法的 f 中包含一個啓發式函數h(n)。
h(n) = estimated cost of the cheapest path from the state at node n to a goal state.
h(n)=結點n 到目標結點的最小代價路徑的代價估計值

Evaluation Function :f(n)=h(n)+g(n).

Note: g(n) denotes the cost of the path from the initial state to Node n.
h(n) = estimated cost of the cheapest path from the state at node n to a goal state.

  • If f(n) = g(n), it is called Uniform-cost Search. (一致代價搜索)
  • If f(n) = g(n) = d(n), it is BFS.
    -If f(n) = h(n), it is called Greedy Best-First Search . (貪婪最佳優先搜索)

Implementation 實現方法

  • Use FIFO queue to store Table OPEN.
  • However best-first search uses f(n) instead of g(n) to order the priority queue.
    然而,最佳優先搜索使用f(n),而不是用g(n)來對優先級隊列進行排序。
  • Identical to that for uniform-cost search.
    實現方法:與一致代價搜索相同
  • Special cases 特例
    Greedy Search貪孌搜索 & A* search A* 搜索

3.4.2 A search (A 搜索)

  • It is hoped that heuristic knowledge can be introduced to narrow the search scope and improve the search efficiency under the condition of finding the best
    solution.
    希望引入啓發知識,在保證找到最佳解的情況下,儘可能減少搜索範圍,提高搜索效率。
  • A search is a typical heuristic Graph search algorithm.
    A搜索是一種典型的啓發式圖搜索算法。
  • Basic idea: define an evaluation function f, evaluate the current search status, and find out the most promising node to expand.
    基本思想:定義一個評價函數 f,對當前的搜索狀態進行評估,找出一個最有希望的結點來擴展。

Search Strategy 搜索策略

  • avoid expanding expensive paths, minimizing the total estimated solution cost.
    避免擴展代價高的路徑,使總的估計求解代價最小化。
  • Every time the elements in Table OPEN are sorted in ascending order according to f value.
    每次按照f 值的大小對OPEN表中的元素進行遞增排序。
  • Always select the node with the smallest f value of the current evaluation function to give priority to the expansion.
    每次擴展結點時,總是選擇當前評價函數f值最小的結點來優先擴展。

Implementation 實現方法

Use FIFO (First-In First-Out) queue to store Table OPEN.

Evaluation Function :f(n) = g(n) + h(n)

  • g(n) :the cost of the path from the initial state to Node n. (already happened)
  • Heuristic Function h(n): estimated cost of the cheapest path from n to the goal.
  • f(n) = estimated cost of the cheapest solution through n .
  • g*(n): The cost of the shortest path from the initial state s to Node n.
    從 s(初始狀態/結點)到結點 n 的最短路徑的代價值.
  • h*(n):The cost of the shortest path from Node n to the goal node.
    從結點n 到目標結點的最短路徑的代價值.
  • f*(n)=g*(n)+h*(n):從s經過n到目標結點的最短路徑的代價值.
    The cost of the shortest path from s to the goal node through n.

Symbol Representation

  • g(n)、h(n)、f(n) is the estimate of g*(n), h*(n), f*(n), respectively.It is a kind of prediction.
    g(n)、h(n)、f(n)分別是g*(n)、h*(n)、f*(n)的估計值,是一種預測。
  • g*(n) is the cost spent from the initial node s to Node n. The cost on the minimum-cost path from s to n, g(n),is calculated as the estimated value of g*(n).
    g*(n)是從初始結點s到達結點n 已花費的代價,計算從 s 到 n 的最小代價路徑上的代價,作爲g*(n)的估計值 g(n)。
  • h*(n) is the cost from Node n to the goal node, which cannot be accurately calculated and can only be predicted. The prediction estimate h(n) can be referred to as heuristic information, depending on the knowledge in the field of problem.
  • h*(n)是從結點n 到目標結點將要花費的代價,無法精確計算出來,只能預測.預測估計值h(n)可依靠問題領域的知識,稱爲啓發式信息。

A search Algorithm

  1. OPEN:=(s), f(s):=g(s)+h(s);

// s is the initial state/node.

  1. LOOP: IF OPEN=( ) THEN EXIT(FAIL);
  2. n:=FIRST(OPEN);
  3. IF GOAL(n) THEN EXIT(SUCCESS);
  4. REMOVE(n, OPEN), ADD(n, CLOSED);
  5. EXPAND(n) →{mi}, compute f (n, mi):=g(n, mi)+h(mi);

//擴展結點n,建立集合{mi},使其包含n的所有後繼結點mi ,並計算每個mi的 f 值;
// mi is a successor of Node n. g(n, mi) is the cost from s to mi through n, f (n, mi) is the estimate of the cost from s and mi to the goal node through n;
// g(n, mi) 是從s經過n到達mi 的代價,
// f(n, mi)是從s經過n和mi 到達目標結點的代價的估計,是擴展 n 後的代價估計;
// h(mi) is the cost of the shortest path from mi to the goal node.h(mi)是從mi 到目標結點的最短路徑的代價值.

ADD(mj, OPEN), and mark the pointer from mj to n;

// 標記mj 到n的指針;
// 將n 的後繼中既不在OPEN中又不在CLOSED中的結點mj放入OPEN表中,並令mj 的指針指向n;
// mj is neither in OPEN,nor in CLOSED;mk is in Table OPEN, m1 is in Table CLOSED

(1) IF f(n, mk)<f(mk) THEN
f(mk):=f(n, mk), and mark the pointer from mk to n;

// f(mk) is the cost calculated before expanding n. If the condition holds, it means that the cost of the path from mk to the goal node after expanding n is lower than that before expanding n. The cost should be modified lower, and the pointer of mk should be modified to point to n.
// f(mk)是擴展 n 之前計算的代價,若條件1成立,說明擴展 n之後,從mk到目標結點的代價比擴展 n 之前的代價小,應修改mk的代價,並使mk的指針指向n。

(2) IF f(n, m1)<f(m1) THEN
{f(m1):=f(n, m1),;mark the pointer from ml to n; ADD(m1, OPEN); }

// 若條件2成立,則修改m1 的代價,且修改m1 的指針,使之指向n。
// 把m1 重新放回OPEN隊列中,不必考慮改m1 後繼結點的指針。

  1. The nodes in Table OPEN are sorted in ascending order according to f value;

// OPEN中的結點按 f 值從小到大排序;

  1. GO LOOP;

Note: if h=0, f(n)=d(n) (the depth of Node n), then A Search will degenerate into Breadth-First Search
若h=0,取估價函數f(n)=d(n)(結點n的深度),則A搜索將退化爲廣度優先搜索。

Solve 8-Puzzle problem using A Search Algorithm

Define the Evaluation Function :f(n) = g(n) + h(n)

  • g(n) is defined as the number of steps to move tiles, that is, the depth of Node n.
    g(n)爲從初始狀態到當前狀態的代價值,定義爲移動將牌的步數,即結點的深度.
  • h(n) is defined as the number of misplaced tiles in the current state.
    h(n)是從n 到目標結點的最短路徑的代價值,定義爲當前狀態中“不在位”的將牌數。
    在這裏插入圖片描述

3.4.3 A* Search Algorithm (A*搜索算法)

  • A Search Algorithm does not have any restrictions on the evaluation function f (n).
    A搜索算法沒有對估價函數 f (n)做任何限制。
  • In fact, the evaluation function is very important to the search process. If the selection is not proper, the solution of the problem may not be found, or the optimal solution of the problem may not be found.
    實際上,估價函數對搜索過程是十分重要的,如果選擇不當,則有可能找不到問題的解,或者找到的不是問題的最優解。
  • A* Algorithm is a heuristic search algorithm with some restrictions on the evaluation function.
    A* 算法就是對估價函數加上一些限制後得到的一種啓發式搜索算法。
  • In A algorithm, if the following condition is satisfied,
  • h(n) ≤ h*(n)
    A algorithm is called A* algorithm.
    在A算法中,如果滿足上述條件,則A算法稱爲A*算法。
  • A∗ search is the most widely known form of best-first earch.
    A*搜索是最佳優先搜索的最廣爲人知的形式。
  • A * algorithm is also known as the best graph search algorithm.
    A*算法也稱爲最佳圖搜索算法。
  • It is proved that if the problem has a solution, the solution can be found and the optimal solution can be found by using A* algorithm. Therefore, A * algorithm is better than A algorithm.
    可證明:若問題有解,則利用A* 算法一定能搜索到解,並且一定能搜索到最優解。因此,A* 算法比A算法好。

Example of A* condition

8-Puzzle problem

  • To find shortest solutions by using A* , need a heuristic function that are two commonly used candidates.
    要用A*算法找到最短距離的解,需要一個啓發式函數,通常有兩個候選.
  • h1(n) is defined as the number of misplaced tiles in the current state
    h1(n) = “不在位”的將牌數
  • h2(n) is defined as the total Manhattan distance (tiles from desired locations)
    h2(n) = 所有將牌與其目標位置之間的距離之和。
    在這裏插入圖片描述
  • Let d(n) =The number of steps that have been moved, that is, the depth of Node n in the search tree
    已移動將牌的步數,即結點n在搜索樹中的深度
  • w(n)=the number of misplaced tiles in the state denoted by Node n
    結點n所表示的狀態中“不在位”的將牌數
  • To place w(n) " misplaced tiles " on their respective target positions, at least need to move w(n) steps. 將w(n)個“不在位”的將牌放在其各自的目標位置上,至少需要移動w(n)步。
  • h(n)* is the actual number of steps from the current position of Node n to its target position, so obviously w(n) will not be greater than h*(n).
    h*(n)是從結點n從當前位置移動到目標結點的實際步數,顯然 w(n) ≤ h*(n).
  • Using w(n) as a heuristic function h(n) can satisfy the requirement of lower bound of h(n).
    以w(n)作爲啓發式函數h(n),可以滿足對h(n) 的下界的要求,即有 h(n)= w(n) ≤ h*(n).
  • Therefore, when w(n) is chosen as a heuristic function to solve the 8-digit problem, A algorithm is also A* algorithm.
    因此,當選擇w(n)作爲啓發式函數解決8數碼問題時,A算法就是A*算法。
    在這裏插入圖片描述
    h(n) is defined as the total Manhattan distance (tiles from desired locations)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章