CS 343H: Artificial Intelligence Week2a: Uninformed Search

advertisement
CS 343H: Artificial Intelligence
Week2a: Uninformed Search
Today
 Agents that Plan Ahead
 Search Problems
 Uninformed Search Methods
 Depth-First Search
 Breadth-First Search
 Uniform-Cost Search
Recall: Rational Agents


A rational agent selects
actions that maximize its
utility function.
Characteristics of the
percepts, environment,
and action space dictate
techniques for selecting
rational actions.
Agent
Sensors
Percepts
?
Actuators
Actions
Environment

An agent is an entity that
perceives and acts.
Reflex Agents
 Reflex agents:
 Choose action based on
current percept (and
maybe memory)
 May have memory or a
model of the world’s
current state
 Do not consider the
future consequences of
their actions
 Consider how the world
IS
 Can a reflex agent be
rational?
Planning Agents
 Plan ahead
 Ask “what if”
 Decisions based on
(hypothesized)
consequences of
actions
 Must have a model of
how the world evolves
in response to actions
 Consider how the
world WOULD BE
Quiz: Reflex or Planning?
Select which type of agent is described:
1. Pacman, where Pacman is programmed to move in the
direction of the closest food pellet
2. Pacman, where Pacman is programmed to move in the
direction of the closest food pellet, unless there is a
ghost in that direction that is less than 3 steps away.
3. A navigation system that first considers all possible
routes to the destination, then selects the shortest
route.
Search Problems
 A search problem consists of:
 A state space
 A successor function
(with actions, costs)
“N”, 1.0
“E”, 1.0
 A start state and a goal test
 A solution is a sequence of actions (a plan)
which transforms the start state to a goal state
Example: Romania
 State space:
 Cities
 Successor
function:
 Roads: Go to adj
city with cost = dist
 Start state:
 Arad
 Goal test:
 Is state ==
Bucharest?
 Solution?
What’s in a State Space?
The world state
specifies every
last detail of the
environment
A search state keeps only the details needed (abstraction)
 Problem 1: Pathing
 States: (x,y) location
 Actions: NSEW
 Successor: update location
only
 Goal test: is (x,y)=END
 Problem 2: Eat-All-Dots
 States: {(x,y), dot booleans}
 Actions: NSEW
 Successor: update location
and possibly a dot boolean
 Goal test: dots all false
State Space Sizes?
 World state:




Agent positions: 120
Food count: 30
Ghost positions: 12
Agent facing: NSEW
 How many
 World states?
120x(230)x(122)x4
 States for pathing?
120
 States for eat-all-dots?
120x(230)
State Space Graphs
 State space graph: A
mathematical representation
of a search problem
 Nodes: abstracted world
configurations
 Arcs: successors (action results)
 Goal test is set of goal nodes
(maybe only one)
 In a search graph, each state
occurs only once!
 We can rarely build this graph
in memory (so we don’t)
G
a
c
b
e
d
f
S
h
p
q
r
Ridiculously tiny search graph
for a tiny search problem
Search Trees
This is now / start
“N”, 1.0
“E”, 1.0
Possible futures
 A search tree:





This is a “what if” tree of plans and outcomes
Start state at the root node
Children correspond to successors
Nodes contain states, correspond to PLANS to those states
For most problems, we can never actually build the whole tree
Quiz
 Consider this 4-state graph:
A
S
G
B
 How big is its search tree (from S)?
Recall: Romania example
Searching with a search tree
 Search:
 Expand out possible plans
 Maintain a fringe of unexpanded plans
 Try to expand as few tree nodes as possible
General Tree Search
 Important ideas:
 Fringe
 Expansion
 Exploration strategy
Detailed pseudocode
is in the book!
 Main question: which fringe nodes to explore?
Example: Tree Search
G
a
c
b
e
d
f
S
h
p
Fringe (potential plans)
q
r
Tree
State Graphs vs. Search Trees
G
a
Each NODE 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 construct both
on demand – and
we construct as
little as possible.
b
c
a
a
e
h
p
q
q
c
a
h
r
p
f
q
G
p
q
r
q
f
c
a
G
Depth First Search
G
a
Strategy: expand
deepest node first
c
b
Implementation:
Fringe is a LIFO
stack
State graph
e
d
f
S
h
p
r
q
S
Search tree
e
d
b
c
a
a
e
h
p
q
q
c
a
h
r
p
f
q
G
p
q
r
q
f
c
a
G
Quiz
 Which solution would depth-first search find if run on the
graph below? Assume ties are broken alphabetically.
For example, a partial plan S->X->A would be expanded
before S->X->B; similarly, S->A->Z would be expanded
before S->B->A
Search Algorithm Properties
Complete? Guaranteed to find a solution if one exists?
Optimal? Guaranteed to find the least cost path?
Time complexity? ~How many nodes get expanded?
Space complexity? ~How big can the fringe get?
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?
DFS
 With cycle checking, DFS is complete.*
…
1 node
b
b nodes
b2 nodes
m tiers
bm nodes
Algorithm
DFS
w/ Path
Checking
Complete
Optimal
Y
N
 When is DFS optimal?
Time
O(bm)
Space
O(bm)
* Or graph search – next lecture.
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
e
d
Search
Tiers
b
c
a
a
e
h
p
q
q
c
a
h
r
p
f
q
G
p
q
r
q
f
c
a
G
Quiz
 Which solution would BFS find if run on
this graph?
BFS
Algorithm
DFS
w/ Path
Checking
BFS
Complete
Optimal
Y
N
O(bm)
O(bm)
Y
N*
O(bs)
O(bs)
s tiers
…
b
Time
Space
1 node
b nodes
b2 nodes
bs nodes
bm nodes
 When is BFS optimal?
BFS complexity: concretely
s
 Russell & Norvig
Quiz
 Which are true about BFS? (b is the branching
factor, s is the depth of the shallowest solution)
 At any given time during the search, the number of
nodes on the fringe can be no larger than bs.
 At any given time during the search, the number of
nodes on the fringe can be as large as b^s.
 The number of nodes considered throughout the
entire search can be no larger than bs.
 The number of nodes considered throughout the
entire search can be as large as b^s.
Comparisons
 When will BFS outperform DFS?
 When will DFS outperform BFS?
Iterative Deepening
Iterative deepening: BFS using DFS as a subroutine:
1. Do a DFS which only searches for paths of
length 1 or less.
2. If “1” failed, do a DFS which only searches paths
of length 2 or less.
3. If “2” failed, do a DFS which only searches paths
of length 3 or less.
….and so on.
Algorithm
DFS
w/ Path
Checking
Complete Optimal
Time
…
b
Space
Y
N
O(bm)
O(bm)
BFS
Y
N*
O(bs)
O(bs)
ID
Y
N*
O(bs)
O(bs)
Costs on Actions
GOAL
a
2
2
c
b
1
3
2
8
2
e
d
3
9
8
START
p
15
2
h
4
1
f
4
q
2
r
Notice that BFS finds the shortest path in terms of number of
transitions. It does not find the least-cost path.
Uniform Cost Search
2
b
Expand cheapest node first:
d
S
1
p
15
Cost
contours
c
a 6
a
h 17 r 11
e 5
11
p
9
e
3
b 4
h 13 r 7
p
f 8
q
q
q 11 c
a
G 10
2
9
2
e
h
8
q
f
c
a
G
f
2
1
r
q
0
S
d
c
8
1
3
Fringe is a priority queue
(priority: cumulative cost)
G
a
p
1
q
16
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 decrease a key’s priority by pushing it again
 Unlike a regular queue, insertions aren’t constant time,
usually O(log n)
 We’ll need priority queues for cost-sensitive search methods
Quiz
 Which solution would uniform cost search
find if run on the graph below?
Uniform Cost Search
 Remember: explores
increasing cost contours
…
c1
c2
c3
Uniform Cost Search
Algorithm
DFS
w/ Path
Checking
Complete Optimal
Time
Space
Y
N
O(bm)
O(bm)
BFS
Y
N
O(bs)
O(bs)
UCS
Y
Y
O(bC*/)
O(bC*/)
…
C*/ tiers
b
Uniform Cost Issues
 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
Search Gone Wrong?
Summary
 Agents that Plan Ahead
 Search Problems
 Uninformed Search Methods
 Depth-First Search
 Breadth-First Search
 Uniform-Cost Search
 Next time: informed search, A*
 If we had arbitrarily large negative costs,
we would have to explore the entire state
space to get an optimal solution.
 Any path, no matter how bad it appears, might
lead to an arbitrarily large reward (negative
cost). Therefore, one would need to exhaust
all possible paths to be sure of finding the
best one.
Search Heuristics
 Any estimate of how close a state is to a goal
 Designed for a particular search problem
 Examples: Manhattan distance, Euclidean distance
10
5
11.2
Heuristics
Best First / Greedy Search
 Expand the node that seems closest…
 What can go wrong?
[demo: greedy]
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
 Like DFS in completeness
(finite states w/ cycle
checking)
…
b
Some hints
 Graph search is almost always better than
tree search (when not?)
 Implement your closed list as a dict or set!
 Nodes are conceptually paths, but better to
represent with a state, cost, last action, an
d reference to the parent node
Extra Work?
 Failure to detect repeated states can cause
exponentially more work (why?)
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
a
h
r
p
f
q
G
p
q
r
q
f
c
a
G
Graph Search
 Very simple fix: never expand a state type twice
 Can this wreck completeness? Why or why not?
 How about optimality? Why or why not?
Some Hints
 Graph search is almost always better than
tree search (when not?)
 Implement your closed list as a dict or set!
 Nodes are conceptually paths, but better to
represent with a state, cost, last action, and
reference to the parent node
Best First Greedy Search
Algorithm
Complete Optimal
Greedy Best-First
Search
Y*
Space
O(bm)
N
…
Time
b
m
 What do we need to do to make it complete?
 Can we make it optimal? Next class!
O(bm)
Uniform Cost Search
 What will UCS do for this graph?
0
1
START
b
0
a
1
GOAL
 What does this mean for completeness?
Best First / Greedy Search
 Strategy: expand the closest node to the goal
2
2
c
h=8
b
1
h=11
9
h=8
S
h=5
2
8
d
3
h=12
G
a
p
h=11
15
h h=6
q
h=9
4
2
e
1
4
1
h=4
h=0
5
3
f
9
h=4
5
r
h=6
[demo: greedy]
Example: Tree Search
G
a
c
b
e
d
f
S
h
p
q
r
Download