1 CS B551: Elements of Artificial Intelligence Instructor: Kris Hauser
2 Recap Agent Frameworks Problem Solving with Search
3 Agenda Search problems Searching, data structures, and algorithms Breadth-first search Depth-first search Uniform-cost search
4 Search problems
5 Defining a Search Problem State space S Successor function: x S SUCC (x) 2 S Initial state s 0 Goal test: x S GOAL? (x) =T or F Arc cost S
6 State Graph Each state is represented by a distinct node An arc (or edge) connects a node s to a node s’ if s’ SUCC (s) An arc (or edge) connects a node s to a node s’ if s’ SUCC (s) The state graph may contain more than one connected component
7 8-Queens Problem State repr. 1 Any placement of 0-8 queensAny placement of 0-8 queens State repr. 2 Any non- conflicting placement of 0-8 queensAny non- conflicting placement of 0-8 queens
8 Representation 1 State: any placement of 0-8 queens Initial state: 0 queens Successor function: Place queen in empty squarePlace queen in empty square Goal test: Non-conflicting placement of 8 queensNon-conflicting placement of 8 queens # of states ~ 64x63x…x57 ~ 3x10 14
9 Representation 2 State: any placement of non-conflicting 0-8 queens in columns starting from left Initial state: 0 queens Successor function: A queen placed in leftmost empty column such that it causes no conflictsA queen placed in leftmost empty column such that it causes no conflicts Goal test: Any state with 8 queensAny state with 8 queens # of states = 2057
10 Path Planning What is the state space ?
11 Formulation #1 Cost of one horizontal/vertical step = 1 Cost of one diagonal step = 2
12 Optimal Solution This path is the shortest in the discretized state space, but not in the original continuous space
13 Formulation #2 Cost of one step: length of segment
14 Formulation #2 Cost of one step: length of segment Visibility graph
15 Solution Path The shortest path in this state space is also the shortest in the original continuous space
16 Example: 8-Puzzle Initial stateGoal state State: Any arrangement of 8 numbered tiles and an empty tile on a 3x3 board
17 15-Puzzle Introduced (?) in 1878 by Sam Loyd, who dubbed himself “America’s greatest puzzle-expert”
18 15-Puzzle Sam Loyd offered $1,000 of his own money to the first person who would solve the following problem: ?
19 But no one ever won the prize !!
20 How big is the state space of the (n 2 -1)-puzzle? 8-puzzle ?? states
21 How big is the state space of the (n 2 -1)-puzzle? 8-puzzle 9! = 362,880 states 15-puzzle 16! ~ 2.09 x states 24-puzzle 25! ~ states But only half of these states are reachable from any given state (but you may not know that in advance)
22 Wlg, let the goal be: A tile j appears after a tile i if either j appears on the same row as i to the right of i, or on another row below the row of i. For every i = 1, 2,..., 15, let n i be the number of tiles j < i that appear after tile i (permutation inversions) N = n 2 + n 3 + + n 15 + row number of empty tile Permutation Inversions n 2 = 0n 3 = 0n 4 = 0 n 5 = 0n 6 = 0n 7 = 1 n 8 = 1n 9 = 1n 10 = 4 n 11 = 0n 12 = 0n 13 = 0 n 14 = 0n 15 = 0 N = 7 + 4
23 Proposition: (N mod 2) is invariant under any legal move of the empty tile Proof: Any horizontal move of the empty tile leaves N unchangedAny horizontal move of the empty tile leaves N unchanged A vertical move of the empty tile changes N by an even increment ( 1 1 1 1)A vertical move of the empty tile changes N by an even increment ( 1 1 1 1) s = s’ = N(s’) = N(s)
24 Proposition: (N mod 2) is invariant under any legal move of the empty tile For a goal state g to be reachable from a state s, a necessary condition is that N(g) and N(s) have the same parity It can be shown that this is also a sufficient condition The state graph consists of two connected components of equal size
25 Searching the State Space It is often not feasible (or too expensive) to build a complete representation of the state graph
26 8-puzzle 362,880 states 15-puzzle 2.09 x states 24-puzzle states 100 millions states/sec sec ~ 55 hours > 10 9 years 8-, 15-, 24-Puzzles
27 Searching
28 Searching the State Space Often it is not feasible (or too expensive) to build a complete representation of the state graph A problem solver must construct a solution by exploring a small portion of the graph
29 Searching the State Space Search tree
30 Searching the State Space Search tree
31 Searching the State Space Search tree
32 Searching the State Space Search tree
33 Searching the State Space Search tree
34 Searching the State Space Search tree
35 Search Nodes and States If states are allowed to be revisited, the search tree may be infinite even when the state space is finite If states are allowed to be revisited, the search tree may be infinite even when the state space is finite
36 Data Structure of a Node PARENT-NODE STATE Depth of a node N = length of path from root to N (depth of the root = 0) BOOKKEEPING5Path-Cost 5Depth RightAction Expanded yes... CHILDREN
37 Node expansion The expansion of a node N of the search tree consists of: 1)Evaluating the successor function on STATE (N) 2)Generating a child of N for each state returned by the function node generation node expansion N
38 Fringe of Search Tree The fringe is the set of all search nodes that haven’t been expanded yet
39 Is it identical to the set of leaves?
40 Search Strategy The fringe is the set of all search nodes that haven’t been expanded yet The fringe is implemented as a priority queue FRINGE INSERT(node,FRINGE)INSERT(node,FRINGE) REMOVE(FRINGE)REMOVE(FRINGE) The ordering of the nodes in FRINGE defines the search strategy
41 Search Algorithm #1 SEARCH#1 1. If GOAL?(initial-state) then return initial-state 2. INSERT(initial-node,FRINGE) 3. Repeat: a.If empty(FRINGE) then return failure b.N REMOVE(FRINGE) c.s STATE(N) d.For every state s’ in SUCCESSORS(s) i. Create a new node N’ as a child of N ii. If GOAL?(s’) then return path or goal state iii. INSERT(N’,FRINGE) Expansion of N
42 Performance Measures Completeness A search algorithm is complete if it finds a solution whenever one exists [What about the case when no solution exists?] Optimality A search algorithm is optimal if it returns a minimum-cost path whenever a solution exists Complexity It measures the time and amount of memory required by the algorithm
43 Blind Search Strategies
44 Blind Strategies Breadth-first BidirectionalBidirectional Depth-first Depth-limitedDepth-limited Iterative deepeningIterative deepening Uniform-Cost (variant of breadth-first) Arc cost = 1 Arc cost = c(action) 0
45 Breadth-First Strategy New nodes are inserted at the end of FRINGE New nodes are inserted at the end of FRINGE FRINGE = (1)
46 Breadth-First Strategy New nodes are inserted at the end of FRINGE New nodes are inserted at the end of FRINGE FRINGE = (2, 3)
47 Breadth-First Strategy New nodes are inserted at the end of FRINGE New nodes are inserted at the end of FRINGE FRINGE = (3, 4, 5)
48 Breadth-First Strategy New nodes are inserted at the end of FRINGE New nodes are inserted at the end of FRINGE FRINGE = (4, 5, 6, 7)
49 Important Parameters 1) Maximum number of successors of any state branching factor b of the search tree 2) Minimal length (≠ cost) of a path between the initial and a goal state depth d of the shallowest goal node in the search tree
50 Evaluation b: branching factor d: depth of shallowest goal node Breadth-first search is: Complete? Not complete?Complete? Not complete? Optimal? Not optimal?Optimal? Not optimal?
51 Evaluation b: branching factor d: depth of shallowest goal node Breadth-first search is: CompleteComplete Optimal if step cost is 1Optimal if step cost is 1 Number of nodes generated: ???
52 Evaluation b: branching factor d: depth of shallowest goal node Breadth-first search is: CompleteComplete Optimal if step cost is 1Optimal if step cost is 1 Number of nodes generated: 1 + b + b 2 + … + b d = ???
53 Evaluation b: branching factor d: depth of shallowest goal node Breadth-first search is: CompleteComplete Optimal if step cost is 1Optimal if step cost is 1 Number of nodes generated: 1 + b + b 2 + … + b d = (b d+1 -1)/(b-1) = O(b d ) Time and space complexity is O(b d )
54 Time and Memory Requirements d # Nodes TimeMemory msec 11 Kbytes 411,111 1 msec 1 Mbyte 6 ~ sec 100 Mb 8 ~ sec 10 Gbytes 10 ~ hours 1 Tbyte 12 ~ days 100 Tbytes 14 ~ years 10,000 Tbytes Assumptions: b = 10; 1,000,000 nodes/sec; 100bytes/node
55 Time and Memory Requirements d # Nodes TimeMemory msec 11 Kbytes 411,111 1 msec 1 Mbyte 6 ~ sec 100 Mb 8 ~ sec 10 Gbytes 10 ~ hours 1 Tbyte 12 ~ days 100 Tbytes 14 ~ years 10,000 Tbytes Assumptions: b = 10; 1,000,000 nodes/sec; 100bytes/node
56 Remark If a problem has no solution, breadth-first may run for ever (if the state space is infinite or states can be revisited arbitrary many times) ?
57 Bidirectional Strategy 2 fringe queues: FRINGE1 and FRINGE2 s Time and space complexity is O(b d/2 ) O(b d ) if both trees have the same branching factor b Question: What happens if the branching factor is different in each direction?
58 Depth-First Strategy New nodes are inserted at the front of FRINGE New nodes are inserted at the front of FRINGE FRINGE = (1)
59 Depth-First Strategy New nodes are inserted at the front of FRINGE New nodes are inserted at the front of FRINGE FRINGE = (2, 3)
60 Depth-First Strategy New nodes are inserted at the front of FRINGE New nodes are inserted at the front of FRINGE FRINGE = (4, 5, 3)
61 Depth-First Strategy New nodes are inserted at the front of FRINGE New nodes are inserted at the front of FRINGE
62 Depth-First Strategy New nodes are inserted at the front of FRINGE New nodes are inserted at the front of FRINGE
63 Depth-First Strategy New nodes are inserted at the front of FRINGE New nodes are inserted at the front of FRINGE
64 Depth-First Strategy New nodes are inserted at the front of FRINGE New nodes are inserted at the front of FRINGE
65 Depth-First Strategy New nodes are inserted at the front of FRINGE New nodes are inserted at the front of FRINGE
66 Depth-First Strategy New nodes are inserted at the front of FRINGE New nodes are inserted at the front of FRINGE
67 Depth-First Strategy New nodes are inserted at the front of FRINGE New nodes are inserted at the front of FRINGE
68 Depth-First Strategy New nodes are inserted at the front of FRINGE New nodes are inserted at the front of FRINGE
69 Evaluation b: branching factor d: depth of shallowest goal node m: maximal depth of a leaf node Depth-first search is: Complete? Optimal?
70 Evaluation b: branching factor d: depth of shallowest goal node m: maximal depth of a leaf node Depth-first search is: Complete only for finite search tree Not optimal Number of nodes generated (worst case): 1 + b + b 2 + … + b m = O(b m ) Time complexity is O(b m ) Space complexity is O(bm) [or O(m)] [Reminder: Breadth-first requires O(b d ) time and space]
71 Depth-Limited Search Depth-first with depth cutoff k (depth at which nodes are not expanded) Three possible outcomes: SolutionSolution Failure (no solution)Failure (no solution) Cutoff (no solution within cutoff)Cutoff (no solution within cutoff)
72 Iterative Deepening Search Provides the best of both breadth-first and depth-first search Provides the best of both breadth-first and depth-first search Main idea: IDS For k = 0, 1, 2, … do: Perform depth-first search with depth cutoff k (i.e., only generate nodes with depth k) Totally horrifying !
73 Iterative Deepening
74 Iterative Deepening
75 Iterative Deepening
76 Performance Iterative deepening search is: CompleteComplete Optimal if step cost =1Optimal if step cost =1 Time complexity is: (d+1)(1) + db + (d-1)b 2 + … + (1) b d = O(b d ) Space complexity is: O(bd) or O(d)
77 Calculation db + (d-1)b 2 + … + (1) b d = b d + 2b d-1 + 3b d-2 +… + db = (1 + 2b b -2 + … + db -d ) b d ( i=1,…, ib (1-i) ) b d = b d ( b/(b-1) ) 2
78 d = 5 and b = 2 BFID 1 1 x 6 = x 5 = x 4 = x 3 = x 2 = x 1 = /63 ~ 2 Number of Generated Nodes (Breadth-First & Iterative Deepening)
79 Number of Generated Nodes (Breadth-First & Iterative Deepening) d = 5 and b = 10 BFID ,0003,000 10,00020, ,000100, ,111123, ,456/111,111 ~ 1.111
80 Recap BFS: Complete, optimal O(b d ) time and spaceO(b d ) time and space DFS: Not complete nor optimal O(bd) space, unbounded timeO(bd) space, unbounded time ID: Complete, optimal O(bd) space, O(b d ) timeO(bd) space, O(b d ) time
81 Homework Readings: R&N Ch HW1 due on 9/22