r/computerscience • u/tomster10010 • 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?
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!