Jump to content

  • Log In with Google      Sign In   
  • Create Account


A* speed


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
23 replies to this topic

#1 Delpee   Members   -  Reputation: 109

Like
0Likes
Like

Posted 02 March 2012 - 07:35 AM

Hello GameDevs,

I recently did an implementation of the A* algorithm. 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.
While implementing a question came to mind, how fast is fast?

Here are some images of the implementation in practice:

Agent moving a long distance
Posted Image

Agent moving through a maze
Posted Image

Two videos showing the implementation in practice:
Video 1
Video 2

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.
These are the results:
40 by 23, no obstacles
Posted Image

100 by 100, no obstacles
Posted Image

40 by 23, difficult terrain
Posted Image

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.

My question to the GameDev boards; Is this an acceptable speed, can it be categorized as fast, or slow?

Discuss...


Thanks in advance,

Yuri van Geffen

Sponsor:

#2 Waterlimon   Crossbones+   -  Reputation: 2355

Like
1Likes
Like

Posted 02 March 2012 - 09:26 AM

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...

Waterlimon (imagine this is handwritten please)


#3 Delpee   Members   -  Reputation: 109

Like
0Likes
Like

Posted 02 March 2012 - 09:43 AM

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...


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.

#4 jefferytitan   Members   -  Reputation: 1651

Like
0Likes
Like

Posted 04 March 2012 - 02:57 PM

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.


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*.

#5 Cornstalks   Crossbones+   -  Reputation: 6966

Like
0Likes
Like

Posted 04 March 2012 - 03:36 PM

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...

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.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#6 Nemean Charisma   Members   -  Reputation: 122

Like
0Likes
Like

Posted 05 March 2012 - 04:22 AM

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
Posted Image

#7 Cornstalks   Crossbones+   -  Reputation: 6966

Like
0Likes
Like

Posted 05 March 2012 - 07:25 AM

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

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?
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#8 Delpee   Members   -  Reputation: 109

Like
0Likes
Like

Posted 05 March 2012 - 12:24 PM

@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?

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.

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?

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!

#9 Delpee   Members   -  Reputation: 109

Like
0Likes
Like

Posted 05 March 2012 - 12:43 PM

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?

#10 Álvaro   Crossbones+   -  Reputation: 11865

Like
0Likes
Like

Posted 05 March 2012 - 12:56 PM

Is your algorithm implemented recursively?

#11 Martins Mozeiko   Crossbones+   -  Reputation: 1413

Like
0Likes
Like

Posted 05 March 2012 - 02:23 PM

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.

#12 smr   Members   -  Reputation: 1553

Like
0Likes
Like

Posted 05 March 2012 - 03:24 PM

There are many different reasons why your A* implementation may not perform well. The most common reasons in my experience have typically been:
  • Using the wrong heuristic
  • Poorly managing the open list (for example, using a slow sort or next-best search)
  • Having a closed 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.

Also, you can optimize away your closed list of cells by instead adding a closed flag to your map for pathfinding. When you start a pathfinding operation you reset the flag on the entire map (you can actually avoid this step by using an integer flag, but that's probably not necessary). Whenever you close a cell, instead of adding the cell to a list, you just set the flag. This will enable you to check if a cell's closed with a simple lookup instead of having to search. This does depend on you being willing to add this flag to your map, which does violate some principles of encapsulation and may offend some of the stricter coders out there. You could instead allocate a separate structure for this maintained by your pathfinder, but you'd have to make sure your structure always matches that of your map. Implementing that separation could be more trouble than it's worth.

#13 Delpee   Members   -  Reputation: 109

Like
0Likes
Like

Posted 07 March 2012 - 03:07 AM

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 ;).

#14 zalzane   Members   -  Reputation: 191

Like
0Likes
Like

Posted 07 March 2012 - 05:19 PM

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.

#15 wodinoneeye   Members   -  Reputation: 666

Like
0Likes
Like

Posted 08 March 2012 - 11:54 PM


@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?

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.

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?

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!



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)
--------------------------------------------Ratings are Opinion, not Fact

#16 Delpee   Members   -  Reputation: 109

Like
0Likes
Like

Posted 10 March 2012 - 05:26 AM

Excluding the rendering from the recorded time ???


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.

#17 slicer4ever   Crossbones+   -  Reputation: 3193

Like
0Likes
Like

Posted 10 March 2012 - 05:34 AM

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.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#18 ApochPiQ   Moderators   -  Reputation: 14273

Like
0Likes
Like

Posted 12 March 2012 - 02:12 PM

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 :-)

#19 Delpee   Members   -  Reputation: 109

Like
0Likes
Like

Posted 12 March 2012 - 03:03 PM

I would love to see it, even just as a way to see the differences! Which sorting mechanism did it use?

#20 ApochPiQ   Moderators   -  Reputation: 14273

Like
0Likes
Like

Posted 12 March 2012 - 03:21 PM

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 :-)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS