Search Tamara Berg CS 560 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana Lazebnik, Percy Liang, Luke Zettlemoyer Course Information • • • • Instructor: Tamara Berg (tlberg@cs.unc.edu) Office Hours: FB 236, Mon/Wed 11:25-12:25pm Course website: http://tamaraberg.com/teaching/Fall_15/ Course mailing list: comp560@cs.unc.edu • TA: Patrick (Ric) Poirson TA office hours: SN 109, Tues/Thurs 4-5pm • Announcements, readings, schedule, etc, will all be posted to the course webpage. Schedule may be modified as needed over the semester. Check frequently! Announcements for today • HW1 will be released on the course website later today, due Sept 10, 11:59pm. – Start early! Recall from last class Search problem components • Initial state • Actions • Transition model Initial state – What state results from performing a given action in a given state? • Goal state • Path cost – Assume that it is a sum of nonnegative step costs Goal state • The optimal solution is the sequence of actions that gives the lowest path cost for reaching the goal Example: Romania • On vacation in Romania; currently in Arad • Flight leaves tomorrow from Bucharest • Initial state – Arad • Actions – Go from one city to another • Transition model – If you go from city A to city B, you end up in city B • Goal state – Bucharest • Path cost – Sum of edge costs (total distance traveled) State space • The initial state, actions, and transition model define the state space of the problem – The set of all states reachable from initial state by any sequence of actions – Can be represented as a directed graph where the nodes are states and links between nodes are actions Vacuum world state space graph Search • Given: – – – – – Initial state Actions Transition model Goal state Path cost • How do we find the optimal solution? – How about building the state space graph and then using Dijkstra’s shortest path algorithm? • Complexity of Dijkstra’s is O(E + V log V), where V is the size of the state space • The state space may be huge! Search: Basic idea • Let’s begin at the start state and expand it by making a list of all possible successor states • Maintain a frontier – the set of all leaf nodes available for expansion at any point • At each step, pick a state from the frontier to expand • Keep going until you reach a goal state or there are no more states to explore. • Try to expand as few states as possible Tree Search Algorithm Outline • Initialize the frontier using the start state • While the frontier is not empty – Choose a frontier node to expand according to search strategy and take it off the frontier – If the node contains the goal state, return solution – Else expand the node and add its children to the frontier Tree search example Start: Arad Goal: Bucharest Tree search example Start: Arad Goal: Bucharest Tree search example Start: Arad Goal: Bucharest Tree search example Start: Arad Goal: Bucharest Tree search example Start: Arad Goal: Bucharest Tree search example Start: Arad Goal: Bucharest Tree search example Start: Arad Goal: Bucharest Handling repeated states • Initialize the frontier using the starting state • While the frontier is not empty – Choose a frontier node to expand according to search strategy and take it off the frontier – If the node contains the goal state, return solution – Else expand the node and add its children to the frontier • To handle repeated states: – Keep an explored set; which remembers every expanded node – Every time you expand a node, add that state to the explored set; do not put explored states on the frontier again – Every time you add a node to the frontier, check whether it already exists in the frontier with a higher path cost, and if yes, replace that node with the new one Search without repeated states Start: Arad Goal: Bucharest Search without repeated states Start: Arad Goal: Bucharest Search without repeated states Start: Arad Goal: Bucharest Search without repeated states Start: Arad Goal: Bucharest Search without repeated states Start: Arad Goal: Bucharest Search without repeated states Start: Arad Goal: Bucharest Search without repeated states Start: Arad Goal: Bucharest Searching • Initialize the frontier using the starting state • While the frontier is not empty – Choose a frontier node to expand according to search strategy and take it off the frontier – If the node contains the goal state, return solution – Else expand the node and add its children to the frontier • To handle repeated states: – Keep an explored set; which remembers every expanded node – Every time you expand a node, add that state to the explored set; do not put explored states on the frontier again – Every time you add a node to the frontier, check whether it already exists in the frontier with a higher path cost, and if yes, replace that node with the new one Remaining question: What should our search strategy be, ie how do we choose which frontier node to expand? Uninformed search strategies • A search strategy is defined by picking the order of node expansion • Uninformed search strategies use only the information available in the problem definition – Breadth-first search – Depth-first search – Iterative deepening search – Uniform-cost search Informed search strategies • Idea: give the algorithm “hints” about the desirability of different states – Use an evaluation function to rank nodes and select the most promising one for expansion • Greedy best-first search • A* search Uninformed search Breadth-first search • Expand shallowest node in the frontier Example state space graph for a tiny search problem Breadth-first search • Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a, h,r,p,q,f,p,q,f,q,c,G) Breadth-first search • Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a, h,r,p,q,f,p,q,f,q,c,G) Breadth-first search • Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a, h,r,p,q,f,p,q,f,q,c,G) Breadth-first search • Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a, h,r,p,q,f,p,q,f,q,c,G) Breadth-first search • Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a, h,r,p,q,f,p,q,f,q,c,G) Breadth-first search • Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a, h,r,p,q,f,p,q,f,q,c,G) Breadth-first search • Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a, h,r,p,q,f,p,q,f,q,c,G) Breadth-first search • Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a, h,r,p,q,f,p,q,f,q,c,G) Breadth-first search • Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a, h,r,p,q,f,p,q,f,q,c,G) Breadth-first search • Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a, h,r,p,q,f,p,q,f,q,c,G) Breadth-first search • Expand shallowest node in the frontier • Implementation: frontier is a FIFO queue Example state space graph for a tiny search problem Example from P. Abbeel and D. Klein Depth-first search • Expand deepest node in the frontier Depth-first search • Expansion order: (S,d,b,a,c,a,e,h,p,q, q, r,f,c,a,G) Depth-first search • Expansion order: (S,d,b,a,c,a,e,h,p,q, q, r,f,c,a,G) Depth-first search • Expansion order: (S,d,b,a,c,a,e,h,p,q, q, r,f,c,a,G) Depth-first search • Expansion order: (S,d,b,a,c,a,e,h,p,q, q, r,f,c,a,G) Depth-first search • Expansion order: (S,d,b,a,c,a,e,h,p,q, q, r,f,c,a,G) Depth-first search • Expansion order: (S,d,b,a,c,a,e,h,p,q, q, r,f,c,a,G) Depth-first search • Expansion order: (S,d,b,a,c,a,e,h,p,q, q,r,f,c,a,G) Depth-first search • Expand deepest unexpanded node • Implementation: frontier is a LIFO queue http://xkcd.com/761/ Analysis of search strategies • Strategies are evaluated along the following criteria: – Completeness • does it always find a solution if one exists? – Optimality • does it always find a least-cost solution? – Time complexity • how long does it take to find a solution? – Space complexity • maximum number of nodes in memory • Time and space complexity are measured in terms of – b: maximum branching factor of the search tree – d: depth of the optimal solution – m: maximum length of any path in the state space (may be infinite) Properties of breadth-first search • Complete? Yes (if branching factor b is finite) • Optimal? Not generally – the shallowest goal node is not necessarily the optimal one Yes – if all actions have same cost • Time? Number of nodes in a b-ary tree of depth d: O(bd) (d is the depth of the optimal solution) • Space? O(bd) BFS Depth Nodes Time Memory 2 110 0.11 ms 107 kilobytes 4 11,110 11 ms 10.6 megabytes 6 10^6 1.1 s 1 gigabyte 8 10^8 2 min 103 gigabytes 10 10^10 3 hrs 10 terabytes 12 10^12 13 days 1 petabyte 14 10^14 3.5 years 99 petabytes 16 10^16 350 years 10 exabytes Time and Space requirements for BFS with b=10; 1 million nodes/second; 1000 bytes/node Properties of depth-first search • Complete? Fails in infinite-depth spaces, spaces with loops Modify to avoid repeated states along path complete in finite spaces • Optimal? No – returns the first solution it finds • Time? May generate all of the O(bm) nodes, m=max depth of any node Terrible if m is much larger than d • Space? O(bm), i.e., linear space! Iterative deepening search • Use DFS as a subroutine 1. Check the root 2. Do a DFS with depth limit 1 3. If there is no path of length 1, do a DFS search with depth limit 2 4. If there is no path of length 2, do a DFS with depth limit 3. 5. And so on… Iterative deepening search Iterative deepening search Iterative deepening search Iterative deepening search Properties of iterative deepening search • Complete? Yes • Optimal? Not generally – the shallowest goal node is not necessarily the optimal one Yes – if all actions have same cost • Time? (d+1)b0 + d b1 + (d-1)b2 + … + bd = O(bd) • Space? O(bd)