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 7844 times)

andrewas

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

Ignore the walls thing, that was a bad guess at what the applet was doing. Digging into the code, the weight is a multiplier on the heuristic value, not on the tile weights. So higher weights are making tiles cheaper than the heuristic predicts, which breaks A* and allows for suboptimal paths to be found in exchange for reduced search times. Note that in your more complex examples, the path length for high weight trials is often longer than the length for weight=1 trials. Whether this is a bad thing can be debated, its realistic that dwarves are not perfect pathfinders, so if the paths aren't too bad, who cares if occasionally they take the long way around.

Except for stone haulers of course, but that's why we have wheelbarrows. To model what DF does by default you would need to set weight to 0.5, but you can't as its an integer value. Using another applet, you can get this result:

Spoiler (click to show/hide)

As you can see, pathing across weight-5 terrain forces the algorithm to search more widely to be sure of finding the optimal path. That's why the common advice is to adjust your default tile cost to 1. How much difference it makes in actual play is another question but for players who don't use high traffic designations its almost certainly worth doing. Sadly, DF doesn't let us adjust the heuristic weight so we can't experiment with sub-optimal pathfinding.
Logged

Bandreus

  • Bay Watcher
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #31 on: July 26, 2013, 08:48:40 pm »

Ignore the walls thing, that was a bad guess at what the applet was doing. Digging into the code, the weight is a multiplier on the heuristic value, not on the tile weights. So higher weights are making tiles cheaper than the heuristic predicts, which breaks A* and allows for suboptimal paths to be found in exchange for reduced search times. Note that in your more complex examples, the path length for high weight trials is often longer than the length for weight=1 trials. Whether this is a bad thing can be debated, its realistic that dwarves are not perfect pathfinders, so if the paths aren't too bad, who cares if occasionally they take the long way around.

Except for stone haulers of course, but that's why we have wheelbarrows. To model what DF does by default you would need to set weight to 0.5, but you can't as its an integer value. Using another applet, you can get this result:

Spoiler (click to show/hide)

As you can see, pathing across weight-5 terrain forces the algorithm to search more widely to be sure of finding the optimal path. That's why the common advice is to adjust your default tile cost to 1. How much difference it makes in actual play is another question but for players who don't use high traffic designations its almost certainly worth doing. Sadly, DF doesn't let us adjust the heuristic weight so we can't experiment with sub-optimal pathfinding.

I see what you mean there. Sadly I don't have the time to dig into the code myself right now, but I guess you're right. Actually it looks like I wasn't careful enough with checking if the length for the shortest paths was consistent when switching weights. I shall take the time to investigate things further and adjust my previous posts accordingly.

Thanks for the heads up!
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!

Gentlefish

  • Bay Watcher
  • [PREFSTRING: balloon-like qualities]
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #32 on: July 27, 2013, 04:14:27 am »

If I can add my two cents, I know it's known that moving diagonally is worth the square root of two instead of one, as in some games sometimes. So maybe that's another problem when dealing with A* pathfinding?

blue sam3

  • Bay Watcher
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #33 on: July 27, 2013, 05:16:44 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.
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 what I studied about the algorithm (computer science student here). 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!

I've noticed (in some very small-scale tests that really need to be done properly) some improvement from using one-way corridor rings in place of straight corridors (so a 1-2 tile corridor going north, a gap of one, then a 1-2 tile corridor going south, rather than a 3-5 tile normal corridor), potentially a result of the much reduced traffic problems.
Logged

Fluoman

  • Bay Watcher
  • Anything the game allows.
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #34 on: July 27, 2013, 07:56:01 am »

There's no way to set the walking direction of a corridor. Or is there?
Logged
"hey, look, my left hand! It's only bones now, gosh, has it been that long since that cave dragon bit it off?"

RtDs!

AutomataKittay

  • Bay Watcher
  • Grinding gears
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #35 on: July 27, 2013, 08:04:23 am »

There's no way to set the walking direction of a corridor. Or is there?

There're a trick using pressure plate and hatch to do it, but no designation method, though. I don't know if pressure plate even works correctly with recently found bug that some dwarves aren't growing up properly.
Logged

jayg

  • Escaped Lunatic
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #36 on: July 28, 2013, 01:33:51 pm »

Wow, this thread really exploded since I checked last.  Thanks for all the pathfinding tips.  I agree with andrewas regarding the PathFinding.js stuff--the way PathFinding.js uses weight is not the same as the way DF uses weight.  And if you set the weight to 0.5 as is the default in DF, then you see essentially flood-fill behavior on even straight-line paths.

So I finally got a fortress up to a decent size with the 0/1/2/-1 traffic costs, and started seeing some FPS drain on it, and decided to do some FPS experiments.  The results are actually pretty disappointing.  I hollowed out 2 big chambers (using dfhack) and used a civilian alert to burrow all my dwarves into them.  I had 163 dwarves in total.  I made a stockpile with 100 logs on it, and did some experiments getting the dwarves to haul all of those logs onto a different stockpile.

Baseline FPS with all of the dwarves idle was about 90-91.

Test 1: haul all of the logs straight across the room in an unimpeded straight line.  Traffic cost 1.  FPS got down to about 50 once a lot of these were in flight.

Test 2: same setup, crank traffic cost up to 100.  FPS got down to 35 this time.

Test 3: same setup, use high traffic in between the stockpiples.  FPS got down to 55 or so.

Test 4: move all the wood to a stockpile directly beneath the original stockpile.  The only usable staircase is on the opposite side of the room.  FPS got down to about 50.

Test 5: same as above, using high (100) traffic costs.  FPS got down to about 42.

Overall, this is a helpful but disappointing result.  We have 2 pairs of tests where we try something with low and high traffic costs.  The high-cost version should have to flood-fill the entire chamber (both chambers for the last tests) compared to the low-cost version.  The cost of finding the path should be something like 300 times greater for the high cost version.  In reality, we see a pretty modest FPS dip between the two; frames taking 28ms to render vs. 20ms--a 40% difference, not 30,000%.

What does this mean?  One way or another, it probably means that pathfinding isn't the dominant cost here.  Either Toady's A* improvements are awesome and clever and can prune a lot of dead paths early, or pathfinding is a really fast operation anyway, so making it 300x slower doesn't actually hurt performance much.  There's a lot of room for guessing about what Toady has done--e.g. maybe he caches a path between two linked stockpiles, so that it's only computed once, instead of 100x in this test.  But you can also imagine that pathfinding on this scale is still fairly cheap; if it takes 10ns to examine one node in a path, then the 300x300 flood-fill would still only take 1ms.  (If this is about right, then it also means you could path through a fully-excavated 300x300 x 120 z-level fort in 100ms.  That's still pretty darn fast, considering.)

The thing I find really fascinating here is that we see a huge drop in FPS just from ordering the dwarves to move the wood, regardless of how expensive the path is to compute.  The expense of the path seems to be pretty minor compared to just the expense of having a bunch of dwarves with an active job.  I confirmed this by making a "merry-go-round" of wood stockpiles, all right next to each other, and linked in a circle.  Once all the dwarves get busy, the FPS hits ~40--pretty much the low point observed from all the tests above.

The implication for FPS seems to be that pathing probably doesn't matter all that much.  Burrowing and making forbidden doors aren't bad ideas, and certainly won't make things any slower.  But this research seems to indicate you'll see much more impact from limiting your population (or keeping a lot of your dwarves idle).  The caveat would be if there are certain things that cause a large number of paths to be generated, then of course pathing performance becomes important.

Would love to hear if someone has any more definitive research that shows a significant real-world improvement from pathing changes.  For folks trying the "cube" layout, maybe you could use dfhack to revert the stair hallways to regular hallways and install a central stairway, and see what it does to your FPS.
Logged

AutomataKittay

  • Bay Watcher
  • Grinding gears
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #37 on: July 28, 2013, 01:52:43 pm »

I recall there being some research on FPS impact here, not so much in pathing research. Item amount and corpse list seem to contribute siginificantly to FPS 'decay' http://www.bay12forums.com/smf/index.php?topic=104643.0 however it might be somewhat dated.

Judging from the result of pathing experiment, it seems higher cost does take longer to calculate. Burrowing isn't really reliable with current hauler behavior, but I've built a few test fortresses that uses minecarts to isolate labor sections from each other reliablly.
Logged

Di

  • Bay Watcher
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #38 on: July 29, 2013, 11:19:22 am »

He needs to walk through the majestic dining hall (dots are supposedly other dwarves blocking his way), a first workshops area, a large stockpile, another workshops area and then the dodge-me corridor leading to the outside world.
Bay12, where isolated dots represent ASCII characters.
Also, good read, thanks for educating us.
Logged
Quote from: Creamcorn
Dwarf Fortress: Where you meet the limit of your imagination, moral compass, sanity and CPU processor.
http://www.bay12forums.com/smf/index.php?topic=103080.0 Fix sober vampires!
http://www.bay12forums.com/smf/index.php?topic=91442.0 Dwarven Cognitive Science

Bandreus

  • Bay Watcher
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #39 on: July 29, 2013, 11:51:55 am »

He needs to walk through the majestic dining hall (dots are supposedly other dwarves blocking his way), a first workshops area, a large stockpile, another workshops area and then the dodge-me corridor leading to the outside world.
Bay12, where isolated dots represent ASCII characters.
Also, good read, thanks for educating us.

I think I should make it as clear as possible my whole reasoning was partially flawed by my misunderstanding in how the JS app I used int example is handling weights. Specifically, I thought the weight was for traversing the tiles, while truth is the weight was applied to the Heuristic.

What's happening is that, by upping the weight to something greater than 1, the Heuristic stops being admissible and A* doesn't compute the a shortest path anymore. This can be seen in a few of those examples just by looking at the reported path length.

Understanding this is very important, otherwise you might get a completely wrong picture of what is going on in there. I think I shall just put a big disclaimer at the beginning of my post, as I don't currently have the time to review/edit the whole thing, nor I'd like to just scrape all of that work entirely, since it took quite some time to be put together.
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!

Di

  • Bay Watcher
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #40 on: July 29, 2013, 01:20:05 pm »

Considering the directional corridors. Dwarves seem to use manehattan heuristics and prioritize diagonal movement mostly. Thus a pair of tunnels would reliably work as one-directional. (Or just make really wide one and don't designate the middle as hight traffic)
Spoiler (click to show/hide)
As for amount of operations on the picture, I guess the setup without walls got more operations because of checking side tiles which were blocked by walls in the top two variants.
Logged
Quote from: Creamcorn
Dwarf Fortress: Where you meet the limit of your imagination, moral compass, sanity and CPU processor.
http://www.bay12forums.com/smf/index.php?topic=103080.0 Fix sober vampires!
http://www.bay12forums.com/smf/index.php?topic=91442.0 Dwarven Cognitive Science

Snaake

  • Bay Watcher
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #41 on: August 02, 2013, 04:06:59 am »

Mostly working off this post by Bandreus (initial text also put in it's own spoiler due to length of post), especially off the last spoilered section, with the "practical" examples:


Spoiler (click to show/hide)

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!

So, what I got out of that post/this thread is:
- vanilla traffic weights are actually decent, if you use them correctly. Low and Restricted weights could maybe be increased some? (or you can just be more trigger-happy with restricted)
- if you don't use traffic designations, setting normal traffic to weight 1 may, in some cases, give more optimal paths. Maybe.
- 10x10 workshop+stockpile spaces are better than I thought (I was moving away from them a bit, partially due to pathfinding concerns with open spaces), provided they're dead ends and only checked when work needs to be done there (and restricted traffic helps with this).
- hallways should be wide enough to avoid bad traffic, but the narrower the better when it comes to fps.
- for 3+ wide corridors, a curving corridor will likely be slightly better for fps than right-angle turns or u-bends (no "dead end" corners to check). Turns should be avoided if possible, though.
- interconnectedness between levels is good. Dunno why I haven't been using it more, probably just got stuck with a central staircase early on, and didn't want to complicate my fortress layout. So multiple stairwells galore!*
- optionally, at first I thought sprawling horizontal forts might even be better than a central stairwell layout, if dwarves can reach most/all places without pathing away from their goal (ie. whole/most of path is at most at a 45 degrees to a straight line path that ignores walls etc.)? Less tiles checked for pathing (relatively speaking) would likely be offset by longer paths in general, though.

* Multiple stairwells and more interconnectedness in general may be good for pathfinding both in terms of cpu cycles used and lengths of paths, but they will effectively weaken the inside security of your fort. Which is probably one reason they don't see more hype?
Logged

Bandreus

  • Bay Watcher
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #42 on: August 02, 2013, 08:34:24 am »

So, what I got out of that post/this thread is:
- vanilla traffic weights are actually decent, if you use them correctly. Low and Restricted weights could maybe be increased some? (or you can just be more trigger-happy with restricted)
- if you don't use traffic designations, setting normal traffic to weight 1 may, in some cases, give more optimal paths. Maybe.
- 10x10 workshop+stockpile spaces are better than I thought (I was moving away from them a bit, partially due to pathfinding concerns with open spaces), provided they're dead ends and only checked when work needs to be done there (and restricted traffic helps with this).
- hallways should be wide enough to avoid bad traffic, but the narrower the better when it comes to fps.
- for 3+ wide corridors, a curving corridor will likely be slightly better for fps than right-angle turns or u-bends (no "dead end" corners to check). Turns should be avoided if possible, though.
- interconnectedness between levels is good. Dunno why I haven't been using it more, probably just got stuck with a central staircase early on, and didn't want to complicate my fortress layout. So multiple stairwells galore!*
- optionally, at first I thought sprawling horizontal forts might even be better than a central stairwell layout, if dwarves can reach most/all places without pathing away from their goal (ie. whole/most of path is at most at a 45 degrees to a straight line path that ignores walls etc.)? Less tiles checked for pathing (relatively speaking) would likely be offset by longer paths in general, though.

* Multiple stairwells and more interconnectedness in general may be good for pathfinding both in terms of cpu cycles used and lengths of paths, but they will effectively weaken the inside security of your fort. Which is probably one reason they don't see more hype?

- I wouldn't change traffic costs but rather work on better, pathfinding-friendlier fort design.

If I really had to change costs, I would set the cost for restricted traffic to something absurdly high and use that to designate dead ends and side rooms. Setting the cost for normal traffic to 1 could also be an interesting possibility.

- More openness, coupled with more connectedness and more compact designs, is always better (imho).

- Regarding corridors, yes, that's pretty much it. Realistically, just stick to 3-wide for main hallways, as the fps-loss from traffic jams is much more severe than the additional computation needed to path through slightly wider corridors.

- Turns are not that bad, though an unobstructed straight line is always better. Definitelly avoid u-bends if at all possible.

- I guess it's because having stairs all around the place hurts aesthetics on top of not making much sense to a normal human being. After all, we only place a few stairwells/elevators in key locations in our own buildings, the slightly longer paths we take to navigate a multiple stories building doesn't seem to be that much of an issue, but then our lives are not controlled by an omnipotent overseer... or are they?  ::)

I've been sticking to central stairwell for a LONG time too.

- Given constant inner volume and good interconnectedness, a fort over multiple, contiguous levels makes pathfinding easier than in a horizontal fort. Practically though, 99% of the problems lie in both the fort design and the fact hundreds of dorfs are pathing all at the same time.
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]