Page 1 of 1

Find maximum value iterative

Posted: Sun Mar 17, 2024 4:40 pm
by SpookyMask
Find the maximum of as many input values as you want. The logic has 1. loop, 2. algorithm and 3. values.

Image

To use the comb, give values. Each value has a combinator with condition "I = [next number]". The numeration starts from 1. Connect the combinators with green wire in the input and red wire in the output.

Set N in combinator (top left) to the number of values.

Turn R on and back off again to reset the max value.

The algorithm constantly loops and updates the max value.


Image

The loop produces single ticks every three seconds, meaning 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, ..., n-1, 0, 0 and then starts from the beginning.

The algorithm calculates how much to add to the max value in order to update it. It updates it if the number is bigger than the max value. For example if the current max value is 7 and the currently processed number is 16, 7 is converted to -7, 16 is added to get 9 and 9 is added to the max number. (9 is > 0 : 16 is greater than 7). These are three operations and that is why the looop "waits" for the algorithm to finish befure it supplies the next number.

The values can be positive, negative, can have multiple items and the condition can become Everything > 0, Anything > 0, Everything < 0, and Anything < 0

Programming in Factorio. What a blast :)


Re: Find maximum value iterative

Posted: Sun Mar 17, 2024 8:53 pm
by Qon
Welcome to combinator programming and the forum!

If you want to peek at my blueprint, here it is:

It finds max by taking the average and filtering out signals that are less, basically halving the amount of input signals left every 4 ticks~.
Clockless (signals just propagate as fast as possible with each signal path equally long to "just work"), stateless (same here, "just works" because of how the looping computation is tuned) and deals with whatever amount of signals you throw at it automatically with no configuration.

Maybe @mmmPI can link the more involved public description I posted before because I'm not sure what terms I would use to search for that post :lol:
That version also has a continuation for sorting all signals (giving each signal an index based on relative size), if that's interesting.

Re: Find maximum value iterative

Posted: Sun Mar 17, 2024 10:04 pm
by SpookyMask
Awesome idea! I couldn't think of it, and you implemented it in such satisfactory way. I see concepts for the first time and I've been playing for years.
That version also has a continuation for sorting all signals (giving each signal an index based on relative size), if that's interesting.
Wow. Please gimme!

Re: Find maximum value iterative

Posted: Sun Mar 17, 2024 10:21 pm
by Qon
SpookyMask wrote:
Sun Mar 17, 2024 10:04 pm
Awesome idea! I couldn't think of it, and you implemented it in such satisfactory way. I see concepts for the first time and I've been playing for years.
That version also has a continuation for sorting all signals (giving each signal an index based on relative size), if that's interesting.
Wow. Please gimme!
Glad you like it :D

viewtopic.php?p=602838#p602838

Re: Find maximum value iterative

Posted: Mon Mar 18, 2024 12:03 am
by mmmPI
Qon wrote:
Sun Mar 17, 2024 8:53 pm
Maybe @mmmPI can link the more involved public description I posted before
Sure can do : viewtopic.php?p=602838, glad i could help you hijacking other people topics with your wonderful blueprint :twisted:
SpookyMask wrote:
Sun Mar 17, 2024 4:40 pm
The loop produces single ticks every three seconds, meaning 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, ..., n-1, 0, 0 and then starts from the beginning.
The meaning is clear, but the wording is incorrect, it produce single tick every three tick. I have verified !
SpookyMask wrote:
Sun Mar 17, 2024 4:40 pm
Set N in combinator (top left) to the number of values.
I changed this by adding 1 "infosignal" in every constant combinators on the right side, and linked them with a green wire, then you can isolate the sum of "infosignal" and pass it on the red wire in the green top left box as a "N", to avoid that manual operation. :)

That add some combinators, but i thought, it may also be possible to set the value against which I is compared automatically, based on this 1 N, if you do a partial sum of the N, the first one will be 1, then it can pass the value in an arithmetic combinator that just do +1 so you can have one on each lane with first having value of 1 the second lane a value of 1+1 so 2 then 2+1 =>3 and so on. To make it easier top copy paste more row of values using overlapping blueprints. But it's not possible to use the N channel, although using other channel it is still possible to make a version that's easy to expand with minimum intervention :




It's not changing the core logic, just a small detail that i thought would fit on this machine, maybe it is also possible to automate the reset everytime the loop is rolling over with some extra combinator for even more details. It is an inspiring machine !