Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 [2] 3

Author Topic: Advanced A* pathfinding/FPS !!SCIENCE!!  (Read 7849 times)

Snateraar

  • Bay Watcher
  • [INORGANIC:BIRD_CRAZY]
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #15 on: July 24, 2013, 09:29:26 am »

Effectively you're designating d > i in a 10x10x10 cube?
Logged
Urist McSnate likes malachite, copper bars, birds and goblinite for its abundance. When possible, he prefers to consume tea and toast. He absolutely detests elves.

A tall, clumsy creature fond of birds and industry.

Trollhammaren

  • Bay Watcher
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #16 on: July 24, 2013, 10:30:44 am »

This is so what I'm doing with my next fort.

Spoiler (click to show/hide)

Snateraar

  • Bay Watcher
  • [INORGANIC:BIRD_CRAZY]
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #17 on: July 24, 2013, 11:30:45 am »

This is so what I'm doing with my next fort.

Spoiler (click to show/hide)

Sounds.....Fun.

I like your username.
Logged
Urist McSnate likes malachite, copper bars, birds and goblinite for its abundance. When possible, he prefers to consume tea and toast. He absolutely detests elves.

A tall, clumsy creature fond of birds and industry.

Bandreus

  • Bay Watcher
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #18 on: July 24, 2013, 12:19:59 pm »

I've been trying to write this post several times now, but I can't help myself and end up with a big-ass wall of text every single time.

A* doesn't behave like a flood-fill (breadth-first) algorithm at all, and the occasional "checking for seemingly sub-optimal tiles" is indeed part of the wanted behavior making the algorithm that great. Yes, as counter-intuitive as this might seem, checking for sub-optimal paths does help with finding the optimal path more quickly.

This might be helpful: Path article on the wiki. Also this interview with Toady, although it might be slightly out of date.

Also, this.

I would also like to point out that having the default weight something greater than 1 (although causing a very neglectable, definitely insignificant overhead), goes a very long way with improving A*'s efficiency when it needs to turn around bumps, corners, u-bends, dead-ends and the like.  Keep in mind the overall efficiency of an algorithm is very dependent on performance in a whole set of very different instances of a problem, and slightly enhanced performance in a very strict sub-set of the problem doesn't equate to good overall results.

I.e.
Spoiler (click to show/hide)
versus
Spoiler (click to show/hide)

Obviously I don't know much about the exact implementation for A* being used in DF, and it's obvious a whole lot of different algorithms are being used in conjunction under different circumstances. Hence I do encourage you into researching and experimenting more, but I felt like I needed to stop by and point out the broader picture needs to be evaluated, rather than focusing on an arbitrarily small set of instances of the problem.

Ultimately, it's just happens the path-finding problem is a hard-to-solve-one, and I doubt any significant result can be achieved via tweaking numbers. But I might as well be wrong here, so I reiterate I encourage further !!science!! be conducted on the matter.

As long as general tips about how to minimize the pathfinding hit on fps go, I would advice focusing on smarter, more careful fort design, sapient use of burrows and traffic zones and all the usual jazz with keeping dorfs away from always pathing from one end of the fortress to the other.

You don't really need tweak much with the traffic zones' numbers, but rather use those the way those are supposed to be used:
- restricted traffic zones for dead-ends and side-rooms which lead nowhere. To discourage the algorithm for checking tiles which don't lead anywhere near the goal.
- high traffic zones for slightly out-of-hand side corridors in order to 'force' a few dwarves to take alternate routes instead of clogging the main hallways all at the same time (traffic jams hit your fps a lot, as lots of dwarves need to recalculate paths multiple times, all at the same time)

Also, if you happen to have areas of the map which you know dwarves should never path through under normal circumstances, locking those off via a forbidden door or similar facility is both very advisable and convenient. Tree farms and abandoned excavation sites are a prime example. Large open areas which shall usually be left alone are always there potentially making the pathing algorithm waste useful time. Whenever you need to harvest wood or stone laying in-there, you can just unforbid the door with ease.

Ultimately, the more compact and optimized your fort is, the less pathfinding is going to be an issue. Conversely, no trick is going to help your fps if you have a huge, scarcely organized fort in which dwarves are left mindlessly wondering around as their limited mind see fit.
Logged
Check out Enôrbomrek: Bluewhips a community fort and story by me.
Clearly, our top dwarven scientists are hard at work creating a new breed of SUPER WAGON that can survive being scuttled by enemy wagonmancers! These new super wagon troopers will be able to carry TWICE the cargo, be 1/3 the size, and NEVER scuttle!

Button

  • Bay Watcher
  • Plants Specialist
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #19 on: July 24, 2013, 03:36:17 pm »

PTW

My "make all hallways out of up-and-down staircases" idea is starting to seem like a viable FPS reduction strategy. I didn't realize A* pathfinding made central staircases such a poor design decision.
Hallways out of staircases?
Tell me more.

Well on a central staircase build you tend to have major hallways going off from the central staircase in all four directions, right?

So just designate them with d-i instead of d-d.

Warning, doing this near an aquifer may cause ≈fun≈

Though this cube thing sounds friggin awesome, aside from problems with living quarters
« Last Edit: July 24, 2013, 03:37:56 pm by Button »
Logged
I used to work on Modest Mod and Plant Fixes.

Always assume I'm not seriously back

Bandreus

  • Bay Watcher
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #20 on: July 24, 2013, 04:44:31 pm »

Noise actually isn't that much of a problem, assuming your bedrooms are 7 tiles away (in all directions) from the surface, the cavern layers and tree/shroom farms, which is where most of the noise will be produced.

I guess your dwarves will still be moderately pissed by smoothing/engraving noises, but you're making their home fancier so just let them whine about it inb4 the increased good-thoughts stream.

Contrary to popular belief, daily work at workshops wont produce noise (although deconstructing those will).
Logged
Check out Enôrbomrek: Bluewhips a community fort and story by me.
Clearly, our top dwarven scientists are hard at work creating a new breed of SUPER WAGON that can survive being scuttled by enemy wagonmancers! These new super wagon troopers will be able to carry TWICE the cargo, be 1/3 the size, and NEVER scuttle!

AutomataKittay

  • Bay Watcher
  • Grinding gears
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #21 on: July 25, 2013, 03:33:22 am »

Noise actually isn't that much of a problem, assuming your bedrooms are 7 tiles away (in all directions) from the surface, the cavern layers and tree/shroom farms, which is where most of the noise will be produced.

I guess your dwarves will still be moderately pissed by smoothing/engraving noises, but you're making their home fancier so just let them whine about it inb4 the increased good-thoughts stream.

Contrary to popular belief, daily work at workshops wont produce noise (although deconstructing those will).

The workshop noise was true in older version, at least it was in 40d. I have no idea when it was removed, but workshop definitely don't wakes up or gives bad thoughts to dwarves since 31.08 that I could see. That's probably where part of the belief came from :D

I've also found that noises don't give too much bad thought ( Haven't used utilities to check, just based on experience ) so I rarely bother to worry about it. Chopping trees happens infrequently enough that a nice bedroom should make up for it anyway.
Logged

Volfgarix

  • Bay Watcher
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #22 on: July 25, 2013, 03:32:39 pm »

Dwarves won't fall to the bottom of staircases when knocked unconscious/stunned?
Logged

AutomataKittay

  • Bay Watcher
  • Grinding gears
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #23 on: July 25, 2013, 04:01:21 pm »

Dwarves won't fall to the bottom of staircases when knocked unconscious/stunned?

Sometimes they will, it seem to depend on how they were knocked out to me. Combat or item dropping on them seem to make them stumble down, while syndrome or pain don't. Passing out on hatches don't let them stumble, nor hatch opening while they're already there.

Course I've not watched them too closely for that, so I might've been mistaken :D
Logged

0cu

  • Bay Watcher
  • Losing is fun!
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #24 on: July 26, 2013, 01:41:58 am »

Dwarves won't fall to the bottom of staircases when knocked unconscious/stunned?

I have never had that happen in any of my forts. Never.
Logged

AutomataKittay

  • Bay Watcher
  • Grinding gears
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #25 on: July 26, 2013, 03:31:04 am »

Spoiler (click to show/hide)
versus
Spoiler (click to show/hide)

A bit late, but I noticed the higher path cost seem to take 11 times longer to find path than the low cost one. Is the time the running time to find a path or something else? If it's the time to find a path, it'd be an argument for limiting dwarven activity as much as possible and reducing open space to wander through. It'd also imply that higher path cost for major movement areas like hallways would take longer to process.

However, the above is just some thoughts, it'd need experimentation in fortress mode to see how it improves active pathfinding since it seem to only be major factor when there're a lot of hauling to be done or legendary workshop workers.
Logged

Bandreus

  • Bay Watcher
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #26 on: July 26, 2013, 05:55:50 am »

Sorry, I should have mentioned this earlier.

In the result shown:
Length - obviously is the length for the shortest path (in the euclidean sense, not simply the number of tiles).

Time - it's the time it took to find the path. Now, don't let this fool you. The app is written in JS and runs in your browser. The execution time is influenced by a number of different things, and ultimately is too hectic a metric to draw any useful insight from it. Just run a Search multiple times in a row with the same parameters and you'll see what I mean. Like this:

Spoiler (click to show/hide)

Operations - now this is the important part, as this is a metric for the number of operations performed to successfully find the shortest path. See how the number of operations never changes if you run the exact same search multiple times (A* is a deterministic algorithm, at least in its simpler implementation).

Also of note, if the shortest path is unobstructed (no unwalkable tiles, corners, u-bends and the like), the number of operations doesn't change even if you rise/lower the weight. (note: this is assuming the Manhattan distance is used as the heuristic).
Spoiler (click to show/hide)

Now, these are very simple instances of the problem, but it goes to show some of the most basic properties of the algorithm. We do know Toady uses a number of algorithms for pathfinding, and he did his own, slightly modified implementation of A*.

But we could make an informed guess at what should work best in terms of making the pathing algorithm do less checks. For instance, by knowing how regular A* works, I would be strongly against setting the weight for normal traffic tiles to 1.

This is not to say SCIENCE shouldn't take place. Maybe you'll find out something very counterintuitive is taking place within DF's pathing code. That wouldn't shock me in the slightest, I guess ;)
« Last Edit: July 26, 2013, 06:01:21 am by Bandreus »
Logged
Check out Enôrbomrek: Bluewhips a community fort and story by me.
Clearly, our top dwarven scientists are hard at work creating a new breed of SUPER WAGON that can survive being scuttled by enemy wagonmancers! These new super wagon troopers will be able to carry TWICE the cargo, be 1/3 the size, and NEVER scuttle!

AutomataKittay

  • Bay Watcher
  • Grinding gears
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #27 on: July 26, 2013, 06:57:58 am »

Thanks for the explaination! It does makes it more clear what the numbers means :D

I believe there've been results in past pathfinding research that hints that lower path cost makes for faster search or less impact on FPS when surrounded by higher cost for busy trafficways, but I might be mis-remembering so take that with a grain of salt. Or it might've been a theortical analysis I saw, hmm.

Either way, thanks for explaining the stats!
Logged

andrewas

  • Bay Watcher
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #28 on: July 26, 2013, 10:46:39 am »

I'm 99% sure that last picture is wrong. I think the 'weight' field in that applet refers to the weight of the walls you can place, rather to to the weight of the empty tiles. I'll check this when I get back to a real computer.

In DF by default, an empty tile has weight 2 and the heuristic assumes a default weight of 1. Therefore pathfinding has to search further afield to cover the possibility of a high-traffic designation which would result in a lower overall path cost but requires the initial move to be away from the goal.
Logged

Bandreus

  • Bay Watcher
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #29 on: July 26, 2013, 02:04:21 pm »

IMPORTANT: be aware the way things are being exposed in this post is partially flawed. Big props to andrewas for pointing out the JS app I used in my examples is applying the specified weight to the Heuristic rather than the cost for traversing tiles (you should read his post).

I'm 99% sure that last picture is wrong. I think the 'weight' field in that applet refers to the weight of the walls you can place, rather to to the weight of the empty tiles. I'll check this when I get back to a real computer.

In DF by default, an empty tile has weight 2 and the heuristic assumes a default weight of 1. Therefore pathfinding has to search further afield to cover the possibility of a high-traffic designation which would result in a lower overall path cost but requires the initial move to be away from the goal.
I can see no issue within the picture you're talking about. Walls are unwalkable tiles, they don't have weights, the pathfinding algorithm simply ignores wall tiles when looking for the shortest path to the goal.

Thanks for the explaination! It does makes it more clear what the numbers means :D

I believe there've been results in past pathfinding research that hints that lower path cost makes for faster search or less impact on FPS when surrounded by higher cost for busy trafficways, but I might be mis-remembering so take that with a grain of salt. Or it might've been a theortical analysis I saw, hmm.

Either way, thanks for explaining the stats!

Are you talking about pathfinding algorithms in general, or DF specifically?

Traffic jams in DF are especially bad because lots of dwarves need to recalculate paths a lot of times in a short amount of timehink very little can be done by toying with costs, to be honest. I guess your best bet is to try and avoid heavy traffic altogether by designing around it. Ofc this might come from lack of knowledge on my side, so take this with a pinch of salt.

Back to A* inner warkings, keep in mind a lot depends on the heuristic used by the algorithm.

I'm not sure about what the heuristic being used in the current DF version is, but if we go with the Toady interview I posted earlier, it looks like that's the Manhattan distance.
Quote
TA: The dwarves themselves mostly move around with A*, with the regular old street-distance heuristic.

Also, keep in mind all that I'm posting mainly comes from my own understanding. This is why I've been posting things like "Don't take this as a solid rule and feel free to experiment all you want within DF".

More rumblings in the spoiler
Spoiler (click to show/hide)

I thought I might as well post some data (WARNING: lots of images follow). I know most of what I'll be showing is trivial, but  this will hopefully help the less nerd-y people with understanding why some designs are more pathfinding-friendly than others.

Spoiler (click to show/hide)

Also, here's a more complex example with some analysis and tips on how to help the pathfinding code go more swiftly.

Spoiler (click to show/hide)

hope this helps!
« Last Edit: July 29, 2013, 12:05:59 pm by Bandreus »
Logged
Check out Enôrbomrek: Bluewhips a community fort and story by me.
Clearly, our top dwarven scientists are hard at work creating a new breed of SUPER WAGON that can survive being scuttled by enemy wagonmancers! These new super wagon troopers will be able to carry TWICE the cargo, be 1/3 the size, and NEVER scuttle!
Pages: 1 [2] 3