CS 8520: Artificial Intelligence Solving Problems by Searching Paula Matuszek Spring, 2013 Slides based on Hwee Tou Ng, aima.eecs.berkeley.edu/slides-ppt, which are in turn based on Russell, aima.eecs.berkeley.edu/slides-pdf. Diagrams are based on AIMA.
Problem-Solving Agents A goal-based agent is essentially solving a problem Given some state and some goal, Figure out how to get from the current state to the goal By taking some sequence of actions. The problem is defined as a state space and a goal. Solving the problem consists of searching the state space for the sequence of actions that leads to the goal. 2
Example: Romania On holiday in Romania; currently in Arad. Flight leaves tomorrow from Bucharest Formulate goal: be in Bucharest Formulate problem: states: various cities actions: drive between cities Find solution: sequence of cities, e.g., Arad, Sibiu, Fagaras, Bucharest 3
Example: Romania 4
Single-state problem formulation A problem is defined by five items: initial state e.g., "at Arad" actions or successor function S(x) = set of action state pairs e.g., S(Arad) = {<Arad to Zerind, Zerind>, } transition model: new state resulting from action goal test, can be explicit, e.g., x = "at Bucharest" implicit, e.g., Checkmate(x) path cost (additive) e.g., sum of distances, number of actions executed, etc. c(x,a,y) is the step cost, assumed to be 0 A solution is a sequence of actions leading from the initial state to a goal state 5
Selecting a state space Real world is absurdly complex therefore state space must be abstracted for problem solving (Abstract) state = set of real states (Abstract) action = complex combination of real actions e.g., "Arad to Zerind" represents a complex set of possible routes, detours, rest stops, etc. For guaranteed realizability, any real state "in Arad must get to some real state "in Zerind" (Abstract) solution = set of real paths that are solutions in the real world Each abstract action should be "easier" than the original problem 6
Vacuum world state space graph States? Actions? Transition Models? Goal Test? Path Cost? 7
Vacuum world state space graph states? integer dirt and robot location actions? Left, Right, Suck transition models? In A, in B, clean goal test? no dirt at all locations path cost? 1 per action 8
Example: The 8-puzzle states? actions? transition model? goal test? path cost? 9
Example: The 8-puzzle states? locations of tiles actions? move blank left, right, up, down transition model? square with tile moved. goal test? = goal state (given) path cost? 1 per move [Note: optimal solution of n-puzzle family is NP-hard] 10
Search The basic concept of search views the state space as a search tree Initial state is the root node Each possible action leads to a new node defined by the transition model Some nodes are identified as goals Search is the process of expanding some portion of the tree in some order until we get to a goal node The strategy we use to choose the order to expand nodes defines the type of search 11
Tree search algorithms Basic idea: offline, simulated exploration of state space by generating successors of already-explored states (a.k.a.~expanding states) 12
Tree search example 13
Tree search example 14
Tree search example 15
Implementation: general tree search 16
Implementation: states vs. nodes A state is a (representation of) a physical configuration A node is a data structure constituting part of a search tree includes state, parent node, action, path cost g(x), depth The Expand function creates new nodes, filling in the various fields and using the SuccessorFn of the problem to create the corresponding states. 17
Search strategies A search strategy is defined by picking the order of node expansion. (e.g., breadth-first, depth-first) Strategies are evaluated along the following dimensions: completeness: does it always find a solution if one exists? time complexity: number of nodes generated space complexity: maximum number of nodes in memory optimality: does it always find a least-cost solution? Time and space complexity are measured in terms of b: maximum branching factor of the search tree d: depth of the least-cost solution m: maximum depth of the state space (may be infinite) 18
Summary Problem-solving agents search through a problem or state space for an acceptable solution. A state space can be specified by An initial state Actions A transition model or successor function S(x) describing the results of actions A goal test or goal state A path cost A solution is a sequence of actions leading from the initial state to a goal state The formalization of a good state space is hard, and critical to success. It must abstract the essence of the problem so that It is easier than the real-world problem. A solution can be found. The solution maps back to the real-world problem and solves it. 19
Uninformed search strategies Uninformed search strategies use only the information available in the problem definition Breadth-first search Uniform-cost search Depth-first search Depth-limited search Iterative deepening search 20
Implementation: general tree search 21
Breadth-first search Expand shallowest unexpanded node Implementation: fringe is a FIFO queue, i.e., new successors go at end 22
Breadth-first search Expand shallowest unexpanded node Implementation: fringe is a FIFO queue, i.e., new successors go at end 23
Breadth-first search Expand shallowest unexpanded node Implementation: fringe is a FIFO queue, i.e., new successors go at end 24
Breadth-first search Expand shallowest unexpanded node Implementation: fringe is a FIFO queue, i.e., new successors go at end 25
Properties of breadth-first search Complete? Yes (if b is finite) Time? 1+b+b2+b3+ +bd + b(bd-1) = O(bd+1) Space? O(bd+1) (keeps every node in memory) Optimal? Yes (if cost = 1 per step) Space is the bigger problem (more than time) 26
Uniform-cost search Expand least-cost unexpanded node Implementation: fringe = queue ordered by path cost Equivalent to breadth-first if step costs all equal Complete? Yes, if step cost >= epsilon (otherwise can loop) Time and space? O(bceiling(C*/ epsilon)) where C* is the cost of the optimal solution and epsilon is the smallest step cost. Can be much worse than breadth-first if many small steps not on optimal path Optimal? Yes nodes expanded in increasing order of g(n) 27
Uniform Cost Search 28
Uniform Cost Search 28
Uniform Cost Search 28
Uniform Cost Search 28
Uniform Cost Search 28
Uniform Cost Search 28
Uniform Cost Search 28
Depth-first search Expand deepest unexpanded node Implementation: fringe = LIFO queue, i.e., put successors at front 29
Depth-first search Expand deepest unexpanded node Implementation: fringe = LIFO queue, i.e., put successors at front 30
Depth-first search Expand deepest unexpanded node Implementation: fringe = LIFO queue, i.e., put successors at front 31
Depth-first search Expand deepest unexpanded node Implementation: fringe = LIFO queue, i.e., put successors at front 32
Depth-first search Expand deepest unexpanded node Implementation: fringe = LIFO queue, i.e., put successors at front 33
Depth-first search Expand deepest unexpanded node Implementation: fringe = LIFO queue, i.e., put successors at front 34
Properties of depth-first search Complete? No: fails in infinite-depth spaces, spaces with loops Modify to avoid repeated states along path then complete in finite spaces Time? O(bm): terrible if m is much larger than d but if solutions are dense, may be much faster than breadth-first Space? O(bm), i.e., linear space! Optimal? No 35
Depth-limited search = depth-first search with depth limit l Nodes at depth l have no successors Solves problem of infinite depth Incomplete Recursive implementation: 36
Iterative deepening search Repeated Depth-Limited search, incrementing limit l until a solution is found or failure. Repeats earlier steps at each new level, so inefficient -- but never more than doubles cost No longer incomplete 37
Iterative deepening search l =0 38
Iterative deepening search l =1 39
Iterative deepening search l =2 40
Iterative deepening search l =3 41
Properties of iterative deepening Complete? Yes Time? (d+1)b0 + d b1 + (d-1)b2 + + bd = O(bd) Space? O(bd) Optimal? Yes, if step cost = 1 42
Summary of Algorithms for Uninformed Search Criterion Uniform Cost Depth First Breadthfirst Depth- Limited Iterative Deepening Complete? Yes Yes No No Yes Time? O(b d ) O(b (ceilingc*/ε) ) O(b m ) O(b l ) O(b d ) Space? O(b d ) O(b (ceilingc*/ε) ) O(bm) O(bl) O(bd) Optimal? Yes Yes No No Yes Where: b is branching factor d is depth of shallowest solution, m is max depth of search tree C* is cost of optimal solution ε is an arbitrarily small number 43
A Caution: Repeated States Failure to detect repeated states can turn a linear problem into an exponential one, or even an infinite one. For example: 8-puzzle Simple repeat -- empty square simply moves back and forth More complex repeats also possible. Save list of expanded states -- the closed list. Add new state to fringe only if it's not in closed list. 44
Summary: Uninformed Search Problem formulation usually requires abstracting away real-world details to define a state space that can feasibly be explored There exists a variety of uninformed search strategies Search strategies can be evaluated by whether they find a solution if one exists: completeness how long they take: time complexity how much memory they take: space complexity whether they find the best solution: optimality Iterative deepening search uses linear space and not much more time than other algorithms: usual choice 45