1 cs 2710, issp 2160 chapter 3, part 2 heuristic search

59
1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

Upload: zoe-todd

Post on 18-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

1

CS 2710, ISSP 2160

Chapter 3, Part 2Heuristic Search

Page 2: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

2

Heuristic Search

• Take advantage of information about the problem

Page 3: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

3

Best-First-Search

• An evaluation function f determines order of nodes on the fringe (there are variations, depending on the search algorithm)

Page 4: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

4

Best-First-Search

• In our framework: – treesearch or graphsearch, with nodes ordered on the fringe in

increasing order by an evaluation function, f(n).

Page 5: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

5

def treesearch (qfun,fringe): while len(fringe) > 0: cur = fringe[0] fringe = fringe[1:] if goalp(cur): return cur fringe = qfun(makeNodes(cur,successors(cur)),fringe) return []

best-first search: qfun appends the liststogether and sorts them in increasing order by f-value

[In the more efficient version, a heap is used to maintain the queue in increasing order by f-value]

Page 6: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

6

Heuristic Evaluation Function,

h(n)• F may involve “heuristic evaluation function”, h(n)

Page 7: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

7

h(n)

• Metric on states. Estimate of shortest distance to some goal.• h : state estimate of distance to goal• h (goal) = 0 for all goal nodes

Page 8: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

8

Greedy Best-First Search

• f (n) = h (n)• Greedy best-first search may switch its strategy mid-search.

For example, it may go depth-first for awhile, but then return to the shallow parts of the tree.

Page 9: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

9

Greedy Example

• In the map domain, h(n) could be the straight line distance from a city to Bucharest

• Greedy search expands the node that currently appears to be closest to the goal

Page 10: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

10

Go from Arad to BucharestOradea

Zerind

AradSibiu

Timisoara

Lugoj

Mehadia

Dobreta

Rimnicu Vilcea

Fagaras

Craiova

Pitesti

Giurgiu

Bucharest

Urziceni

Vaslui

Iasi

Neamt

Hirsova

Eforie

71

75

151

140

118

99

80

97

146

138

120

75

70

111101

90

211

85

366

329

374

380

253 176

0

193

160

244

241

Page 11: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

11

Greedy Example

Arad 366

Sibiu 253Zerind 374 Timisoara 329

Arad 366 Oradea 380 Fagaras 178 Rimniciu 193

Bucharest 0Sibiu 253

Page 12: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

12

Greedy Search

• Complete?– Nope

• Optimal?– Nope

• Time and Space?– It depends

Page 13: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

13

Best of Both

• In an A* search we combine the best parts of Uniform-Cost and Best-First.

• [Simple example in lecture]• We want to use the cost so-far to allow optimality and

completeness, while at the same time using a heuristic to draw us toward a goal.

Page 14: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

14

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

g(n): actual cost from start to nh(n): estimated distance from n.state to a

goalEven if h continuously returns good values

for states along a path, if no goal is reached, g will eventually dominate h and force backtracking to more shallow nodes.

Page 15: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

Example

• Figure 3.24 shows the progress of A* on the Romanian route finding problem. The h-values it uses are in Figure 3.22

• The next slide shows the state space with both the edge costs and the h-values in one diagram; this will help you trace through Figure 3.24

15

Page 16: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

16

Go from Arad to BucharestOradea

Zerind

AradSibiu

Timisoara

Lugoj

Mehadia

Dobreta

Rimnicu Vilcea

Fagaras

Craiova

Pitesti

Giurgiu

Bucharest

Urziceni

Vaslui

Iasi

Neamt

Hirsova

Eforie

71

75

151

140

118

99

80

97

146

138

120

75

70

111101

90

211

85

366

329

374

380

253 176

0

193

160

244

241

Page 17: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

17

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

• If h(n) does not overestimate the real cost then the treesearch version of A* is optimal.

• An h function that does not overestimate is called admissible

Page 18: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

18

A* with an admissible heuristic is optimal

• Let: g2 be a suboptimal goal on fringe and gO be an optimal goal, g(gO) = C*

• C* < g(g2) (since g2 is suboptimal)• h(g2) = 0 (since g2 is a goal)• So f(g2) = g(g2) and • C* < f(g2)

Page 19: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

19

Proof continued

• Let n be a node on the fringe that is on an optimal solution path

• Since h is admissible: f(n) = g(n) + h(n) <= C*

• For g2 to be the first goal found, it would need to be first on the fringe

• But f(n) <= C* < f(g2)

Page 20: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

20

A* is complete

• Even if the heuristic is not admissible • (As long as all edgecosts exceed some finite e and that the

branching factor, b, is finite. The wrap-up notes mention these details)

Page 21: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

21

A* and Memory• Does A* solve the memory problems with BrFS and Uniform

Cost?– A* has same or smaller memory requirement than BrFS or Uniform

Cost – How is A* related to BrFS And UC?– BrFS = A* with edgecost(n1,n2) = c, h(n) = 0 (for some positive c)– UC = A* with h(n) = 0– But it might not be sufficiently better to make A* practically

feasible

Page 22: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

22

Note

• Placement of goalp test (and return if successful) in algorithm is critical.

• Optimality guarantee lost if nodes are tested when they are generated [elaboration and example]

• True for uniform-cost search too

Page 23: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

23

Note for A*

• Assuming f-costs are nondecreasing along any path:– Can draw contours in the state space– Inside a contour labeled 300 are all nodes

with f(n) less than or equal to 300– A* fans out from start, expanding nodes in

bands of increasing f-cost.– h(n) = 0 and edgecosts equal: contours are

round– With better heuristics, the bands narrow and

stretch toward the goal node

Page 24: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

24

EG Admissible Heuristics

The 8-puzzle (a small version of the 15 puzzle).

Sample heuristicsH1: Number of misplaced tilesH2: Manhattan distance

Page 25: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

25

8 Puzzle Example

• H1(S) = 7• H2(S) = 2+3+3+2+4+2+0+2 = 18Which heuristic is better?

Page 26: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

26

Informedness

• Let h1 and h2 be admissible heuristics. If h1(n) <= h2(n) for all n, then h2 is more informed than h1 and

• Fewer nodes will be expanded, on average, with h2 than with h1

• The larger values the better (without going over)

Page 27: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

27

A* is often not feasible

• Still a memory hog• What can we do?• Use an iterative deepening style strategy!

Page 28: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

28

IDA*

• Like iterative deepening, but search to f-contours rather than fixed depths.

• Each iteration expands all nodes within a particular f-value range.

Page 29: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

29

Def fLimSearch(fringe,fLim): nextF = INFINITY while fringe: cur = fringe[0] fringe = fringe[1:] curF = cur.gval + h(cur) if curF <= fLim: if goalp(cur): return(cur,curF) succNodes = makeNodes(cur,successors(cur)) for s in succNodes: fVal = s.gval + h(s) if fVal > fLim and fVal < nextF: nextF = fVal fringe = succNodes + fringe return ([],nextF)

Page 30: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

30

def IDAstar(start): result = [] startNode = Node(start) fLim = h(startNode) while not result: result, FLim = fLimSearch([startNode],fLim) return result

Page 31: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

31

IDA*• Worst case, space is O(bd) (if edgecosts are all equal and the

heuristic is admissible; we aren’t analyzing other cases)• Optimal, if h is admissible • The number of iterations grows as the number of possible f

values grow. Let x = average # nodes with the same f-value. The lower x is, the fewer new nodes, on average, are expanded on each iteration.

• Practical if x above is not too low – avoids the overhead of maintaining a sorted queue, and realizes the space savings of depth-first search

Page 32: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

32

Beam Search

• Cheap, unpredictable search• For problems with many solutions, it may be worthwhile to

discard unpromising paths• Greedy best first search that keeps a fixed number of nodes on

the fringe

Page 33: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

33

Beam Search

def beamSearch(fringe,beamwidth): while len(fringe) > 0: cur = fringe[0] fringe = fringe[1:] if goalp(cur): return cur newnodes = makeNodes(cur, successors(cur)) for s in newnodes: fringe = insertByH(s, fringe) fringe = fringe[:beamwidth]return []

Page 34: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

34

Beam Search

• Optimal? Complete?• Hardly!• Space?• O(b) (generates the successors)• Often useful

Page 35: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

35

General Notes before Continuing

Page 36: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

36

Search strategies differ along many dimensions

• Basic strategy: depth-first, breadth-first, least-actual-cost (g(n)), best first (h(n)), or a mixture?

• Is the algorithm iterative, starting by looking at a small part of the state space and then successively looking at larger parts of it? (e.g., iterative deepening and IDA*)

Page 37: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

37

Search strategies differ along many dimensions

• Does it pay attention to cycles? (i.e., our treesearch vs. graphsearch)

• Can it backtrack? Or are parts of the search tree/graph irrevocably pruned? (e.g., beam search)

• Does it only look ahead toward goal (h), or does it also consider how far it has come so far? (g)

Page 38: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

38

A note on optimality• It might be desirable to be greedy (e.g., greedy best-first vs.

A*)• Simon: people are often “satisficers”: often, they stop as soon

as they find a satisfactory solution• Consider choosing a line at the grocery store, or finding a

parking space

Page 39: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

Creating Heuristics

39

Page 40: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

40

Combining Heuristics

• If you have lots of heuristics and none dominates the others and all are admissible…

• Use them all!• h(n) = max(h1(n), …, hm(n))

Page 41: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

Relaxed Heuristic• Relaxed problem

A problem with fewer restrictions on the actions

The cost of an optimal solution to a relaxed problem is an admissible heuristic for the original problem.

41

Page 42: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

42

Relaxed Problems• Exact solutions to different (relaxed) problems• H1 (# of misplaced tiles) is perfectly accurate if a tile could

move to any square• H2 (sum of Manhattan distances) is perfectly accurate if a tile

could move 1 square in any direction

Page 43: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

43

Relaxed Problems• If problem is defined formally as a set of constraints, relaxed

problems can be generated automatically• Absolver (Prieditis, 1993)

– Discovered a better heuristic for 8 puzzle and the first useful heuristic for Rubik’s cube

• Next slide: formal definition of a problem that will allow us to relax it in order to automatically generate heuristics

• This looks forward to the planning section of the course

Page 44: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

Systematic Relaxation

• Precondition List– A conjunction of predicates that must hold true before

the action can be applied• Add List

– A list of predicates that are to be added to the description of the world-state as a result of applying the action

• Delete List– A list of predicates that are no longer true once the

action is applied and should, therefore, be deleted from the state description

• Primitive Predicates– ON(x, y) :tile x is on cell y– CLEAR(y) :cell y is clear of tiles– ADJ(y, z) :cell y is adjacent to cell z

44

Page 45: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

Here is the full definition of s move for the n-puzzle

Move(x, y, z):precondition list ON(x, y), CLEAR(z), ADJ(y, z)add list ON(x, z), CLEAR(y)delete list ON(x, y), CLEAR(z)

45

Page 46: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

(1) Removing CLEAR(z) and ADJ(y, z) gives “# tiles out of place”.

Misplaced distance is 1+1=2 moves

1 2 34 5 6 78 9 10 11

12 13 14 15

15 1 2 34 5 6 78 9 10 11

13 14 12

46

Page 47: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

(2) Removing CLEAR(z) gives “Manhattan distance”.

Manhattan distance is 6+3=9 moves

1 2 34 5 6 78 9 10 11

12 13 14 15

15 1 2 34 5 6 78 9 10 11

13 14 12

47

Page 48: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

Pattern Database Heuristics

The idea behind pattern database heuristics is to The idea behind pattern database heuristics is to store exact solution costs for every possible store exact solution costs for every possible sub-problem instance.sub-problem instance.

48

Page 49: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

Solve part of the problem, ignoring the other tiles

37

1112 13 14 15

14 73

15 1211 13

37

1112 13 14 15

7 1312

15 311 14

37

1112 13 14 15

12 117 14

13 315 49

Page 50: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

50

Pattern Databases• optimal solution cost of the subproblem <= optimal solution

cost of the full problem. • Run exhaustive search to find optimal solutions for every

possible configuration of 3, 7, 11, 12, 13, 14, 15, and store the resulting path costs

• Do the same for the other tiles and space (maybe in two subsets)

• Do this once before any problem solving is performed. Expensive, but can be worth it, if the search will be applied to many problem instances (deployed)

Page 51: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

Pattern Databases

• In our example, we have three subproblems (subsets of 7, 4, and 4 tiles)

• State S has specific configurations of those subsets• h(s)?

51

Page 52: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

• h(s)?• Look up the exact costs for s’s configurations of the 7, 4, and 4

subsets of tiles in the database• Take the max! • The max of a set of admissible heuristics is admissible• What if it isn’t feasible to have entries for all possibilities? ….

52

Page 53: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

• What if it isn’t feasible to have entries for all possibilities? ….• Take the max of:

– The exact costs we do have, and the Manhattan distance for those we don’t have

53

Page 54: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

Sums of admissible heurstics

• We would like to take the sum rather than the max, since the result is more informed

• In general, adding two admissible heuristics might not be admissible

• For example, moves that solve one subproblem might help another subproblem

• But we can choose patterns that are disjoint, so we can sum them

54

Page 55: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

Disjoint Pattern Database Heuristics

Patterns that have no tiles in common. (As in our example) When calculating costs Patterns that have no tiles in common. (As in our example) When calculating costs

for a pattern, only count moves of the tiles in the patternfor a pattern, only count moves of the tiles in the pattern

Add together the heuristic values for the individual patterns.Add together the heuristic values for the individual patterns.

The sum is admissible and more informed than taking the maxThe sum is admissible and more informed than taking the max

55

Page 56: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

Examples for Disjoint Pattern Database Heuristics

1 2 3

4 5 6 7

8 9 10 11

12 13 14 15

5 10 14 7

8 3 6 1

15 12 9

2 11 4 1320 moves needed to solve red tiles25 moves needed to solve blue tiles

Overall heuristic is sum, or 20+25=45 moves

56

Page 57: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

1 2 34 5 6 78 9 10 11

12 13 14 15

5 10 14 78 3 6 1

15 12 92 11 4 13

Overall heuristic is sum of the Manhattan Distance of each tile which is 39 moves.

A trivial example of disjoint pattern database heuristics is Manhattan Distance in A trivial example of disjoint pattern database heuristics is Manhattan Distance in the case that we view every tile as a single pattern databasethe case that we view every tile as a single pattern database

57

Page 58: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

58

Other Sources of Heuristics

• Ad-hoc, informal, rules of thumb (guesswork)• Approximate solutions to problems (algorithms

course)• Learn from experience (solving lots of 8-

puzzles) (later in this course)

Page 59: 1 CS 2710, ISSP 2160 Chapter 3, Part 2 Heuristic Search

Wrap Up• The material in these notes is covered in R&N 3.5-6• The exam will be concerned only with the treesearch, not the

graphsearch, versions of the heuristic search algorithms; any question concerning the graphsearch versions will be about the uninformed algorithms

• Detail to bring out: we assumed in these and the previous notes that all edge costs exceed some finite positive value e; that b (the branching factor) and h-values are finite; and f values are nondecreasing along any path.

• IDA* is optimal in terms of solution time and space for admissible heuristic tree searches. Further reading (not required): R.E. Korf, Depth-First Iterative-Deepening: An Optimal Admissible Tree Search

• A* time and space complexity: Order roughly the number of nodes with f(n) smaller than the cost of the optimal path

59