Comparing multiple values from multiple circuit networks
Posted: Thu Jan 05, 2017 11:00 pm
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
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
