18
Artificial Intelligence Intelligence Ian Gent [email protected] Search: 3

8 Puzzle 2

Embed Size (px)

Citation preview

Page 1: 8 Puzzle 2

Artificial IntelligenceIntelligence

Ian [email protected]

Search: 3

Page 2: 8 Puzzle 2

Artificial IntelligenceIntelligence

Part I : Best First SearchPart II: Heuristics for 8s PuzzlePart III: A*

Search 3

Page 3: 8 Puzzle 2

3

Search Reminder

Search states, Search trees Don’t store whole search trees, just the frontier

B(a B )

Im p oss ib le

b(a b )

Im p oss ib le

a(a )

C h oose B

B(A B C )

Im p oss ib le

b(A b C )

S o lu tion

C(A C )

C h oose B

cA c

im p oss ib le

A(A )

C h oose C

A B C , A B c , A b C , A b c , aB C , ab C , ab cs ta te = ()C h oose A

Page 4: 8 Puzzle 2

4

Best First Search

All the algorithms up to now have been hard wired I.e. they search the tree in a fixed order use heuristics only to choose among a small number of

choicese.g. which letter to set in SAT / whether to be A or a

Would it be a good idea to explore the frontier heuristically?

I.e. use the most promising part of the frontier?This is Best First Search

Page 5: 8 Puzzle 2

5

Best First Search

Best First Search is still an instance of general algorithm

Need heuristic score for each search stateMERGE: merge new states in sorted order of score

I.e. list always contains most promising state first can be efficiently done if use (e.g.) heap for list

• no, heaps not done for free in Lisp, Prolog.

Search can be like depth-first, breadth-first, or in-between list can become exponentially long

Page 6: 8 Puzzle 2

6

Search in the Eights Puzzle

The Eights puzzle is different to (e.g.) SAT can have infinitely long branches if we don’t check for

loopsbad news for depth-first,still ok for iterative deepening

Usually no need to choose variable (e.g. letter in SAT)there is only one piece to move (the blank)we have a choice of places to move it to

we might want to minimise length of pathin SAT just want satisfying assignment

Page 7: 8 Puzzle 2

7

Search in the Eights Puzzle

Are the hard wired methods effective? Breadth-first very poor except for very easy problems Depth-first useless without loop checking

not much good with it, either Depth-bounded -- how do we choose depth bound? Iterative deepening ok

and we can use increment = 2 (why?)still need good heuristics for move choice

Will Best-First be ok?

Page 8: 8 Puzzle 2

8

Search in the Eights Puzzle

How can we use Best-First for the Eights puzzle?We need good heuristic for rating states Ideally want to find guaranteed shortest solution

Therefore need to take account of moves so far And some way of guaranteeing no better solution

elsewhere

Page 9: 8 Puzzle 2

9

Manhattan distance heuristic

There is an easy lower bound on #moves requiredJust calculate how far each piece is from its goal

add up this for each piece sum is minimum number of moves possible

This is Manhattan distance because pieces move according to Manhattan geometry

Manhattan is not exact Why not?

Can use it as heuristic as estimate of distance to solution makes sense to explore apparently nearest first

Page 10: 8 Puzzle 2

10

The Eights Puzzle

Inaccuracy of Manhattan Manhattan distance = ? optimal solution = 18

1 2 3

8 4

7 6 5

2 1 6

4 8

7 5 3

Page 11: 8 Puzzle 2

11

Using Heuristics

Take the Manhattan distance as an example In Best first, order all states in list by Manhattan In Depth first, order only new states by Manhattan

still hope to explore most promising first

In Breadth first, similarlyHeuristics important to all search algorithms

Almost all problems solved by search solved by good heuristics

Excepting small problems like 8’s puzzle

Page 12: 8 Puzzle 2

12

Manhattan Distance in 8’s

Manhattan distance in 8’s puzzle is NOT a good heuristic

It can be misled Suppose we have a small Manhattan distance for move A but any solution for move A must reverse move A

eventually (e.g. to allow a vital move B) We have in reality made the solution 2 moves longer

moving piece A and then putting it back again Heuristic thinks we are closer to a solution

Infinite loops can occur in Best First + Manhattan

Page 13: 8 Puzzle 2

13

Total distance Heuristic

Can use Manhattan as basis of excellent heuristicThe result will in fact be the A* algorithm

sorry about the name pronounced “A star”

Total distance heuristic takes account of moves so far Manhattan distance + moves to reach this position

This must be a lower bound on #moves from start state to goal state via the current state

Page 14: 8 Puzzle 2

14

A-ghastly name-*

Actually the name is just A*The Total distance heuristic has a guarantee

1. heuristic score is guaranteed lower bound on true path cost via the current state

2. heuristic score of solution is the true cost of solution

A* = Best First + heuristic with this guaranteeA* guarantees that first solution found is optimalHelpful because we can stop searching immediately

otherwise must continue to find possible better solutions e.g. in Depth First for 8s puzzle.

Page 15: 8 Puzzle 2

15

The A* Guarantee

A* guarantees to find optimal solutionProof: suppose not, and we derive a contradiction

Then there is a solution with higher cost found firstmust be earlier in list than precursor of optimal solution

heuristic cost = true cost (by guarantee 2) true cost of worse solution > true cost of optimal true cost of optimal heuristic cost of precursor (guar. 1) true cost of worse solution > heuristic cost of precursor precursor of optimal earlier in list than worse solution Contradiction, w5 (which was what was wanted)

Page 16: 8 Puzzle 2

16

Branch and Bound

BnB is not always bed and breakfastBranch and bound is similarly inspired to A*Unlike A* may not guarantee optimal solution firstAs in A*, look for a bound which is guaranteed lower

than the true costSearch the branching tree in any way you like

e.g. depth first (no guarantee), best first

Cut off search if cost + bound > best solution found If heuristic is cost + bound, search = best first

then BnB = A*

Page 17: 8 Puzzle 2

17

BnB example: TSP

Consider the Travelling Salesperson ProblemBranch and Bound might use depth-first searchCost so far is sum of costs of chosen edgesBound might be cost of following minimum spanning

tree of remaining nodes• MST: tree connected to all nodes of min cost among all such trees

all routes have to visit all remaining nodes can’t possibly beat cost of MST

Bounds often much more sophisticated e.g. using mathematical programming optimisations

Page 18: 8 Puzzle 2

18

Summary and Next Lecture

Summary Best first tries to explore most promising node first In 8s puzzle, Manhattan distance is one heuristic Total distance is much better and has guarantees Best First + Guarantees = A* Branch and Bound also uses guaranteed bounds

Next Lecture: Heuristics in decision problems so far looked at heuristics for optimisation what about when just want any old solution, e.g. SAT Look at heuristics in these situations