Astar Algorithm
Astar Algorithm
Search
Andrew W. Moore
Professor
School of Computer Science
Carnegie Mellon University
www.cs.cmu.edu/~awm
[email protected]
412-268-7599
Note to other teachers and users of these slides. Andrew would be delighted if you found this source
material useful in giving your own lectures. Feel free to use these slides verbatim, or to modify them to fit
your own needs. PowerPoint originals are available. If you make use of a significant portion of these
slides in your own lecture, please include this message, or the following link to the source repository of Slide 1
Andrew’s tutorials: http://www.cs.cmu.edu/~awm/tutorials . Comments and corrections gratefully received.
Overview
• The inadequacies of “Best First Greedy”
heuristic search.
• Good trick: take account of your cost of getting
to the current state.
• When should the search stop?
• Admissible heuristics
• A* search is complete
• A* search will always terminate
• A*’s dark secret
• Saving masses of memory with IDA* (Iterative
Deepening A*)
Slide 2
Let’s Make “Best first Greedy” Look
Stupid!
4
2 1 1 2
S A B C G
2 1 1 2
S A B C G
Slide 5
When should A* terminate?
Idea: As soon as it generates a goal state?
Look at this example:
1
1 S h=3
B 1
h=7 h=8
A h=2 C
7 D 1
h=1
7
G h=0
Slide 6
Correct A* termination rule:
A* Terminates Only When a Goal State Is Popped
from the Priority Queue
1
1 S h=3
B 1
h=7 h=8
A h=2 C
7 D 1
h=1
7
G h=0
Slide 7
A* revisiting states
Another question: What if A* revisits a state that was
already expanded, and discovers a shorter path?
1
1 S h=3
B 1
h=7 h=8
A C h=2
1/2 D 1
h=1
In this example a state that
had been expanded gets 7
re-expanded. How and G
why?
Slide 8
A* revisiting states
What if A* visits a state that is already on the queue?
h=8
1
1 S h=3
B 1
h=7
A C h=8
1/2 D 1
h=1
In this example a state that had
been on the queue and was note that this h
G 7 value has changed
waiting for expansion had its
from previous
priority bumped up. How and page.
why?
Slide 9
c ost of Remind
The A* Algorithm e m in
(n ) i s
der: g n path to n
e r:
heuristic h(n) is a
e
R
r t e s t k now cost to a stimate of
sh o goal fro
mn
Slide 11
Admissible Heuristics
• Write h*(n) = the true minimal cost to goal
from n.
• A heuristic h is admissible if
h(n) <= h*(n) for all states n.
• An admissible heuristic is guaranteed
never to overestimate cost to goal.
• An admissible heuristic is optimistic.
Slide 12
8-Puzzle Example
Example 1 5 Goal 1 2 3
State State
2 6 3 4 5 6
7 4 8 7 8
Which of the following are admissible heuristics?
• h(n) = Number of tiles in wrong
position in state n
• h(n) = 0 • h(n) = min (2, h*[n])
• h(n) = Sum of Manhattan • h(n) = h*(n)
distances between each tile and • h(n) = max (2, h*[n])
its goal location
• h(n) = 1 Slide 13
A* with Admissible Heuristic
Guarantees Optimal Path
• Simple proof
• Your lecturer will attempt to give it from
memory.
• He might even get it right. But don’t hold
your breath.
Slide 14
Is A* Guaranteed to i.e. is it
Terminate? complete?
• There are finitely many acyclic paths in the search
tree.
• A* only ever considers acyclic paths.
• On each iteration of A* a new acyclic path is
generated because:
– When a node is added the first time, a new path
exists.
– When a node is “promoted”, a new path to that
node exists. It must be new because it’s shorter .
• So the very most work it could do is to look at every
acyclic path in the graph.
• So, it terminates. Slide 15
Comparing Iterative Deepening with A*
From Russell and Norvig, Page 107, Fig 4.8
Slide 18
IDA* : Memory Bounded Search
• Iterative deepening A*. Actually, pretty different from A*. Assume
costs integer.
1. Do loop-avoiding DFS, not expanding any node with f(n)
> 0. Did we find a goal? If so, stop.
2. Do loop-avoiding DFS, not expanding any node with f(n)
> 1. Did we find a goal? If so, stop.
3. Do loop-avoiding DFS, not expanding any node with f(n)
> 2. Did we find a goal? If so, stop.
4. Do loop-avoiding DFS, not expanding any node with f(n)
> 3. Did we find a goal? If so, stop.
…keep doing this, increasing the f(n) threshold by 1 each
time, until we stop.
• This is
Complete
Guaranteed to find optimal
More costly than A* in general.
Slide 19
What You Should Know
• Thoroughly understand A*.
• Be able to trace simple examples of A* execution.
• Understand “admissibility” of heuristics. Proof of
completeness, guaranteed optimality of path.
• Be able to criticize best first search.
References:
Nils Nilsson. Problem Solving Methods in Artificial Intelligence.
McGraw Hill (1971) E&S-BK 501-5353 N71p.
Judea Pearl. Heuristics: Intelligent Search Strategies for Computer
Problem Solving. Addison Wesley (1984) E&S-BK 501-535 P35h.
Chapters 3 & 4 of Stuart Russell and Peter Norvig. Artificial
Intelligence: A Modern Approach.
Slide 20
Proof: A* with Admissible Heuristic Guarantees Optimal Path
• Suppose it finds a suboptimal path, ending in goal state G1
where f(G1) > f* where f* = h* (start) = cost of optimal path.
• There must exist a node n which is
Unexpanded
The path from start to n (stored in the BackPointers(n)
values) is the start of a true optimal path
Why must such a node
• f(n) >= f(G1) (else search wouldn’t have ended) exist? Consider any
because it’s on optimal path s,n1,n2…goal.
• Also f(n) = g(n) + h(n) If all along it were
optimal path
= g*(n) + h(n) expanded, the goal
By the would’ve been reached
<= g*(n) + h*(n) admissibility along the shortest path.
= f* Because n is on assumption
the optimal path
contradicting
So f* >= f(n) >= f(G1) top of slide
Slide 21
Exercise Part 1
In the following maze the successors of a cell include any cell directly to the
east, south, west or north of the current cell except that no transition may pass
through the central barrier. for example successors(m) = { d , n , g }.
a b
c d e
f s h k m n
p q r t g
The search problem is to find a path from s to g. We are going to examine the
order in which cells are expanded by various search algorithms. for example,
one possible expansion order that breadth first search might use is:
shfkpcqarbtdg
There are other possible orders depending on which of two equal-distance-
from-start states happen to be expanded first. For example s f h p k c q r a t b
g is another possible answer. continued->
Slide 22
Exercise Part 1 continued
a b
c d e
f s h k m n
p q r t g
Assume you run depth-first-search until it expands the goal node. Assume
that you always try to expand East first, then South, then West, then North.
Assume your version of depth first search avoids loops: it never expands a
state on the current path. What is the order of state expansion?
Slide 23
Exercise Part 2
a b
c d e
f s h k m n
p q r t g
Slide 25
more Another Example Question
Does this modified version of A* always find the optimal path to a
solution? Why or why not?
Start Start
A … A …
B B
… …
C C
… …
(a) Detecting a Cycle (b) Removing the detected cycle
Slide 26