Warm- up We ll o-en have a warm- up exercise for the 10 minutes before class starts. Here s the first one Write the pseudo code for breadth first search and depth first search IteraAve version, not recursive class TreeNode TreeNode[] children() boolean isgoal() BFS(TreeNode start) DFS(TreeNode start)
Announcements Upcoming due dates F 5 pm Proj 0 M 11:59 pm HW 1 Be careful of edx Amezone (UTC)! Account forms Pick- up a-er lecture (if you like) Step- by- step videos Found in edx Courseware (linked on edx Syllabus)
CS 188: ArAficial Intelligence Search Instructors: Stuart Russell and Pat Virtue
Today Agents that Plan Ahead Search Problems Uninformed Search Methods Depth- First Search Breadth- First Search Uniform- Cost Search
Reflex agents: Choose acaon based on current percept (and maybe memory) May have memory or a model of the world s current state Do not consider the future consequences of their acaons Consider how the world IS Reflex Agents
Agents that Plan Ahead Planning agents: Decisions based on predicted consequences of acaons Must have a transi/on model: how the world evolves in response to acaons Must formulate a goal Consider how the world WOULD BE Spectrum of deliberaaveness: Generate complete, opamal plan offline, then execute Generate a simple, greedy plan, start execuang, replan when something goes wrong
Demo Mastermind
Demo Replanning
Search Problems
A search problem consists of: A state space Search Problems For each state, a set AcAons(s) of allowable acaons A transiaon model Result(s,a) {N, E} N 1 A step cost funcaon c(s,a,s ) E 1 A start state and a goal test A soluaon is a sequence of acaons (a plan) which transforms the start state to a goal state
Search Problems Are Models
Example: Travelling in Romania 71 Oradea Neamt State space: CiAes Arad 118 75 Zerind 140 Timisoara 111 70 75 Drobeta 151 Sibiu 99 Fagaras 80 Rimnicu Vilcea Lugoj Mehadia 120 Pitesti 211 97 146 101 85 138 Bucharest 90 Craiova Giurgiu 87 Iasi 92 142 98 Urziceni Vaslui Hirsova 86 Eforie AcAons: Go to adjacent city TransiAon model Result(A, Go(B)) = B Step cost Distance along road link Start state: Arad Goal test: Is state == Bucharest? SoluAon?
What s in a State Space? The real world state includes every last detail of the environment A search state abstracts away details not needed to solve the problem Problem: Pathing State representaaon: (x,y) locaaon AcAons: NSEW TransiAon model: update locaaon Goal test: is (x,y)=end MN states Problem: Eat- All- Dots State representaaon: {(x,y), dot booleans} AcAons: NSEW TransiAon model: update locaaon and possibly a dot boolean Goal test: dots all false MN2 MN states
Quiz: Safe Passage Problem: eat all dots while keeping the ghosts perma- scared What does the state representaaon have to specify? (agent posiaon, dot booleans, power pellet booleans, remaining scared Ame)
State Space Graphs and Search Trees
State Space Graphs State space graph: A mathemaacal representaaon of a search problem Nodes are (abstracted) world configuraaons Arcs represent transiaons resulang from acaons The goal test is a set of goal nodes (maybe only one) In a state space graph, each state occurs only once! We can rarely build this full graph in memory (it s too big), but it s a useful idea
More Examples 71 Oradea Neamt Arad 118 75 Zerind 140 Timisoara 151 Sibiu 99 Fagaras 80 Rimnicu Vilcea 87 Iasi 92 Vaslui 111 Lugoj 97 Pitesti 211 142 70 75 Drobeta Mehadia 120 146 101 85 138 Bucharest 90 Craiova Giurgiu 98 Urziceni Hirsova 86 Eforie
More Examples L R L R S S R R L R L R L L S S S S R L R L S S
Search Trees This is now / start N, 1.0 E, 1.0 Possible futures A search tree: A what if tree of plans and their outcomes The start state is the root node Children correspond to possible acaon outcomes Nodes show states, but correspond to PLANS that achieve those states For most problems, we can never actually build the whole tree
Quiz: State Space Graphs vs. Search Trees Consider this 4- state graph: How big is its search tree (from S)? S a a b S G b G G a b G a b G Important: Lots of repeated structure in the search tree!
Tree Search
Search Example: Romania 71 Oradea Neamt Arad 118 75 Zerind Timisoara 140 151 Sibiu 99 Fagaras 80 Rimnicu Vilcea 87 Iasi 92 Vaslui 111 Lugoj 97 Pitesti 211 142 70 75 Drobeta Mehadia 120 146 101 85 138 Bucharest 90 Craiova Giurgiu Urziceni 98 Hirsova 86 Eforie
Searching with a Search Tree Search: Expand out potenaal plans (tree nodes) Maintain a fronaer of paraal plans under consideraaon Try to expand as few tree nodes as possible
General Tree Search function TREE-SEARCH(problem) returns a solution, or failure initialize the frontier using the initial state of problem loop do if the frontier is empty then return failure choose a leaf node and remove it from the frontier if the node contains a goal state then return the corresponding solution expand the chosen node, adding the resulting nodes to the frontier Important ideas: FronAer Expansion ExploraAon strategy Main quesaon: which fronaer nodes to explore?
A Note on ImplementaAon Nodes have state, parent, acaon, path- cost A child of node by acaon a has state = result(node.state,a) 5 4 Node PARENT ACTION = Right PATH-COST = 6 parent acaon = node = a 6 7 1 3 8 2 STATE path- cost = node.path- cost + step- cost(node.state, a, self.state) Extract soluaon by tracing back parent pointers, collecang acaons
Depth- First Search
Depth- First Search S a b d p a c e p h f r q q c G a q e p h f r q q c G a S G d b p q c e h a f r q p h f d b a c e r Strategy: expand a deepest node first Implementa4on: Fron/er is a LIFO stack
Search Algorithm ProperAes
Search Algorithm ProperAes Complete: Guaranteed to find a soluaon if one exists? OpAmal: Guaranteed to find the least cost path? Time complexity? Space complexity? b 1 node b nodes Cartoon of search tree: b is the branching factor m tiers b 2 nodes m is the maximum depth soluaons at various depths b m nodes Number of nodes in enare tree? 1 + b + b 2 +. b m = O(b m )
Depth- First Search (DFS) ProperAes What nodes does DFS expand? Some le- prefix of the tree. Could process the whole tree! If m is finite, takes Ame O(b m ) b 1 node b nodes b 2 nodes How much space does the fronaer take? Only has siblings on path to root, so O(bm) m tiers Is it complete? m could be infinite, so only if we prevent cycles (more later) b m nodes Is it opamal? No, it finds the le-most soluaon, regardless of depth or cost
Breadth- First Search
Breadth- First Search Strategy: expand a shallowest node first Implementa4on: Fron/er is a FIFO queue S b p a d q c h e r f G S Search Tiers b a d c a h e r p h q e r f p q p q f q c G q c G a a
Breadth- First Search (BFS) ProperAes What nodes does BFS expand? Processes all nodes above shallowest soluaon Let depth of shallowest soluaon be s Search takes Ame O(b s ) s tiers b 1 node b nodes b 2 nodes How much space does the fronaer take? b s nodes Has roughly the last Aer, so O(b s ) Is it complete? s must be finite if a soluaon exists, so yes! b m nodes Is it opamal? Only if costs are all 1 (more on costs later)
Quiz: DFS vs BFS
Quiz: DFS vs BFS When will BFS outperform DFS? When will DFS outperform BFS? [Demo: dfs/bfs maze water (L2D6)]
Video of Demo Maze Water DFS/BFS (part 1)
Video of Demo Maze Water DFS/BFS (part 2)
IteraAve Deepening Idea: get DFS s space advantage with BFS s Ame / shallow- soluaon advantages Run a DFS with depth limit 1. If no soluaon Run a DFS with depth limit 2. If no soluaon Run a DFS with depth limit 3... b Isn t that wastefully redundant? Generally most work happens in the lowest level searched, so not so bad!
Finding a Least- Cost Path b 2 3 1 a d 2 c 3 8 2 e 9 8 2 GOAL 2 f START 1 p 4 15 q 4 h r 2 BFS finds the shortest path in terms of number of acaons. It does not find the least- cost path. We will now cover a similar algorithm which does find the least- cost path.
Uniform Cost Search
Uniform Cost Search Strategy: expand a cheapest node first: Frontier is a priority queue (priority: cumulative cost) b 3 S 1 2 p 1 a d 15 8 9 q 2 c h e 8 2 r f G 2 1 S 0 d 3 e 9 p 1 b 4 c 11 e 5 h 17 r 11 q 16 Cost contours a 6 a h 13 r 7 p q f p q f 8 q c G q 11 c G a 10 a
Uniform Cost Search (UCS) ProperAes What nodes does UCS expand? Processes all nodes with cost less than cheapest soluaon! If that soluaon costs C* and arcs cost at least ε, then the effecave depth is roughly C*/ε Takes Ame O(b C*/ε ) (exponenaal in effecave depth) C*/ε tiers b c 1 c 2 c 3 How much space does the fronaer take? Has roughly the last Aer, so O(b C*/ε ) Is it complete? Assuming best soluaon has a finite cost and minimum arc cost is posiave, yes! Is it opamal? Yes! (Proof next lecture via A*)
Uniform Cost Issues Remember: UCS explores increasing cost contours The good: UCS is complete and opamal! c 1 c 2 c 3 The bad: Explores opaons in every direcaon No informaaon about goal locaaon Start Goal We ll fix that soon!
Video of Demo Empty UCS
Video of Demo Maze with Deep/Shallow Water - - - DFS, BFS, or UCS? (part 1)
Video of Demo Maze with Deep/Shallow Water - - - DFS, BFS, or UCS? (part 2)
Video of Demo Maze with Deep/Shallow Water - - - DFS, BFS, or UCS? (part 3)
Tree Search vs Graph Search
Tree Search: Extra Work! Failure to detect repeated states can cause exponenaally more work. State Graph Search Tree O(2 m ) O(m)
Tree Search: Extra Work! Failure to detect repeated states can cause exponenaally more work. State Graph Search Tree O(2m 2 ) O(4 m ) m=20: 800 states 1,099,511,627,776 tree nodes
General Tree Search function TREE-SEARCH(problem) returns a solution, or failure initialize the frontier using the initial state of problem loop do if the frontier is empty then return failure choose a leaf node and remove it from the frontier if the node contains a goal state then return the corresponding solution expand the chosen node, adding each child to the frontier
General Graph Search function GRAPH-SEARCH(problem) returns a solution, or failure initialize the frontier using the initial state of problem initialize the explored set to be empty loop do if the frontier is empty then return failure choose a leaf node and remove it from the frontier if the node contains a goal state then return the corresponding solution add the node to the explored set expand the chosen node, adding each child to the frontier but only if the child is not already in the frontier or explored set Theorem: each state appears at most once in the search tree constructed
Tree Search vs Graph Search Graph search Avoids infinite loops: m is finite in a finite state space Eliminates exponenaally many redundant paths Requires memory proporaonal to its runame!
Search Gone Wrong?