heuristic search - khoury college of computer sciences · 2015. 9. 7. · straight-line distance to...

64
Heuristic Search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley 2. RN, AIMA

Upload: others

Post on 17-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Heuristic Search

Robert PlattNortheastern University

Some images and slides are used from:1. CS188 UC Berkeley2. RN, AIMA

Page 2: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Recap: What is graph search?

Graph search: find a path from start to goal

– what are the states?

– what are the actions (transitions)?

– how is this a graph?

Start state Goal state

Page 3: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Recap: What is graph search?

Graph search: find a path from start to goal

– what are the states?

– what are the actions (transitions)?

– how is this a graph?

Start state

Goal state

Page 4: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Recap: BFS/UCS

Image: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)

Page 5: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Recap: BFS/UCS

Image: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)

Start GoalIt's like this

Notice that we search equally far in all directions...

Page 6: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Idea

Is it possible to use additional information to decide which direction to search in?

Page 7: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Idea

Is it possible to use additional information to decide which direction to search in?

Yes!

Instead of searching in all directions, let's bias search in the direction of the goal.

Page 8: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Example

Stright-line distances to Bucharest

Page 9: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Example

Start state

Goal state

Expand states in order of their distance to the goal

– for each state that you put on the fringe: calculate straight-line distance to the goal

– expand the state on the fringe closest to the goal

Page 10: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Example

Start state

Goal state

Expand states in order of their distance to the goal

– for each state that you put on the fringe: calculate straight-line distance to the goal

– expand the state on the fringe closest to the goal

Heuristic:

Greedy search

Page 11: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Greedy Search

Image: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)

Page 12: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Greedy Search

Each time you expand a state, calculate the heuristic for each of the states that you add to the fringe.

– heuristic:

– on each step, choose to expand the state with the lowest heuristic value.

i.e. distance to Bucharest

Page 13: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Greedy Search

Each time you expand a state, calculate the heuristic for each of the states that you add to the fringe.

– heuristic:

– on each step, choose to expand the state with the lowest heuristic value.

i.e. distance to Bucharest

This is like a guess about how far the state is from the goal

Page 14: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Example: Greedy Search

Page 15: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Example: Greedy Search

Page 16: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Example: Greedy Search

Page 17: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Example: Greedy Search

Path: A-S-F-B

Page 18: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Example: Greedy Search

Notice that this is not the optimal path!

Path: A-S-F-B

Page 19: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Example: Greedy Search

Notice that this is not the optimal path!

Path: A-S-F-B

Greedy Search:– Not optimal– Not complete– But, it can be very fast

Page 20: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Greedy vs UCS

Greedy Search:– Not optimal– Not complete– But, it can be very fast

UCS:– Optimal– Complete– Usually very slow

Page 21: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Greedy vs UCS

Greedy Search:– Not optimal– Not complete– But, it can be very fast

UCS:– Optimal– Complete– Usually very slow

Can we combine greedy and UCS???

Page 22: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Greedy vs UCS

Greedy Search:– Not optimal– Not complete– But, it can be very fast

UCS:– Optimal– Complete– Usually very slow

Can we combine greedy and UCS???

YES: A*

Page 23: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Greedy vs UCS

UCS

Image: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)

Page 24: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Greedy vs UCS

UCS Greedy

Image: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)

Page 25: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Greedy vs UCS

UCS Greedy

A*

Image: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)

Page 26: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

A*

Image: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)

Page 27: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

A*

: a state

: minimum cost from start to

: heuristic at (i.e. an estimate of remainingcost-to-go)

UCS: expand states in order of

Greedy: expand states in order of

A*: expand states in order of

Page 28: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

A*

: a state

: minimum cost from start to

: heuristic at (i.e. an estimate of remainingcost-to-go)

UCS: expand states in order of

Greedy: expand states in order of

A*: expand states in order of

What is “cost-to-go”?

Page 29: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

A*

: a state

: minimum cost from start to

: heuristic at (i.e. an estimate of remainingcost-to-go)

UCS: expand states in order of

Greedy: expand states in order of

A*: expand states in order of

What is “cost-to-go”?– minimum cost required

to reach a goal state

Page 30: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

A*

Slide: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)

S a d

b

Gh=5

h=6

h=2

1

8

11

2h=6

h=0

ch=7

3

e h=1

1

S

a

b

c

ed

dG

G

g = 0 h=6

g = 1 h=5

g = 2 h=6

g = 3 h=7

g = 4 h=2

g = 6 h=0

g = 9 h=1

g = 10 h=2

g = 12 h=0

A*: expand states in order of

Page 31: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

When should A* terminate?

Slide: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)

S

B

A

G

2

3

2

2h = 1

h = 2

h = 0h = 3

Should we stop when we enqueue a goal?

No: only stop when we dequeue a goal

Page 32: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Is A* optimal?

A

GS

1 3

h = 6

h = 0

5

h = 7

What went wrong?Actual cost-to-go < heuristicThe heuristic must be less than the actual cost-to-go!

Slide: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)

Page 33: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

When is A* optimal?

It depends on whether we are using the tree search or the graph search version of the algorithm.

Recall: – in tree search, we do not track the explored set– in graph search, we do

Page 34: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Recall: Breadth first search (BFS)

What is the purpose of the explored set?

Page 35: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

When is A* optimal?

It depends on whether we are using the tree search or the graph search version of the algorithm.

Optimal if h is admissibleOptimal if h is consistent

Page 36: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

When is A* optimal?

It depends on whether we are using the tree search or the graph search version of the algorithm.

Optimal if h is admissible

– h(s) is an underestimate of the true cost-to-go.

Optimal if h is consistent

– h(s) is an underestimate of the cost of each arc.

Page 37: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

When is A* optimal?

It depends on whether we are using the tree search or the graph search version of the algorithm.

Optimal if h is admissible

– h(s) is an underestimate of the true cost-to-go.

Optimal if h is consistent

– h(s) is an underestimate of the cost of each arc.

What is “cost-to-go”?– minimum cost required

to reach a goal state

Page 38: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

When is A* optimal?

It depends on whether we are using the tree search or the graph search version of the algorithm.

Optimal if h is admissible

– h(s) is an underestimate of the true cost-to-go.

Optimal if h is consistent

– h(s) is an underestimate of the cost of each arc.

More on this later...

Page 39: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Admissibility: Example

Stright-line distances to Bucharest

h(s) = straight-line distance to goal state (Bucharest)

Page 40: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Admissibility

Stright-line distances to Bucharest

h(s) = straight-line distance to goal state (Bucharest)

Is this heuristic admissible???

Page 41: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Admissibility

Stright-line distances to Bucharest

h(s) = straight-line distance to goal state (Bucharest)

Is this heuristic admissible???YES! Why?

Page 42: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Admissibility: Example

h(s) = ?

Start state Goal state

Can you think of an admissible heuristic for this problem?

Page 43: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Admissibility

Why isn't this heuristic admissible?

A

GS

1 3

h = 6

h = 0

5

h = 7

Slide: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)

Page 44: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Consistency

What went wrong?

Slide: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)

S

A

B

C

G

1

1

1

23

h=2

h=1

h=4h=1

h=0

S (0+2)

A (1+4) B (1+1)

C (2+1)

G (5+0)

C (3+1)

G (6+0)

State space graph Search tree

Page 45: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Consistency

Cost of going from s to s'

s s'

Page 46: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Consistency

Rearrange terms

Page 47: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Consistency

Cost of going from s to s' implied by heuristic

Actual cost of going from s to s'

Page 48: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Consistency

Cost of going from s to s' implied by heuristic

Actual cost of going from s to s'

Page 49: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Consistency

Consistency implies that the “f-cost” never decreases along any path to a goal state.– the optimal path gives a goal state its lowest f-cost.

A* expands states in order of their f-cost.

Given any goal state, A* expands states that reach the goal state optimally before expanding states the reach the goal state suboptimally.

Page 50: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Consistency implies admissibility

Suppose:

Then:

Page 51: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Consistency implies admissibility

Suppose:

Then:

Page 52: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Consistency implies admissibility

Suppose:

Then: admissible

Page 53: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Consistency implies admissibility

Suppose:

Then:

Page 54: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Consistency implies admissibility

Suppose:

Then:

admissible

Page 55: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Consistency implies admissibility

Suppose:

Then:

admissibleadmissible

Page 56: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Consistency implies admissibility

Suppose:

Then:

Page 57: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

A* vs UCS

Uniform-cost expands equally in all “directions”

A* expands mainly toward the goal, but does hedge its bets to ensure optimality

Start Goal

Start Goal

Slide: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)

Page 58: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

A* vs UCS

Slide: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)

Greedy UCS A*

Page 59: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Choosing a heuristic

The right heuristic is often problem-specific.

But it is very important to select a good heuristic!

Page 60: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Choosing a heuristic

How much better is ?

Consider the 8-puzzle:

: number of misplaced tiles

: sum of manhattan distances between each tile and its goal.

Page 61: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Choosing a heuristic

Consider the 8-puzzle:

: number of misplaced tiles

: sum of manhattan distances between each tile and its goal.

Average # states expanded on a random depth-24 puzzle:

(by depth 12)

Page 62: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Choosing a heuristic

Consider the 8-puzzle:

: number of misplaced tiles

: sum of manhattan distances between each tile and its goal.

Average # states expanded on a random depth-24 puzzle:

(by depth 12)

So, getting the heuristic right can speed things up by multiple orders of magnitude!

Page 63: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

Choosing a heuristic

Consider the 8-puzzle:

: number of misplaced tiles

: sum of manhattan distances between each tile and its goal.

Why not use the actual cost to goal as a heuristic?

Page 64: Heuristic Search - Khoury College of Computer Sciences · 2015. 9. 7. · straight-line distance to the goal – expand the state on the fringe closest to the goal Heuristic: Greedy

How to choose a heuristic?

Nobody has an answer that always works.

A couple of best-practices:– solve a relaxed version of the problem– solve a subproblem