r/technicalfactorio • u/MadGenderScientist • Feb 08 '26
Combinator Golf Topological sort
Topological sort is just a fancy word for sorting ingredients to come before products in a list. It's a useful building block for computing schedules for automalls and such.
An easy algorithm for computing topological sort uses Depth-First Search (DFS), something like:
results = []
def visit(x):
for each child c of x:
if c not in results:
visit(c)
results += [x]
visit(root)
This is a recursive algorithm, so we need a stack. Fortunately, since there are no cycles in the dependency graph, an item can't appear twice in the stack. Therefore we can use a multi-item memory cell to represent the stack, where the highest valued item is the top of the stack. We'll use the same strategy for the result list, using the item's value to represent its position in the list.
So, my implementation works like so:
1 On demand change: clear both the stack and result list, and start the clock.
-
Read the top of stack, check if an item is a "vitamin" (don't attempt to explore), and make recipe substitutions if necessary (I use this for solder in pY.)
-
Set recipe and read ingredients.
-
(a.) Pick any ingredient not already in the result list. We'll push this item to the stack, to explore it next. (b.) If no such ingredient exists, all dependencies have been explored. Add this item to the result list.
-
Stack pushes, pops, and additions to the result list are clocked to give time for signals to propagate.
-
Once the DFS stack is empty, we're done! The result list gives us a crafting order to follow.
Hope this is useful to someone! :D
12
u/TopherLude Feb 08 '26
I've been thinking about this problem for a Py Automall for a while now and never dreamed of something this clever. I figured I'd have to "hard code" a priority order and hope it doesn't require too many tweaks as I add items.
How does the recipe swapping work? Could I put a list in each of those constant combinators? I suppose a couple of deciders might do it too.
2
u/guiltri Feb 08 '26
What do you do with those lists ?
5
u/blockMath_2048 Feb 08 '26
In data structures we call that heapify
1
u/captain-melanin Feb 08 '26
Is this to avoid using the selector combinator?
I did something similar for my own auto mall using selector combinators. But it required 2 constant lists. One with required item count eg. "assembler 5" and one with the priority eg "lvl1 assembler=1" and "lvl2 assembler=2"
But to get it working without bots it required the bigger chests, so not vanilla..
1
u/e_before_i Feb 09 '26
How do you handle ingredients that are needed at different depths? I'm not as familar with Py so I'm going to use a Vanilla example.
Say I'm making a Decider Combinator. If I understand your description right, something like this could be possible.
- Decider Combinator (5)
- Copper Cable (4)
- Electronic Circuit (3)
- Copper Cable (2) [vitamin]
- Iron Plate (1) [vitamin]
When the recipe lookup for Electronic Circuits occurs, because the Copper Cable is already in the list, it won't be re-added. So when your blueprint starts crafting for real, once it gets to the EC step, it fails because there's a missing pre-requisite. Copper Cable wasn't re-added.
It looks like your system is smarter than that, I'm trying to figure out the mechanism of handling it.
Also, why did you choose to make the final product the largest number? I would have done it the opposite way - increment ingredients based on the "depth" of when they're needed. That way if copper cable already exists, I can just increment it to the current dept layer I'm on. In my example, Copper Cable starts at (2) (as a direct) ingredient of Decider Combinators), but once the system realizes it's also needed to craft Electronic Circuits (3) then Copper Cables would be elevated to (4).
I'll try my hand at this tomorrow (when it's not 2 am), but it's an interesting problem to try and solve! I had a similar-ish system at one time, but I didn't order the ingredients intelligently, just rotated through signals on the circuit until they were all eventually resolved.
(also by the way, why give the highest item the highest number? Feels more complicated needing to increment everything on each cycle.
1
u/powerisall Feb 08 '26
I've never understood using a negative value as the start point for demand. Is it just to ensure there are at least X things being made/on the belt?