Planning and search Lecture 1: Introduction and Revision of Search Lecture 1: Introduction and Revision of Search 1 Contact and web page ♦ Lecturer: Natasha Alechina ♦ email: nza@cs.nott.ac.uk ♦ web page: http://www.cs.nott.ac.uk/∼nza/G52PAS ♦ Textbook: Stuart Russell and Peter Norvig. Artificial Intelligence: A Modern Approach, 3rd edition ♦ Slides: mostly by Stuart Russell (big thanks!) Lecture 1: Introduction and Revision of Search 2 Outline ♦ What this module is about ♦ Where search and planning fit in AI ♦ Reminder of uninformed search algorithms ♦ Definition of planning ♦ Plan of the module ♦ What to read for next week Lecture 1: Introduction and Revision of Search 3 Problem-solving agents Simple problem-solving agent: function Simple-Problem-Solving-Agent( percept) returns an action static: seq, an action sequence, initially empty state, some description of the current world state goal, a goal, initially null problem, a problem formulation state ← Update-State(state, percept) if seq is empty then goal ← Formulate-Goal(state) problem ← Formulate-Problem(state, goal) seq ← Search( problem) if seq=failure then return a null action action ← First(seq) seq ← Rest(seq) return action Lecture 1: Introduction and Revision of Search 4 Example of a search problem: 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 Lecture 1: Introduction and Revision of Search 5 Example: Romania Oradea 71 75 Neamt Zerind 87 151 Iasi Arad 140 Sibiu 92 99 Fagaras 118 Timisoara 111 Vaslui 80 Rimnicu Vilcea Lugoj Pitesti 97 142 211 70 98 Mehadia 75 Dobreta 146 85 101 86 138 Bucharest 120 Craiova Hirsova Urziceni 90 Giurgiu Eforie Lecture 1: Introduction and Revision of Search 6 Single-state problem formulation A problem is defined by four items: initial state e.g., “at Arad” successor function S(x) = set of action–state pairs e.g., S(Arad) = {hDrive(Arad, Zerind), Zerindi, . . .} goal test, can be explicit, e.g., x = “at Bucharest” implicit, e.g., HasAirport(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 Lecture 1: Introduction and Revision of Search 7 Selecting a state space Real world is absurdly complex ⇒ state space must be abstracted for problem solving (Abstract) state = set of real states (Abstract) action = complex combination of real actions e.g., “Drive(Arad, 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! Lecture 1: Introduction and Revision of Search 8 Example: The 8-puzzle 7 2 5 8 3 Start State 4 5 1 2 3 6 4 5 6 1 7 8 Goal State states?? actions?? goal test?? path cost?? Lecture 1: Introduction and Revision of Search 9 Example: The 8-puzzle 7 2 5 8 3 Start State 4 5 1 2 3 6 4 5 6 1 7 8 Goal State states??: integer locations of tiles (ignore intermediate positions) actions?? goal test?? path cost?? Lecture 1: Introduction and Revision of Search 10 Example: The 8-puzzle 7 2 5 8 3 Start State 4 5 1 2 3 6 4 5 6 1 7 8 Goal State states??: integer locations of tiles (ignore intermediate positions) actions??: move blank left, right, up, down (ignore unjamming etc.) goal test?? path cost?? Lecture 1: Introduction and Revision of Search 11 Example: The 8-puzzle 7 2 5 8 3 Start State 4 5 1 2 3 6 4 5 6 1 7 8 Goal State states??: integer locations of tiles (ignore intermediate positions) actions??: move blank left, right, up, down (ignore unjamming etc.) goal test??: = goal state (given) path cost?? Lecture 1: Introduction and Revision of Search 12 Example: The 8-puzzle 7 2 5 8 3 Start State 4 5 1 2 3 6 4 5 6 1 7 8 Goal State states??: integer locations of tiles (ignore intermediate positions) actions??: move blank left, right, up, down (ignore unjamming etc.) goal test??: = goal state (given) path cost??: 1 per move Lecture 1: Introduction and Revision of Search 13 Tree search algorithms Basic idea: offline, simulated exploration of state space by generating successors of already-explored states (a.k.a. expanding states) function Tree-Search( problem, strategy) returns a solution, or failure initialize the search tree using the initial state of problem loop do if there are no candidates for expansion then return failure choose a leaf node for expansion according to strategy if the node contains a goal state then return the corresponding solution else expand the node and add the resulting nodes to the search tree end Lecture 1: Introduction and Revision of Search 14 Tree search example Arad Timisoara Sibiu Arad Fagaras Oradea Rimnicu Vilcea Arad Zerind Lugoj Arad Oradea Lecture 1: Introduction and Revision of Search 15 Tree search example Arad Timisoara Sibiu Arad Fagaras Oradea Rimnicu Vilcea Arad Zerind Lugoj Arad Oradea Lecture 1: Introduction and Revision of Search 16 Tree search example Arad Timisoara Sibiu Arad Fagaras Oradea Rimnicu Vilcea Arad Zerind Lugoj Arad Oradea Lecture 1: Introduction and Revision of Search 17 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 parent, children, depth, path cost g(x) States do not have parents, children, depth, or path cost! parent, action State Node 5 4 6 1 88 7 3 22 depth = 6 g=6 state The Expand function creates new nodes, filling in the various fields and using the SuccessorFn of the problem to create the corresponding states. Lecture 1: Introduction and Revision of Search 18 Implementation: general tree search Lecture 1: Introduction and Revision of Search 19 function Tree-Search( problem, fringe) returns a solution, or failure fringe ← Insert(Make-Node(Initial-State[problem]), fringe) loop do if fringe is empty then return failure node ← Remove-Front(fringe) if Goal-Test(problem, State(node)) then return node fringe ← InsertAll(Expand(node, problem), fringe) function Expand( node, problem) returns a set of nodes successors ← the empty set for each action, result in Successor-Fn(problem, State[node]) do s ← a new Node Parent-Node[s] ← node; Action[s] ← action; State[s] ← result Path-Cost[s] ← Path-Cost[node] + Step-Cost(State[node], action, result) Depth[s] ← Depth[node] + 1 add s to successors return successors Lecture 1: Introduction and Revision of Search 20 Search strategies A strategy is defined by picking the order of node expansion Strategies are evaluated along the following dimensions: completeness—does it always find a solution if one exists? time complexity—number of nodes generated/expanded 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 ∞) Lecture 1: Introduction and Revision of Search 21 Uninformed search strategies Uninformed 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 Lecture 1: Introduction and Revision of Search 22 Breadth-first search Expand shallowest unexpanded node Implementation: fringe is a FIFO queue, i.e., new successors go at end A B D C E F G Lecture 1: Introduction and Revision of Search 23 Breadth-first search Expand shallowest unexpanded node Implementation: fringe is a FIFO queue, i.e., new successors go at end A B D C E F G Lecture 1: Introduction and Revision of Search 24 Breadth-first search Expand shallowest unexpanded node Implementation: fringe is a FIFO queue, i.e., new successors go at end A B D C E F G Lecture 1: Introduction and Revision of Search 25 Breadth-first search Expand shallowest unexpanded node Implementation: fringe is a FIFO queue, i.e., new successors go at end A B D C E F G Lecture 1: Introduction and Revision of Search 26 Uniform-cost search Expand least-cost unexpanded node Implementation: fringe = queue ordered by path cost, lowest first Equivalent to breadth-first if step costs all equal Lecture 1: Introduction and Revision of Search 27 Depth-first search Expand deepest unexpanded node Implementation: fringe = LIFO queue, i.e., put successors at front A B C D H E I J F K L G M N O Lecture 1: Introduction and Revision of Search 28 Depth-first search Expand deepest unexpanded node Implementation: fringe = LIFO queue, i.e., put successors at front A B C D H E I J F K L G M N O Lecture 1: Introduction and Revision of Search 29 Depth-first search Expand deepest unexpanded node Implementation: fringe = LIFO queue, i.e., put successors at front A B C D H E I J F K L G M N O Lecture 1: Introduction and Revision of Search 30 Depth-first search Expand deepest unexpanded node Implementation: fringe = LIFO queue, i.e., put successors at front A B C D H E I J F K L G M N O Lecture 1: Introduction and Revision of Search 31 Depth-first search Expand deepest unexpanded node Implementation: fringe = LIFO queue, i.e., put successors at front A B C D H E I J F K L G M N O Lecture 1: Introduction and Revision of Search 32 Depth-first search Expand deepest unexpanded node Implementation: fringe = LIFO queue, i.e., put successors at front A B C D H E I J F K L G M N O Lecture 1: Introduction and Revision of Search 33 Depth-first search Expand deepest unexpanded node Implementation: fringe = LIFO queue, i.e., put successors at front A B C D H E I J F K L G M N O Lecture 1: Introduction and Revision of Search 34 Depth-first search Expand deepest unexpanded node Implementation: fringe = LIFO queue, i.e., put successors at front A B C D H E I J F K L G M N O Lecture 1: Introduction and Revision of Search 35 Depth-first search Expand deepest unexpanded node Implementation: fringe = LIFO queue, i.e., put successors at front A B C D H E I J F K L G M N O Lecture 1: Introduction and Revision of Search 36 Depth-first search Expand deepest unexpanded node Implementation: fringe = LIFO queue, i.e., put successors at front A B C D H E I J F K L G M N O Lecture 1: Introduction and Revision of Search 37 Depth-first search Expand deepest unexpanded node Implementation: fringe = LIFO queue, i.e., put successors at front A B C D H E I J F K L G M N O Lecture 1: Introduction and Revision of Search 38 Depth-first search Expand deepest unexpanded node Implementation: fringe = LIFO queue, i.e., put successors at front A B C D H E I J F K L G M N O Lecture 1: Introduction and Revision of Search 39 Depth-limited search Sometimes DFS does not terminate (infinite branch) Fix: introduce a depth limit l Backtrack when reach l (as if found a leaf node) Lecture 1: Introduction and Revision of Search 40 Iterative deepening search Do depth-limited search with l = 1, 2, 3, . . . Lecture 1: Introduction and Revision of Search 41 Iterative deepening search l = 0 Limit = 0 A A Lecture 1: Introduction and Revision of Search 42 Iterative deepening search l = 1 Limit = 1 A B A C B A C B A C B Lecture 1: Introduction and Revision of Search C 43 Iterative deepening search l = 2 Limit = 2 A A B D C E F B G D C E A C E F F G D C E D C E F B G D C E A B G A B A B D A F D C E G A B G F F B G D C E F Lecture 1: Introduction and Revision of Search G 44 Iterative deepening search l = 3 Limit = 3 A A B C D H E J I B F K L N C D G M O H E I J K C E I J G M L N D O H E I C E I J I J K J L K L G G M N O H J N M O H I J F K O H E I J L G L G M N O H J N M O H E I J F L K G N M B F K O O A E I N C D C D M L B F K G A B F K N E A E I M L C D C D C D G B F B F K H F A B H O E C A D D B A B F K N C A B H G M L A B F A D A L G M N C D O H E I J F K L G M Lecture 1: Introduction and Revision of Search N 45 O Summary of basic search algorithms Criterion Complete? Time Space Optimal? Breadth- Uniform- DepthFirst Cost First Yesa O(bd) O(bd) Yesc DepthLimited Yesa,b No No (Yes, if l ≥ d) ∗ O(b⌈C /ǫ⌉) O(bm) O(bl ) ∗ O(b⌈C /ǫ⌉) O(bm) O(bl) Yes No No Iterative Deepening Yesa O(bd) O(bd) Yesc C ∗ is the cost of the optimal solution, ǫ is the minimal cost of a step, b the branching factor, d the depth of the shallowest solution, m the maximum depth of the search tree, l the depth limit. a complete if b is finite; b complete if step costs ≥ ǫ, c optimal if step costs are identical Lecture 1: Introduction and Revision of Search 46 Planning Planning: devising a plan of action to achieve the goal (for example: buy milk, bananas, and a cordless drill) Also talking about states of the world and actions, but more sophisticated representation States have structure (properties); actions have pre- and post-conditions. Action: Buy(x) Precondition: At(p), Sells(p, x) Effect: Have(x) At(p) Sells(p,x) Buy(x) Have(x) Lecture 1: Introduction and Revision of Search 47 Search vs. planning contd. Planning systems do the following: 1) open up action and goal representation to allow selection 2) divide-and-conquer by subgoaling 3) relax requirement for sequential construction of solutions Search States Data structures Actions Code Code Goal Plan Sequence from S0 Planning Logical sentences Preconditions/outcomes Logical sentence (conjunction) Constraints on actions Lecture 1: Introduction and Revision of Search 48 Plan of the module: search topics ♦ Another revision lecture on properties of uninformed search algorithms, heuristic search (A∗) ♦ Graph search, direction of search ♦ Local search (annealing, tabu, ...) ♦ Population-based methods (genetic algorithms...) ♦ Reducing search to SAT ♦ Search with non-determinism and partial observability ♦ Logical agents; first-order logic Lecture 1: Introduction and Revision of Search 49 Plan of the module: planning topics ♦ Situation calculus ♦ What is classical planning. Forward planning. ♦ Classical planning continued. Regression Planning ♦ Classical planning continued. Partial-Order Planning ♦ Classical planning continued. GraphPlan. ♦ Classical planning continued. SatPlan. ♦ Planning with time and resources ♦ HTN planning. ♦ Planning and acting in non-deterministic domains. Lecture 1: Introduction and Revision of Search 50 What to read for the next lecture Chapter 3 in Russell and Norvig (this is revision) Lecture 1: Introduction and Revision of Search 51