That mod also has a combinator for setting the recipe of machines which I will also need, but that's kind of off topic for this thread. The lower half takes a list of things, chooses on of the things, replaces that item in the list with all the ingredients needed to produce it, and just keeps looping like that until the list is empty. Everything produced along the way is stored in one memory combinator (connected to bottom left medium pole) and also stores things that have no recipe in the memory connected to the bottom right medium pole (basic raw ingredients).
The order of selecting ingredients to iteratively evaluate matter for speed of completing the full computation (but not for final result). If several things require electronic circuits then if we can avoid evaluating what those are made up of until later then we can calculate the cost of all green circuits in one step by just multiplying by the count of all of them.
I use pushbuttons mod with different recipes to test it atm. The red color signal resets the output memories.
Selecting the minimum signalThe upper half does that. It replaces a single combinator (anything != 0 -> anything) with a massive chunk of combinators that iteratively loops in a similar fashion to the recipe cost calculator, but instead selecting one signal and then removing it and everything with a higher value until only the minimum is left. This takes some time to stabilize, but seems to complete within 50 ticks or so with each cycle taking about 9 ticks. This means that the clock in the lower recipe calculator needs to wait for the result a bit longer. It was stable at 12 ticks with a single combinator for selecting a signal, now that the majority of the time is spent in the big thing above it needs to be slowed down to 50-60 ticks per cycle. But the ordering of evaluations is improved enough that we still gain a speedup by reducing the number of evaluations.
So the difference between the anything selector and the thing above is that one selects one of the signals depending on something that seems to be the same order as the order shown in crafting window while my creation selects the one with the lowest value after a lot more waiting. The iterative process can be watched by mousing over the top right substation with the red and green wires connected to it.
Selecting the minimum signal isn't really optimal either, we actually want the signal that exist the least often in the tree of ingredients. But since most things are made out of several simpler things this is a good approximation. When something requires electronic ciruits then those add up and therefore pushed back in the queue of evaluations.
The process of selecting the minimal signal would take logarithmic time O(log(n)) in expectation if the signals were random or if the basic anything selector selected a random signal. This is because then half of the values would be expected to be >= the chosen signal end eliminated in a single step.
In practice the basic things are used more and are often ordered earlier in the crafting window (sort order) so the highest value signal is also likely to be selected in each step and we get something more linear O(n). But it's still pretty decent.
The games own cost calculator sees sulphuric acid as a basic raw resource even though it has a simple recipe that can be evaluated easily, so I added a combinator (can easily be disabled if you want) that also sends the sulphuric acid to the raw resource memory. It is still evaluated though so the actual raw resource cost is the one without that signal. And the petroleum gas signal, water and iron ore signal will then differ (more correct) from game calculation raw resource requirements.
The whole thing:
Just the minimum signal selector:
If you want to improve or speed it up then please do
Use red(!) wire to connect to the empty combinator at the bottom as if it was just an anything selector.