Chapter03 PDF
Chapter03 PDF
Chapter 3
Chapter 3 1
Outline
♦ Problem-solving agents
♦ Problem types
♦ Problem formulation
♦ Example problems
♦ Basic search algorithms
Chapter 3 3
Problem-solving agents
Restricted form of general agent:
Chapter 3 5
Example: Romania
Oradea
71
Neamt
Zerind 87
75 151
Iasi
Arad 140
92
Sibiu Fagaras
99
118 Vaslui
80
Timisoara Rimnicu Vilcea
142
111 Pitesti 211
Lugoj 97
70 98
146 85 Hirsova
Mehadia 101 Urziceni
75 138 86
Bucharest
Dobreta 120
90
Craiova Eforie
Giurgiu
Chapter 3 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) = {hArad → Zerind, Zerindi, . . .}
goal test, can be
explicit, e.g., x = “at Bucharest”
implicit, e.g., N oDirt(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
Chapter 3 12
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., “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!
Chapter 3 13
Example: The 8-puzzle
7 2 4 5
1 2 3
5 6 4 5 6
8 3 1 7 8
states??
actions??
goal test??
path cost??
Chapter 3 19
Example: The 8-puzzle
7 2 4 5
1 2 3
5 6 4 5 6
8 3 1 7 8
Chapter 3 20
Example: The 8-puzzle
7 2 4 5
1 2 3
5 6 4 5 6
8 3 1 7 8
Chapter 3 21
Example: The 8-puzzle
7 2 4 5
1 2 3
5 6 4 5 6
8 3 1 7 8
Chapter 3 22
Example: The 8-puzzle
7 2 4 5
1 2 3
5 6 4 5 6
8 3 1 7 8
Chapter 3 23
Example: robotic assembly
P
R R
R R
Chapter 3 24
Tree search algorithms
Basic idea:
offline, simulated exploration of state space
by generating successors of already-explored states
(a.k.a. expanding states)
Chapter 3 25
Tree search example
Arad
Chapter 3 26
Tree search example
Arad
Chapter 3 27
Tree search example
Arad
Chapter 3 28
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
depth = 6
State 5 4 Node
g=6
6 1 88
state
7 3 22
The Expand function creates new nodes, filling in the various fields and
using the SuccessorFn of the problem to create the corresponding states.
Chapter 3 29
Implementation: general tree search
Chapter 3 30
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 ∞)
Chapter 3 31
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
Chapter 3 32
Breadth-first search
Expand shallowest unexpanded node
Implementation:
fringe is a FIFO queue, i.e., new successors go at end
A
B C
D E F G
Chapter 3 33
Breadth-first search
Expand shallowest unexpanded node
Implementation:
fringe is a FIFO queue, i.e., new successors go at end
A
B C
D E F G
Chapter 3 34
Breadth-first search
Expand shallowest unexpanded node
Implementation:
fringe is a FIFO queue, i.e., new successors go at end
A
B C
D E F G
Chapter 3 35
Breadth-first search
Expand shallowest unexpanded node
Implementation:
fringe is a FIFO queue, i.e., new successors go at end
A
B C
D E F G
Chapter 3 36
Properties of breadth-first search
Complete??
Chapter 3 37
Properties of breadth-first search
Complete?? Yes (if b is finite)
Time??
Chapter 3 38
Properties of breadth-first search
Complete?? Yes (if b is finite)
Time?? 1 + b + b2 + b3 + . . . + bd + b(bd − 1) = O(bd+1), i.e., exp. in d
Space??
Chapter 3 39
Properties of breadth-first search
Complete?? Yes (if b is finite)
Time?? 1 + b + b2 + b3 + . . . + bd + b(bd − 1) = O(bd+1), i.e., exp. in d
Space?? O(bd+1) (keeps every node in memory)
Optimal??
Chapter 3 40
Properties of breadth-first search
Complete?? Yes (if b is finite)
Time?? 1 + b + b2 + b3 + . . . + bd + b(bd − 1) = O(bd+1), i.e., exp. in d
Space?? O(bd+1) (keeps every node in memory)
Optimal?? Yes (if cost = 1 per step); not optimal in general
Space is the big problem; can easily generate nodes at 100MB/sec
so 24hrs = 8640GB.
Chapter 3 41
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
Complete?? Yes, if step cost ≥
∗ /e
Time?? # of nodes with g ≤ cost of optimal solution, O(bdC )
where C ∗ is the cost of the optimal solution
∗ /e
Space?? # of nodes with g ≤ cost of optimal solution, O(bdC )
Optimal?? Yes—nodes expanded in increasing order of g(n)
Chapter 3 42
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
A
B C
D E F G
H I J K L M N O
Chapter 3 43
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
A
B C
D E F G
H I J K L M N O
Chapter 3 44
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
A
B C
D E F G
H I J K L M N O
Chapter 3 45
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
A
B C
D E F G
H I J K L M N O
Chapter 3 46
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
A
B C
D E F G
H I J K L M N O
Chapter 3 47
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
A
B C
D E F G
H I J K L M N O
Chapter 3 48
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
A
B C
D E F G
H I J K L M N O
Chapter 3 49
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
A
B C
D E F G
H I J K L M N O
Chapter 3 50
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
A
B C
D E F G
H I J K L M N O
Chapter 3 51
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
A
B C
D E F G
H I J K L M N O
Chapter 3 52
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
A
B C
D E F G
H I J K L M N O
Chapter 3 53
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
A
B C
D E F G
H I J K L M N O
Chapter 3 54
Properties of depth-first search
Complete??
Chapter 3 55
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??
Chapter 3 56
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??
Chapter 3 57
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??
Chapter 3 58
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
Chapter 3 59
Depth-limited search
= depth-first search with depth limit l,
i.e., nodes at depth l have no successors
Recursive implementation:
Chapter 3 60
Iterative deepening search
Chapter 3 61
Iterative deepening search l = 0
Limit = 0 A A
Chapter 3 62
Iterative deepening search l = 1
Limit = 1 A A A A
B C B C B C B C
Chapter 3 63
Iterative deepening search l = 2
Limit = 2 A A A A
B C B C B C B C
D E F G D E F G D E F G D E F G
A A A A
B C B C B C B C
D E F G D E F G D E F G D E F G
Chapter 3 64
Iterative deepening search l = 3
Limit = 3 A A A A
B C B C B C B C
D E F G D E F G D E F G D E F G
H I J K L M N O H I J K L M N O H I J K L M N O H I J K L M N O
A A A A
B C B C B C B C
D E F G D E F G D E F G D E F G
H I J K L M N O H I J K L M N O H I J K L M N O H I J K L M N O
A A A A
B C B C B C B C
D E F G D E F G D E F G D E F G
H I J K L M N O H I J K L M N O H I J K L M N O H I J K L M N O
Chapter 3 65
Properties of iterative deepening search
Complete??
Chapter 3 66
Properties of iterative deepening search
Complete?? Yes
Time??
Chapter 3 67
Properties of iterative deepening search
Complete?? Yes
Time?? (d + 1)b0 + db1 + (d − 1)b2 + . . . + bd = O(bd)
Space??
Chapter 3 68
Properties of iterative deepening search
Complete?? Yes
Time?? (d + 1)b0 + db1 + (d − 1)b2 + . . . + bd = O(bd)
Space?? O(bd)
Optimal??
Chapter 3 69
Properties of iterative deepening search
Complete?? Yes
Time?? (d + 1)b0 + db1 + (d − 1)b2 + . . . + bd = O(bd)
Space?? O(bd)
Optimal?? Yes, if step cost = 1
Can be modified to explore uniform-cost tree
Numerical comparison for b = 10 and d = 5, solution at far right leaf:
N (IDS) = 50 + 400 + 3, 000 + 20, 000 + 100, 000 = 123, 450
N (BFS) = 10 + 100 + 1, 000 + 10, 000 + 100, 000 + 999, 990 = 1, 111, 100
IDS does better because other nodes at depth d are not expanded
BFS can be modified to apply goal test when a node is generated
Chapter 3 70
Summary of algorithms
Chapter 3 71
Repeated states
Failure to detect repeated states can turn a linear problem into an exponential
one!
A A
B B B
C C C C C
Chapter 3 72
Graph search
Chapter 3 73
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
Graph search can be exponentially more efficient than tree search
Chapter 3 74