You are on page 1of 26

Introduction To Informed Search & A*

G51IAI Introduction to AI Andrew Parkes


http://www.cs.nott.ac.uk/~ajp/

PLEASE NOTE
MONDAY OCT 30th NO FORMAL LECTURE! Question & Answer instead
(you ask the questions!), especially regarding the coursework(s).

Bring Questions!

Shortest Path Problems


Common in real-life problems
e.g. select routes for planes to minimise fuel use

With that

planning and puzzles we saw

finding a plan or solving a puzzle becomes find a path

But the relevant graphs are now


implicit nodes and edges are only given by rules exponentially larger than occurs with explicit maps ) 15-puzzle has 16!/2 states (approx 1013

Shortest Path Problems


When the graph becomes too large blind search is no longer practical Problem is that blind search does not look in the right places To see this think of the search pattern of Uniform Cost Search On the next 2 slides we will see an example based on a map of Romania from AIMA
These include an animation of how UCS performs the search, hence printed version will be a mess! (BTW I removed the Oradea-Sibiu edge to simplify the animation and fix an error in an earlier version)

Study this map of Romania. the two cities takes between cities and try and calculate The optimal route between Note the distances us through Rimnicu and Pitesti. the shortest route between Sibiu and Bucharest. Then press space to see the Press space to continue with the slideshow. optimal route marked in red. Neamt
Zerind 75 71 Oradea 87 Iasi 92 Vaslui Sibiu 80 Lugoj 70 Mehadia 146 75 Dobreta 120 Craiova 138 Rimnicu 97 Pitesti 101 211 Urziceni 86 98 Hirsova 99 Faragas 142

Arad
140 118 Timisoara 111

Optimal route is (80+97+101) = 278 miles

Bucharest
90 Giurgui

86

Eforie

Nodes Expanded 1.Sibiu 2.Rimnicu 215 Zerind 75

ANIMATION OF UCS.
3.Faragas 4.Arad 5.Pitesti 6.Zerind 7.Craiova 9.Bucharest 278 GOAL!! Fringe in RED with g Visited in BLUE 87 Iasi 92 Vaslui 142 Hirsova Neamt 8.Timisoara

71

286 Oradea

Arad
118

140 140 Sibiu

Optimal route is (80+97+101) = 278 miles 99 99 Faragas

Timisoara 80 111 258 80 211 Lugoj Rimnicu 98 Urziceni 369 177 86 70 97 Pitesti Mehadia 146 101 Bucharest 75 138 310 (F) Dobreta 278 (R,P) 90 226 (R) 120 Craiova 336 [315 (R,P)] Giurgui

86

Eforie

Remarks
We expanded Arad and Zerind even though they are in totally the wrong direction! The search pattern was expanding concentric circles

Uniform Cost Search in Graphs


Search Pattern: smaller cost nodes before larger Fringe in red Visited in blue Blind: still ignores goals This region is basically wasted effort outline of graph goal start

increasing cost

Heuristic Search
Desired Search Pattern: biased in direction of goal(s) Fringe in red Visited in blue When reaches goal will have had to explore less of the graph Reduces time and memory usage outline of graph goal start

increasing cost

Heuristic Search
Want to achieve this pattern but stay
complete optimal outline of graph goal start

If bias the search too much then could miss goals or miss shorter paths

increasing cost

More Realistic Picture


In real problems the graphs are messy and the search pattern not clean What we think is correct direction might be ultimately wrong Standard solution to stay complete and optimal:
A* search pronounced A star

outline of graph

start

goal

But first do a more general heuristic method:


Best First Search

increasing cost

Heuristic Search
Suppose have some estimates, h , of the cost from a (fringe) node to the goal Want to favour small h Expand A first? outline of graph hA hB goal

start

gA gB

A B

increasing cost

Best First Search (BestFS)


Give each node, n, a score or fullcost : f(n) Usually f(n) is based g and h
Small f values are better.

BestFS is same as UCS except use f(n) instead of just g(n) Search nodes with f before those of f+1 BestFS Queue:
explicitly store the f value of a node with that node want smallest f value first if using REMOVE-FIRST then sort the queue in increasing order alternatively, instead of REMOVE-FIRST, just scan all queue and use REMOVE-SMALLEST-SCORE

Best First Search


PROCEDURE BestFS( problem, SCORE_FN ) 1. fringe MAKE-EMPTY-QUEUE() 2. fringe INSERT( start_node ) // with its f value 3. loop {
1. 2. 3. 4. 5. if fringe is empty then return false // finished without goal node REMOVE-SMALLEST-SCORE(fringe) if node is a goal
1. 2. print node and g return true // that found a goal

EXPAND(node) // Lf is set of neighbours with their f values // obtained using SCORE_FN fringe INSERT-IF-NEW(Lf, fringe ) // ignore revisited nodes // unless they are better Lf

UCS & BestFS


UCS just uses f = g

PROCEDURE UCS( problem ) return BestFS( problem , g )

Greedy Search
Greedy First: try to move to nodes that are as close to the goal as possible Do not know true cost to goal, so just use the estimate h Want to implement Expand small h first : simply use f = h PROCEDURE Greedy( problem) return BestFS( problem, h )

Greedy Search
Dives in what h says is the best direction This direction is just an estimate and can be wrong
outline of graph goal start

Properties of Greedy Search


Incomplete: if nodes are not checked for revisiting then it can be incomplete it can get stuck in a loop. Nonoptimality: sometimes the best short-term decision is sub-optimal in the long term
not surprising because it ignores the actual path cost g

Practical: It is often a good way to solve big problems


it is fast the memory usage is small because the search dives towards a solution (this makes it more like Depth-First Search) often the solutions are acceptable even when sub-optimal

Self-Study: Carefully go through the map of Romania examples in heuristic-searches.htm in the heuristic searches section or the course notes

Perfect Information Search


Suppose that by magic (by an oracle in CS parlance) we knew the True minimum cost from any node N to the goal I.e. suppose we know hT(n) In UCS we can expect to know gT(n) True min cost, fT(n) for going via n , fT(n) = gT(n) + hT(n)

Perfect Information Search


Supposing that could use the true cost in the search, we could do BestFS( problem, fT(n) ) Properties:
it would never expand a node that was not on some minimum cost path because such non perfect nodes will never be at the front of the queue the search would never make a mistake

Comparisons
1. UCS: BestFS( g )
complete, optimal, high memory usage

2. Perfect-Info: BestFS( gT(n) + hT(n) )


complete, optimal, low memory usage

3. Greedy:

BestFS(

incomplete, suboptimal, low memory usage

A* Search
Cannot implement Perfect Info! Try to get best combination
use g(n) : best path cost so far use heuristic h(n), as do not have the true hT(n)

A* Search is
BestFS( g + h )

That is, define f(n) = g(n) + h(n)


estimated minimum cost via n and use BestFS( f(n) )

Effects of h choices
Two extreme cases
h=0 A* becomes UCS : complete&optimal but search pattern undirected h too large : if h is large enough to dominate g then becomes like Greedy. uses less memory but lose optimality.

Usually when we say A* we exclude the last case, and imply that we have an: Admissible Heuristic: h(n) hT(n)
we never over-estimate distance h(n) must provide a valid lower bound on cost to the goal the g term does not get swamped and misled, but the search pattern improves A* is complete and optimal, and memory usage can be much lower than UCS

A* with Admissible Heuristic


Complete and Optimal Search
as for Uniform Cost Search

Search Pattern seeks out goals


Reduced memory and time usage

It is good on maps/graphs It is essential on planning and puzzles

Summary
Best First Search
similar to Uniform Cost Search but with a general score derived from g and h Greedy Search: use h only dive towards goals, but can miss best paths A* search with Admissible Heuristic

Self-study: heuristic searches section of the website Expectations:


Given an example,
e,g, a map or graph or a small planning problem or puzzle,

then you should be able to work through what BestFirst, UCS, Greedy, and A* would do

Next lecture: The example of A* in astar.ppt

Questions?

You might also like