Announcements
Homework 1:
stanford.ai-class.com To be done individually; due Sunday Review section: Tomorrow 11:00-12:00, Skilling Auditorium
Project 1: Search
cs221.stanford.edu Start early and ask questions You can work in groups of up to 3 people; due Thursday
Contact us
cs221.11.12@gmail.com
CS 221: Artificial Intelligence
Fall 2011
Lecture 2: Search
(Slides from Dan Klein, with help from Stuart Russell, Andrew Moore, Teg Grenager, Peter Norvig)
Outline
Problem-solving agents (Book: 3.1)
Problem types and problem formulation
Search trees and state space graphs (3.3) Uninformed search (3.4)
Depth-first, Breadth-first, Uniform cost Search graphs
Informed search (3.5)
Greedy search, A* search Heuristics, admissibility
3
Agents
act = AgentFn(percept) sensors agent fn
actuators
4
Reflex Agents
Reflex agents:
Choose action based on current state May have memory or a model of the world s current state; use percept Do not consider the future consequences of their actions Act on how the world IS
Strategy: Dots: WNES No Dots: NESW
Can a reflex agent be rational (optimal)?
Goal Based Agents
Goal-based 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 Act on how the world WOULD BE
WWGBAD?
WWGBAD?
Problem types
Fully observable, deterministic
single-belief-state problem
Non-observable
sensorless (conformant) problem
Partially observable/non-deterministic
contingency problem interleave search and execution
Unknown state space
exploration problem execution first
9
Search Problems
A search problem consists of:
A state space
N, 1
A transition model
E, 1
A start state, goal test, and path cost function
A solution is a sequence of actions (a plan) which transforms the start state to a goal state
Transition Models
Successor function
Successors( ) = {(N, 1, ), (E, 1, )}
Actions and Results
Actions( Result( Cost( ) = {N, E} , N) = ; Result( , N, ) = 1; Cost( , E) = , E, )=1
Example: Romania
State space:
Cities
Successor function:
Go to adj city with cost = dist
Start state:
Arad
Goal test:
Is state == Bucharest?
Solution?
State Space Graphs
State space graph: A mathematical representation of a search problem
For every search problem, there s a corresponding state space graph The successor function is represented by arcs
S
a b d h p q r c e f
This can be large or infinite, so we won t create it in memory
Ridiculously tiny search graph for a tiny search problem
Exponential State Space Sizes
Search Problem: Eat all of the food Pacman positions: 10 x 12 = 120 Food count: 30
Ghost positions: 12 Pacman facing: up, down, left, right
Search Trees
N, 1 E, 1
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 paths to those states For most problems, we can never actually build the whole tree
Another Search Tree
Search:
Expand out possible plans Maintain a frontier of unexpanded plans Try to expand as few tree nodes as possible
General Tree Search
Important ideas:
Frontier (aka fringe) Expansion Exploration strategy
Detailed pseudocode is in the book!
Main question: which frontier nodes to explore?
State Space vs. Search Tree
a b d c e h p q r f
Each NODE in in the search tree is an entire PATH in the state space.
d We construct both on demand and we construct as little as possible. b a c a p q h q c a e r f p q h q
e r f c a
G
p q
States vs. Nodes
Nodes in state space graphs are problem states
Represent an abstracted state of the world Have successors, can be goal / non-goal, have multiple predecessors
Nodes in search trees are paths
Represent a path (sequence of actions) which results in the node s state Have a problem state and one parent, a path length, (a depth) & a cost The same problem state may be achieved by multiple search tree nodes
State Space Graph
Search Tree
Parent
Depth 5
Action Node
Depth 6
Depth First Search
Strategy: expand deepest node first Implementation: Frontier is a LIFO stack S
p q a b d h r c e f
d b a c a p q h q c a e r f
G
e h p q q c a r f
G
p q
[demo: dfs]
Breadth First Search
Strategy: expand shallowest node first Implementation: Fringe is a FIFO queue
S
a b d
G c e h q r f
p
S
d Search Tiers b a c a p q h q c a e r f
G
e h p q q c a r f
G
p q
[demo: bfs]
Santayana s Warning
Those who cannot remember the past are condemned to repeat it. George Santayana 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
d b a c a p q h q c a e r f
G
e h p q q c a r f
G
p q
Graph Search
Very simple fix: never expand a state twice
Can this wreck completeness? Lowest cost?
Graph Search Hints
Graph search is almost always better than tree search (when not?) Implement explored as a dict or set Implement frontier as priority Q and set
Costs on Actions
2 b 1 3
START
GOAL
2 c 8 2 d 9 4 h 4 q r 8 2 e f 1 3 2
1 p
15
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.
Uniform Cost Search
2 Expand cheapest node first: Frontier is a priority queue S 1
S
p b a
G 8
c
1
d
2
h
2 1
r f
9
q
15 9
0 e p q 1 16
d b 4 Cost contours a 6 c a
3 11 e 5 h 13 r 7 p q f 8
G 10
h 17 r 11 p q q c a f
G
q 11 c a
Uniform Cost Issues
Remember: explores increasing cost contours The good: UCS is complete and optimal! The bad:
Explores options in every direction No information about goal location
c1 c2 c3
Start
Goal
[demos: ucs, ucs2]
Uniform Cost Search
What will UCS do for this graph?
0 1
START
b 0 1
GOAL
What does this mean for completeness?
AI Lesson
To do more, Know more
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
Greedy Best First Search
Expand the node that seems closest to goal
What can go wrong?
[demos: gbf1, gbf2]
Greedy goes wrong
Best First / Greedy Search
Strategy: expand the closest node to the goal
2 b h=11 3 S h=12 1 p h=11 15 1 d h=8 4 q h=9 4 a h=8 8 9 h 2 G c h=5 2 h=4 e 1 h=6 3 r
[demos: gbf1, gbf2]
2 f 5 h=6
h=0 5
h=4
Combining UCS and Greedy
Uniform-cost orders by path cost, or backward cost g(n) Best-first orders by distance to goal, or forward cost h(n)
5 1 S h=6 c h=7 1 1 1 b h=6 a h=5 3 d h=2 e h=1 2
G h=0
A* Search orders by the sum: f(n) = g(n) + h(n)
A* Search Progress
source: wikipedia page for A* Algorithm; by Subh83
When should A* terminate?
Should we stop when we enqueue a goal?
2 S
h=3
A
h=2
2 G
h=0
B
h=1
No: only stop when we dequeue a goal
Is A* Optimal?
1 A
h=6
3
h=0
h=7
G 5
What went wrong?
Actual bad path cost (5) < estimate good path cost (1+6)
We need estimates (h=7) to be less than actual (5) costs!
Admissible Heuristics
A heuristic h is admissible (optimistic) if:
where
is the true cost to a nearest goal
Never overestimate!
Creating Admissible Heuristics
Most of the work in solving hard search problems optimally is in coming up with admissible heuristics Often, admissible heuristics are solutions to relaxed problems, where new actions are available
366
15 Inadmissible heuristics are often useful too (why?)
Optimality of A*: Blocking
Notation: g(n) = cost to node n h(n) = estimated cost from n to the nearest goal (heuristic) f(n) = g(n) + h(n) = estimated total cost via n G*: a lowest cost goal node G: another goal node
Optimality of A*: Blocking
Proof: What could go wrong? We d have to have to pop a suboptimal goal G off the frontier before G* This can t happen: Imagine a suboptimal goal G is on the queue Some node n which is a subpath of G* must also be on the frontier (why?) n will be popped before G
Properties of A*
Uniform-Cost
b
A*
b
UCS vs A* Contours
Uniform-cost expanded in all directions
Start Goal
A* expands mainly toward the goal, but does hedge its bets to ensure optimality
Start
Goal
[demos: conu, cona]
Example: 8 Puzzle
What are the states? How many states? What are the actions? What states can I reach from the start state? What should the costs be?
8 Puzzle
Heuristic: Number tiles misplaced Why is it admissible? h(start) = 8 This is a relaxed-problem heuristic:
UCS
Average nodes expanded when optimal path has length 4 steps 8 steps 12 steps
112 TILES 13
6,300 39
3.6 x 106 227
Move A to B if adjacent(A,B) and empty(B)
8 Puzzle
What if we had an easier 8-puzzle where any tile could slide one step at any time, ignoring other tiles? Total Manhattan distance Why admissible? h(start) = 3 + 1 + 2 + = 18 Relaxed problem:
TILES
MANHATTAN
Average nodes expanded when optimal path has length 4 steps 8 steps 12 steps
13 12
39 25
227 73
Move A to B if adjacent(A,B) and empty(B)
Trivial Heuristics, Dominance
Dominance: ha hc if
Heuristics form a semi-lattice:
Max of admissible heuristics is admissible
Trivial heuristics
Bottom of lattice is the zero heuristic (what does this give us?) Top of lattice is the exact heuristic
Other A* Applications
Path finding / routing problems Resource planning problems Robot motion planning Language analysis Machine translation Speech recognition
Summary: A*
A* uses both backward costs, g(n), and (estimates of) forward costs, h(n) A* is optimal with admissible heuristics Heuristic design is key: often use relaxed problems A* is not the final word in search algorithms (but it does get the final word for today)