MASSACHUSETTS INSTITUTE of TECHNOLOGY Department of Electrical Engineering and Computer Science 6.034 Artificial Intelligence Fall, 2010
Total Page:16
File Type:pdf, Size:1020Kb
MASSACHUSETTS INSTITUTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.034 Artificial Intelligence Fall, 2010 Recitation 2, Thursday September 23 Search Me! Prof. Bob Berwick 1. Difference between Various Search Strategies: Uninformed search, and a bit more The most important data structure in the search algorithm is the queue Q, the list of partial paths. To make things efficient, we may also use a list of nodes already examined, via an extended list E. A partial path is a path from the start node to some node X, in reverse order, e.g., (X B A S) The head of a partial path is the most recent node of the path, e.g., X. Let S be the start node and G be the goal node. Here is a simple, uninformed search algorithm: 1: Initialize Q with partial path ((S)) as only entry2: while Q is not empty do 3: Pick some path N from Q 4: if head(N)=G, then 5: return N (we’ve reached the goal) 6: end if 7: remove N from Q 8: for all children c in head(N) not in E do 9: extend the partial path N to c 10: end for 11: Add these extensions of N somewhere in Q 12: end while 13: Signal failure Two operations on Q primarily determine the search strategy: • Step 3: Which path to extend? Strategy of picking element(s) N from Q. • Step 11: How should the newly extended paths be added to Q? Strategy for adding path extensions from node(s) N.1 Search Strategy N Add extensions of N to Q Depth-first First element Front of Q Breadth-first First element End of Q Best-first Best by heuristic value Anywhere in Q Hill-Climbing (no backup) First element Replace Q with sorted extensions of N Hill-Climbing (w/backup) First element Front of Q, sorted by heuristic value Beam (width k, no backup) Best k by heuristic value Anywhere in Q All basic search methods, except for some so called informed/heuristic search methods (like best-first search and beam search), pick the first partial path in Q. As we will see, the most variation is in where the extended paths are inserted (and in some cases, also how). 1Note that in ‘classical’ algorithms terminology, the paths added to Q, or the enqueued items, are called the ‘open list’; the extended list is called the ‘closed list.’ A further note on the extended list. This is a lousy implementation. Most often space is allocated in the node itself to keep a mark, which makes adding to the extended list and checking whether a node is there a constant time operation. Alternatively, a hash table may be used for efficient detection. In any case, the incremental space cost of an extended list will be proportional to the number of nodes in the worst case, which can be very high. To implement the searches w/o backtracking, we simply add If, in addition, we use an extended list E, then the only difference is that (1) we add head(N) to E after N is extended; and (2) after we remove N from Q, we first check whether head(N) is in E. If head(N) is in E, then we continue at the top of the while loop without extending N. Otherwise, we continue with the remaining steps of the while loop (extending N and adding extensions to Q). To implement a search method without backtracking/backups (BT), instead of adding all the extensions of Depth-fiN to Q, only onerst is added. Let’s try out DFS; BFS; Hill-climb w/backup, ie, backtracking. DFS: pickPick firstfirst element ofof QQ;; add Add path path extensions extensions to to front front of of Q Q Step Q Extended list 1 (S) 2 (A S) (B S) S 3 Q Visited C 3 (C A S) (D A S) (B S) A, S 5 1 (S) S 2 G 4 (D A S) (B S) C, A, S A 2 5(A S) (B(G S) D A S) (B S) A, B, D,S C, A, S 4 1 D 3 6(C A S)Success (D A S)- (Bpop S) list C,D,B,A,SG, D, C, A, S S w/G 4 (D A S) (B S) C,D,B,A,S B 5 Added(G D A pathsS) (B S)underlined.G,C,D,B,A,S Why didn’t we add (C D A S) in Step 5? Added paths in blue We showYour the turn partial – BFS: paths pick in reversedfirst element order; of theQ; addhead extensions is the first tonode. end of Q. Step Q Extended list 1 (S) 2 (A S) (B S) S 3 (B S)(C A S)(D A S) A, S 4 [what happens now?] 5 6 Hill-climbing, with backup. (So this is a little bit informed!) Pick firstHill-climbing element of Q. Add path(with extensions backup) (sorted by heuristic value) to front of Q. Heuristic value is a measure of the ‘goodness’ of the path, e.g., an estimate of how far to go, as the crow flies; or in some other terms if not a map. (We will see this a bit later how to work this into optimal search.)Pick Notefirst element that hillof Q;- climbing Add path extensions only looks (sorted at by next heuristic locally value) bestto front step. of Q (We tack the heuristic value to the front of the list, to 3 keep track; note sorting. What if no backup?) C 5 Q Visited 2 G Step Q Extended list A 1 (10 S) S 4 1 (10 S) 2 (2 A S) (3 B S) A,B,S D 2 (2 A S) (3 B S) S S 3 (1 C A S) (4 D A S) (3 B S) C,D,B,A,S 1 3 (1C A S) (4 D A S) (3 B S) A, S B 4 (4 D A S) (43 B(4 S D) A S) (3 B S) C, A, S C,D,B,A,S 5 (0 G D A S5) (3(0 B G S D) A S) (3 B S)D, C, A, S G,C,D,B,A,S Heuristic Values 6 Success G, D, C, A, S A=2 C=1 S=10 B=3 D=4 G=0 Added paths in blue; heuristic value of head is in front. We show the partial paths in reversed order; the head is the first node. Best-first search: idea is to use an evaluation function for each node, an estimate of its ‘desirability’, and then expand most desirable unexpanded node along the entire ‘fringe’. Step Q Extended list 1 (10 S) 2 (2 A S) (3 B S) S 3 (1C A S) (3 B S) (4 D A S) A, S 4 (3 B S) (4 D A S) C, A, S 5 (0 G B S) (4 D A S) B, C, A, S 6 Success G, B, C, A, S Cost and Performance of Various Search Strategies (branching factor = b, depth = d) Worst case time = proportional to # nodes visited Worst case space= proportional to maximum length of Q Fewest Guaranteed Search Strategy Worst Time Worst Space Nodes? to find path? Depth-first (with backup) bd+1 bd No Yes Breadth-first bd+1 bd Yes Yes Hill-Climbing (no backup) d b No No Hill-Climbing (with backup) bd+1 bd No Yes Best-first bd+1 bd No Yes Beam (beam width k, no backup) kd kb No No How could we combine the space efficiency of DFS with BFS? (BFS guaranteed to find path to goal with minimum number of nodes.) Answer: Iterative Deepening Search (IDS) – search DFS, level by level, until we run out of time. Let’s see. Counting Nodes in a Tree Why is (bd+1 – 1)/(b-1) the number of nodes in a tree? (branching factor = b, depth = d) If each node has b immediate descendents: o o o o b Then Level 0 (the root) has 1 node. Level 1 has b nodes. Level 2 has b * b = b2 nodes. Level 3 has b2* b = b3 nodes. … Level d has bd-1* b = bd nodes. So the total number of nodes is: 2 3 4 d N = 1 + b + b + b + b + … + b 2 3 4 d d+1 bN = b + b + b + b + … + b + b Subtracting: (b – 1)N = bd+1 – 1 N = bd+1 – 1 b – 1 So we could do this to implement iterative deepening search (IDS): 1: Initialize Dmax=1. (The goal node is of unknown depth d) 2: Do 3: DFS from S for fixed depth Dmax 4: If found a goal node, depth d ≤ Dmax then exit 5: Dmax = Dmax + 1 Cost is: O(b1+b2+…+bL)=O(bL) where L= length to goal. But isn’t IDS wasteful because we repeat searches on the different iterations? No. For example, suppose b=10 and d=5. Then the total # number of nodes N we look at for in each case is: N(IDS) ≈ db+ (d–1)b2+ … + b5 = 123,450, while for BFS the # of nodes is appro: N(BFS) ≈ b + b2 + … + b5 = 111,110, or only about 10% less. Most of the time is spent at depth d. So, IDS is asymptotically optimal; because ‘most’ of the time is spent in the fringe of the search tree.