Search Algorithms
Total Page:16
File Type:pdf, Size:1020Kb
Search Algorithms 3 AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 1 3 Search Algorithms 3.1 Problem-solving agents 3.2 Basic search algorithms 3.3 Heuristic search 3.4 Local search Hill-climbing Simulated annealing∗ Genetic algorithms∗ • • • 3.5 Online search∗ 3.6 Adversarial search 3.7 Metaheuristic∗ ∗ may be learnt as extended knowledge AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 2 Problem-solving agents Problem-solving agents: finding sequences of actions that lead to desirable states (goal-based) State: some description of the current world states – abstracted for problem solving as state space Goal: a set of world states Action: transition between world states Search: the algorithm takes a problem as input and returns a solution in the form of an action sequence The agent can execute the actions in the solution AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 3 Problem-solving agents def Simple-Problem-Solving-Agent( p) s, an action sequence, initially empty state, some description of the current world state g, a goal, initially null problem, a problem formulation state Update-State(state, p) ← if s is empty then g Formulate-Goal(state) ← problem Formulate-Problem(state,g) ← s Search( problem) ← if s=failure then return a null action action First(s, state) ← s Rest(s, state) ← return an action Note: offline vs. online problem solving AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 4 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 AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 5 Example: Romania Oradea 71 Neamt Zerind 87 75 151 Iasi Arad 140 92 Sibiu 99 Fagaras 118 Vaslui 80 Timisoara Rimnicu Vilcea 142 111 Pitesti 211 Lugoj 97 70 98 85 Hirsova Mehadia 146 101 Urziceni 75 138 86 Bucharest Dobreta 120 90 Craiova Eforie Giurgiu AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 6 Problem types Deterministic, fully observable = single-state problem Agent knows exactly which⇒ state it will be in; solution is a sequence Non-observable = conformant problem Agent may⇒ have no idea where it is; solution (if any) is a sequence Nondeterministic and/or partially observable = contingency prob- lem ⇒ percepts provide new information about current state solution is a contingent plan or a policy often interleave search, execution Unknown state space = exploration problem (“online”) ⇒ AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 7 Example: vacuum world Single-state, start in #5. Solution?? 1 2 3 4 5 6 7 8 AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 8 Example: vacuum world Single-state, start in #5. Solution?? [Right, Suck] 1 2 Conformant, start in 1, 2, 3, 4, 5, 6, 7, 8 3 4 e.g., Right goes{ to 2, 4, 6, 8 }. Solution?? { } 5 6 7 8 AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 9 Example: vacuum world Single-state, start in #5. Solution?? [Right, Suck] 1 2 Conformant, start in 1, 2, 3, 4, 5, 6, 7, 8 3 4 e.g., Right goes{ to 2, 4, 6, 8 }. Solution?? { } [Right,Suck,Left,Suck] 5 6 Contingency, start in #5 7 8 Murphy’s Law: Suck can dirty a clean carpet Local sensing: dirt, location only Solution?? AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 10 Example: vacuum world Single-state, start in #5. Solution?? [Right, Suck] 1 2 Conformant, start in 1, 2, 3, 4, 5, 6, 7, 8 3 4 e.g., Right goes{ to 2, 4, 6, 8 }. Solution?? { } [Right,Suck,Left,Suck] 5 6 Contingency, start in #5 7 8 Murphy’s Law: Suck can dirty a clean carpet Local sensing: dirt, location only. Solution?? [Right, if dirt then Suck] AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 11 Problem formulation A problem is defined formally by five components initial state that the agent starts – any state s S (set of states), the initial state S0 S e.g., In(Arad∈ ) (“at Arad”) ∈ actions: given a state s, Action(s) returns the set of actions that can be executed in s e.g., from the state In(Arad), the applicable actions are Go(Sibiu),Go(Timisoara),Go(Zerind) { } transition model: a function Result(s, a) (or Do(a, s)) that re- turns the state that results from doing action a in the state s; – also use the term successor to refer to any state reachable from a given state by a single action e.g., Result(In(Arad),Go(Zerind)) = In(Zerind) AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 12 Problem formulation goal test, can be explicit, e.g., x = In(Bucharest) implicit, e.g., NoDirt(x) cost (action or path): function that assigns a numeric cost to each path (of actions) e.g., sum of distances, number of actions executed, etc. c(s,a,s′) is the step cost, assumed to be 0 ≥ A solution is a sequence of actions – [a1, a2, , an] leading from the··· initial state to a goal state AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 13 Example: vacuum world state space graph R L R L S S R R L R L R L L SS S S R L R L S S states?? actions?? goal?? cost?? AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 14 Example: vacuum world state space graph R L R L S S R R L R L R L L SS S S R L R L S S states??: integer dirt and robot locations (ignore dirt amounts etc.) actions?? goal?? cost?? AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 15 Example: vacuum world state space graph R L R L S S R R L R L R L L SS S S R L R L S S states??: integer dirt and robot locations (ignore dirt amounts etc.) actions??: Left, Right, Suck, NoOp goal?? cost?? AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 16 Example: vacuum world state space graph R L R L S S R R L R L R L L SS S S R L R L S S states??: integer dirt and robot locations (ignore dirt amounts etc.) actions??: Left, Right, Suck, NoOp goal??: no dirt cost?? AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 17 Example: vacuum world state space graph R L R L S S R R L R L R L L SS S S R L R L S S states??: integer dirt and robot locations (ignore dirt amounts etc.) actions??: Left, Right, Suck, NoOp goal??: no dirt cost??: 1 per action (0 for NoOp) AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 18 Example: the 8-puzzle 5 4 514 2 3 6 1 88 68 84 7 33 22 7 6 25 Start State Goal State states?? actions?? goal?? cost?? AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 19 Example: the 8-puzzle 5 4 514 2 3 6 1 88 68 84 7 33 22 7 6 25 Start State Goal State states??: integer locations of tiles (ignore intermediate positions) actions?? goal?? cost?? AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 20 Example: the 8-puzzle 5 4 514 2 3 6 1 88 68 84 7 33 22 7 6 25 Start State Goal State states??: integer locations of tiles (ignore intermediate positions) actions??: move blank left, right, up, down (ignore unjamming etc.) goal?? cost?? AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 21 Example: the 8-puzzle 5 4 514 2 3 6 1 88 68 84 7 33 22 7 6 25 Start State Goal State states??: integer locations of tiles (ignore intermediate positions) actions??: move blank left, right, up, down (ignore unjamming etc.) goal??: = goal state (given) cost?? AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 22 Example: the 8-puzzle 5 4 514 2 3 6 1 88 68 84 7 33 22 7 6 25 Start State Goal State states??: integer locations of tiles (ignore intermediate positions) actions??: move blank left, right, up, down (ignore unjamming etc.) goal??: = goal state (given) cost??: 1 per move Note: optimal solution of n-Puzzle family is NP-hard AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 23 Example: robotic assembly P R R R R R states??: real-valued coordinates of robot joint angles parts of the object to be assembled actions??: continuous motions of robot joints goal??: complete assembly with no robot included cost??: time to execute AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 24 Basic (tree) search algorithms Simulated (offline) exploration of state space of a tree by generating successors of already-explored states def Tree-Search( problem) 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 by certain strategy if the node contains a goal state then return the corresponding solution expand the node and add the resulting nodes to the search tree Frontier: all the leaf nodes available for expansion at moment, which separates two regions of the state-space graph (tree) – an interior region where every state has been expanded – an exterior region of states that have not yet been reached AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 25 Example: tree search Arad Sibiu Timisoara Zerind Arad Fagaras Oradea Rimnicu Vilcea Arad Lugoj Arad Oradea AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 26 Example: tree search Arad Sibiu Timisoara Zerind Arad Fagaras Oradea Rimnicu Vilcea Arad Lugoj Arad Oradea AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 27 Example: tree search Arad Sibiu Timisoara Zerind Arad Fagaras Oradea Rimnicu Vilcea Arad Lugoj Arad Oradea Note: loopy path (repeated state) in the leftmost To avoid exploring redundant paths by using a data structure reached set – remembering every expanded node — newly generated nodes in the reached set can be discarded AI Slides (7e) c Lin Zuoquan@PKU 1998-2021 3 28 Implementation: states vs.