2PP

CS 188: Artificial Intelligence
Spring 2006
Lecture 2: Queue-Based Search
8/31/2006
Dan Klein – UC Berkeley
Many slides from either Stuart Russell or Andrew Moore
Announcements
ƒ Lab Friday 1-5pm in Soda 275
ƒ Learn Python
ƒ Start on Project 1.1: Mazeworld
ƒ Come for whatever times you like
ƒ No sections this Monday
ƒ Project 1.1 posted due 9/8
ƒ You can do most of it after today
1
Today
ƒ Agents that Plan Ahead
ƒ Search Problems
ƒ Uniformed Search Methods
ƒ Depth-First Search
ƒ Breadth-First Search
ƒ Uniform-Cost Search
ƒ Heuristic Search Methods
ƒ Greedy Search
ƒ A* Search
Reflex Agents
ƒ Reflex agents:
ƒ Choose action based
on current percept and
memory
ƒ May have memory or
a model of the world’s
current state
ƒ Do not consider the
future consequences
of their actions
ƒ Can an reflex agent
be rational?
2
Goal-Based Agents
ƒ Goal-based
agents:
ƒ Plan ahead
ƒ Decisions based
on (hypothesized)
consequences of
actions
ƒ Must have a model
of how the world
evolves in
response to
actions
Search Problems
ƒ A search problem consists of:
ƒ A state space
ƒ A successor function
“N”, 1.0
“E”, 1.0
ƒ A start state and a goal test
ƒ A solution is a sequence of actions which
transform the start state to a goal state
3
Search Trees
“E”, 1.0
“N”, 1.0
ƒ A search tree:
ƒ
ƒ
ƒ
ƒ
ƒ
This is a “what if” tree
Current state at the root node
Children correspond to successors
Nodes labeled with states, correspond to PATHS to those states
For most problems, can never actually build the whole tree
ƒ So, have to find ways of using only the important parts of the tree!
State Space Graphs
ƒ There’s some big graph
in which
ƒ Important: For most
problems we could
never actually build this
graph
ƒ How many states in
Pacman?
G
a
ƒ Each state is a node
ƒ Each successor is an
outgoing arc
c
b
e
d
f
S
h
p
q
r
Laughably tiny search graph
for a tiny search problem
4
Example: Romania
Another Search Tree
ƒ Search:
ƒ Expand out possible plans
ƒ Maintain a fringe of unexpanded plans
ƒ Try to expand as few tree nodes as possible
5
States vs. Nodes
ƒ Problem graphs have problem states
ƒ Have successors
ƒ Search trees have search nodes
ƒ Have parents, children, depth, path cost, etc.
ƒ Expand uses successor function to create new search tree nodes
ƒ The same problem state may be in multiple search tree nodes
General Tree Search
ƒ Important ideas:
ƒ Fringe
ƒ Expansion
ƒ Exploration strategy
Detailed pseudocode
is in the book!
ƒ Main question: which fringe nodes to explore?
6
Example: Tree Search
G
a
c
b
e
d
f
S
h
p
r
q
State Graphs vs Search Trees
G
a
Each NODE in in the
search tree is an
entire PATH in the
problem graph.
c
b
e
d
f
S
h
p
r
q
S
e
d
We almost always
construct both on
demand – and we
construct as little
as possible.
b
c
a
a
e
h
p
q
q
c
h
r
p
f
q
G
p
q
r
q
f
c
G
a
a
7
Review: Depth First Search
G
a
Strategy: expand
deepest node first
c
b
e
d
Implementation:
Fringe is a LIFO
stack
f
S
h
p
r
q
S
e
d
b
c
a
a
e
h
h
p
q
q
p
r
p
f
q
c
q
r
q
f
c
G
a
G
a
Review: Breadth First Search
G
a
Strategy: expand
shallowest node first
c
b
e
d
Implementation:
Fringe is a FIFO
queue
S
f
h
p
r
q
S
Search
Tiers
p
e
d
b
c
a
a
e
h
p
q
q
c
h
r
p
f
q
G
q
r
q
f
c
G
a
a
8
Search Algorithm Properties
ƒ
ƒ
ƒ
ƒ
Complete? Guaranteed to find a solution if one exists?
Optimal? Guaranteed to find the least cost path?
Time complexity?
Space complexity?
Variables:
n
Number of states in the problem
b
The average branching factor B
(the average number of successors)
Cost of least cost solution
C*
s
Depth of the shallowest solution
m
Max depth of the search tree
DFS
Algorithm
DFS
Depth First
Search
Complete Optimal
Time
Space
N
LMAX)
O(B
Infinite
O(LMAX)
Infinite
N
N N
b
START
a
GOAL
ƒ Infinite paths make DFS incomplete…
ƒ How can we fix this?
9
DFS
ƒ With cycle checking, DFS is complete.
1 node
b
b nodes
…
b2 nodes
m tiers
bm nodes
Algorithm
DFS
Complete
w/ Path
Checking
Y
Optimal
Time
Space
O(bm+1)
N
O(bm)
ƒ When is DFS optimal?
BFS
Algorithm
DFS
w/ Path
Checking
BFS
Complete
Optimal
Y
N
O(bm+1)
O(bm)
Y
N*
O(bs+1)
O(bs)
b
s tiers
…
Time
Space
1 node
b nodes
b2 nodes
bs nodes
bm nodes
ƒ When is BFS optimal?
10
Comparisons
ƒ When will BFS outperform DFS?
ƒ When will DFS outperform BFS?
Costs on Actions
GOAL
a
2
2
c
b
1
3
2
8
2
e
d
3
9
8
START
h
4
1
p
15
f
2
4
q
1
r
Notice that BFS finds the shortest path in terms of number of
transitions. It does not find the least-cost path.
We will quickly cover an algorithm which does find the least-cost path.
11
Uniform Cost Search
2
b
Expand cheapest node first:
d
S
1
p
15
Cost
contours
c
a 6
a
p
h 13 r 7
p
f 8
q
q
q 11 c
h
G 10
8
f
1
1
r
q
h 17 r 11
e 5
11
9
2
e
9
e
3
b 4
2
0
S
d
c
8
1
3
Fringe is a priority queue
G
a
q
p
1
q
16
f
c
G
a
a
Priority Queue Refresher
ƒ A priority queue is a data structure in which you can insert
and retrieve (key, value) pairs with the following operations:
pq.push(key, value)
inserts (key, value) into the queue.
pq.pop()
returns the key with the lowest value, and
removes it from the queue.
ƒ You can promote or demote keys by resetting their priorities
ƒ Unlike a regular queue, insertions into a priority queue are not
constant time, usually O(log n)
ƒ We’ll need priority queues for most cost-sensitive search
methods.
12
Uniform Cost Search
ƒ What will UCS do for this graph?
0
b
1
0
a
START
1
GOAL
ƒ What does this mean for completeness?
Uniform Cost Search
Algorithm
DFS
w/ Path
Checking
Complete Optimal
Time
Space
Y
N
O(bm+1)
O(bm)
BFS
Y
N
O(bs+1)
O(bs)
UCS
Y*
Y
O(C* bC*/ε)
O(bC*/ε)
b
…
C*/ε tiers
We’ll talk more
about uniform cost
search’s failure
cases later…
13
Uniform Cost Problems
ƒ Remember: explores
increasing cost contours
…
c≤1
c≤2
c≤3
ƒ The good: UCS is
complete and optimal!
ƒ The bad:
ƒ Explores options in every
“direction”
ƒ No information about goal
location
Start
Goal
Extra Work?
ƒ Failure to detect repeated states can cause
exponentially more work. Why?
14
Graph Search
ƒ In BFS, for example, we shouldn’t bother
expanding the circled nodes (why?)
S
e
d
b
c
a
a
e
h
p
q
q
c
h
r
p
f
q
G
p
q
r
q
f
c
G
a
a
Graph Search
ƒ Very simple fix: never expand a node twice
ƒ Can this wreck correctness? Why or why not?
15
Search Gone Wrong?
Best-First / Greedy Search
16
Best-First / Greedy Search
ƒ Expand the node that seems closest…
ƒ What can go wrong?
Best-First / Greedy Search
GOAL
a
2
2
h=0
h=8
b
c
1
h=11
2
2
e
d
3
1
9
h=8
START
h=12
5
h=4
h=5
8
p
15
h=4
h
4
1
f
9
5
h=6
4
3
r
q
h=11
h=9
h=6
17
Best-First / Greedy Search
ƒ A common case:
ƒ Best-first takes you straight
to the (wrong) goal
b
…
ƒ Worst-case: like a badlyguided DFS in the worst
case
ƒ Can explore everything
ƒ Can get stuck in loops if no
cycle checking
b
…
ƒ Like DFS in completeness
(finite states w/ cycle
checking)
18