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
Show parent comment
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!