CSCI 4310 Lecture 4: Search 2 Including A*. Book Winston Chapters 4,5,6.

36
CSCI 4310 Lecture 4: Search 2 – Including A*

description

Heuristically Informed Searches Explore the promising choices first Using external information saves work

Transcript of CSCI 4310 Lecture 4: Search 2 Including A*. Book Winston Chapters 4,5,6.

Page 1: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

CSCI 4310Lecture 4: Search 2 – Including A*

Page 2: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Book Winston Chapters 4,5,6

Page 3: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Heuristically Informed Searches Explore the promising choices

first

Using external information saves work

Page 4: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Branch-and-Bound Searching for a path from S to G We know the path S-G has

minimum distance 14. Stop expanding W

S

A C R

VQF WRE

4 3

5

92 3 4 1 7

Page 5: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Branch and Bound Underestimate remaining distance at

each step Book uses straight-line distance

between cities as underestimate of highway distance

Knowing this optimal solution enables bounded tree branches

Heuristic:length total path =length traveled + estimate remaining distance

Page 6: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Redundancy Checks Requires storing partial paths Stop expansion of S-A-E

S

A C E

VQF WRE

4 35

92 3 4 1 7

Page 7: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

One caveat This works for episodic traversal If S-E-Q does not influence paths

from Q to the goal ie. If there is a tripwire on link E-

Q that blows the bridge from Q-R, it’s back to the drawing board

Page 8: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Branch & Bound + Redundancy

S

A D3 4

Total path cost

Page 9: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Branch & Bound + Redundancy

S

A D

DB7 8

43

Page 10: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Branch & Bound + Redundancy

S

A D

DB7 8 A E

43

9 6

Page 11: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Branch & Bound + Redundancy

S

A D

DB7 8 A E

43

9 6

C E B F1211 11 10

Page 12: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

A* Search

Adding techniques A* includes a heuristic for

remaining distance Measure of cost to reach the goal

Page 13: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

A* Search A* algorithm maintains 2 lists OPEN = nodes that need to be

expanded CLOSED = expanded nodes Initial Lists

OPEN = {START NODE} CLOSED = { }

Page 14: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

A* Search f (n) = g (n) + h (n)

f(n): cost from n to goal g(n): cost to get to n h(n): estimate of least cost path

from n to goal f(n) must be maintained by each

node

Using an estimate of remaining distance and dynamic programming

Page 15: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Search algorithm properties Complete:

The algorithm is guaranteed to find a solution when there is one

Optimal: The algorithm finds the path with

the least cost

Page 16: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

A* properties If h(n) never over-estimates the

cost to reach the goal from the present position

h(n) is an admissible heuristic Also sometimes called an optimistic

heuristic If h(n) is admissible, A* is

complete and optimal

Page 17: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

A* Pathfinding Navigation Obstacle Avoidance More non-intuitive applications

8-puzzle

Page 18: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Heuristics for 8-puzzle h1: number of misplaced tiles

Any tile out of place must be moved at least once

h2: sum of the orthogonal distances of the tiles from goal position No tile can move diagonally city block or Manhattan distance “1” is 3 from its goal position

1 23 4 56 7 8

7 2 45 68 3 1

Page 19: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Heuristics for 8-puzzle h1: number of misplaced tiles h2: sum of the orthogonal distances of

the tiles from goal position

Are both heuristics admissible?

Yes1 2

3 4 56 7 8

7 2 45 68 3 1

Page 20: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

A* search algorithmadapted from Bryan Stout, gamasutra

priorityQueue Open, list ClosedAStarSearch

// s is the start nodes.g = 0; s.h = GoalDistEstimate( s ); s.f = s.g + s.h; s.parent = null;

push s on Openwhile Open is not empty {pop node n from Open // n has the lowest fif n is a goal node construct path and return success

for each successor n' of n { // n' is a child of n in the treenewg = n.g + cost(n,n')if n' is in Open or Closed AND n'.g <= newgskipn'.parent = n; n'.g = newg;n'.h = GoalDistEstimate( n' ); n'.f = n'.g + n'.hif n' is in Closedremove it from Closedif n' is not yet in Openpush n' on Open}push n onto Closed}return failure // if no path found

Page 21: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

When path finding goes wrong

Page 22: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Example path finding with A*1 2 3 4 5 6 7

8 13 14

15 16 17 18 20 21

22 27 28

29 30 31 32 33 34 35

start

goal

Page 23: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Example1 2 3 4 5 6 7

8 13 14

15 16 18 20 21

22 27 28

29 30 31 32 33 34

S

G

17

18 161,4 = 5 1,6 = 7

Assumptions (simplifications):

Costs 1 to move to any squareNo diagonal movesHeuristic is minimum number of orthogonal moves

OPEN = 17CLOSED =

Page 24: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Example1 2 3 4 5 6 7

8 13 14

15 16 17 18 20 21

22 27 28

29 30 31 32 33 34

S

G

17

18 161,4 = 5 1,6 = 7

172,5 = 7

We would not actually move hereRemember, path from S to G is not returned until the termination of the algorithm

Nodes already in open or closed list

OPEN = 18,16CLOSED = 17

Page 25: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Example1 2 3 4 5 6 7

8 13 14

15 16 17 18 20 21

22 27 28

29 30 31 32 33 34

S

G

17

18 161,4 = 5 1,6 = 7

172,5 = 7

15 172,7 = 9

Nodes already in open or closed list

Page 26: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Example1 2 3 4 5 6 7

8 13 14

15 16 17 18 20 21

22 27 28

29 30 31 32 33 34

S

G

17

18 161,4 = 5 1,6 = 7

172,5 = 7

15 172,7 = 9

8 22163,6 = 93,8 = 11

With multiple choices from 15, follow the path with theleast f value (22). Recall f is the sum of cost and heuristic

Nodes already in open or closed list

Page 27: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Example1 2 3 4 5 6 7

8 13 14

15 16 17 18 20 21

22 27 28

29 30 31 32 33 34

S

G

17

18 161,4 = 5 1,6 = 7

172,5 = 7

15 172,7 = 9

8 22163,6 = 93,8 = 11

15 29

4,6 = 104,7 = 11

Nodes already in open or closed list

Page 28: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Example1 2 3 4 5 6 7

8 13 14

15 16 17 18 20 21

22 27 28

29 30 31 32 33 34S G

17

18 161,4 = 5

Now a straight shot to the goal

1,6 = 7

172,5 = 7

15 172,7 = 9

8 22163,6 = 93,8 = 11

15 29

4,6 = 104,7 = 11

Nodes already in open or closed list

Page 29: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Game situations more complex

Page 30: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Game situations more complex May have real-time constraints Estimate (heuristic) function will

have more factors Uneven terrain Coming under enemy guns or entering

dangerous areas Weather Shortest is not always the best Go slightly longer but avoid minefield

A* performs well in these situations

Page 31: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Why heuristic underestimates Overestimates can cause us to

miss good paths We can wander away from good

paths permanently, as we believe they are worse than they actually are

Page 32: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Why heuristic underestimates Estimating every path as 0 is an

underestimate, but… This is just breadth-first search We do not gain any performance

Page 33: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Why heuristic underestimates Admissible heuristics just

guarantee optimality We would also like performance

improvement

Page 34: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

A* Considerations Problem is much harder in a

continuous environment Optimizations almost always find a

way to reduce the continuous space into a few discrete choices for consideration

Reduce the branching factor Limit the number of choices you have

at each decision step

Page 35: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Improvements Transforming the Search Space

Usually easier to clean up your search space than tweak your algorithm

Have a scripted opening sequence This is very common in Chess Reduce the space by following a

known good opening

Page 36: CSCI 4310 Lecture 4: Search 2  Including A*. Book Winston Chapters 4,5,6.

Improvements Transforming the Search Space

Increase granularity in pathfinding

Ex: rather than step-by-step from an address in Denton to an address in Brazil

First country by country