Introduction to A*
theory.stanford.edu
theory.stanford.edu
I wrote most of these notes in 1997 while working on a game. Little did I realize that it'd be one of my most popular web pages.
The diagrams are colorful but I don't like them (http://simblob.blogspot.com/2013/12/diagrams-on-my-pathfindi...) so I'm now making new interactive diagrams, starting with breadth first search (http://www.redblobgames.com/pathfinding/tower-defense/). While writing that page, I realized that I need to explain graphs (http://www.redblobgames.com/pathfinding/grids/graphs.html) (many game developers don't know graph theory) and suggest optimizations for grids (http://www.redblobgames.com/pathfinding/grids/algorithms.htm...) (a common use case, with interesting variants of A* like Jump Point Search).
I'm also unhappy with the overall structure and navigation so I have a rough plan for how to organize the new pages (https://twitter.com/redblobgames/status/410182845777195008/p...). I'm taking it one page at a time instead of trying to do it all at once. Feedback appreciated!
1. The graph (square grid for now but I'll make other types) — nodes and edges and edge weights 2. The search algorithm (breadth first search for now) — visited, open, costs, parent pointers. 3. The SVG visualization — a polygon for each node colored by its search state, and overlays for text or arrows
When the slider moves, I rewind or advance the search algorithm, which tells me which nodes have changed. I then update those nodes in the diagram. I considered running search once and recording a trace, but it turned out the performance bottleneck was the SVG, not the algorithm, so I didn't bother. It's fast enough to re-run at each step.
I also thought it would be better to have the starting point inside the convex hull or the ending point higher behind it, so that the algorithm would look further than 1 square in the counter-heuristic directions. As it is, the heuristic was exactly correct about the length of the final path, it just happened to start searching to the right instead of starting straight down.
http://grail.cs.washington.edu/projects/crowd-flows/
It's currently being used in Supreme Commander 2, and in the up and coming Planetary Annihilation. Here's a livestream where the developers demonstrate an implementation in an early build of their game:
https://www.youtube.com/watch?v=5Qyl7h7D1Q8&feature=youtu.be...
In my comsci classes, I'm pretty sure we talked about Dijkstra's algorithm...and I'm pretty sure we talked about priority queues. But not until I recently tried implementing the algorithm on my own (just for a fun six-degrees-of-separation network graph) did I realize how important having a priority queue was...I wish the two concepts had been taught in tandem and shown how they relate to algorithm performance (though yes, I do realize, according to Wikipedia, that Dijkstra's original algorithm did not have a min-priority queue).
It'd be fun to go to a college com sci class now and see if these concepts are much better explained now that we have the ability to show them easily via interactive means (I'm sure many here have seen this wonderful site: http://qiao.github.io/PathFinding.js/visual/)...It's not only that they can be seen and interacted with, but it's conceivably much easier for the average com sci student to attempt to build an interactive visualization to demonstrate these concepts....A bit harder when you're working with only C/C++
[1]: https://en.wikipedia.org/wiki/File:Astar_progress_animation....
[2]: https://upload.wikimedia.org/wikipedia/commons/2/23/Dijkstra...
[3]: https://en.wikipedia.org/wiki/User:Subh83/CommonsContrib#Ani...
Do you have plans to share some of your data/sections early? If you'd like, I'm even willing to help proof your dissertation. My email is in my profile.
This is actually a very general insight, and you can drop the "game" from it.
I found this video more helpful initially: https://www.youtube.com/watch?v=eTx6HQ9Veas
OT: As an exercise I did a 6510 Assembly implementation on the c64 a while ago on a pretty tiny grid (20x10 IIRC) and that worked wonderfully. Not sure if there would be any practical use for it, but there you go.
AI course which goes into the details leading up to the A* algorithm (45 minutes)
Lecture Slides: http://faculty.cs.byu.edu/~ringger/Winter2014-CS312/lectures...
Lecture Video: http://faculty.cs.byu.edu/~ringger/Winter2014-CS312/lectures...
There is a part 2 to this lecture called "The Optimality of A* ."
Lecture 2 Slides: http://faculty.cs.byu.edu/~ringger/Winter2014-CS312/lectures...
http://web.cs.du.edu/~sturtevant/papers/incnew.pdf
Specifically, look at the Martelli G family example.
The numbers in the nodes represent heuristic values at each node and the numbers on edges represent edge weights; we're looking to get from S to T.
Our heuristic is admissible because it never overestimates the distance to the target.
Consistency dictates that the heuristic value at any node may at most be the weight of any out-edge added to the heuristic value of the node that the edge connects to. So in our example, the heuristic at A is 3, but at B it is 0, but 3 > 1 + 0, so our heuristic is inconsistent.
The reason this causes issues is because our algorithm assumes that any visited path is the shortest path to the end vertex of that path. So when we traverse this graph, we will visit B first, because the sum of its heuristic value and the respective edge weight is less than that of A.
In other words, if the heuristic is consistent, then it implies that A* will expand the optimal number of states.
Edit: The paper you cited uses a traversal slightly different from traditional A*.
So if you do A* with an inconsistent heuristic, you need to revisit nodes if you explore them a second time with a cheaper cost (i.e., you can re-expand nodes in your closed list). If you do this, you will find optimal solutions even with an inconsistent heuristic.
The only requirement on your heuristic if you A* to find optimal solutions is that it be admissible.
A nitpick: "breadth-first heuristic search" is an algorithm developed by Zhou and Hansen and, while it's related to A* (in that it uses an admissible heuristic to prune the search space), it's not actually a variant of A*. (It's not a best-first search algorithm, since it doesn't expand nodes in increasing order of f cost.)
Dijkstra, A-star and D-star all share a similar structure.
Also, if you were considering D, consider looking at D-Lite. It's an alternate approach to the same problem, and more easily understood/implemented.
http://victor.hwanger.com/a-technical-peek-into-motion-plann...
Breadth-first-search does not prune a search space; it just sorts the search space based on depth. It also assumes the cost of every path is equal.
Example: Finding the shortest road between two cities.
With breadth-first-search, we would consider all connected cities, and then the cities they are connected to, until we reach our destination. We would end up with a route, that travels through the least amount of cities, irregardless of the lengths of the actual roads. We would get the most simple route not the shortest route nor the fastest route.
Feature: calculating using actual costs
To get the shortest route we need to be able to annotate road-length (as cost) in our graph. Using that information we can choose to expand shorter routes before we dive into the longer routes. We might expand A -> B -> C before A -> D because the cummulative road-length of A -> B -> C might be less than the direct road between A -> D. For this to happen we would constantly sort our working set, based on the calculated cost.
As soon as a completed route (to the destination) is at the top of our working set, we can stop and deliver the answer.
Optimization: Pruning
Now, this working set, gets larger with every iteration. If we were to search for the shortest route from A -> D and the answer would be 100 miles, than the working set will contain all routes from A to anywhere, that are less than a 100 miles.
This is very expensive in terms of memory and time: we need to prune the search space. Now, our working set is already sorted based on cost. So we can safely remove every route that ends in the same place as a route above it in the working set. In other words, if A -> B -> C is higher in the working set than A -> D -> C, than we remove A -> D -> C. We simply don't have to expand routes from the same origin twice.
Optimization: using a lower bound
If the costs are truly unpredictable, and we want a perfect answer, this is the best we can do. But costs are rarely unpredictable! Can we provide a lower bound? Sure! Let's use the geospatial distance between two cities. No road is shorter, than the actual distance between the cities.
In this scenario, the costs of our working-set would be calculated as the actual costs of the partial route plus the lower bound. So, the cost of A -> B -> C would be calculated as the actual road length from A upto C plus the distance from C to the destination.
Now this impacts the sorting of the working set. Which is why it is so important it is a lower bound and not an estimate. Because as soon a completed route is at the top of our working-set we call it quits, and deliver the answer.
In a nutshell: A* is a best-first-search that is capable of utilizing a lower bound to sort the search space. It's behavior is far removed from breadth-first-search.
For example, if the task is to find the shortest path between two cities on a highway map, you can calculate a straight-line "as the crow flies" distance to the goal from any point on the map - that is a usable distance metric. A* makes use of that additional information to find the shortest possible path while evaluating the fewest necessary number of alternative routes.
[Caveat: My dad invented A*, so I've probably got this laughably wrong. :-) ]
That being said, great tutorial!