You are on page 1of 84

Announcements

Oops! We didnt nish last lecture.


Mea culpa! Im used to smaller tutorials and talks where you can schedule
more =me and the goal is to ensure that everyone understands everything.

Class has been expanded by 50. Please do not sit on steps!


Homework 1: Search

On edX. Due Tue 9/6 at 11:59pm. Instant grading, many submits allowed.

Project 1: Search
Released today! Due Friday 9/16 at 5pm.
Start early and ask ques=ons. Its longer than most!

Week 1 notes released Friday


Sec=ons
You can go to any. Exam prac=ce session begin Monday 9/5.

Contest!

CS 188: Ar=cial Intelligence


Uninformed and Informed Search

Instructors: Josh Hug & Adam Janin


University of California, Berkeley
[These slides were created by Dan Klein, Pieter Abbeel and Anca Dragan for CS188 Intro to AI at UC Berkeley (ai.berkeley.edu)]

Today
Finish up
Uninformed Search
Informed Search
Heuris=cs
Greedy Search
A* Search

Graph Search

Recap: Search

Recap: Search
Search problem:

States (congura=ons of the world)


Ac=ons and costs
Successor func=on (world dynamics)
Start state and goal test

Search tree:
Nodes: represent plans for reaching states
Plans have costs (sum of ac=on costs)

Search algorithm:
Systema=cally builds a search tree
Chooses an ordering of the fringe (unexplored nodes)
Op=mal: nds least-cost plans

Example: Pancake Problem

Cost: Number of pancakes ipped

Example: Pancake Problem

Example: Pancake Problem


State space graph with costs as weights
4
2

3
3

4
3

4
3

2
2

4
3

General Tree Search

Ac=on: ip top two


Cost: 2

Ac=on: ip all four


Path to reach goal:
Cost: 4
Flip four, ip three
Total cost: 7

The One Queue


All these search algorithms are the
same except for fringe strategies
Conceptually, all fringes are priority
queues (i.e. collec=ons of nodes with
alached priori=es)
Prac=cally, for DFS and BFS, you can
avoid the log(n) overhead from an
actual priority queue, by using stacks
and queues
Can even code one implementa=on
that takes a variable queuing object

Breadth-First Search

Breadth-First Search
G

Strategy: expand a
shallowest node rst

Implementa6on: Fringe
is a FIFO queue

d
S

h
p

q
S

d
Search
Tiers

e
h
p
q

q
c
a

h
r

q
G

p
q

r
q

f
c
a

Breadth-First Search (BFS) Proper=es


What nodes does BFS expand?
Processes all nodes above shallowest solu=on
Let depth of shallowest solu=on be s
s tiers
s
Search takes =me O(b )

How much space does the fringe take?

1 node
b nodes
b2 nodes
bs nodes

Has roughly the last =er, so O(bs)

Is it complete?
s must be nite if a solu=on exists, so yes!

Is it op=mal?
Only if costs are all 1 (more on costs later)

bm nodes

Quiz: DFS vs BFS


When will BFS outperform DFS?

When will DFS outperform BFS?

[Demo: dfs/bfs maze water (L2D6)]

Video of Demo Maze Water DFS/BFS (part 1)

Video of Demo Maze Water DFS/BFS (part 2)

Cost-Sensi=ve Search
GOAL

2
c

b
1

8
2

START

15

4
q

2
r

BFS nds the shortest path in terms of number of ac=ons.


It does not nd the least-cost path. We will now cover
a similar algorithm which does nd the least-cost path.

Uniform Cost Search


2

Strategy: expand a
cheapest node first:

S
1

Fringe is a priority queue


(priority: cumulative cost)

15

1
r

S 0

Cost
contours

b 4

a 6

h 17 r 11

e 5

11

h 13 r 7

f 8

q 11 c
a

G 10

f
c
a

16

Uniform Cost Search (UCS) Proper=es


What nodes does UCS expand?
Processes all nodes with cost less than cheapest solu=on!
If that solu=on costs C* and arcs cost at least , then the
eec=ve depth is roughly C*/
C*/ tiers
Takes =me O(bC*/) (exponen=al in eec=ve depth)

How much space does the fringe take?


Has roughly the last =er, so O(bC*/)

Is it complete?
Assuming best solu=on has a nite cost and minimum arc cost
is posi=ve, yes!

Is it op=mal?
Yes! (Proof via A*)

c1
c2
c3

Uniform Cost Issues


Remember: UCS explores increasing cost
contours

c1
c2
c3

The good: UCS is complete and op=mal!


The bad:

Explores op=ons in every direc=on


No informa=on about goal loca=on

Well x that soon!

Start

Goal

[Demo: empty grid UCS (L2D5)]


[Demo: maze with deep/shallow
water DFS/BFS/UCS (L2D7)]

Video of Demo Contours UCS Empty

Video of Demo Maze with Deep/Shallow Water --- DFS, BFS, or UCS? (part 2)

Video of Demo Contours UCS Pacman Small Maze

The One Queue


All these search algorithms are the
same except for fringe strategies
Conceptually, all fringes are priority
queues (i.e. collec=ons of nodes with
alached priori=es).
Prac=cally, for DFS and BFS, you can
avoid the log(n) overhead from an
actual priority queue, by using stacks
and queues.
Can even code one implementa=on
that takes a variable queuing object.
This will be the preferred approach on
project 1.

Up next: Informed Search


Uninformed Search

Informed Search

DFS
BFS
UCS

Heuris=cs
Greedy Search
A* Search
Graph Search

Search Heuris=cs
A heuris=c is:
A func=on that es6mates how close a state is to a goal
Designed for a par=cular search problem
Examples: Manhalan distance, Euclidean distance for
pathing

10
5
11.2

Example: Heuris=c Func=on


Arad
Bucharest
Cralova

Fagaras
Rimnicu V

h(x)

336
0
160
176
193

Greedy Search
Arad
Bucharest
Cralova

Fagaras
Rimnicu V

h(x)

336
0
160
176
193

Example: Heuris=c Func=on


Heuris=c: the number of the largest pancake that is s=ll out of place
3

h(x)

4
3

4
4

3
4

4
3

Greedy Search

Greedy Search
Expand the node that seems closest

What can go wrong?


Resul=ng path to Bucharest is not the shortest!

Greedy Search
Strategy: expand a node that you think is
closest to a goal state

Heuris=c: es=mate of distance to nearest goal for


each state

A common case:

Best-rst takes you straight to the (wrong) goal

Worst-case: like a badly-guided DFS


[Demo: contours greedy empty (L3D1)]
[Demo: contours greedy pacman small maze (L3D4)]

Video of Demo Contours Greedy (Empty)

Video of Demo Contours Greedy (Pacman Small Maze)

A* Search

A* Search

UCS

Greedy

A*

Combining UCS and Greedy


Uniform-cost orders by path cost, or backward cost g(n)
Greedy orders by goal proximity, or forward cost h(n)
8

S
1

S
h=6
c
h=7

a
1

3
h=5

b
h=6

d
h=2

g = 1
h=5 a

h=1
G
h=0

g = 2
h=6
g = 3
h=7

A* Search orders by the sum: f(n) = g(n) + h(n)

g = 0
h=6

d g = 4
h=2

g = 9
e h=1

g = 6
G h=0

g = 10
h=2

g = 12
G h=0
Example: Teg Grenager

When should A* terminate?


Should we stop when we enqueue a goal?
h = 2

2
S

g h +

h = 3

h = 0

h = 1

No: only stop when we dequeue a goal

0 3 3

S->A

2 2 4

S->B

2 1 3

S->B->G 5 0 5
S->A->G 4 0 4

Is A* Op=mal?
h = 6

3
g h +

h = 7

h = 0

5
What went wrong?
Actual bad goal cost < es=mated good goal cost
We need es=mates to be less than actual costs!

S
S->A

0 7 7
1 6 7

S->G

5 0 5

Admissible Heuris=cs

Idea: Admissibility

Inadmissible (pessimis=c) heuris=cs break


op=mality by trapping good plans on the fringe

Admissible (op=mis=c) heuris=cs slow down


bad plans but never outweigh true costs

Admissible Heuris=cs
A heuris=c h is admissible (op=mis=c) if:

where is the true cost to a nearest goal


Examples:
15

11.5

0.0

Coming up with admissible heuris=cs is most of whats involved


in using A* in prac=ce.

Op=mality of A* Tree Search

Op=mality of A* Tree Search


Assume:
A is an op=mal goal node
B is a subop=mal goal node
h is admissible
Claim:
A will exit the fringe before B

Op=mality of A* Tree Search: Blocking


Proof:
Imagine B is on the fringe
Some ancestor n of A is on the
fringe, too (maybe A!)
Claim: n will be expanded before B
1. f(n) is less or equal to f(A)

Deni=on of f-cost
Admissibility of h
h = 0 at a goal

Op=mality of A* Tree Search: Blocking


Proof:
Imagine B is on the fringe
Some ancestor n of A is on the
fringe, too (maybe A!)
Claim: n will be expanded before B
1. f(n) is less or equal to f(A)
2. f(A) is less than f(B)

B is subop=mal
h = 0 at a goal

Op=mality of A* Tree Search: Blocking


Proof:
Imagine B is on the fringe
Some ancestor n of A is on the
fringe, too (maybe A!)
Claim: n will be expanded before B
1. f(n) is less or equal to f(A)
2. f(A) is less than f(B)
3. n expands before B
All ancestors of A expand before B
A expands before B
A* search is op=mal

Proper=es of A*

Proper=es of A*
Uniform-Cost

A*

UCS vs A* Contours
Uniform-cost expands equally in all
direc=ons
Start

A* expands mainly toward the goal,


but does hedge its bets to ensure
op=mality

Start

Goal

Goal

[Demo: contours UCS / greedy / A* empty (L3D1)]


[Demo: contours A* pacman small maze (L3D5)]

Video of Demo Contours (Empty) -- UCS

Video of Demo Contours (Empty) -- Greedy

Video of Demo Contours (Empty) A*

Video of Demo Contours (Pacman Small Maze) A*

Comparison

Greedy

Uniform Cost

A*

A* Applica=ons

A* Applica=ons
Video games
Pathing / rou=ng problems
Resource planning problems
Robot mo=on planning
Language analysis
Machine transla=on
Speech recogni=on

[Demo: UCS / A* pacman =ny maze (L3D6,L3D7)]
[Demo: guess algorithm Empty Shallow/Deep (L3D8)]

Video of Demo Pacman (Tiny Maze) UCS / A*

Video of Demo Empty Water Shallow/Deep Guess Algorithm

Crea=ng Heuris=cs

Crea=ng Admissible Heuris=cs


Most of the work in solving hard search problems op=mally is in coming up
with admissible heuris=cs
Oyen, admissible heuris=cs are solu=ons to relaxed problems, where new
ac=ons are available
366
15

Inadmissible heuris=cs are oyen useful too

Example: 8 Puzzle

Start State

Ac=ons

What are the states?


How many states?
What are the ac=ons?
How many successors from the start state?
What should the costs be?

Goal State

8 Puzzle I

Heuris=c: Number of =les misplaced


Why is it admissible?
h(start) = 8
This is a relaxed-problem heuris=c
Start State

Goal State

Average nodes expanded


when the op=mal path has
4 steps 8 steps 12 steps
UCS
112
6,300 3.6 x 106
TILES
13
39
227
Sta=s=cs from Andrew Moore

8 Puzzle II
What if we had an easier 8-puzzle where
any =le could slide any direc=on at any
=me, ignoring other =les?
Total ManhaMan distance

Start State

Goal State

Why is it admissible?
Average nodes expanded
when the op=mal path has

h(start) = 3 + 1 + 2 + = 18

4 steps 8 steps 12 steps


TILES
MANHATTAN

13
12

39
25

227
73

8 Puzzle III
How about using the actual cost as a heuris=c?
Would it be admissible?
Would we save on nodes expanded?
Whats wrong with it?

With A*: a trade-o between quality of es=mate and work per node
As heuris=cs get closer to the true cost, you will expand fewer nodes but usually
do more work per node to compute the heuris=c itself

Semi-Lazce of Heuris=cs

Trivial Heuris=cs, Dominance


Dominance: ha hc if

Heuris=cs form a semi-lazce:


Max of admissible heuris=cs is admissible

Trivial heuris=cs
Bolom of lazce is the zero heuris=c (what
does this give us?)
Top of lazce is the exact heuris=c

Graph Search

Tree Search: Extra Work!


Failure to detect repeated states can cause exponen=ally more work.
State Graph

Search Tree

Graph Search
In BFS, for example, we shouldnt bother expanding the circled nodes (why?)
S

d
b

e
h
p
q

q
c
a

h
r

q
G

p
q

r
q

f
c
a

Graph Search
Idea: never expand a state twice
How to implement:
Tree search + set of expanded states (closed set)
Expand the search tree node-by-node, but
Before expanding a node, check to make sure its state has never been
expanded before
If not new, skip it, if new add to closed set

Important: store the closed set as a set, not a list


Can graph search wreck completeness? Why/why not?
How about op=mality?

A* Graph Search Gone Wrong?


State space graph

Search tree
S (0+2)

A
1

S
h=2

h=4

h=1

B
h=1

A (1+4)

B (1+1)

C (2+1)

C (3+1)

G (5+0)

G (6+0)

G
h=0

Closed Set: S B C A

Consistency of Heuris=cs
Main idea: es=mated heuris=c costs actual costs

A
h=4
h=2

Admissibility: heuris=c cost actual cost to goal

h=1
3

h(A) actual cost from A to G


Consistency: heuris=c arc cost actual cost for each arc
h(A) h(C) cost(A to C)

Consequences of consistency:

The f value along a path never decreases


h(A) cost(A to C) + h(C)
A* graph search is op=mal

Op=mality of A* Search
With a admissible heuris=c, Tree A* is op=mal.
With a consistent heuris=c, Graph A* is op=mal.
With h=0, the same proofs shows that UCS is op=mal.
Skipping proof because of =me constraints. Available from:
Lecture notes on edge.edx.org.
Previous years lectures on edge.edx.org, edx.org or YouTube.
Any book on the topic (e.g. page 97 in Russell & Norvig).

Op=mality of A* Graph Search

Op=mality of A* Graph Search


Sketch: consider what A* does with a
consistent heuris=c:
Fact 1: In tree search, A* expands nodes in
increasing total f value (f-contours)

Fact 2: For every state s, nodes that reach


s op=mally are expanded before nodes
that reach s subop=mally
Result: A* graph search is op=mal

f 1
f 2
f 3

Op=mality
Tree search:

A* is op=mal if heuris=c is admissible


UCS is a special case (h = 0)

Graph search:

A* op=mal if heuris=c is consistent


UCS op=mal (h = 0 is consistent)

Consistency implies admissibility


In general, most natural admissible heuris=cs
tend to be consistent, especially if from
relaxed problems

Search Gone Wrong?

A*: Summary

A*: Summary
A* uses both backward costs and (es=mates of) forward costs
A* is op=mal with admissible / consistent heuris=cs
Heuris=c design is key: oyen use relaxed problems

Tree Search Pseudo-Code

Graph Search Pseudo-Code

Op=mality of A* Graph Search


Consider what A* does:
Expands nodes in increasing total f value (f-contours)
Reminder: f(n) = g(n) + h(n) = cost to n + heuris=c
Proof idea: the op=mal goal(s) have the lowest f value, so
it must get expanded rst

Theres a problem with this


argument. What are we assuming
is true?

f1
f2
f3

Op=mality of A* Graph Search


Proof:
New possible problem: some n on path to G*
isnt in queue when we need it, because some
worse n for the same state dequeued and
expanded rst (disaster!)
Take the highest such n in tree
Let p be the ancestor of n that was on the
queue when n was popped
f(p) < f(n) because of consistency
f(n) < f(n) because n is subop=mal
p would have been expanded before n
Contradic=on!

You might also like