## Min / Max Combinator circuit help

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
MrDrummer
Fast Inserter
Posts: 131
Joined: Sat Nov 22, 2014 2:51 pm
Contact:

### Min / Max Combinator circuit help

So when I played Factorio last, it was 0.14 and there was a mod called Math Co-Processor, which allowed getting the min, max and random signal from the values on the poles ajacent to it.

This allowed for getting the min/max of any number of different signals.

This mod has since stopped development.

How would I do this with combinators? I had tried it myself back along, but my brain fried. Plus the way i did everything meant exponentially more combinators the more values I wish to compare.

Context: I'd like to get the lowest storage depot quantity for my iron, copper, coal and stone. But ideally a blueprint that can be used elsewhere.

quyxkh
Filter Inserter
Posts: 851
Joined: Sun May 08, 2016 9:01 am
Contact:

### Re: Min / Max Combinator circuit help

How tricky are you prepared to get with your wiring? With isolated inputs (input wires not already driving other inputs) you can select the signal set from n sets that has the min/max of a particular signal in 3n-3 combinators and log_2 n time.

Start with the basic trick that leads to 3n-3 combinator usage and linear time: negate the second set (Each×-1⇒Each) and wire the result and the first set to an (Iron<0⇒Everything) comparator, then wire the second set and the comparator result to an (Each-0⇒Each) isolator:
Select the signal set having the smallest iron signal
snap@T1700752=512x160-2-18.25,z2.jpg (5.35 KiB) Viewed 3923 times
The second set's signals are on the isolator's green input, so if the comparator doesn't do anything that'll be your output. The comparator sees the first set minus the second set, but only outputs the difference if the second set's iron was greater, so the isolator sees B (on the green wire) + (A-B) (on the red wire) with the difference added only if (A-B)'s iron is < 0. B+A-B is A, so you get all of A's values if A's iron is least, B's otherwise.

This gets the correct result 3 ticks worst case 0 ticks best: best case is some A values change but B's iron remains least, if some B values change but B's iron remains least then tick 0 B stays the best, tick 1 the isolator's outputting any new B values; worst case is tick 0 B's iron becomes less than A's, tick 1 the negator result's live, tick 2 the comparator result's live, tick 3 the isolator result's live. To add another input, the bottom right isolator's output is your new A signal, make a new row and wire it up:
Same, for three sets
snap@T1778538=528x224-2.25-18.25,z2.jpg (8.59 KiB) Viewed 3923 times
If linear time's okay, having the right result as of say half a second ago with ten inputs doesn't leave you wishing you could get the delay down under a quarter second, you can just drag-spam the bottom two rows there and the least will show up on your bottom right isolator otuputs. But the log time wiring change is obvious: for each set of four inputs, wire the pairs up separately and treat their outputs as the A and B inputs to the middle:
Logarithmic-time wiring for four sets, readout's in the middle.
snap@T1855492=560x304-2.5-19.5,z2.jpg (12.32 KiB) Viewed 3923 times

This does sometimes have transients, if a B signal changes to have greater iron it'll take a couple ticks for the lesser value to overtake it, so if tick one the A set is iron 43 and the B set is iron 12, then tick 2 the B set gets iron 600, on tick 3 and 4 you'll see the 600-iron set until the negator/comparator pair's result catches up. If that's intolerable you can put a pulse converter and a delay on there, don't output anything if the iron value has risen in the last two ticks, but generally you're dealing with small per-tick changes, one inserter hand capacity at a time, and calculation results lagging a little isn't worth worrying about.

Edit: I keep thinking it's doable with 2n-1 combinators, that just looks _so_ sparse, are any of the isolators but the last really necessary?

Edit 2: the setup's pretty easy to adapt, if instead of delivering the signal set with the smallest value for iron youi want to deliver the largest value presented for each signal regardless of origin, change the comparators from Iron < 0 ⇒ Everything to Each > 0 ⇒ Each, that might be what your "min/max of an arbitrary number of signals" means.

MrDrummer
Fast Inserter
Posts: 131
Joined: Sat Nov 22, 2014 2:51 pm
Contact:

### Re: Min / Max Combinator circuit help

Thank you so much for spending time helping me with my problem!

I have not built this in my test save quite yet, but how does it handle two lowest values that are the same?

copper: 1,000,000
iron: 2,000,000
coal: 500,000
stone: 500,000

I used to use the Random combinator from the mentioned mod, but I guess I need to figure out a way to pick one. (or is that what the "any" selector does?)

As far as compactness goes, I don't really care - this will be used in a master controller anyway. I just want to be able to slap down a blueprint and configure the values i want. (And possibly be able to disect it to expand if needed)

I'll be sure to play around using your explanation in the near future

quyxkh
Filter Inserter
Posts: 851
Joined: Sun May 08, 2016 9:01 am
Contact:

### Re: Min / Max Combinator circuit help

Okay, for picking the signal that has the lowest numeric value from a single set, not max coal from a lot of mixed-inventory sets but max item from a single mixed-inventory set, that one I don't know. Pretty sure I remember XKnight and maybe DaveMcW in a discussion about that, their conclusion was ~it's hard~. Best way to isolate a single item signal from a set I know of is feeding the set to a set-filter stack-filter-inserter that has the items available, but that won't be random and might not be greatest or least. See viewtopic.php?t=52697 for my take on that, it's unwieldy though.

Maybe a little mental aikido, is there some way to achieve your ends (some kind of load balancing, but what?) that uses combinators' strengths?

phi1010
Inserter
Posts: 46
Joined: Sat Aug 20, 2016 3:31 pm
Contact:

### Re: Min / Max Combinator circuit help

I just built a maximum calculator, trading space for time: viewtopic.php?f=193&t=71845

You might be able to calculate a minimum with this too, if you:
Change the rounding to floor instead of ceil:
* remove the S := -1 constant calculator
* Change the sum from M := Each/N to M := S/N
and request values lower instead of higher than average:
* change the >= filter to =<

Or simply try to negate the input (and maybe add the sum of all items to each, to enforce positive values) -- I haven't tested it for negative values yet, usually this might either trigger inability to distinguish between large numbers with small differerences, e.g. {-9,-8}, or elimination of the maximum itself, causing an empty result.

Getting a random item's value is probably impossible, since there is no way to apply a distinguishing filter operation to one that doesn't apply to the other too, if both items have the same value. A constant combinator with different signals for each item would be a minimum requirement -- or a signal where every item can occur independently, e.g. from sushi belt.

WilliamR
Manual Inserter
Posts: 2
Joined: Wed Oct 17, 2018 4:08 pm
Contact:

### Re: Min / Max Combinator circuit help

So,... Is there a chance that we simply get a min / max operator for the arithmetic combinator? Seems like this is a no brainer and an easy extension...

DaveMcW
Smart Inserter
Posts: 3256
Joined: Tue May 13, 2014 11:06 am
Contact:

### Re: Min / Max Combinator circuit help

Suggestion Forum is this way: viewforum.php?f=6

MrDrummer
Fast Inserter
Posts: 131
Joined: Sat Nov 22, 2014 2:51 pm
Contact:

### Re: Min / Max Combinator circuit help

Figured i'd post an update on my original purpose of this thread...

I have got a train controller working! The Min/Max shown at the start of this thread is used all the time! Very helpful

My main purpose for removing an item from a min/max "test" was to prevent trains going to a coal outpost for example when I have plenty in storage.

I think the easy solution is to just disable the resulting min signal from doing anything when the item in question is over a large number (10mil for arguments sake), and when you want to disable an item that is being compared, just set it to be over that threshold. This would be for min, in the context of "go to the ore pickup station for the ore we have the least of in storage"

Then, since uranium or coal is above everything else that we care about (under 10mil), the others will get priority. If everything is over the magic number (10mil), then nothing happens with the chosen output.

A combinator mode for min/max (maybe even random) would be cool too

Honktown
Filter Inserter
Posts: 748
Joined: Thu Oct 03, 2019 7:10 am
Contact:

### Re: Min / Max Combinator circuit help

10 mil in storage? yikes! Every level of Mining Productivity you lost another million ore. Sure it might be like, one patch worth you're losing, but the efficiency!
I have mods! I guess!

MrDrummer
Fast Inserter
Posts: 131
Joined: Sat Nov 22, 2014 2:51 pm
Contact:

### Re: Min / Max Combinator circuit help

It's more because I have not got the processing for said ore up and running yet. I'd rather everything comes to a halt than for trains to back up because the stations meant to unload are chocka!

quyxkh
Filter Inserter
Posts: 851
Joined: Sun May 08, 2016 9:01 am
Contact:

### Re: Min / Max Combinator circuit help

To pick a single highest or lowest: burst the wire out through N pairs, once for each signal you're interested in, the pairs forward that signal to itself and to signal-I. The outputs from each pair are the inputs to the above selection network, which will pick exactly one (I, resource) pair having the desired value, if coal is lowest at 723 it'll output {coal=723,I=723}. If there's duplicates it'll pick one fairly arbitrarily, you can work out the priority.
pic
If anybody's still interested, thinking I couldn't be the first person to invent that, I googled for it and discovered an everything comparison *does not compare a signal against itself* so you can get all highest or lowest values (or no value if duplicates exist) much more easily, everything<coal output coal does in fact output coal if that's the highest value. This is much more useful than my read on what the wiki says, "The Everything input functions as a logical AND, and will return true only if ALL non-zero inputs pass the conditional.", because if coal is a non-zero signal it does not pass that "< Coal" conditional. I hope the wiki's wrong.

MrDrummer
Fast Inserter
Posts: 131
Joined: Sat Nov 22, 2014 2:51 pm
Contact:

### Re: Min / Max Combinator circuit help

Thanks! Will save this for when I next take a crack at Factorio.

### Who is online

Users browsing this forum: fderty