# A* speed

## Recommended Posts

Delpee    109
[size=3]Hello GameDevs,[/size]

[size=3]I recently did an implementation of the[b] A* algorithm[/b]. I'm pretty satisfied with the result as this is my first real 'adventure' in the area of AI (I have been programming for about 7 years). The implementation delivers results pretty quick and accurate.[/size]
[size=3]While implementing a question came to mind, [b]how fast is fast?[/b][/size]

[size=3]Here are some images of the implementation in practice:[/size]

[size=3]Agent moving a long distance[/size]
[size=3][img]http://i207.photobucket.com/albums/bb39/Delpeepic/screenshot003edit-1.png[/img][/size]

[size=3]Agent moving through a maze[/size]
[size=3][img]http://i207.photobucket.com/albums/bb39/Delpeepic/screenshot001edit-1.png[/img][/size]

[size=3]Two videos showing the implementation in practice:[/size]

[size=3]To see how fast my implementation worked I did a stresstest. I made the program calculate thousands of paths from random startpoints to random endpoints in my grid. I did this in an empty grid of 40 by 23, an obstacle-filled grid of 40 by 23 and an empty grid of 100 by 100.[/size]
[size=3]These are the results:[/size]
[size=3]40 by 23, no obstacles[/size]
[size=3][img]http://i207.photobucket.com/albums/bb39/Delpeepic/Xtrememode660ppsnoterrain40by23result.png[/img][/size]

[size=3]100 by 100, no obstacles[/size]
[size=3][img]http://i207.photobucket.com/albums/bb39/Delpeepic/Xtrememode360ppseasyterrain100by100result-1.png[/img][/size]

[size=3]40 by 23, difficult terrain[/size]
[size=3][img]http://i207.photobucket.com/albums/bb39/Delpeepic/Xtrememode120ppsdificultterrain40by23result-1.png[/img][/size]

[size=3]Note that I'm mainly concerned about the average calculation time, as you can see, at the 40 by 23 grid with a difficult terrain there is an average calculation time of about 8ms. Also note that I don't have diagonal search, only horizontal and vertical.[/size]

[size=3]My question to the GameDev boards; [b]Is this an acceptable speed, can it be categorized as fast, or slow?[/b][/size]

[size=3]Discuss...[/size]

[size=3]Yuri van Geffen[/size]

##### Share on other sites
Waterlimon    4398
I think you can double the speed on mostly empty areas by spreading from both the start and end and finding the path when the 2 areas collide somewhere, but in a tight maze it might be slower...

##### Share on other sites
Delpee    109
[quote name='Waterlimon' timestamp='1330701966' post='4918617']
I think you can double the speed on mostly empty areas by spreading from both the start and end and finding the path when the 2 areas collide somewhere, but in a tight maze it might be slower...
[/quote]

That is indeed something to think about. In practice though the world is probably not empty (otherwise there is no point to A*), so I don't know if it would make that much of a difference though.

##### Share on other sites
jefferytitan    2523
[quote name='Delpee' timestamp='1330703015' post='4918623']
That is indeed something to think about. In practice though the world is probably not empty (otherwise there is no point to A*), so I don't know if it would make that much of a difference though.
[/quote]

It depends upon the context. For example, the Fallout 3 and New Vegas worlds are by no means empty, but there are still large flat(ish) areas that could bog down A*.

##### Share on other sites
Cornstalks    7030
[quote name='Waterlimon' timestamp='1330701966' post='4918617']
I think you can double the speed on mostly empty areas by spreading from both the start and end and finding the path when the 2 areas collide somewhere, but in a tight maze it might be slower...
[/quote]
You have to be careful with that, as you could easily double your running time too. Imagine the start in the top left corner, and end in the bottom right. The search from the start could go down the bottom and then to the right, while the search from the end could to go the top and then to the left, completely missing each other. You could also have the start and end on a diagonal, but just off by one, and the two searches could go by each other the entire time but just miss each other.

For Dijkstra's, it just might be effective, but for A* I'd be really careful with that.

##### Share on other sites
Hi, I am pretty new with A*. I have implemented one but somehow I face a weird path of A*. Would you like to help me? Thanks
[img]http://img850.imageshack.us/img850/8779/weirdastarpath.png[/img]

##### Share on other sites
Cornstalks    7030
[quote name='Nemean Charisma' timestamp='1330942956' post='4919406']
Hi, I am pretty new with A*. I have implemented one but somehow I face a weird path of A*. Would you like to help me? Thanks
[/quote]
You should probably start a new thread (so you don't hijack this one) and post some code of what you're doing in your program so people can help you. I'm sure there would be people willing to help

@OP: 71ms for a 40x23 map seems like a longish time to me (I know, the average is ~7.8). Was that just a weird burp in the system (i.e. maybe another process was started up that consumed some of the CPU and the OS gave less CPU time slices), or did you see ~71ms more than once?

I'm not sure how fast it compares to professional games, so I can't tell you that. Have you run it with a profiler to find your bottlenecks though?

##### Share on other sites
Delpee    109
[quote name='Cornstalks' timestamp='1330953939' post='4919431']
@OP: 71ms for a 40x23 map seems like a longish time to me (I know, the average is ~7.8). Was that just a weird burp in the system (i.e. maybe another process was started up that consumed some of the CPU and the OS gave less CPU time slices), or did you see ~71ms more than once?
[/quote]
71ms is indeed a long time. I'm not realy sure why, but sometimes the calc-time spikes. Could have something to do with with some other program running at the same time (maybe the snipping tool I used for screenshot?). I'm not realy worrying as the average is way lower. Sometimes it's also way lower then average.

[quote name='Cornstalks' timestamp='1330953939' post='4919431']
I'm not sure how fast it compares to professional games, so I can't tell you that. Have you run it with a profiler to find your bottlenecks though?
[/quote]
Good tip, will do! I will post the results.

I just see the first picture of the stresstest isn't right, replacing it right now!

##### Share on other sites
Delpee    109
I just tested my implementation on a 100x100 randomly generated world (1 in 10 tiles are blocked). After about a second I consistently get a Stack Overflow error. Is this because I use lists to store my closed nodes?

alvaro    21246

##### Share on other sites
bubu LV    1436
Not sure about differences in PC's, but my A* implementation finds path on 400x400 maze field that stretches from one corner to another in ~7 msec.

##### Share on other sites
smr    2468
There are many different reasons why your A* implementation may not perform well. The most common reasons in my experience have typically been:[list=1]
[*]Using the wrong heuristic
[*]Poorly managing the open list (for example, using a slow sort or next-best search)
[*]Having a closed list.
[/list]

Make sure you're using the correct distance approximation heuristic for your map. For 2D tile-based games Manhattan distance is usually a good heuristic.

If you can avoid iterating over the entire open list each time you need to check the next grid cell then do so.

##### Share on other sites
Delpee    109
Thanks smr for your response. I think it mainly has to do with the iteration of my closed list. My heuristic is allready calculated Manhattan style. Maybe I could improve the open list a bit, but I surely should get rid of the closed one as everyone is telling me ;).

##### Share on other sites
zalzane    191
OP, probably one of the most overlooked optimizations for a* is that instead of maintaining seperate open and closed "lists", have a 2d array of chars that represent the state of a node (0=n/a, 1=open, 2=closed, 3=unpassable), with a seperate 2d array of uints for important node-specific statistics such as distance travelled.

Another extremely useful optimization is to use binary heaps to sort your open list values. I recall that when I tested the binary heap sort versus the std::list sort, the difference in speed was half an order of magnitude, even when there were very few values in the heap which is one of the binary sorting algorithm's weaknesses.

##### Share on other sites
wodinoneeye    1689
[quote name='Delpee' timestamp='1330971871' post='4919525']
[quote name='Cornstalks' timestamp='1330953939' post='4919431']
@OP: 71ms for a 40x23 map seems like a longish time to me (I know, the average is ~7.8). Was that just a weird burp in the system (i.e. maybe another process was started up that consumed some of the CPU and the OS gave less CPU time slices), or did you see ~71ms more than once?
[/quote]
71ms is indeed a long time. I'm not realy sure why, but sometimes the calc-time spikes. Could have something to do with with some other program running at the same time (maybe the snipping tool I used for screenshot?). I'm not realy worrying as the average is way lower. Sometimes it's also way lower then average.

[quote name='Cornstalks' timestamp='1330953939' post='4919431']
I'm not sure how fast it compares to professional games, so I can't tell you that. Have you run it with a profiler to find your bottlenecks though?
[/quote]
Good tip, will do! I will post the results.

I just see the first picture of the stresstest isn't right, replacing it right now!
[/quote]

Excluding the rendering from the recorded time ???

An A* testing app I helped work on years ago had selectable parameter for render every X path steps (was a 512x512 map) along with high performance timers for the actual recorded A* processing time. (heapQ, flags on map etc.. amazing how far its performance we managed to increase)

##### Share on other sites
Delpee    109
[quote name='wodinoneeye' timestamp='1331272457' post='4920587']
Excluding the rendering from the recorded time ???
[/quote]

What I do is get a DateTime.Now before calling the algorithm-function and directly after the function returns the path I take the difference between the old DateTime.Now and the current one. In this way I exclude any other code from the time tracking.

##### Share on other sites
slicer4ever    6760
how are you sorting your open list? pushing/popping items off the list is probably one of the most time consuming aspect's of pathfinding systems. binary heap would speed up most systems 10 fold if your not already using them.

##### Share on other sites
ApochPiQ    22999
For comparison, I had an A* implementation that could cover a loosely connected network of ~30k nodes in roughly a millisecond on a Core 2 Quad a couple of years ago (no multithreading involved). I could probably dig up and sanitize the source if you're interested, but it's scary stuff :-)

##### Share on other sites
Delpee    109
I would love to see it, even just as a way to see the differences! Which sorting mechanism did it use?

##### Share on other sites
ApochPiQ    22999
I started with a std::priority_queue but ended up writing some custom gibberish (IIRC) by the end. I honestly don't recall clearly.

It's buried on a hard drive on a dormant machine I don't use anymore, so it may be a day or three before I can dig it up. Add another evening or so to sanitize and document it.

If you don't hear back from me relatively soon, feel free to drop me a PM and pester me into doing it, I've been meaning to publish that code for a while :-)

##### Share on other sites
Delpee    109
Awesome, I'll await the post ;-).

##### Share on other sites
[quote name='Waterlimon' timestamp='1330701966' post='4918617']
I think you can double the speed on mostly empty areas by spreading from both the start and end and finding the path when the 2 areas collide somewhere, but in a tight maze it might be slower...
[/quote]
This is a good advice for you ,and I am really agree with .

##### Share on other sites
Cornstalks    7030
[quote name='amile duan' timestamp='1331605226' post='4921544']
[quote name='Waterlimon' timestamp='1330701966' post='4918617']
I think you can double the speed on mostly empty areas by spreading from both the start and end and finding the path when the 2 areas collide somewhere, but in a tight maze it might be slower...
[/quote]
This is a good advice for you ,and I am really agree with .
[/quote]

##### Share on other sites
slicer4ever    6760
[quote name='amile duan' timestamp='1331605226' post='4921544']
[quote name='Waterlimon' timestamp='1330701966' post='4918617']
I think you can double the speed on mostly empty areas by spreading from both the start and end and finding the path when the 2 areas collide somewhere, but in a tight maze it might be slower...
[/quote]
This is a good advice for you ,and I am really agree with .
[/quote]

I have to agree with cornstalks, on a theoretical principle, it can speed things up, but i expect cornstalk's example of where it would fail's would be common. as well, if it does succeed and follow the same path to meet with the starting search grid, if searching from start to end produces the same path as start/end meeting in the middle, than you have most likly gained nothing.

the only scenario where this is useful imo, is if the end point is blocked from the starting point, in a small room, where the end search would produce that the two nodes are not reachable much quicker than start-end search.