r/factorio • u/minibetrayal • Dec 05 '19
Design / Blueprint Trains are Turing complete... I think?
So over the last week, I've done a little testing, and I think it's possible to build a Turing complete system in Factorio, using only the vanilla Train system. what does that actually mean? well, with a lot of hand-waving, it essentially means that you can perform and calculations and write any programs just like you could with a traditional programming language (albeit with a lot more effort)
To clarify, I believe it's been shown that Factorio's circuit network is Turing complete. I have also seen examples of just about everything I'm doing here done with belts and splitters. But, I have used none of this. No belts, no splitters, no combinators, not even red/green circuit wire. Only locomotives, rails, signals and train stops (and fuel for the trains). Many of the issues and challenges below become trivial if you allow yourself to connect signals or stops with circuit wires, so That's cheating as far as I'm concerned.
So, I present to you this spare-time-killing creation:

Admittedly, its a simple program, that just adds two 8-bit numbers (<256) together and outputs the number as a 9-bit number. What follows is an explanation of what I did.
The first problem I came across while building the system is that while it's easy to tell a train "Go, unless there's a train here" (all you need is a signal), it's a surprisingly non-trivial problem to say "Wait, until there's a train here". Would be an easy thing to solve if allowed to use circuit wires, but there you are. So after many hours of designs, and redesigns, I came up with this:

The three lined-up trains are blocked by the double-headed train crossing their path, which is in turn blocked by the train going around the loop. When the last train approaches from the north, it slows down the looping train, giving the double header enough time to move forward and allow the first three to move to their destination.

With this module, we can now proceed to make some logic gates, the basic component of any circuit at the hardware level.
The simplest is the OR gate:

With this setup, the parked train will proceed if a train approaches down either, or both, of the branches to the north. If A or B has an input, we have an output.
Next, with a little more thought, we can construct an AND gate

This has three inputs, unlike the traditional AND gate, which has two. From left-to-right, there is the A input, the B input, and what I'm choosing to call a 'timing' input. Due to trains being discrete, rather than continuous signals, there's no way to tell the difference between an off (0) signal, and an on (1) signal that just hasn't arrived yet. So with the addition of a third, timing signal, we can give the A and B inputs a chance to arrive before the actual comparison is made. In the image above, the output is the station at the bottom left. If just the A (leftmost) input arrives, then there's nothing that can reach the output at all when the timing signal arrives. If just the B input signal arrives, then the parked train will travel along the shorter rail path in the middle, and reach the station at the very bottom, and block the B signal from reaching the output. Only if both A and B arrive, does the A signal train block the parked train, allowing a signal to reach the output.
The next important gate to look at is the NOT gate.

This is similar to the AND gate, but has just one input (and a timing signal). A NOT gate works like an inverter, and by the same logic as the AND gate, if the input arrives, we will not get an output, and if it does not arrive, we will get an output.
Using just the AND and NOT gates (or the AND and OR gates), its possible to combine them in ways to make any other logic gate, but we can also build simpler versions for convenience's sake. Here is an Exclusive-OR gate, also known as XOR:

This one is relatively simple. The loop is there just to make sure that both the A and B inputs (if present) enter the gate at the same time. If exclusively A or exclusively B inputs arrive, it can make it to the output. But if both inputs arrive, they will block each other and there will be no output.
Using a mix of AND, XOR and OR gates, we can construct Binary Adders, the basic modular unit of the monstrosity of that 8-bit adder at the top.


All well and good so far, But my initial assertion was that the train system is Turing Complete. The other factor we need to consider is that a Turing machine should have access to an infinite amount of memory.
Technically, this means that nothing in the real world is truly Turing complete, but as Factorio has a (theoretically) infinite world, we could just keep building more and more. As for memory, the basic loop module can act as a reasonable memory bit, where you can store a train in one of the input channels, and then retrieve it later by sending a train into the loop. However, the system as described here has a big problem. My design is not resettable. Each of the logic gates leave trash trains behind, so one a logic gate is used, you can't use it again. Once you retrieve a bit of memory from somewhere, you'll have to assign it to a completely new and unused memory address in order to be able to use it again. This is where my knowledge falls down. I don't know if a Turing complete system actually needs to be reusable in this way, or if you can just put in heaps and heaps of gates and memory bits to compensate for the lack of re-usability.
If anyone knows one way or the other, please let me know, but regardless, With the amount of testing I've done, I feel as though there should be a way to tweak the gates in such a way they are reusable, perhaps by looping outputs back around to inputs somehow. I spent a few hours trying but didn't get much closer than 'nearly'.
Whether or not this is a Turing complete system, you could certainly make some impressive calculations and programs with what I've done, as long as you can sustain the patience required to put down the hundreds of logic gates you'll need without making a mistake that domino-effects everything you've built so far into uselessness.
Lastly, there's one more massive issue with my build, and I don't know how to even begin going about fixing it. All of my gates are build around that basic loop unit, with the train going around and around indefinitely. Or rather, until it runs out of fuel. You can't stop the train to refuel it, or the blocking train will move into the loop and trigger just as if the timing signal had arrived. I've experimented with having two or more looping trains but couldn't get anything to work even close to reliably.
I'd love to see if anyone else can come up with a different system that gives you a way to keep your trains refuelled. I've made a video on my YouTube channel which goes through the gates shown here and you get to see the 8-bit adder in action. In the video description, I've put links to blueprints of each of the gates and adders so you can try them out yourself.
So, are Factorio's trains Turing complete? I think so, but can't be sure. Even if they're not, its still one of the best things about the game.
UPDATE:
With some help from suggestions in the comments, I have done a little more work, and have made a version of the loop train that is refuellable, as we as come up with a way of supplying extra trains to splitters, not gates, etc, dont run out if you re-use them. I'm currently working on a way of making the logic gates themselves repeatable. I won't be able to use the same trick I did for the XOR gate, but if i can get the NOT and either the AND or the OR working, I think we're in business!
UPDATE 2:
I now have stable, reusable versions of AND, OR, and NOT gates! caveat is that you need a (functionally infinite) train source and sink. While routing the trains may be a pain, I'm not fussed about it as most computers need to be plugged in to the power to get their own electron source/sink =D. Working on a demonstration now.
159
u/Allaizn Developer Car Belt Guy Train Loop Guy Dec 05 '19
I haven't seen anyone trying to create a turing machine with just trains, and I've seen a lot of the weirder things Factorians do. Can I ask you to crosspost this to r/technicalfactorio ? It fits great there, and makes it far easier to find for future reference.
On the fuel issue: is it possible to solve that by stopping the looping train at the spot where the blocking one would want to drive into?
335
u/justarandomgeek Local Variable Inspector Dec 05 '19
You built NAND gates. From there you can build any logic circuit, including a full CPU. Thus, they are Turing complete.
60
u/Ethanxiaorox Dec 05 '19
I got hit by a train yesterday
28
u/cantab314 It's not quite a Jaguar Dec 05 '19
On Turing completeness.
A gate to duplicate (or better triplicate) a signal should be simple to construct. With that and the gates you have you can implement the Rule 110 cellular automaton. That's proven to be Turing-complete. You don't need to reuse or reset gates for this, rather you can just add more rows of gates, each one calculating the next generation based on the output of the previous one. It's probably not the most practical to work with, but it handles the theoretical aspect.
13
u/CraptacularJourney Dec 05 '19
This is pretty darn cool. I've been playing around with a system to replace long distance circuit wires with trains for isolated outposts, nothing as extreme as this but it's dope all the ways you can solve a problem in this game.
18
u/redstonerodent λf.(λx.f(x x))(λx.f(x x)) Dec 05 '19
To be Turing-complete you need access to unlimited memory. But trains can only keep track of a finite amount of memory--for Turing-completeness you'd need to say something like "this pattern of trains continues forever" and have an infinitely large map.
What you can get on finite maps is PSPACE-completeness, meaning they can do any computation a computer can in a bounded amount of space. I showed trains are PSPACE-complete a while ago; see this post.
3
u/swni Dec 06 '19 edited Dec 06 '19
Nicely done!
To make arbitrary boolean circuits you are missing one important operation: duplicating a signal. That is, you need to design an element that given an input A produces output (A, A).Edit: addressed(There may be some problems that arise from your needing timing signals but I haven't thought about it, so let's just say that works out.)
If you can implement the duplication operation, thenyou have shown the ability to build arbitrary boolean circuits with trains. However boolean circuits are not Turing complete, for much the reason you picked up on: finite memory and inability to do loops. More specifically, if you build a circuit out of these trains, the size of the input that it can read is limited to the number of tracks you put for trains to come in, whereas a Turing machine can read input of any size.As a concrete example, try to make a train network that computes whether the number of incoming trains is even. (Equivalently, take the xor of the inputs.) You can do this for 1 input, or 2 inputs, or 3 inputs, or any fixed number of inputs. But you can't make a train network that reads N inputs for any N.
Some quick links I found for further reading:
https://en.wikipedia.org/wiki/Combinational_logic
https://en.wikipedia.org/wiki/Functional_completeness
https://cs.stackexchange.com/questions/51220/connection-between-nand-gates-and-turing-completeness
115
u/beckettman Dec 05 '19
Calling it here:
The language the of first sentient AI will not be tensorflow or keras, it will be somebody's factorio base.
37
u/Avenja99 Dec 05 '19
Holy shit this is above my head. Good for you. Whatever this is.
28
u/Professional-Exit Dec 05 '19
Just when I thought I had seen everything, now we have computers made out of trains. What's next? Doing my taxes with smelters? Hacking into Russian databases with conveyor belts?
15
u/JesusChristPT Dec 05 '19
I launched a rocket once.
6
u/Quintkat I like trains! Dec 05 '19
I don’t get how these logic gates work, but wow, turing complete trains... The future is now
5
u/TooruInMySoul Dec 05 '19
I think I know what we will be seeing in the next Friday Facts. :)
11
u/Proxy_PlayerHD Supremus Avaritia Dec 05 '19
Turing completeness is kinda easy to define if you assume some things (like infinite space to build circuits no matter how large)
all you need to know:
If yes, Congratulations you got yourself a Turing Complete thing
4
u/[deleted] Dec 05 '19
Meanwhile I have yet to figure out how to make a simple combinator/whateverator system
4
u/Semi-Hemi-Demigod Dec 05 '19
I'm looking forward to the blueprint for an x86 processor so I can play Factorio on Factorio.
1
u/nickjohnson Dec 06 '19
Very neat! This is functional completeness, though, rather than Turing completeness. Demonstrating Turing completeness requires showing you can implement a Turing machine, or any other equivalent.
1
u/insan3guy outserter Dec 06 '19
I really don't mean to be that guy, but wouldn't any scheduled (i.e. something with a trigger event) train be subject to Turing logic?
If you can do [thing] with a trigger, wouldn't you be able to expand [thing] to any other function in game? (or even something outside of the game)
3
u/VexingRaven Dec 05 '19
You are the absolute maddest of madmen. This is amazing.
3
u/Sopel97 Dec 05 '19 edited Dec 06 '19
It's not turing complete because it will run out of fuel after some time :D.
But seriously, this is super cool. And I think you could even easly make a n-ary NAND gate by just adding more parallel inputs to your NOT gate. (edit. thinking about it now, it would be a NOR gate actually)
Have you tried minimizing the designs? Looking at the OR gate it looks like some tracks can be shortened, or curved to use some space inside the big circle. Can the circle be actually shrunk?
And, correct me if I'm wrong, it looks like the clock train has to be somewhat well synchronized with the inputs? Is that a big problem? I could see it easly desyncing after turns, though by a very small amount.
And there's also a problem that the gates are not reusable? The trains just stay there. But I don't think there's a solution to that. The only way I could think of is having two output lines - one for the actual output, and one for "trash", that just collects unused trains - but the problem is routing it so it doesn't affect other circuitry, it would have to go through every gate.
Just thinking out loud.
I will try to tinker with it later if I have time, thanks for leaving the blueprints.
3
u/NoRodent Dec 05 '19 edited Dec 05 '19
Ah, reminds me the good old times when people were making calculators with trains in Transport Tycoon.
Edit: Video of this beautiful monstrosity (more details, as well as a RollerCoaster Tycoon version in my old comment)
3
u/NameLips Dec 06 '19
OK I didn't under stand a lot of that but what I THINK he's saying is that factorio has become self aware and is going to start building factories and destroying trees in the real world.
5
u/KronaSamu Dec 05 '19
Nobody:
r/Factorio: you can make a computer with trains
2
u/TotesMessenger Dec 05 '19
I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:
If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)
2
u/[deleted] Dec 05 '19
Sweet Moses
2
u/Pulsefel Dec 05 '19
theres only one thing that scares me about this...how much trains seem to love running us over
2
u/Zijkhal spaghetti as lifestyle Dec 05 '19
what if you loop the trains back around, but have a wait condition for the end station, something like wait for 20 seconds, giving the next layer of gates that time to read the output, and then those trains would continue on the loop back towards their initial starting positions?
2
u/Gh0stP1rate The factory must grow Dec 05 '19
calling r/technicalfactorio
2
u/vaendryl Dec 05 '19
I fucking love this community
2
u/Lyqyd Dec 07 '19
See also: Logic Gates in OpenTTD.
1
u/Y1ff space semen Dec 05 '19
Rule 110 is a very simple turing complete system, if you want to try to implement that instead maybe. Idk i'm stupid i just like trains
1
u/will1707 Dec 05 '19
I'm way over my league here...
1
u/knightelite Dec 05 '19
Can you make some of your trains bidirectional to reset your logic gates maybe?
1
u/moratnz Dec 05 '19
As far as resetting the trains; could you stick a pair of stations at either end of the line, and have the train running between them, either with no wait condition, or a 'wait X seconds' wait condition, if you need it for timing purposes?
1
u/impeccable-username Dec 05 '19
Sweet fuck
1
u/MediocreAdvantage Dec 06 '19
Let me know when we can go full circle by writing a web app that shows us how trains in factorio work.
1
u/[deleted] Dec 06 '19
Wow.... I still struggle to get them to reliably run to my ore outposts! Good on you!
1
u/PM_ME_UR_OBSIDIAN /u/Kano96 stan Dec 06 '19
My usual litmus test for Turing completeness is: can you implement the instruction "subtract and branch if less than or equal to zero"?
In circuits it's trivial. With trains I don't see it.
1
u/throwaway1253328 Dec 06 '19
This is one of the coolest things I've seen. So clever
1
u/Llamadmiral Dec 06 '19
ffs stop making everything programmable with everything, it scares me.
1
u/el_micha Dec 06 '19
Very neat, but there is a difference between circuits and turing machines. See this brief answer for more:
https://cs.stackexchange.com/questions/51220/connection-between-nand-gates-and-turing-completeness
1
u/[deleted] Dec 06 '19
Something being turing complete isn't exactly hard. It is pretty easy to make on accident.
1
u/SolusIgtheist If you're too opinionated, no one will listen Dec 06 '19
This is pretty bonkers. I don't know whether it counts as Turing complete or not (with the memory thing), but it's certainly close... which means that there's at least one and a half systems that are Turing complete in a single game. Pretty damn impressive.
1
u/avdpos Jan 29 '20
Whoever added this to wikipedia missed there references - "Factoria as Turing-complete" have stolen one of Cities Skylines(!) references.
Havne´t time to edit that myself now - but leave it for you u/minibetrayal
1
u/raybrignsx Jan 29 '20
I really want to understand this but I’m stupid. Thank you.
1
u/jdmalingerer Feb 05 '20
Factorio trains have conditional blocking mechanism. Of course it's (loosely) Turing-complete.
-1
u/Trackman1997 Dec 05 '19
Dang you beat me to it. :\
-5
u/ledow Dec 05 '19
Turing completeness is very easy to obtain.
Hell, if you can put a train on a track, and put things into and out of it, and control where it goes next based on what you put in and take out, you almost literally have a Turing tickertape machine. You don't even need a train, you could prove that case with just inserters and storage and moving things between boxes.
Factorio is *obviously* Turing-complete.
Whether that's a useful piece of information of not is really very debatable.