cse 473: artificial intelligence - university of … · cse 473: artificial intelligence spring...

31
CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi Search with Cost & Heuristics slides from Dan Klein, Stuart Russell, Andrew Moore, Dan Weld, Pieter Abbeel, Luke Zettelmoyer

Upload: truongkhue

Post on 13-Sep-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

CSE 473: Artificial Intelligence Spring 2014

Hanna Hajishirzi Search with Cost & Heuristics

slides from Dan Klein, Stuart Russell, Andrew Moore, Dan Weld, Pieter Abbeel, Luke Zettelmoyer

Page 2: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Announcement

§  PS1 will be on the web soon! §  Start early

2

Page 3: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Recap: Search

§  Search  problem:  §  States  (configura5ons  of  the  world)  §  Successor  func5on;  drawn  as  a  graph  §  Start  state  and  goal  test    

§  Search  tree:  §   Nodes:  represent  plans  for  reaching  states  §   Plans  have  costs  (sum  of  ac5on  costs)  

 3

Page 4: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

General Tree Search

§  Important  ideas:  §  Fringe  §  Expansion  §  Explora5on  strategy  

§ Main  ques5on:  which  fringe  nodes  to  explore?  

§  Search  Algorithms:    §  Systema5cally  builds  a  search  tree  §  Chooses  an  ordering  of  the  fringe  (unexplored  

nodes)  

Page 5: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Outline

 §  Uninformed  Search  Methods  (part  review  for  some)  

§  Depth-­‐First  Search  §  Breadth-­‐First  Search  §  Uniform-­‐Cost  Search  

§  Heuris5c  Search  Methods  (new  for  all)  §  Best  First  /  Greedy  Search  §  A*  

Page 6: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Review: Depth First Search

S

G

d

b

p q

c

e

h

a

f

r

Strategy: expand deepest node first

Implementation: Fringe is a LIFO queue (a stack)

Page 7: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Review: Depth First Search

S

a

b

d p

a

c

e

p

h

f

r

q

q c G

a

q e

p

h

f

r

q

q c G

a

S

G

d

b

p q

c

e

h

a

f

r q p

h f d

b a

c

e

r

Expansion ordering:

(d,b,a,c,a,e,h,p,q,q,r,f,c,a,G)

Page 8: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Review: Breadth First Search

S

G

d

b

p q

c

e

h

a

f

r

Strategy: expand shallowest node first Implementation: Fringe is a FIFO queue

Page 9: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Review: Breadth First Search

S

a

b

d p

a

c

e

p

h

f

r

q

q c G

a

q e

p

h

f

r

q

q c G

a

S

G

d

b

p q

c e

h

a

f

r

Search

Tiers

Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a,h,r,p,q,f,p,q,f,q,c,G)

Page 10: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

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 maximum branching factor B

(the maximum number of successors for a state) C* Cost of least cost solution d Depth of the shallowest solution m Max depth of the search tree

Page 11: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

DFS

§  Infinite paths make DFS incomplete… §  How can we fix this? §  Check new nodes against path from S

§  Infinite search spaces still a problem §  If the left subtree has unbounded depth

Algorithm Complete Optimal Time Space DFS Depth First

Search N N O(BLMAX) O(LMAX)

START

GOAL a

b

No No Infinite Infinite

Page 12: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

DFS

Algorithm Complete Optimal Time Space DFS w/ Path

Checking Y if finite N O(bm) O(bm)

…b 1 node

b nodes

b2 nodes

bm nodes

m tiers

Page 13: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

BFS

§  When is BFS optimal?

Algorithm Complete Optimal Time Space DFS w/ Path

Checking

BFS

Y N O(bm) O(bm)

Y Y* O(bd) O(bd)

…b 1 node

b nodes

b2 nodes

bm nodes

d tiers

bd nodes

Page 14: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Comparisons

§  When will BFS outperform DFS?

§  When will DFS outperform BFS?

Page 15: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue
Page 16: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

16

Page 17: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Iterative Deepening Iterative deepening uses 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 Complete Optimal Time Space DFS w/ Path

Checking

BFS

ID

Y N O(bm) O(bm)

Y Y* O(bd) O(bd)

Y Y* O(bd) O(bd)

…b

Page 18: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Costs on Actions

Notice that BFS finds the shortest path in terms of number of transitions. It does not find the least-cost path.

START

GOAL

d

b

p q

c

e

h

a

f

r

2

9 2

8 1

8

2

3

1

4

4

15

1

3 2

2

Page 19: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Uniform Cost Search

START

GOAL

d

b

p q

c

e

h

a

f

r

2

9 2

8 1

8

2

3

1

4

4

15

1

3 2

2

Expand cheapest node first: Fringe is a priority queue

Page 20: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Uniform Cost Search §  Generaliza5on  of  breadth-­‐first  search  §  Priority  queue  of  nodes  to  be  explored  §  Cost  func5on  f(n)  applied  to  each  node  

Add initial state to priority queue While queue not empty Node = head(queue) If goal?(node) then return node

Add children of node to queue

Page 21: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Priority Queue Refresher

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.

§  Unlike a regular queue, insertions aren’t constant time,

usually O(log n) §  We’ll need priority queues for cost-sensitive search methods

§  A priority queue is a data structure in which you can insert and retrieve (key, value) pairs with the following operations:

Page 22: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Uniform Cost Search

S

a

b

d p

a

c

e

p

h

f

r

q

q c G

a

q e

p

h

f

r

q

q c G

a

Expansion order: (S,p,d,b,e,a,r,f,e,G) S

G

d

b

p q

c

e

h

a

f

r

3 9 1

16 4 11

5

7 13

8

10 11

17 11

0

6

3 9

1

1

2

8

8 1

15

1

2

Cost contours

2

Page 23: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Uniform Cost Search Algorithm Complete Optimal Time Space DFS w/ Path

Checking

BFS

UCS

Y N O(bm) O(bm)

Y Y* O(bd) O(bd)

Y* Y O(bC*/ε) O(bC*/ε)

…b

C*/ε tiers

Page 24: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

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 Start Goal

c ≤ 3

c ≤ 2 c ≤ 1

Page 25: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Uniform Cost: Pac-Man

§  Cost of 1 for each action §  Explores all of the states, but one

Page 26: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Search Heuristics

§  Any estimate of how close a state is to a goal §  Designed for a particular search problem

10

5 11.2

§  Examples: Manhattan distance, Euclidean distance

Page 27: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Heuristics

H(x)

Page 28: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Best First / Greedy Search Best first with f(n) = heuristic estimate of distance to goal

Page 29: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Best First / Greedy Search

§  Expand the node that seems closest…

§  What can go wrong?

253

178

Page 30: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

Best First / Greedy Search §  A common case:

§  Best-first takes you straight to the (wrong) goal

§  Worst-case: like a badly-guided 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

…b

Page 31: CSE 473: Artificial Intelligence - University of … · CSE 473: Artificial Intelligence Spring 2014 Hanna Hajishirzi ... If goal?(node) then return node Add children of node to queue

To Do:

§  Look at the course website: §  http://www.cs.washington.edu/

cse473/14sp §  Do the readings (Ch 3) §  Do PS0 if new to Python §  Start PS1, when it is posted

§  START PS1 ASAP