190 likes | 276 Views
Artificial Intelligence. Search: 3. Ian Gent ipg@cs.st-and.ac.uk. Artificial Intelligence. Search 3. Part I : Best First Search Part II: Heuristics for 8s Puzzle Part III: A*. Search Reminder. Search states, Search trees Don’t store whole search trees, just the frontier.
E N D
Artificial Intelligence Search: 3 Ian Gent ipg@cs.st-and.ac.uk
Artificial Intelligence Search 3 Part I : Best First Search Part II: Heuristics for 8s Puzzle Part III: A*
Search Reminder • Search states, Search trees • Don’t store whole search trees, just the frontier
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 choices • e.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
Best First Search • Best First Search is still an instance of general algorithm • Need heuristic score for each search state • MERGE: 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
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 loops • bad 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 path • in SAT just want satisfying assignment
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?
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
Manhattan distance heuristic • There is an easy lower bound on #moves required • Just 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
The Eights Puzzle • Inaccuracy of Manhattan • Manhattan distance = ? • optimal solution = 18
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, similarly • Heuristics important to all search algorithms • Almost all problems solved by search solved by good heuristics • Excepting small problems like 8’s puzzle
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
Total distance Heuristic • Can use Manhattan as basis of excellent heuristic • The 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
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 guarantee • A* guarantees that first solution found is optimal • Helpful because we can stop searching immediately • otherwise must continue to find possible better solutions • e.g. in Depth First for 8s puzzle.
The A* Guarantee • A* guarantees to find optimal solution • Proof: suppose not, and we derive a contradiction • Then there is a solution with higher cost found first • must 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)
Branch and Bound • BnB is not always bed and breakfast • Branch and bound is similarly inspired to A* • Unlike A* may not guarantee optimal solution first • As in A*, look for a bound which is guaranteed lower than the true cost • Search 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*
BnB example: TSP • Consider the Travelling Salesperson Problem • Branch and Bound might use depth-first search • Cost so far is sum of costs of chosen edges • Bound 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
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