Searching: Deterministic Single-Agent: Andrew W. Moore Professor School of Computer Science Carnegie Mellon University
Searching: Deterministic Single-Agent: Andrew W. Moore Professor School of Computer Science Carnegie Mellon University
Deterministic
single-agent
Andrew W. Moore
Professor
School of Computer Science
Carnegie Mellon University
www.cs.cmu.edu/~awm
[email protected]
412-268-7599
Note to other teachers and users of these slides. Andrew would be delighted if you found this source
material useful in giving your own lectures. Feel free to use these slides verbatim, or to modify them to fit
your own needs. PowerPoint originals are available. If you make use of a significant portion of these
slides in your own lecture, please include this message, or the following link to the source repository of Slide 1
Andrew’s tutorials: http://www.cs.cmu.edu/~awm/tutorials . Comments and corrections gratefully received.
Overview
• Deterministic, single-agent, search problems
• Breadth First Search
• Optimality, Completeness, Time and Space
complexity
• Search Trees
• Depth First Search
• Iterative Deepening
• Best First “Greedy” Search
Slide 2
1
A search problem
a GOAL
b c
e
d
f
START h
p r
q
2
Our Search Problem
GOAL
a
b c
e
d f
START
h
p r
q
Q = {START, a , b , c , d , e , f , h , p , q , r , GOAL}
S = { START }
G = { GOAL }
succs(b) = { a }
succs(e) = { h , r }
succs(a) = NULL … etc.
cost(s,s’) = 1 for all transitions
Slide 5
e
d f
START
h
p r
q
at
Q = {START, a , b , c , d , e , f , h , p , q , r , GOAL}
S = { START } h
G = { GOAL } r e ? W is?
succs(b) = { a } e ca ke th
i
succs(e) = { h , r }
do w are l
succs(a) = NULL … etc. y s
cost(s,s’) = 1 for all transitions Wh blem
pro Slide 6
3
Search Problems
Slide 7
8-Queens
What next?
Slide 8
4
More Search Problems
But there are plenty of things which we’d
Scheduling
normally call search problems that don’t fit our
rigid definition… • A search problem has five components:
• Q , S , G , succs , cost 8-Queens
• Q is a finite set of states.
• S ⊆ Q is a non-empty set of start states.
• G ⊆ Q is a non-empty set of goal states.
• succs : Q Æ P(Q) is a function which takes a
state as input and returns a set of states as
output. succs(s) means “the set of states you
can reach from s in one step”.
• cost : Q , Q Æ Positive Number is a function
which takes two states, s and s’, as input. It
What snext?
returns the one-step cost of traveling from to
Can s’. The cost function is only defined when s’ is
y ou t a successor state of s.
hink
of exam
ples
? Slide 9
Slide 10
5
Our definition excludes…
Hidden State
Game Chance
against
adversary
Slide 11
e
d f
START
h
p r
q
Label all states that are reachable from S in 1 step but aren’t reachable
in less than 1 step.
Then label all states that are reachable from S in 2 steps but aren’t
reachable in less than 2 steps.
Then label all states that are reachable from S in 3 steps but aren’t
reachable in less than 3 steps.
Etc… until Goal state reached.
Slide 12
6
Breadth-first Search
a GOAL
b c
0 steps e
from start d
f
START h
p r
q
Slide 13
1 step
Breadth-first Search
from start GOAL
a
b c
0 steps e
from start d
f
START h
p r
q
Slide 14
7
1 step
Breadth-first Search
from start GOAL
a
b c
0 steps e
from start d
f
START h
p r
q
2 steps
from start
Slide 15
1 step
Breadth-first Search
from start GOAL
a
b c
0 steps e
from start d
f
START h
3 steps
from start
p r
q
2 steps
from start
Slide 16
8
4 steps
1 step
Breadth-first Search from start
b c
0 steps e
from start d
f
START h
3 steps
from start
p r
q
2 steps
from start
Slide 17
e
d f
START
h
p r
q
Also, when you label a state, record the predecessor state. This record
is called a backpointer. The history of predecessors is used to
generate the solution path, once you’ve found the goal:
“I’ve got to the goal. I see I was at f before this. And I was at r before I
was at f. And I was…
…. so solution path is S Æ e Æ r Æ f Æ G”
Slide 18
9
4 steps
1 step
Backpointers from start
b c
0 steps e
from start d
f
START h
3 steps
from start
p r
q
2 steps
from start
Slide 19
4 steps
1 step
Backpointers from start
b c
0 steps e
from start d
f
START h
3 steps
from start
p r
q
2 steps
from start
Slide 20
10
Starting Breadth First Search
For any state s that we’ve labeled, we’ll remember:
•previous(s) as the previous state on a shortest path from START state
to s.
On the kth iteration of the algorithm we’ll begin with Vk defined as the
set of those states for which the shortest path from the start costs
exactly k steps
Then, during that iteration, we’ll compute Vk+1, defined as the set of
those states for which the shortest path from the start costs exactly k+1
steps
We begin with k = 0, V0 = {START} and we’ll define, previous(START)
= NULL
Then we’ll add in things one step from the START into V1. And we’ll
keep going.
Slide 21
BFS
a GOAL
b c
e
d
f
START h
V0 p
q
r
Slide 22
11
BFS
a GOAL
b c
e
d
f
START h
V0 p
q
r
V1
Slide 23
BFS
a GOAL
b c
e
d
f
START h
V0 p
q
r
V1
V2
Slide 24
12
BFS
a GOAL
b c
e
d
f V3
START h
V0 p
q
r
V1
V2
Slide 25
BFS V4
a GOAL
b c
e
d
f V3
START h
V0 p
q
r
V1
V2
Slide 26
13
Breadth First Search
V0 := S (the set of start states)
previous(START) := NIL
k := 0
while (no goal state is in Vk and Vk is not empty) do
Vk+1 := empty set
For each state s in Vk
For each state s’ in succs(s)
If s’ has not already been labeled
Set previous(s’) := s
Add s’ into Vk+1
k := k+1
If Vk is empty signal FAILURE
Else build the solution path thus: Let Si be the ith state in the shortest
path. Define Sk = GOAL, and forall i <= k, define Si-1 = previous(Si).
Slide 27
BFS V4
a GOAL
b c
e
ntly
e convenie f te).
d
a rc h s p a c V3
our se ors(sta
START Suppose y to o btain phredecess
u
allowed yo wa y to do BFS
?
fe re nt
p n you th
ink of a dif
V0 • Ca r g
avoid storin
q
e a b le to
ld you b ad to
• And wou a t w e ’d p reviously h
Vs1omething th
store? V2
Slide 28
14
Another way: Work back
GOAL
a
b c
e
d f
START
h
p r
q
Label all states that can reach G in 1 step but can’t reach it in less than
1 step.
Label all states that can reach G in 2 steps but can’t reach it in less
than 2 steps.
Etc. … until start is reached.
“number of steps to goal” labels determine the shortest path. Don’t
need extra bookkeeping info.
Slide 29
15
Costs on transitions
a GOAL
2 2
b c
5
5
1 8
2 e
3 d f
9 1 9
START h
1 4 5
p 4 3 r
1 q
5
Slide 31
16
Uniform-Cost Search
• A conceptually simple BFS approach
when there are costs on transitions
• It uses priority queues
Slide 33
Slide 34
17
Priority Queue Refresher
For mo
re
A priority queue is a data structure or Sedg details, see Kn
wick or uth
any boo ba
in which you can insert and k with th sically
“algorith e word
m
appeari s” prominently
retrieve (thing, value) pairs with ng in th
e title.
the following operations:
Init-PriQueue(PQ) initializes the PQ to be empty.
Uniform-Cost Search
• A conceptually simple BFS approach when
there are costs on transitions
• It uses a priority queue
18
Starting UCS
a GOAL
2 2
b c
2
5
1 8
2 e
3 d
f
9 1 9
START
h
1 4 5
4 3
p 15 r
q
PQ = { (S,0) }
Slide 37
UCS Iterations
a GOAL
2 2
b c
2
5
1 8
2 e
3 d
f
9 1 9
START
h
1 4 5
4 3
p 15 r
q
Iteration:
1. Pop least-cost state from PQ
PQ = { (S,0) } 2. Add successors
Slide 38
19
UCS Iterations
a GOAL
2 2
b c
2
5
1 8
2 e
3 d
f
9 1 9
START
h
1 4 5
4 3
p 15 r
q
Iteration:
1. Pop least-cost state from PQ
PQ = { (p,1), (d,3) , (e,9) } 2. Add successors
Slide 39
UCS Iterations
a GOAL
2 2
b c
2
5
1 8
2 e
3 d
f
9 1 9
START
h
1 4 5
4 3
p 15 r
q
Iteration:
1. Pop least-cost
state from PQ
PQ = { (d,3) , (e,9) , (q,16) } 2. Add successors
Slide 40
20
UCS Iterations
a GOAL
2 2
b c
2
5
1 8
2 e
3 d
f
9 1 9
START
h
1 4 5
4 3
p 15 r
q
Iteration:
1. Pop least-cost
state from PQ
PQ = { (b,4) , (e,5) , (c,11) , (q,16) } 2. Add successors
Slide 41
UCS Iterations
a GOAL
2 2
b c
2
5
1 8
2 e
3 d
f
9 1 9
h d he
r e: s
d5 wa
START
p e n e ia
a p 4 v n
h at h g to e est-know
1
t e w e t t in
No 4 at g 3 ly b r
p 15
a liz ed th previous
e q e
• d r r th a n th Iteration: d
be t t e to e ngeleast-cost
o g et s c1.haPop
t a
way ority
w state from PQ
PQ = { (b,4) , (e,5) , (c,11)so, e(q,16) ’s pri } 2. Add successors
• and Slide 42
21
UCS Iterations
a GOAL
2 2
b c
2
5
1 8
2 e
3 d
f
9 1 9
START
h
1 4 5
4 3
p 15 r
q
Iteration:
1. Pop least-cost
state from PQ
PQ = { (e,5) , (a,6) , (c,11) , (q,16) } 2. Add successors
Slide 43
UCS Iterations
a GOAL
2 2
b c
2
5
1 8
2 e
3 d
f
9 1 9
START
h
1 4 5
4 3
p 15 r
q
Iteration:
1. Pop least-cost
state from PQ
PQ = { (a,6),(h,6),(c,11),(r,14),(q,16) } 2. Add successors
Slide 44
22
UCS Iterations
a GOAL
2 2
b c
2
5
1 8
2 e
3 d
f
9 1 9
START
h
1 4 5
4 3
p 15 r
q
Iteration:
1. Pop least-cost
state from PQ
PQ = { (h,6),(c,11),(r,14),(q,16) } 2. Add successors
Slide 45
UCS Iterations
a GOAL
2 2
b c
2
5
1 8
2 e
3 d
f
9 1 9
START
h
1 4 5
4 3
p 15 r
q
Iteration:
1. Pop least-cost
state from PQ
PQ = { (q,10), (c,11),(r,14) } 2. Add successors
Slide 46
23
UCS Iterations
Note what happen
ed here:
way to get to p
• h found a 2new a ay
GOAL
ly 2 the best known w
than
as m or e co st
• but it w c
b
io rit y w as un ch anged 2
• and so p’s pr 1 8
5
2 e
3 d
f
9 1 9
START
h
1 4 5
4 3
p 15 r
q
Iteration:
1. Pop least-cost
state from PQ
PQ = { (q,10), (c,11),(r,14) } 2. Add successors
Slide 47
UCS Iterations
a GOAL
2 2
b c
2
5
1 8
2 e
3 d
f
9 1 9
START
h
1 4 5
4 3
p 15 r
q
Iteration:
1. Pop least-cost
state from PQ
PQ = { (c,11),(r,13) } 2. Add successors
Slide 48
24
UCS Iterations
a GOAL
2 2
b c
2
5
1 8
2 e
3 d
f
9 1 9
START
h
1 4 5
4 3
p 15 r
q
Iteration:
1. Pop least-cost
state from PQ
PQ = { (r,13) } 2. Add successors
Slide 49
UCS Iterations
a GOAL
2 2
b c
2
5
1 8
2 e
3 d
f
9 1 9
START
h
1 4 5
4 3
p 15 r
q
Iteration:
1. Pop least-cost
state from PQ
PQ = { (f,18) } 2. Add successors
Slide 50
25
UCS Iterations
a GOAL
2 2
b c
2
5
1 8
2 e
3 d
f
9 1 9
START
h
1 4 5
4 3
p 15 r
q
Iteration:
1. Pop least-cost
state from PQ
PQ = { (G,23) } 2. Add successors
Slide 51
u discover
UCS
Que st ion: Iterations
Is “t erm in ate as soon as yo
criterion?
stopping
the goal” the right
a GOAL
2 2
b c
2
5
1 8
2 e
3 d
f
9 1 9
START
h
1 4 5
4 3
p 15 r
q
Iteration:
1. Pop least-cost
state from PQ
PQ = { (G,23) } 2. Add successors
Slide 52
26
UCS terminates
a GOAL
2 2
b c
2
5
1 8
the
ly once the go al is2 popped efrom
Terminate on s a shorter path.
3 eue. Elsde we may mis
qu
priorit y 9 1
f
9
START
h
1 4 5
4 3
p 15 r
q
Iteration:
1. Pop least-cost
state from PQ
PQ = { } 2. Add successors
Slide 53
27
Judging a search algorithm
N number of states in the problem
B the average branching factor (the average number of successors) (B>1)
L the length of the path from start to goal with the shortest number of steps
Q the average size of the priority queue
Slide 55
Slide 56
28
Search Tree Representation
GOAL
a
b c
e
d f
START
h
p q r
thr ough
go
e r d o we h BFS?
t o rd e wit
Wha arch tre
e
the s Slide 57
29
DFS in action
a GOAL
b c
START e
START d d f
START d b START h
START d b a
START d c p r
q
START d c a
START d e
START d e r
START d e r f
START d e r f c
START d e r f c a
START d e r f GOAL
Slide 59
Slide 60
30
DFS Algorithm
We use a data structure we’ll call a Path to represent the , er, path from the
START to the current state.
E.G. Path P = <START, d, e, r >
Along with each node on the path, we must remember which successors we
still have available to expand. E.G. at the following point, we’ll have
P = <START (expand=e , p) ,
d (expand = NULL) ,
e (expand = h) ,
r (expand = f) >
Slide 61
DFS Algorithm
Let P = <START (expand = succs(START))>
While (P not empty and top(P) not a goal)
if expand of top(P) is empty
then
remove top(P) (“pop the stack”)
else
let s be a member of expand of top(P)
remove s from expand of top(P)
make a new item on the top of path P:
s (expand = succs(s))
This algorithm can be
If P is empty written neatly with
return FAILURE recursion, i.e. using the
program stack to
Else implement P.
return the path consisting of states in P
Slide 62
31
Judging a search algorithm
N number of states in the problem
B the average branching factor (the average number of successors) (B>1)
L the length of the path from start to goal with the shortest number of steps
Q the average size of the priority queue
Slide 63
Slide 64
32
Judging a search algorithm
N number of states in the problem
B the average branching factor (the average number of successors) (B>1)
L the length of the path from start to goal with the shortest number of steps
LMAX Length of longest path from start to anywhere
Q the average size of the priority queue
33
Questions to
ponder
• How would you
prevent DFS from
looping?
Slide 67
Questions to Answer 1:
ponder PC-DFS (Path Checking DFS):
• How would you Don’t recurse on a state
prevent DFS from if that state is already in
looping? the current path
Answer 2:
• How could you
MEMDFS (Memoizing DFS):
force it to give an
optimal solution? Remember all states
expanded so far. Never
expand anything twice.
Slide 68
34
Questions to Answer 1:
ponder PC-DFS (Path Checking DFS):
• How would you Don’t recurse on a state
prevent DFS from if that state is already in
looping? the current path
Answer 2:
• How could you
MEMDFS (Memoizing DFS):
force it to give an
optimal solution? Remember all states
expanded so far. Never
expand anything twice.
Slide 69
Questions to Answer 1:
ponder PC-DFS (Path Checking DFS):
• How would youFS is Don’t recurse on a state
CD
prevent DFS n Pfrom
he S?
if that state is already in
looping? ns w MDF the current path
o
i ME
as c an S
oc DF
e re ter th EM
th bet M
Are hen S? Answer 2:
• How could w DF
ns Cyou
asio an P MEMDFS (Memoizing DFS):
c
force
ere ettit
oc er tto
h give an
th sb Remember all states
Are optimal
i solution?
expanded so far. Never
expand anything twice.
Slide 70
35
Judging a search algorithm
N number of states in the problem
B the average branching factor (the average number of successors) (B>1)
L the length of the path from start to goal with the shortest number of steps
LMAX Length of longest cycle-free path from start to anywhere
Q the average size of the priority queue
36
Judging a search algorithm
N number of states in the problem
B the average branching factor (the average number of successors) (B>1)
L the length of the path from start to goal with the shortest number of steps
LMAX Length of longest cycle-free path from start to anywhere
Q the average size of the priority queue
Maze example
Imagine states are cells in a maze, you can move N, E, S, W. What
would plain DFS do, assuming it always expanded the E successor
first, then N, then W, then S?
Expansion order E, N, W, S
S
What would BFS do?
Other questions: What would PCDFS do?
What would MEMDFS do? Slide 74
37
Two other DFS examples
G
Order: N, E, S, W?
G Order: N, E, S, W
with loops prevented
S
Slide 75
Slide 76
38
Invent An Algorithm Time!
Here’s a way to dramatically decrease costs
sometimes. Bidirectional Search. Can you
guess what this algorithm is, and why it can
be a huge cost-saver?
Slide 77
Slide 78
39
N number of states in the problem
B the average branching factor (the average number of successors) (B>1)
L the length of the path from start to goal with the shortest number of steps
LMAX Length of longest cycle-free path from start to anywhere
Q the average size of the priority queue
Slide 79
Iterative Deepening
Iterative deepening is a simple algorithm which
uses DFS as a subroutine:
length 2)
be uc u
s.
m m reg
te
fs
e
ea B u
gr S. e m
Cost is
O(b1 + b2 + b3 + b4 … + bL) = O(bL) Slide 80
40
Maze example
Imagine states are cells in a maze, you can move N, E, S, W. What
would Iterative Deepening do, assuming it always expanded the E
successor first, then N, then W, then S?
S
Expansion order E, N, W, S
Slide 81
41
N number of states in the problem
B the average branching factor (the average number of successors) (B>1)
L the length of the path from start to goal with the shortest number of steps
LMAX Length of longest cycle-free path from start to anywhere
Q the average size of the priority queue
42
Heuristic Search
Suppose in addition to the standard search
specification we also have a heuristic.
A heuristic function maps a state
onto an estimate of the cost to the
goal from that state.
Can you think of examples of heuristics?
• E.G. for the 8-puzzle?
• E.G. for planning a path through a maze?
Euclidian Heuristic
a GOAL
2 2
h=0
h=8 c
b 2
5
1 8 h=5 h=4
h=11
2 e
3 d
f
h=8 9 1 9
h=4
START
h
1 4 h=6 5
h=12
4 3
p 15 r
q
h=11 h=6
h=9
Slide 86
43
Euclidian Heuristic
a GOAL
2 2
h=0
h=8 c
b 2 .
alg orithm 5
1 8
yq ueueh=4
h=5
ic
eurist
h=11
t h er priorit 2 et he h
3 • Anod e , prior
ity is
t
this 9 im f
•h=8But .
1 9
h=4
START value h
1 4 h=6 5
h=12
4 3
p 15 r
q
h=11 h=6
h=9
Slide 87
44
What you should know
• Thorough understanding of BFS, LCBFS,
UCS. PCDFS, MEMDFS
• Understand the concepts of whether a
search is complete, optimal, its time and
space complexity
• Understand the ideas behind iterative
deepening and bidirectional search
• Be able to discuss at cocktail parties the
pros and cons of the above searches
Slide 89
45