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

jayg

  • Escaped Lunatic
    • View Profile
Advanced A* pathfinding/FPS !!SCIENCE!!
« on: July 20, 2013, 05:23:10 pm »

FPS has been driving me crazy lately, so I've been doing a lot of thinking and research on pathfinding to see if there's room for improvement.  I've made a couple of interesting discoveries.  I'm assuming a familiarity with A* in the post, but the conclusions and suggestions should be useful even if you don't understand the math.

Most of the discussions on A* pathfinding seem to be incorrect in a small but important way.  A* performs best when the actual path distance is close to the estimated distance ("manhattan" distance in our case).  With the default traffic weight of 2, that proves not to be the case.  Short, unimpeded straight-line paths in a large open space are actually surprisingly expensive--but I think there are ways to fix this.

I ran a series of experiments.  I used two 1-tile wood stockpiles with just a single log between them, set to take from links only.  I keep reversing the give/take settings on the stockpiles to get the dwarves to haul the log back and forth between them, and I watch the path they take.  For the experiments below, use the following key:
S/D - source/destination
. - floor, normal traffic
H/L/R - high/low/restricted traffic
= - for a few experiments, used to mark the path taken by the dwarf.

Experiment 1 - demonstrate that default settings search too widely for a potential path.
Spoiler (click to show/hide)

This could be really problematic.  In a fortress with no traffic designations, essentially every path ever plotted is going to be calculated like a "flood fill" path.  It's examining all kinds of alternatives that are never used.  In CS terms, we're examining O(N^2) tiles for a path of length N.  If you set your normal traffic pattern to cost 1 (or paint your whole fortress as high traffic), straight-line paths become O(N).  Summary: if you have a fairly open fort that's well-designed for compatibility with A*, setting the "normal" traffic cost to 1 could have a pretty significant impact on your FPS--and it's probably good for your fort whether or not you've paid attention to A* in your fort design.

Since 1 is the lowest traffic cost you can set, you might think we've hit a wall here, and that there's no way to set higher-priority paths than this.  That's only half true.  Although the game doesn't let you set lower traffic costs, d_init.txt will let you set a traffic weight of 0--or even a negative number (!)--for your next embark.  (I can't see how to set this manually once your fort has started; it's clearly in the save files somewhere, though.  If someone can figure it out, it'd be a great contribution to science, as it would let you use the techniques below to test FPS impact on a mature fort.)

This opens up all sorts of interesting possibilities.  For all the experiments below, I use the following weights: High traffic: 0.  Normal: 1.  Low: 2.  Restricted: -1.

Aside: how this "breaks" A*.
Spoiler (click to show/hide)

Experiment 2: unfindable 0-cost paths
Spoiler (click to show/hide)

Experiment 3: 0-cost paths aren't followed if they take you backwards.
Spoiler (click to show/hide)

Experiment 4: 0-cost paths are abandoned when they stop heading in the right direction.
Spoiler (click to show/hide)

Together, I think these show that 0-cost paths could be incredibly useful in speeding up pathing.  You might expect that dwarves would always use them, but that's not the case, because A* uses two numbers: the path cost, plus the distance to the destination.  Even if you can follow the path for free, A* won't follow it if it takes you further from your goal.  Dwarves don't follow these paths mindlessly if they're going in the wrong direction, which makes them pretty safe to use throughout the fort.  It's a good way to make them prioritize a search along a certain path--to help them quickly find the hallway that exits your tree farm, for example.

Negative paths are a lot more interesting.  I was worried that they might lead to a game crash--for example, if you make a loop of negative tiles, the dwarf might try to follow the loop in circles forever, to get an infinitely cheap path.  This doesn't appear to be the case; it seems like the code never re-examines a tile when doing pathing, so you'll never make a complete loop.  However, negative paths are so attractive that dwarves can be "pulled" to them from far away, and can follow them a lot further than might be advisable.  Also, large blocks of negative tiles are a bad idea--I've found dwarves zig-zagging across the blocks to pick up bonus points.  In short: use a little caution in playing with negative cost tiles.

Experiment 5: negative paths will be followed even if they lead away from the destination.
Spoiler (click to show/hide)

Experiment 6: dwarves do not appear to maximize usage of negative paths.
Spoiler (click to show/hide)

Negative paths definitely need more research.  I'd love to hear if anyone else is able to get them to do something interesting.  Since they don't seem to follow the normal rules, it's not yet possible to just "do the math" on them to figure out what would happen in a given scenario--if we can figure out the rules, we'll be able to harness them.

To put everything together, how would you use this to do efficient, fort-wide pathing?  I think you want several things.  First, straight-line paths should be very fast.  That's easy: just set normal traffic to size 1.  That should make any straight-line (or mostly-straight-line) paths compute pretty quickly.  Then, you just want to get your dwarves quickly to the point where they only have straight lines to compute.  Imagine one of the worst cases for pathing today: a stone on a fully excavated level needs to be moved to a stockpile above it.  The stockpile lies west of the stone (on a higher level), but the staircase is to the east.  Current techniques will find their way quickly to the point directly beneath the stockpile, then will essentially start a "flood fill" looking for stairs.  My hope is that you could insert a negative path through the excavated chamber that leads to the stairs.  Dwarves will quickly catch the negative path, which they will follow with high priority towards the stairs.  Once they hit the stairs, a straight-line search should get them fairly quickly to the stockpile.  If this works, I'm imagining a network of negative paths designed to connect all the major chambers of your fort, with some "nets" in large chambers to grab dwarves who end up pathing through the large rooms on accident.  This network is very tightly connected; any dwarf who touches a part of it in their path can effectively path very efficiently to any other piece of it.  So you'd design the network so that it only halts in locations where you have nothing but a straight path left to your destination.  Possibly you could tie it together with 0-cost paths as well, to help get around some tricky corners, or to reduce the size of this tight network.

I'm going to start a new embark today with these traffic costs set, and see what I can get to develop.  I won't be able to say for sure what impact it has on FPS until I get big enough to see some lag, and then I can do some road painting and see what happens.  If someone figures out how to get 0/negative traffic costs post-embark, it'd be a huge step forward.
Logged

Sergarr

  • Bay Watcher
  • (9) airheaded baka (9)
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #1 on: July 20, 2013, 05:39:40 pm »

The d_init is what they should use for everything.
Logged
._.

jayg

  • Escaped Lunatic
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #2 on: July 20, 2013, 05:53:32 pm »

The d_init is what they should use for everything.

Sadly no.  You can edit each fort's traffic costs inside the traffic menu, which doesn't save back to d_init so it must save elsewhere.  It appears to snapshot from d_init at embark, but after that, I don't know how to change to a value below what the game allows.  (Pro tip: save before doing much traffic painting, or you may accidentally hit 'q' and reset high traffic to 1 instead of 0.)
Logged

Urist Da Vinci

  • Bay Watcher
  • [NATURAL_SKILL: ENGINEER:4]
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #3 on: July 20, 2013, 06:28:58 pm »

I've advocated setting the NORMAL path cost to 1 in the past, but found that targeted use of HIGH traffic zones gave better results.

If 90% of your dwarves are idlers, you only need to have very efficient pathing between the beds, dining/meeting hall, and food/booze storage to get good fort FPS.

Tapeworm

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

I've heard that if you have, for example, multiple chambers branching off from a corridor that a dwarf passes through, it's best to set all of the doorways to Low traffic so that dwarves who aren't going to these chambers won't consider them for pathing through a corridor, leading to higher FPS by cutting down on 'flood filling'. Does this actually help?

Similarly, when you have wide (4+) corridors, is it desirable to set High traffic zones in the middle of it? You could try it out among your other experiments; I sadly have no mature forts to test with myself yet.

Your experiments on negative-cost weights are very promising - a shame we only have four weights to work with!
Logged

Di

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

Btw, DF uses euclidean metric not Manhattan.
« Last Edit: July 29, 2013, 01:30:47 pm by Di »
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

jayg

  • Escaped Lunatic
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #6 on: July 21, 2013, 07:26:24 pm »

I've heard that if you have, for example, multiple chambers branching off from a corridor that a dwarf passes through, it's best to set all of the doorways to Low traffic so that dwarves who aren't going to these chambers won't consider them for pathing through a corridor, leading to higher FPS by cutting down on 'flood filling'. Does this actually help?

Sort of, but not really.  With default traffic settings, 'low' is 2.5x as costly as 'normal'.  Which means that flood fill won't make it through that doorway until it has filled 2.5 steps past the door--maybe slightly more if it's in the right direction, or slightly less if it's not.  When your total path is in the hundreds of steps and not tens, then the difference here is laughably small.  You'd need very large traffic values to really shut off a corridor, which then makes it very expensive any time you do need to path through that corridor.  For corridors you really don't need, the conventional wisdom of forbidding the doorways is probably a great idea, and much more effective than simply setting high traffic.

Similarly, when you have wide (4+) corridors, is it desirable to set High traffic zones in the middle of it? You could try it out among your other experiments; I sadly have no mature forts to test with myself yet.

Again, the problem is with the ratio between the traffic costs.  The difference between 'high' and 'normal' isn't all that significant.  Yes, you will path through the 'high' regions with slightly higher priority, but for every 2 steps you take on a 'high' path, you'll also check an extra step along 'normal' paths.  This might improve your pathing performance slightly, but it doesn't change the equation entirely.  The strongest way to get your dwarf to path quickly in the right direction is for it to be pathing in the right direction along a 1-cost (or lower!) pathway.  That's the only situation in which the dwarf won't waste time checking side paths--the only situation in which the work done is proportional to path length, and not its square.  So the big challenge for a game-changing reduction in pathing cost is to "get water to flow uphill"--to find a way to force dwarves to walk away from their goal without doing a flood-fill while doing it.  Alternatively, of course, you can just have so many stairways and open spaces that a dwarf never has to walk away from its goal.
Logged

Tapeworm

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

Oh, well I guess my hearsay was false. Can you tell I have no experience in pathfinding?

I've tried looking for where the traffic costs are stored in the save folder; my first thought was to attempt to look through world.sav to find a list of traffic costs, but I had no luck finding anything like that. At the same time, I can't imagine anywhere else it could be. Perhaps they aren't encoded in decimal, or maybe they're arranged weirdly. This was using traffic costs of 1:2:7:26. Next time I'll generate a world with something like 12:22:34:96 and see if that helps any.
Logged

WanderingKid

  • Bay Watcher
  • The Overfiend
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #8 on: July 22, 2013, 03:21:28 pm »

PTW.  I've got huge issues with FPS once I get to a reasonable fort so any and all tricks I can get my hands on have my attention.  Thanks for doing this and reporting your findings in such detail.

Quietust

  • Bay Watcher
  • Does not suffer fools gladly
    • View Profile
    • QMT Productions
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #9 on: July 22, 2013, 03:55:33 pm »

Traffic costs are stored inside world.sav along with all of your other in-game settings (such as standing orders, waypoints/notes, hauling routes, etc.).
Logged
P.S. If you don't get this note, let me know and I'll write you another.
It's amazing how dwarves can make a stack of bones completely waterproof and magmaproof.
It's amazing how they can make an entire floodgate out of the bones of 2 cats.

Button

  • Bay Watcher
  • Plants Specialist
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #10 on: July 23, 2013, 01:11:18 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.
Logged
I used to work on Modest Mod and Plant Fixes.

Always assume I'm not seriously back

Volfgarix

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

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

chevil

  • Bay Watcher
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #12 on: July 24, 2013, 08:07:26 am »

make all hallways out of up-and-down staircases
This sounds like a brilliant idea. Could somebody test the effectiveness of staircase hallways.
Logged

Karakzon

  • Bay Watcher
  • [ethics:give a shit?: denied]
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #13 on: July 24, 2013, 08:09:20 am »

I do find that using ramps, while taking longer than stairs, will be less FPS heavy on your system. I think its probably to do with the fact that they have a set direction(s) to them compared to the stairs. Hence why i only use stairs if they are really the only option.
Logged
I am Dyslexic. No its not going to change any time soon.
Bolts of Exsanguination THE terrifying glacier export, get yours today!

andrewas

  • Bay Watcher
    • View Profile
Re: Advanced A* pathfinding/FPS !!SCIENCE!!
« Reply #14 on: July 24, 2013, 08:59:08 am »

Hallways out of staircases?
Tell me more.

Staircases allow for travel in all directions, and you can place stockpiles on them. So, if you have a sufficiently 3D fortress layout, you can make entire volumes out of staircases, set them all to high-traffic, and pathfinding will tend to head straight to the destination without having to explore any other tiles. In practice, you need to have some obstacles, but pathing around the solid tiles of a workshop is a tiny performance hit compared to pathing through a maze of corridors.

Taken to the ultimate, you have bedrooms and dining rooms in the walls of a staircase cube, stockpiles and workships are build in the cube, once a dwarf is in the cube it can effectively fly straight to any point in the cube.
Logged
Pages: [1] 2 3