Bay 12 Games Forum

Please login or register.

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

Author Topic: Route caching  (Read 5881 times)

Ogantai

  • Bay Watcher
    • View Profile
Route caching
« on: December 02, 2008, 12:06:37 am »

Consider the following:

1 dwarf with Item Hauling enabled at point A.
Pile of 10 objects to be stored at B.
Stockpile at C.

The dwarf with Store item in stockpile job starts out at location A, paths A->B, then B->C, then must recalculate routes C->B and B->C 9 more times to complete the hauling.

Considering that A* is O(nn) in a worst-case scenario, the less pathfinding DF has to do the better. Instead, why not have the dwarf cache the precalculated B->C route, then instead of pathfinding C->B just reverse the route to arrive where he started, cache that, then repeat B->C and C->B from cache until done? That reduces the number of graph searches that must be done from 20 from 2. Perhaps store the last p routes in cache, let all dwarves share a common cache, and have then test if their prospective routes share endpoints within s squares (probably 1-2) of routes already in the cache. That reduces a potentially cpu-intensive 500 square long path calculation from the the armor stockpile to the xX(Pigtail Sock)Xx that Urist dropped to a much simpler calculation from the precached route's endpoint at xX(Pigtail Left Glove)Xx to the square adjacent to it.

Obviously the values of s and p will have to be tweaked (just let us set them in the init file) but this would really reduce the strain DF places on lower-end computers with high pop fortresses.
Logged

Andir

  • Bay Watcher
    • View Profile
Re: Route caching
« Reply #1 on: December 02, 2008, 12:38:24 am »

It's just a method of reducing CPU load by using memory.  He could just make each path an object array of points (from endpoint to endpoint) give it a lifetime to be deleted, a slightly lower travel cost and optimize the path finding algorithm to "jump" to the end of each route assigned to a tile. I think this would alleviate some of the overhead of path finding, promote "common" trails and overall complicate the poor thing.  He may already be doing some of this as well.
Logged
"Having faith" that the bridge will not fall, implies that the bridge itself isn't that trustworthy. It's not that different from "I pray that the bridge will hold my weight."

Ogantai

  • Bay Watcher
    • View Profile
Re: Route caching
« Reply #2 on: December 02, 2008, 12:40:34 am »

It's just a method of reducing CPU load by using memory. 
Yep, but it's easier (and cheaper) to add more memory than to upgrade the cpu (in most cases).  ;D

Edit: Don't forget that running A* isn't exactly easy on memory either; compared to RBFS it's a pig. Given a sufficiently large fortress (both in area and in population) a route table would actually reduce average memory usage.
« Last Edit: December 02, 2008, 12:43:19 am by Ogantai »
Logged

scribbler

  • Bay Watcher
    • View Profile
Re: Route caching
« Reply #3 on: December 03, 2008, 01:11:34 pm »

I had suggested, while discussing something else, that DF run basic pathfinding for a set of locations based on use, landmarks (workshops, etc.) or arbitrary grid coordinates, such as every 16 squares. A dwarf would only have to path to and from this main route and updating it for traffic changes, tunnels etc, could be done in the background, like while giving orders or trading. Just assume any delay in using the new bridge is word getting around that it's done.
Once everything is caught up, extra CPU time (small forts, long trades, etc.) could be used to double check the route tables based on route usage/density or oldest calculated (just in case something was overlooked in calculating or updating).
Logged
End the slaughter of dorf kittens!
No self respecting beard wants to wear his pet as clothing! Dorfs need population control for pets and bad thoughts from products made from the animals they choose to bond with.
---------
"There are two means of refuge from the miseries of life: music and cats."
-Albert Schweitzer

Footkerchief

  • Bay Watcher
  • The Juffo-Wup is strong in this place.
    • View Profile
Re: Route caching
« Reply #4 on: December 03, 2008, 01:23:01 pm »

I had suggested, while discussing something else, that DF run basic pathfinding for a set of locations based on use, landmarks (workshops, etc.) or arbitrary grid coordinates, such as every 16 squares. A dwarf would only have to path to and from this main route and updating it for traffic changes, tunnels etc, could be done in the background, like while giving orders or trading. Just assume any delay in using the new bridge is word getting around that it's done.
Once everything is caught up, extra CPU time (small forts, long trades, etc.) could be used to double check the route tables based on route usage/density or oldest calculated (just in case something was overlooked in calculating or updating).

This would also have the cool side benefit of having dwarves follow habitual "trails," much like people do in real life.
Logged

eerr

  • Bay Watcher
    • View Profile
Re: Route caching
« Reply #5 on: December 03, 2008, 03:51:53 pm »

what about wide open spaces?
Logged

Mondark

  • Bay Watcher
    • View Profile
Re: Route caching
« Reply #6 on: December 03, 2008, 04:08:57 pm »


This would also have the cool side benefit of having dwarves follow habitual "trails," much like people do in real life.

Well, to some degree, they already do, as some paths are always the best way to go.  You can see this in an frequently traveled outdoor area, the grass turns reddish brown along the path, if it gets stepped on enough.
Logged
Fefeshnelmargalip

Ogantai

  • Bay Watcher
    • View Profile
Re: Route caching
« Reply #7 on: December 03, 2008, 05:53:34 pm »

I had suggested, while discussing something else, that DF run basic pathfinding for a set of locations based on use, landmarks (workshops, etc.) or arbitrary grid coordinates, such as every 16 squares. A dwarf would only have to path to and from this main route and updating it for traffic changes, tunnels etc, could be done in the background, like while giving orders or trading. Just assume any delay in using the new bridge is word getting around that it's done.
Once everything is caught up, extra CPU time (small forts, long trades, etc.) could be used to double check the route tables based on route usage/density or oldest calculated (just in case something was overlooked in calculating or updating).

This would also have the cool side benefit of having dwarves follow habitual "trails," much like people do in real life.
It would be even better if we could designate those trails manually. Kind of a "Dwarves on Rails" idea, where I could specify a route from my crafter's workshop to my finished goods stockpile, and have DF keep in cache persistently.
Logged

Footkerchief

  • Bay Watcher
  • The Juffo-Wup is strong in this place.
    • View Profile
Re: Route caching
« Reply #8 on: December 03, 2008, 07:13:40 pm »


This would also have the cool side benefit of having dwarves follow habitual "trails," much like people do in real life.

Well, to some degree, they already do, as some paths are always the best way to go.  You can see this in an frequently traveled outdoor area, the grass turns reddish brown along the path, if it gets stepped on enough.

Yeah, I think that's more an artifact of the pathfinding though -- they seem to prefer long sections of diagonal and orthogonal travel to straight-line approximations, if that makes any sense.
Logged

Warlord255

  • Bay Watcher
  • Master Building Designer
    • View Profile
Re: Route caching
« Reply #9 on: December 03, 2008, 08:07:14 pm »

Abstracting out a bit;

Due to the nature of fortress arrangement, a cached route would be harder to do because the route constantly changes.
Logged
DF Vanilla-Spice Revised: Better balance, more !!fun!!
http://www.bay12forums.com/smf/index.php?topic=173907.msg7968772#msg7968772

LeadfootSlim on Steam, LeadfootSlim#1851 on Discord. Hit me up!

Ogantai

  • Bay Watcher
    • View Profile
Re: Route caching
« Reply #10 on: December 03, 2008, 08:12:12 pm »

Abstracting out a bit;

Due to the nature of fortress arrangement, a cached route would be harder to do because the route constantly changes.
That's why you store the last n used routes. If the route changes, the old route gets dropped from the table and the new one gets put in.
Logged

sir monko

  • Bay Watcher
    • View Profile
    • wsa
Re: Route caching
« Reply #11 on: December 03, 2008, 08:30:18 pm »

Logged

scribbler

  • Bay Watcher
    • View Profile
Re: Route caching
« Reply #12 on: December 04, 2008, 12:46:11 am »

It would be even better if we could designate those trails manually. Kind of a "Dwarves on Rails" idea, where I could specify a route from my crafter's workshop to my finished goods stockpile, and have DF keep in cache persistently.
Well, you can designate high traffic areas, a route cache would make more use of them.
Yeah, I think that's more an artifact of the pathfinding though -- they seem to prefer long sections of diagonal and orthogonal travel to straight-line approximations, if that makes any sense.
Yeah, the way math works out moving diagonally 10 squares is half the steps without diagonals and covers 14.14 squares distance. So, most programs favor thew diagonals.
Abstracting out a bit;

Due to the nature of fortress arrangement, a cached route would be harder to do because the route constantly changes.
That's why you store the last n used routes. If the route changes, the old route gets dropped from the table and the new one gets put in.
I'm advocating sectioning the areas so that routes would be recalculated when there was some construction or traffic change in the areas they travelled.
There's probably some helpful math based on the fact that most of the map space isn't really used or at least relevant to most pathing and so on but I'm tired and I'd rather leave that discussion to those more interested in it. Maybe they can use my ideas and come up with some useful algorithms for Toady.
Logged
End the slaughter of dorf kittens!
No self respecting beard wants to wear his pet as clothing! Dorfs need population control for pets and bad thoughts from products made from the animals they choose to bond with.
---------
"There are two means of refuge from the miseries of life: music and cats."
-Albert Schweitzer

Ogantai

  • Bay Watcher
    • View Profile
Re: Route caching
« Reply #13 on: December 04, 2008, 01:27:58 am »

It would be even better if we could designate those trails manually. Kind of a "Dwarves on Rails" idea, where I could specify a route from my crafter's workshop to my finished goods stockpile, and have DF keep in cache persistently.
Well, you can designate high traffic areas, a route cache would make more use of them.
That wouldn't do anything to reduce CPU usage though. Designating traffic areas just weights the graph, it still calculates paths the same way.
Logged

TrombonistAndrew

  • Bay Watcher
    • View Profile
Re: Route caching
« Reply #14 on: December 04, 2008, 04:17:30 am »

There has been lots of research along these lines by professionals finding good algorithms to solve the travelling salesman problem.

http://en.wikipedia.org/wiki/Traveling_salesman_problem

Particularly interesting solutions include the any colony optimization (usable in DF), and the TSP discussion toward the bottom of the page. There are also some genetically based algorithms not specifically listed on wiki. Granted, this isn't exactly the same problem, as the travelling salesman problem is with connecting multiple points via the shortest path, and pathfinding in DF is just connecting two points, but the tile-based structure of DF makes the problem fairly similar.
Logged
Pages: [1] 2 3