Comparing multiple values from multiple circuit networks

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
Post Reply
torne
Filter Inserter
Filter Inserter
Posts: 341
Joined: Sun Jan 01, 2017 11:54 am
Contact:

Comparing multiple values from multiple circuit networks

Post by torne »

I'm trying to come up with a combinator network which can find the maximum values for each signal amongst multiple inputs (separate wires). It's hard to explain exactly what I mean so I'll try with an example using the four science pack colours:

Input signals:
Input A: 5 red, 3 green, 1 blue
Input B: 2 red, 2 blue, 1 purple
Input C: 3 red, 5 green, 1 purple
Input D: 2 green, 2 blue

Desired output signals:
Output A: 1 red
Output B: 1 blue, 1 purple
Output C: 1 green, 1 purple
Output D: 1 blue

i.e. each output should have a signal for each corresponding input signal that is the highest value amongst all the inputs (and if there's a tie, all the signals in the tie should be set), and all the other signals zero. This should work for all signals on the wire, so it has to be entirely in terms of "each" operations in the combinators.

The use case for this is hard to explain without a lot of detail, but imagine the numbers represent how desirable it is to fetch a given item from a given place (as some function of stock level, delivery speed, etc) and the outputs will control whether items are requested from that place or not.

I can do this for *two* inputs easily by subtracting one from the other and using the sign of the result to "filter" out signals from each input, removing the signals where the other input had a larger value. Doing this to compare every input to every other input in turn, leaving each with only the values that were larger than all the others, seems like it would work, but is going to be quite big since it will increase in complexity with the square of the number of inputs. It feels like there should be a way to do this more compactly by first comparing pairs of inputs, then comparing pairs of pairs of inputs, and so on (logarithmic complexity), but I can't quite think of how to lay it out and make it work.

I'm wondering if anyone has tried to build something like this before and had any other ideas ;)

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Comparing multiple values from multiple circuit networks

Post by DaveMcW »

1. Input Y = Global max for each signal.
2. Input Z = (Y * -1) + 1
3. For each signal set, add Input Z and output values greater than zero.
torne wrote:It feels like there should be a way to do this more compactly by first comparing pairs of inputs, then comparing pairs of pairs of inputs, and so on (logarithmic complexity), but I can't quite think of how to lay it out and make it work.
Calculating the global max is easy to do this way. The basic comparison function is:
1. C = B * -1
2. D = A + C > 0, input count // Basic filter, but return the difference instead of 1
3. E = D - 2000000000
4. F = E + B < 0, input count // Add B back, to get the original A
5. Output = F + 2000000000
6. Repeat steps 1 through 5, swapping A and B to solve for cases where B is the max. Modify it slightly to account for B ≥ A, instead of just B > A.

torne
Filter Inserter
Filter Inserter
Posts: 341
Joined: Sun Jan 01, 2017 11:54 am
Contact:

Re: Comparing multiple values from multiple circuit networks

Post by torne »

Ahh, yes; thanks. Finding the max across them all can be done as a binary tree, and then they can all just be filtered by the max. That should be doable in a reasonable amount of space for reasonable numbers of inputs :)

Post Reply

Return to “Gameplay Help”