CS 188: Artificial Intelligence Spring 2006 Lecture 2: Queue-Based Search 8/31/2006 Announcements Lab Friday 1-5pm in Soda 275 Learn Python Start on Project 1.1: Mazeworld Come for whatever times you like No sections this Monday Project 1.1 posted due 9/8 Dan Klein – UC Berkeley Many slides from either Stuart Russell or Andrew Moore Today Agents that Plan Ahead Search Problems Uniformed Search Methods Depth-First Search Breadth-First Search Uniform-Cost Search Heuristic Search Methods You can do most of it after today Reflex Agents Reflex agents: Choose action based on current percept and memory May have memory or a model of the world’s current state Do not consider the future consequences of their actions Can an reflex agent be rational? Greedy Search A* Search Goal-Based Agents Goal- based agents: Plan ahead Decisions based on (hypothesized) consequences of actions Must have a model of how the world evolves in response to actions Search Problems A search problem consists of: A state space A successor function “N”, 1.0 “E”, 1.0 A start state and a goal test A solution is a sequence of actions which transform the start state to a goal state 1 Search Trees “E”, 1.0 “N”, 1.0 State Space Graphs There’s some big graph in which A search tree: This is a “what if” tree Current state at the root node Children correspond to successors Nodes labeled with states, correspond to PATHS to those states For most problems, can never actually build the whole tree Important: For most problems we could never actually build this graph How many states in Pacman? G a Each state is a node Each successor is an outgoing arc c b e d f S h p q r Laughably tiny search graph for a tiny search problem So, have to find ways of using only the important parts of the tree! Example: Romania Another Search Tree Search: Expand out possible plans Maintain a fringe of unexpanded plans Try to expand as few tree nodes as possible States vs. Nodes General Tree Search Problem graphs have problem states Have successors Search trees have search nodes Have parents, children, depth, path cost, etc. Expand uses successor function to create new search tree nodes The same problem state may be in multiple search tree nodes Important ideas: Fringe Expansion Exploration strategy Detailed pseudocode is in the book! Main question: which fringe nodes to explore? 2 Example: Tree Search State Graphs vs Search Trees G a d f f S r q e d h p Each NODE in in the search tree is an entire PATH in the problem graph. c b e S G a c b h p r q S We almost always construct both on demand – and we construct as little as possible. p e d b c a a h e h p q q r p f q c q r q f c G a G a Review: Depth First Search G a Strategy: expand deepest node first e d Implementation: Fringe is a LIFO stack f S p r q p p e a a h e q r p q q c r p f q G q f c c a a G e h p a q q h r p f q c q f c G a G DFS Algorithm DFS Depth First Search Complete Optimal Time Space N LMAX) O(B Infinite O(LMAX) Infinite N N N b Variables: n Number of states in the problem b The average branching factor B (the average number of successors) Cost of least cost solution C* q r a Search Algorithm Properties Complete? Guaranteed to find a solution if one exists? Optimal? Guaranteed to find the least cost path? Time complexity? Space complexity? p e b a r q d Search Tiers h f h S d c e d S S b c b Implementation: Fringe is a FIFO queue h G a Strategy: expand shallowest node first c b Review: Breadth First Search s Depth of the shallowest solution m Max depth of the search tree START a GOAL Infinite paths make DFS incomplete… How can we fix this? 3 DFS BFS With cycle checking, DFS is complete. Algorithm DFS 1 node b Optimal Y N O(bm+1) O(bm) Y N* O(bs+1) O(bs) BFS b nodes … Complete w/ Path Checking Time Space b2 nodes m tiers s tiers bm Algorithm DFS w/ Path Checking 1 node b Complete Optimal Y N b nodes … b2 nodes nodes Time bs nodes Space O(bm+1) bm nodes O(bm) When is DFS optimal? When is BFS optimal? Costs on Actions Comparisons When will BFS outperform DFS? GOAL a 2 2 c b 1 3 2 e d 3 9 When will DFS outperform BFS? 2 8 8 START h 4 1 p 15 f 2 4 q 1 r Notice that BFS finds the shortest path in terms of number of transitions. It does not find the least-cost path. We will quickly cover an algorithm which does find the least-cost path. Priority Queue Refresher Uniform Cost Search 2 b Expand cheapest node first: 3 Fringe is a priority queue d p 15 Cost contours c a 6 a h 17 r 11 e 5 11 p 9 e 3 d h 13 r 7 p f 8 q q q 11 c G 10 2 9 2 e h 8 q f c G f 1 A priority queue is a data structure in which you can insert and retrieve (key, value) pairs with the following operations: 1 r q 0 S b 4 c 8 S 1 G a 1 p 1 q 16 pq.push(key, value) inserts (key, value) into the queue. pq.pop() returns the key with the lowest value, and removes it from the queue. You can promote or demote keys by resetting their priorities Unlike a regular queue, insertions into a priority queue are not constant time, usually O(log n) We’ll need priority queues for most cost-sensitive search methods. a a 4 Uniform Cost Search Uniform Cost Search Algorithm What will UCS do for this graph? 0 DFS b 1 w/ Path Checking Complete Optimal Time Space Y N O(bm+1) O(bm) BFS Y N O(bs+1) O(bs) UCS Y* Y O(C* bC*/ε) O(bC*/ε) 0 a START 1 b … GOAL We’ll talk more about uniform cost search’s failure cases later… C*/ε tiers What does this mean for completeness? Uniform Cost Problems Remember: explores increasing cost contours … Extra Work? c≤1 c≤2 Failure to detect repeated states can cause exponentially more work. Why? c≤3 The good: UCS is complete and optimal! The bad: Explores options in every “direction” No information about goal location Start Goal Graph Search Graph Search In BFS, for example, we shouldn’t bother expanding the circled nodes (why?) Very simple fix: never expand a node twice S e d b c a a h e h p q q c a r p f q G p q r q f c G a Can this wreck correctness? Why or why not? 5 Search Gone Wrong? Best-First / Greedy Search Best-First / Greedy Search Best-First / Greedy Search Expand the node that seems closest… GOAL a 2 2 h=0 h=8 b c 1 h=11 2 h=4 h=5 8 2 e d 3 1 9 h=8 START h=12 p 15 f 9 h=4 h 4 1 5 h=6 4 3 r q h=11 What can go wrong? 5 h=9 h=6 Best-First / Greedy Search A common case: Best-first takes you straight to the (wrong) goal b … Worst-case: like a badlyguided DFS in the worst case Can explore everything Can get stuck in loops if no cycle checking b … Like DFS in completeness (finite states w/ cycle checking) 6
© Copyright 2026 Paperzz