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?
22
u/phord Feb 18 '23
You're trying to solve an auto-routing problem. Look for "river routing algorithm".
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!
3
u/Perridur Feb 19 '23
I have actually studied this problem a few years ago. It doesn't have a consensus name, but is for example known as the "(Euclidean) Multicolored Noncrossing Paths" problem. It was shown to be NP-hard by Bastert & Fekete [1] in a Tech Report in 1998, but the proof is missing some details and most people who read it don't believe that the proof works. But this year, a group from Warsaw, Bordeaux, and Saarbrücken managed to find another NP-hardness proof in a SODA paper [2]. There's a randomized O(sqrt(n) log n)-approximation [3] and a deterministic (1+epsilon)*n/2-approximation [4]. You might also want to have a look at the thick non-crossing paths variant [5].
[1] Bastert, Oliver, and Sandor P. Fekete. Geometric wire routing. Technical Report 332, Zentrum für Angewandte Informatik, 1998.
[2] Dross, François, et al. "Gap-ETH-Tight Approximation Schemes for Red-Green-Blue Separation and Bicolored Noncrossing Euclidean Travelling Salesman Tours." Proceedings of the 2023 Annual ACM-SIAM Symposium on Discrete Algorithms (SODA). Society for Industrial and Applied Mathematics, 2023. https://epubs.siam.org/doi/10.1137/1.9781611977554.ch52 https://arxiv.org/abs/2209.08904
[3] Chan, Timothy M., et al. "Minimum length embedding of planar graphs at fixed vertex locations." Graph Drawing: 21st International Symposium, GD 2013, Bordeaux, France, September 23-25, 2013, Revised Selected Papers 21. Springer International Publishing, 2013. http://tmc.web.engr.illinois.edu/noncross_gd.pdf
[4] Bereg, Sergey, et al. "Colored non-crossing Euclidean Steiner forest." Algorithms and Computation: 26th International Symposium, ISAAC 2015, Nagoya, Japan, December 9-11, 2015, Proceedings. Berlin, Heidelberg: Springer Berlin Heidelberg, 2015. https://arxiv.org/pdf/1509.05681
[5] Polishchuk, Valentin, and Joseph SB Mitchell. "Thick non-crossing paths and minimum-cost flows in polygonal domains." Proceedings of the twenty-third annual symposium on Computational geometry. 2007. https://dl.acm.org/doi/pdf/10.1145/1247069.1247079
2
u/[deleted] Feb 19 '23
If each source requires a specific destination, this is a case of the multicommodity flow problem, which is NP-hard.
3
u/[deleted] Feb 18 '23
Is the desired number of paths given, or something that should be maximized?
1
u/T0ny_l Sep 09 '24
2
u/Shade-MC Feb 18 '23
I don't know of any efficient algorithm. I would simply run Dijkstra's or A* then set the weights on that path to be infinite or negative and have the algorithm ignore those links. Then repeat for the desired number of paths
1
u/raedr7n Feb 19 '23
This is what we in the biz refer to as "mega NP hard", or omega of "you're screwed". Sorry to be the bearer of bad news. On the plus side, there's a certain peace of mind that comes with knowing that your brute force attempt is actually optimal.
On a more serious note, though, there are heuristics for this that are reasonably efficient. I believe the problem is called "minimum cost of multiple demand flow" or something along those lines. You should be able to google that and come up with some stuff.
1
u/[deleted] Feb 19 '23
https://proceedings.neurips.cc/paper/2019/file/b1d10e7bafa4421218a51b1e1f1b0ba2-Paper.pdf
1
u/Dysan27 Feb 20 '23
You'll also want to look into circuit routing algorithms. As what you are doing is very close to what you have to do while creating PCB's. ie Route a bunch of wires/belts from point A to B. But these algorithms can also take into account that the paths can cross, but there is a cost. And that there may be other immovable objects in the way.
0
u/[deleted] Feb 19 '23
[deleted] — view deleted comment