6PP

CS 188: Artificial Intelligence
Spring 2006
Lecture 2: Queue-Based Search
8/31/2006
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
Dan Klein – UC Berkeley
Many slides from either Stuart Russell or Andrew Moore
Today
ƒ Agents that Plan Ahead
ƒ Search Problems
ƒ Uniformed Search Methods
ƒ Depth-First Search
ƒ Breadth-First Search
ƒ Uniform-Cost Search
ƒ Heuristic Search Methods
ƒ You can do most of it after today
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?
ƒ Greedy Search
ƒ A* Search
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
1
Search Trees
“E”, 1.0
“N”, 1.0
State Space Graphs
ƒ There’s some big graph
in which
ƒ 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
ƒ 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
ƒ So, have to find ways of using only the important parts of the tree!
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
States vs. Nodes
General Tree Search
ƒ 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
ƒ Important ideas:
ƒ Fringe
ƒ Expansion
ƒ Exploration strategy
Detailed pseudocode
is in the book!
ƒ Main question: which fringe nodes to explore?
2
Example: Tree Search
State Graphs vs Search Trees
G
a
d
f
f
S
r
q
e
d
h
p
Each NODE in in the
search tree is an
entire PATH in the
problem graph.
c
b
e
S
G
a
c
b
h
p
r
q
S
We almost always
construct both on
demand – and we
construct as little
as possible.
p
e
d
b
c
a
a
h
e
h
p
q
q
r
p
f
q
c
q
r
q
f
c
G
a
G
a
Review: Depth First Search
G
a
Strategy: expand
deepest node first
e
d
Implementation:
Fringe is a LIFO
stack
f
S
p
r
q
p
p
e
a
a
h
e
q
r
p
q
q
c
r
p
f
q
G
q
f
c
c
a
a
G
e
h
p
a
q
q
h
r
p
f
q
c
q
f
c
G
a
G
DFS
Algorithm
DFS
Depth First
Search
Complete Optimal
Time
Space
N
LMAX)
O(B
Infinite
O(LMAX)
Infinite
N
N N
b
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*
q
r
a
Search Algorithm Properties
Complete? Guaranteed to find a solution if one exists?
Optimal? Guaranteed to find the least cost path?
Time complexity?
Space complexity?
p
e
b
a
ƒ
ƒ
ƒ
ƒ
r
q
d
Search
Tiers
h
f
h
S
d
c
e
d
S
S
b
c
b
Implementation:
Fringe is a FIFO
queue
h
G
a
Strategy: expand
shallowest node first
c
b
Review: Breadth First Search
s
Depth of the shallowest solution
m
Max depth of the search tree
START
a
GOAL
ƒ Infinite paths make DFS incomplete…
ƒ How can we fix this?
3
DFS
BFS
ƒ With cycle checking, DFS is complete.
Algorithm
DFS
1 node
b
Optimal
Y
N
O(bm+1)
O(bm)
Y
N*
O(bs+1)
O(bs)
BFS
b nodes
…
Complete
w/ Path
Checking
Time
Space
b2 nodes
m tiers
s tiers
bm
Algorithm
DFS
w/ Path
Checking
1 node
b
Complete
Optimal
Y
N
b nodes
…
b2 nodes
nodes
Time
bs nodes
Space
O(bm+1)
bm nodes
O(bm)
ƒ When is DFS optimal?
ƒ When is BFS optimal?
Costs on Actions
Comparisons
ƒ When will BFS outperform DFS?
GOAL
a
2
2
c
b
1
3
2
e
d
3
9
ƒ When will DFS outperform BFS?
2
8
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.
Priority Queue Refresher
Uniform Cost Search
2
b
Expand cheapest node first:
3
Fringe is a priority queue
d
p
15
Cost
contours
c
a 6
a
h 17 r 11
e 5
11
p
9
e
3
d
h 13 r 7
p
f 8
q
q
q 11 c
G 10
2
9
2
e
h
8
q
f
c
G
f
1
ƒ A priority queue is a data structure in which you can insert
and retrieve (key, value) pairs with the following operations:
1
r
q
0
S
b 4
c
8
S
1
G
a
1
p
1
q
16
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.
a
a
4
Uniform Cost Search
Uniform Cost Search
Algorithm
ƒ What will UCS do for this graph?
0
DFS
b
1
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*/ε)
0
a
START
1
b
…
GOAL
We’ll talk more
about uniform cost
search’s failure
cases later…
C*/ε tiers
ƒ What does this mean for completeness?
Uniform Cost Problems
ƒ Remember: explores
increasing cost contours
…
Extra Work?
c≤1
c≤2
ƒ Failure to detect repeated states can cause
exponentially more work. Why?
c≤3
ƒ The good: UCS is
complete and optimal!
ƒ The bad:
ƒ Explores options in every
“direction”
ƒ No information about goal
location
Start
Goal
Graph Search
Graph Search
ƒ In BFS, for example, we shouldn’t bother
expanding the circled nodes (why?)
ƒ Very simple fix: never expand a node twice
S
e
d
b
c
a
a
h
e
h
p
q
q
c
a
r
p
f
q
G
p
q
r
q
f
c
G
a
ƒ Can this wreck correctness? Why or why not?
5
Search Gone Wrong?
Best-First / Greedy Search
Best-First / Greedy Search
Best-First / Greedy Search
ƒ Expand the node that seems closest…
GOAL
a
2
2
h=0
h=8
b
c
1
h=11
2
h=4
h=5
8
2
e
d
3
1
9
h=8
START
h=12
p
15
f
9
h=4
h
4
1
5
h=6
4
3
r
q
h=11
ƒ What can go wrong?
5
h=9
h=6
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)
6