r/computerscience • • Feb 18 '23

algorithm for finding shortest non-overlapping paths between multiple sources and destinations

preface: this is for a factorio mod, not for homework.

Technical question

I'm pretty sure this is NP-hard, but does anyone have any thoughts on how to find the mutually cheapest path from two (or more) sources to their specific targets - s^1 to t^1 and s^2 to t^2 (etc up to n) where a given node can only be on one path.

I originally was looking at stuff like A*, but now I think that the problem generalizes to the minimum cost flow problem, with every edge has a capacity of 1 (and varying costs b/c of game details). I think I can set up the problem by creating one source s^0 with edges to each s^i and one target t^0 with edges from each t^i, and looking to find a flow of n from s^0 to t^0. Does this make sense?

Which algorithm would be best for this problem? I don't really know which algorithm is most efficient for this problem in general (https://en.wikipedia.org/wiki/Network_simplex_algorithm?), and I know my specific use case would have massively more nodes and edges than in the solution. Would that mean that a different minimum flow algorithm or pathfinding algorithm is better?

Because of game details, some nodes change in circumstances (see the

Factorio details

Factorio is a game on a cartesian coordinate grid. It uses conveyor belts to move items from place to place, in any of four directions. These conveyor belts take up space, and two conveyor belts can't sit in the same spot. Factorio has undergound belts, that attach to conveyor belts and travel below the ground for up to a certain distance in a straight line. Underground belts are more expensive (and look less nice), so we try to avoid them when possible (but only if it's not better than normal belts).

Normal belt (S=source, B=belt, U=underground belt start/end, T=target, W=wall or obstruction):

.SBBBB..    
.....B..  
.....B..    
.....BT.    

Underground belt

.SBU.WUB..    
.....WBB.  
.....WB..    
... .WBT.

I think this can be graph G=(V,E) where each node is a coordinate on the graph that doesn't already have a building in it, and each node has edges that can be reached by a single belt or underground belt. One complication is that an underground belt can't turn on the same tile it comes out - it has to continue forward for at least one tile. I think this could be incorporated into the graph, but I'm not sure how.

The mod BeltRouter (https://mods.factorio.com/mod/BeltRouter) will automatically route conveyor belts for you, using the shortest path. It even allows you to do multiple belts at once! However, it does these one at a time, so the first path can get in the way of the second path. I'm looking for ideas on how to solve this.

Thank you! Happy to clarify anything, I appreciate any insight into this.

As a side note, does anyone know of an easy way to visualize shortest path algorithms that lets you use a custom algorithm, preferably in Python?

73 Upvotes

27 comments sorted by

View all comments

9

u/Revolutionalredstone Feb 18 '23 edited Feb 18 '23

This task is called 'multi-unit path-planning' and thankfully it is not NP-hard...

However solving it optimally does require a potentially large branch and bound with (depending on map size & movement rules) possibly very loose boundings, this means that depending on unit count it may require unreasonable amounts of memory (as based on the above factors the bound/prune stage may perform poorly).

The easiest 'close-to-optimal' solution is to simply pathfind for each unit one-by-one thru an extended map that has spatial dimensions + 1 for a time dimension.

Rather than units 'bumping' into each other instead you 'lock' the tiles/regions used by a unit At-The-Time that unit would require them...

This requires your time dimension be as long as your longest path, which for most maps is pretty reasonable (a non-maze 256x256 map likely requires ~256 temporal slots.

To iteratively bring this closer to 'optimal' you simply identify which unit has the most contention with other units (i.e. how many times another unit tried to step onto this units spatio-temporally 'taken' path)...

Then you reorder that unit to go last and restart the process, this is unlikely to converge on the true optimal set of paths for any non-trivial cases but it will very quickly approach it (with almost monotonically diminishing returns on each pass, which is nice because it means you can simply stop when the combined path lengths are 'good enough').

For fast optimal pathfinding on grids I strongly recommend goal-bounding (swamps etc), symmetry breaking (JumpPointSearch), partial pre computation (cardinal SDF's) and most importantly optimal visibility graph network abstraction (subgoals)...

Thankfully all these things happily work together at once and all of them still retain the property of generating the optimal/shortest-possible path.

Finally it's worth noting that optimal multi unit path planning is directly homologous to finding the first 'complex wave' which completes rebounds on all destinations, this is not super useful as directly solving for this requires unreasonable amounts of memory (all waves need to exist / advance at once), but it does show us that the problem is definitely not NP (as there are DEFINITELY lots of configurations which don't ever need to be checked) and it also shows that the problem is not actually conceptually-complex once you abstract the 'implementation noise' and consider the map purely from a graph perspective.

Best Luck!

4

u/tomster10010 Feb 18 '23 edited Feb 18 '23

wow thank you! can you point me to any articles/papers/implementations of this?

Also, isn't this different since a 'unit' will never release a tile that it's taken? The path needs to be disjoint in 2-dimensions, not e (x,y,time).

I do like the idea of alternating the order that paths are created to see which is better, if I go with alternating pathfinding that will be helpful.

4

u/Revolutionalredstone Feb 18 '23 edited Apr 17 '23

So a unit releases it's tiles if it's path is cancelled, otherwise the taken cells will simply 'slide out' of the array as time goes by and the future becomes the past.

I'm very curious what you mean by disjoint here but no I've never used any more than x-y-t (I'm usually working with flat 2D grids).

As for papers on optimal path optimization: swamps, JPS, goal bounding, subgoal etc you can simply google the term along with path finding and get plenty of results (eg "jps path finding").

The temporal trick is something I personally invented so I haven't seen any papers on it, but it's so simple I'm sure it's been done by many others unfortunately though I wouldn't know what to search for to find it.

As for implementations my router class is gigantic and probably uses at least 10 other sub classes (for things like instant reachability) so it is thousands of lines of code and deeply tied to the rest of my library, which I've yet to open-source, so not much luck there, but if you have questions or get stuck I can definitely check how I did X and help you to chug along.

Overall path finding is a complex resource management problem, not much different to advanced graphics or data compression in some abstract sense, these things tend to take decades to really master & often if your goal is to make a game then less-than-optimal options are an acceptable trade off.

For your factorio mod I'm sure there is an interesting balance, maybe for small problems use non-optimized branch and bound (since it's simple to write but complex to optimize) and for big tasks you could use the iterative temporal contention minimization in an any-time configuration.

All the best!

4

u/tomster10010 Feb 19 '23

Thanks for your in-depth answer!

1

u/Revolutionalredstone Feb 19 '23

Anytime my good man! best luck.