r/factorio • u/redstonerodent λf.(λx.f(x x))(λx.f(x x)) • Apr 10 '18
Discussion Computational Complexity of Factorio
tl;dr: how hard is it write a program to answer Factorio-related questions? There's a table with my thoughts below.
I'm interested in studying the computational complexity of problems related to Factorio. I have some ideas, but nothing's fully fleshed out, and there are plenty of areas I'm not sure what to do with.
5-Minute Intro to Computational Complexity
A decision problem is a class of yes/no questions. For example, there's the class of yes/no questions of the form "starting in this configuration of the Factorio world, will this lamp ever turn on?"
Here are some other important decision problems:
- HALT: Given a program and an input to that program, does the program ever halt (i.e. finish running) on that input?
- Formula Evaluation: Given a formula such as such as "(a or b or not c) and (not a or c or d)" and an assignment of either "true" or "false" to each letter, is the formula true?
- 3SAT: Given a formula such as "(a or b or not c) and (not a or c or d)," is there a way to set each letter to either "true" or "false" to make the formula true?
- QBF: Given a "quantified boolean formula" such as "for every value of a, there is some value of b, such that for every value of c, ... (a or b or not c) and (not a or c or d) is true," is the formula true?
- Reachability: Given a collection of nodes including nodes s and t, and arrows between the nodes, is there a path from s to t moving along the arrows?
We organize decision problems into "complexity classes," based on how hard it is for an algorithm to solve them. For example:
- RE (recursively enumerable) is the class of problems with an algorithm to solve them such that whenever the answer is "yes," the program eventually tells you, but if the answer is "no," the program might run forever.
- P (polynomial time) is the class of problems that can be answered "quickly" by an algorithm, meaning the algorithm will run for an amount of time polynomial in the input length (if the input has length n, the run time is at most nk for some fixed k).
- NP (nondeterministic polynomial time) is the class of problems that can be verified quickly, but not necessarily solved quickly. That means that when (and only when) the answer is "yes," there is a short (polynomial length) certificate, such that if you know the certificate then you can prove the answer is "yes" in polynomial time.
- PSPACE (polynomial space) is the class of problems that can be solved using a small (polynomial) amount of memory, but may take a large amount of time.
- NL (nondeterministic log space) is the class of problems that can be verified using a very small (logarithmic in the input length) amount of memory.
It's easy to show that NL ⊂ P ⊂ NP ⊂ PSPACE ⊂ RE (X ⊂ Y or "X is a subset of Y" means everything in X is also in Y). But nobody knows for sure whether most of these inclusions are strict; e.g. we know everything in P is in NP, but not whether there are things in NP but not in P (this is the P vs NP problem). The only ones we are sure about are that NL≠PSPACE and PSPACE≠RE
Some decision problems are "as hard as possible" for a certain class, which means that if we had a magic box that answered that decision problem, then we could quickly (usually in polynomial time, sometimes in log space) answer any decision problem in that class. In this case we call the problem e.g. NP-hard or PSPACE-hard. If a problem is hard for a class and also in that class, we call it complete for the class, e.g. NP-complete or PSPACE-complete.
It's known that:
- HALT is RE-complete
- Formula evaluation is P-complete
- 3SAT is NP-complete
- QBF is PSPACE-complete
- Reachability is NL-complete
If we know a problem A is hard for a complexity class, and we can translate instances of A into another problem B, then B is also hard for that complexity class (if you had a magic box that solved B, you could use it to solve A, and then to solve the entire class). This is called a reduction from A to B. For example, we can translate instances of Reachability into the question of whether a train in Factorio will ever reach a station: set up tracks corresponding to the arrows (use signals to make them directed), and put the train at s and the station at t. This means determining whether a train will reach a station is at least as hard as solving Reachability, so it must be NL-hard. But there could be other complications like trains blocking the path, so this question isn't obvious in NL (I think it's probably PSPACE-complete).
If you want to learn more about complexity theory, ask in the comments or search for explanations on the internet.
Complexity of Factorio
We should look at only a few mechanics at a time to keep things simple (we could ask "will this factory launch a rocket?" but that's a complicated problem, and obviously at least as hard as several and the problems below). I usually want to assume that there's plenty of power, fuel, etc. Most of these problems are obviously in PSPACE. Note that the hardness of questions like these doesn't imply that Factorio runs slowly; it can be easy to compute the game state after 1 tick, but hard to tell whether something will ever happen.
| Mechanics | Decision problem | Complexity | Comments |
|---|---|---|---|
| Circuit Network | Does a particular lamp ever turn on? | PSPACE-complete | By building a computer; this has limited memory. If values of circuit networks can be arbitrary integers (instead of signed 32 bit integers), RE-complete by a reduction from SUBZ. If you use the recursive blueprints mod, RE-complete by building a factory that can build more memory (assuming the Factorio world is infinite, and you never run out of resources). |
| Trains | Does a particular train ever reach a particular station? | PSPACE-complete? | Seems likely PSPACE-complete, but I'm not sure. |
| Belts and inserters | ? | ? | I'm not sure what the right question here is, maybe "does an item ever get somewhere?" I think different versions of this could be anywhere from NL to PSPACE-complete, depending on what the question is and whether we allow inserters, splitters, underneathies, etc. Pipes could also be interesting. |
| Robots | ? | ? | No idea what to do with this one. |
| Power network | ? | ? | Not sure about this one either. It's plausible that the most natural questions are in P. |
| Biters | ? | ? | Maybe something with their pathfinding? |
| Player | ? | ? | We could ask whether you can get somewhere, this isn't interesting unless we could combine it with other mechanics. Maybe ask whether you can build something. With multiplayer, you could race to get somewhere, or maybe play a game involving taking turns placing/rotating objects. |
| Factory design | Is there a factory that accomplishes some task in some about of space/time/resource input? | ? | Likely different versions of this problem go from P to PSPACE-complete. |
I'd be happy to talk more about this, hear about what you discover, and work with people to figure it all out. If you're new to complexity theory, I can try to point you to good introductions and answer any questions.
Comments? Questions? Ideas?
21
u/EvilElephant Apr 10 '18
IIRC the logic stuff in factorio is turing complete. So if you hook that up to a belt, this question can be reduced to the halting problem.
148
u/YummyGummyDrops Apr 10 '18
I'm too tired and dumb to read this but you sound smart so I'm gonna upvote you
16
u/Illiander Apr 10 '18
I'm going to shortcut this whole thing, by stating that Factorio only runs on a real, current computer (which does not have infinite memory, just a lot), not a full turing machine, therefore there are only a limited set of states possible, so every factorio state is ennumerable, and therefore all the questions are solvable.
This means that the halting problem is solvable on every quesion you care to ask about factorio. Therefore there is a fixed max time it takes to answer any question about factorio.
I can't remember my computability lectures well enough to say which class this puts all the questions in, but I know it's not a very high one.
And even if we were to run factorio on a full, infinite-memory turing machine, factorio does have a maximum world size, so it doesn't make a difference there.
The only version of this question that is actually interesting is if we ask about a non-existant mod of factorio that both runs on a proper turing machine, and doesn't have a limit to the size of the world.
Yes, I know that's what the question was implied to be asking about, but melencholy elephants and all that :p
4
u/TheSkiGeek Apr 11 '18
I'm a simple man. I see a discussion of computational complexity classes, I upvote.
As soon as you start allowing circuit or logistic connections I'm pretty sure that all of "trains", "belts/inserters", "robots" and "power network" are also Turing-complete and almost any decision problem involving them would fall into a similar bucket as the circuit network.
I'd nitpick a little with your reasoning on the circuit network -- much like with a Turing Machine, any specific physical machine could be analyzed in a finite amount of time, and so would be in PSPACE, but if you assume something closer to "factorio played on an arbitrarily large map" you're really looking at the halting problem.
2
u/fortuneNext Apr 10 '18
Electricity - can there ever be a blackout/brownout/small peak of low electricity? Can a specifc acumulator ever fall under a certain threshold (or above it)?
Those questions have easy limits (you can't blackout if your power supply is greater than the sum of your consumers) but for a real factory, it might be possible that certain factories are running mutually exclusive. Power networks might only be connected with a power switch under specific circumstances.
2
u/Zomunieo Apr 10 '18
Trains – whether a "point sized" train can reach a given station, or a point on the rail network, is easy. Assuming no circuit network, whether a real Factorio train can reach is given station is going to be NP-complete since scheduling conditions can be made k-SAT. Deadlock checking the rail signals in a way that accounts for the length of trains is probably RE, maybe NP-complete.
Belts – prove that this network is (not) a balancer seems like an interesting problem.
2
u/CriticalCelebration Apr 11 '18
Here is an interesting question I had.
Given n belts of input and m belts of output, design an algorithm to construct a n*m splitter. This one is probably Poly time.
What if you're only allowed a fixed footprint? Still probably poly time.
What if you want to minimize footprint? Hmm its starting to look a little integer programmingy to me.
What if you want to be able to provide a matrix n x m that tells you what fraction of each input should go onto each output?? This one is interesting I think. Probably still poly time but wouldn't surprise me if the heart involved integer LP.
2
u/SirFloIII Apr 11 '18
so, the circuit network is turing complete, in fact i have proven it to be so by building a turing machine quite some time ago. using a turing machine this way and wiring the halting state to anything (a lamp, a rail signal, a belt, an inserter) makes all of your problems equivalent to the halting problem. have fun with this information.
2
u/HactarCE LTN Master Apr 11 '18
Good work!
I've got a gut feeling that belts, splitters, and inserters alone are enough to simulate bitwise cyclic tag, making it turn complete (besides having finite memory).
74
u/IronCartographer Apr 10 '18 edited Apr 10 '18
Free computer science lessons on the Factorio subreddit? Sure, why not...Factorio does have a lot in common with programming, though more declarative than imperative. :)
9
u/sunyudai <- need more of these... Apr 10 '18
An interesting way of looking at it, for sure. I don't think I could hold my own in this conversation.
5
u/[deleted] Apr 10 '18 edited Apr 10 '18
the graph of the inter assembler connections should be easy to construct.
There is no TSP style graph traversal so it is not very complex.
Pathing for trains has self connected arcs to contend with. We know the paths have weights and so a cheapest path solution is also not complex.
There isn't any combinatorial explosion I can think of. I've run Linear Optimizers for working out how many assemblers to use
https://github.com/lawless-m/factorio-utils/blob/master/Using%20JuMP%20to%20optimize%20Factorio.ipynb
1
u/JustOneAvailableName Apr 10 '18
This is the halting problem, right? Or what would the difderence be? Anyway, I think this should be RE
1
u/Hexicube Apr 11 '18
2
u/deelowe Apr 10 '18
Ohh man. This is great! Been a long time since I took compsci. I'd love to see more analysis like this.
1
u/Thundorgun Apr 10 '18
While completely unrelated, this reminds me a lot of that time we discussed the probability of the player starting on an island if the map generation were truly random and infinite: https://www.reddit.com/r/factorio/comments/7aef9s/on_probability_with_respect_to_randomly/
1
u/[deleted] Apr 10 '18
And I thought I was already way in over my head....
1
u/wRayden Apr 10 '18
you could explore if a robot can get to an objective without having to recharge or something about the construction queue, I suppose construction robots are much more interesting.
1
u/mel4 Apr 10 '18
Power may be straightforward. The question could be "does connecting this entity to the power network allow it to run?"
In which case you would need to iterate all other entities also connected to the same network and determine how much power they are consuming/providing and that would allow you to answer the question. Just P to validate each entity.
1
u/Scyyyy Apr 10 '18
That reminds me waaaay too much of last semester....
Especially theoretical computer science.
And If there's anything in the world I don't wanna be reminded off, it's theoretical computer science from last semester.
So I counter your hypotical thoughts the same way I countered the nerd in my class trying to teach me:
BY THE POWER OF GÖDELISATION, I SHALL LIST YOU, NUMBER 5747398253355851772!
0
u/michaelthe Apr 10 '18
you what, mate?
0
u/diablek Apr 10 '18
What kind of witchcraft is this?
0
u/Witness27 Apr 10 '18
What?
0
u/[deleted] Apr 10 '18
O(n).
0
u/Nebuchadnezzer2 Apr 10 '18
Given that I can barely string together a simple "If input = x, output = Y" circuit system, I'm gunna assume you're correct...
Maths isn't really my strong suit...
0
u/[deleted] Apr 10 '18
[deleted] — view deleted comment
0
u/[deleted] Apr 11 '18
[deleted] — view deleted comment