06/26/22 1 Solving problems by searching Course Teacher: Moona Kanwal
Mar 26, 2015
04/10/23 1
Solving problems by searching
Course Teacher: Moona Kanwal
04/10/23 2
Outline
Well-defined ProblemsWell-defined Problems Solutions (as a sequence of actions).Solutions (as a sequence of actions). ExamplesExamples Search TreesSearch Trees Uninformed Search AlgorithmsUninformed Search Algorithms
04/10/23 3
Problem-Solving
Goal formulationGoal formulation (final state): limits objectives
Problem formulationProblem formulation (set of states): actions+states
Search Search (solution): sequence of actions
ExecutionExecution (follows states in solution)
04/10/23 4
Problem types
Deterministic, fully observableDeterministic, fully observable single-state problem: initial state known, results of action known
Non-observableNon-observable sensor less problem (conformant problem): initial state not known, results of action known
Nondeterministic and/or partially observableNondeterministic and/or partially observable contingency problem: unknown states on some results of action
Unknown state spaceUnknown state space exploration problem: unknown states, unknown actions
04/10/23 5
Well Defined Problems
A problem is defined by four itemsA problem is defined by four items:
1.1. initial stateinitial state2.2. actions or successor functionactions or successor function S(x) = set of
action–state pairs
3.3. goal test or predicate goal test or predicate γ : S → {0, 1} is 1 iff goal state
Explicit: concrete goal implicit : abstract description of goal
path costpath cost (additive):g, assigns a numeric cost to a given path.
04/10/23 6
Well Defined Problems
State-Space State-Space An Initial State, s0 ∈ S. A Set of Operators (or Actions), {Ai|i = 1, 2, . . }
Formally, the state space is the set of states Formally, the state space is the set of states that can be reached by any sequence of that can be reached by any sequence of actions applied to the initial state, e.g.,actions applied to the initial state, e.g.,
S = {sS = {s00, . . . ,A, . . . ,Ai1i1ss00, . . . ,A, . . . ,Ai1i1AiAi22ss00, . . . ,A, . . . ,Ai1i1AAi2i2 ・・・・・・ AAikikss00, . . . }, . . . }
A path is a particular sequence of actions A path is a particular sequence of actions applied to the initial state.applied to the initial state.
04/10/23 7
Solutions
A solution is a sequence of actions A solution is a sequence of actions leading from the initial state to a leading from the initial state to a goal stategoal state
A solution is a path that satisfies A solution is a path that satisfies the goal test.the goal test.
An optimal solution is a solution An optimal solution is a solution with minimal cost.with minimal cost.
04/10/23 8
04/10/23 9
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
04/10/23 10
Example: Romania
04/10/23 11
04/10/23 12
Example: The 8-puzzle
states? actions? goal test? path cost?
04/10/23 13
Example: The 8-puzzle
states? locations of tileslocations of tiles actions? move blank left, right, up, downmove blank left, right, up, down goal test? = goal state (given)goal state (given) path cost?1 per move1 per move
04/10/23 14
04/10/23 15
Example: vacuum world
Single-state, Single-state, start in #5start in #5.
Solution?
04/10/23 16
Example: vacuum world
Single-state, Single-state, start in #5start in #5.
Solution? [Right, Suck][Right, Suck]
SensorlessSensorless, start in {1,2,3,4,5,6,7,8} e.g., Right goes to {2,4,6,8} Solution?
04/10/23 17
Example: vacuum world
SensorlessSensorless, start in {1,2,3,4,5,6,7,8} e.g., Right goes to {2,4,6,8} Solution? [Right,Suck,Left,Suck]
ContingencyContingency Nondeterministic: Suck may
dirty a clean carpet Partially observable: location, dirt at current location. Percept: [L, Clean], i.e., start in #5 or #7
Solution?
04/10/23 18
Example: vacuum world
SensorlessSensorless, start in {1,2,3,4,5,6,7,8} e.g., Right goes to {2,4,6,8} Solution? [Right,Suck,Left,Suck]
ContingencyContingency Nondeterministic: Suck may
dirty a clean carpet Partially observable: location, dirt at current
location. Percept: [L, Clean], i.e., start in #5 or #7
Solution? [Right, if dirt then Suck]
04/10/23 19
Vacuum world state space graph
states? actions? goal test? path cost?
04/10/23 20
Vacuum world state space graph
states? integer dirt and robot locationinteger dirt and robot location actions? Left, Right, SuckLeft, Right, Suck goal test? no dirt at all locationsno dirt at all locations path cost? 11 per actionper action
04/10/23 21
Example: robotic assembly
states?: real-valued coordinates of robot joint real-valued coordinates of robot joint angles parts of the object to be assembledangles parts of the object to be assembled
actions?: continuous motions of robot jointscontinuous motions of robot joints goal test?: complete assemblycomplete assembly path cost?: time to executetime to execute
04/10/23 22
04/10/23 23
04/10/23 24
04/10/23 25
Tree search algorithms
Basic ideaBasic idea: offline, simulated exploration of state offline, simulated exploration of state
space by generating successors of already-space by generating successors of already-explored states (a.k.a.expanding states)explored states (a.k.a.expanding states)
04/10/23 26
Tree search example
04/10/23 27
Tree search example
04/10/23 28
Tree search example
04/10/23 29
Implementation: general tree search
04/10/23 30
Implementation: states vs. nodes
A state is a (representation of) a physical configurationA state is a (representation of) a physical configuration A node is a data structure constituting part of a search tree A node is a data structure constituting part of a search tree
includes state, parent node, action, path cost g(x), depthincludes state, parent node, action, path cost g(x), depth
The Expand function creates new nodes, filling in the various The Expand function creates new nodes, filling in the various fields and using the Success or Fn of the problem to create the fields and using the Success or Fn of the problem to create the corresponding states.corresponding states.
04/10/23 31
04/10/23 32
Search strategies
Time and space complexity are measured Time and space complexity are measured in terms of in terms of b: maximum branching factor of the b: maximum branching factor of the
search treesearch tree d: depth of the least-cost solutiond: depth of the least-cost solution m: maximum depth of the state space m: maximum depth of the state space
(may be (may be ∞∞))
04/10/23 33
04/10/23 34
Uninformed search strategies
Uninformed search strategies use only the Uninformed search strategies use only the information available in the problem information available in the problem definitiondefinition
Breadth-first searchBreadth-first search Uniform-cost searchUniform-cost search Depth-first searchDepth-first search Depth-limited searchDepth-limited search Iterative deepening searchIterative deepening search
04/10/23 35
Breadth-first search
Expand shallowest unexpanded nodeExpand shallowest unexpanded node Implementation:
fringe is a FIFO queue, i.e., new successors go at end
04/10/23 36
Breadth-first search
Expand shallowest unexpanded nodeExpand shallowest unexpanded node Implementation:
fringe is a FIFO queue, i.e., new successors go at end
04/10/23 37
Breadth-first search
Expand shallowest unexpanded node Implementation:
fringe is a FIFO queue, i.e., new successors go at end
04/10/23 38
Breadth-first search
Expand shallowest unexpanded node Implementation:
fringe is a FIFO queue, i.e., new successors go at end
04/10/23 39
04/10/23 40
04/10/23 41
04/10/23 42
04/10/23 43
04/10/23 44
04/10/23 45
04/10/23 46
04/10/23 47
04/10/23 48
04/10/23 49
04/10/23 50
04/10/23 51
04/10/23 52
04/10/23 53
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)
04/10/23 54
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 ≥ ε Time? # of nodes with g ≤ cost of optimal solution,
O(bceiling(C*/ ε)) where C* is the cost of the optimal solution
Space? # of nodes with g ≤ cost of optimal solution, O(bceiling(C*/ ε))
Optimal? Yes – nodes expanded in increasing order of g(n)
04/10/23 55
04/10/23 56
04/10/23 57
04/10/23 58
04/10/23 59
04/10/23 60
04/10/23 61
04/10/23 62
04/10/23 63
04/10/23 64
04/10/23 65
04/10/23 66
04/10/23 67
04/10/23 68
04/10/23 69
04/10/23 70
04/10/23 71
04/10/23 72
04/10/23 73
04/10/23 74
04/10/23 75
04/10/23 76
04/10/23 77
04/10/23 78
04/10/23 79
Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front
04/10/23 80
Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front
04/10/23 81
Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front
04/10/23 82
Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front
04/10/23 83
Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front
04/10/23 84
Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front
04/10/23 85
Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front
04/10/23 86
Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front
04/10/23 87
Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front
04/10/23 88
Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front
04/10/23 89
Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front
04/10/23 90
Depth-first search
Expand deepest unexpanded node Implementation:
fringe = LIFO queue, i.e., put successors at front
04/10/23 91
04/10/23 92
04/10/23 93
04/10/23 94
04/10/23 95
04/10/23 96
04/10/23 97
04/10/23 98
04/10/23 99
04/10/23 100
04/10/23 101
04/10/23 102
04/10/23 103
Properties of depth-first search
Complete? No: fails in infinite-depth spaces, spaces with loops Modify to avoid repeated states along path
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
04/10/23 104
Depth-limited search
= depth-first search with depth limit l,i.e., nodes at depth l have no successors
Recursive implementation:
04/10/23 105
Iterative deepening search
04/10/23 106
Iterative deepening search
04/10/23 107
Iterative deepening search
04/10/23 108
Iterative deepening search
04/10/23 109
Iterative deepening search
04/10/23 110
Iterative deepening search
Number of nodes generated in a depth-limited search to depth d with branching factor b:
NDLS = b0 + b1 + b2 + … + bd-2 + bd-1 + bd
Number of nodes generated in an iterative deepening search to depth d with branching factor b:
NIDS = (d+1)b0 + d b^1 + (d-1)b^2 + … + 3bd-2 +2bd-1 + 1bd
For b = 10, d = 5, NDLS = 1 + 10 + 100 + 1,000 + 10,000 + 100,000 =
111,111 NIDS = 6 + 50 + 400 + 3,000 + 20,000 + 100,000 =
123,456
Overhead = (123,456 - 111,111)/111,111 = 11%
04/10/23 111
Properties of iterative deepening search
Complete? Yes Time? (d+1)b0 + d b1 + (d-1)b2 + … +
bd = O(bd) Space? O(bd) Optimal? Yes, if step cost = 1
04/10/23 112
Summary of algorithms
04/10/23 113
Repeated states
Failure to detect repeated states can turn a linear problem into an exponential one!
04/10/23 114
Graph search
04/10/23 115
Summary
Problem formulation usually requires abstracting away real-world details to define a state space that can feasibly be explored
Variety of uninformed search strategies
Iterative deepening search uses only linear space and not much more time than other uninformed algorithms