## Advanced circuit network conditions for train behavior in 1.1

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
foamy
Filter Inserter
Posts: 432
Joined: Mon Aug 26, 2019 4:14 am
Contact:

### Re: Advanced circuit network conditions for train behavior in 1.1

DaveMcW wrote:
Thu Dec 10, 2020 10:21 am
You can multiply everything by -1 to convert it to a maximum finder.
On testing, there's more to it than that. Factorio truncates negative numbers as well. Thus, for an example I hit right now, 323 / 15 = 21, and -323 / 15 = -21.

For finding the minimum of a positive number, this works; the result of the division is guaranteed to be equal to or less than all of the input signals. It fails on negative numbers, because that no longer holds true. For a simple example, -5 / 2 = -2, but the input signals could be -3 and -2.

If the comparator is also flipped to greater-than it will now successfully find a maximum, but that maximum is actually the minimum if you've multiplied the original dataset by -1, which doesn't really advance me any. I think for this to work properly you need to be working strictly in the domain of positive numbers.

That means that to find a maximum you need to take your inputs, multiply them all by -1, and add a large positive constant. Then, out the other end, subtract that positive constant again, and then multiply that by -1. Unless I'm missing something clever, anyway.

EDIT:

Realized a simpler way would be to flip the comparison to greater-than-or-equals, but bias the input with a large negative constant, and then remove it from the output.

EDIT2:

It is also important to note that the arithmetic combinator that converts the gated inputs to a single-channel sum for use in the division operation cannot be fed a sum beyond Factorio's bit-range, or this operation will fail. In finding the minimum of positive numbers this won't usually arise unless you're working with very large circuit-driven or combinator-set values, but it meant my initial plan of using MIN_INT for the negative bias in the maximum-finder failed. Wound up choosing -10M, but this imposes bounds on both the number of signals it can process and the maximum range those signals can assume.
Last edited by foamy on Fri Dec 11, 2020 10:42 pm, edited 2 times in total.

Hildron
Manual Inserter
Posts: 3
Joined: Wed Nov 01, 2017 6:34 pm
Contact:

### Re: Advanced circuit network conditions for train behavior in 1.1

burninghey wrote:
Thu Dec 10, 2020 2:50 pm
is there a final bp for this?
Not a final BP as you need 2 decider and one arithmetic combinator and it depends on your station layout how you want to organize them. I blueprinted only the needed combinators for a loading station. With that you can adapt your station layouts.

I don't know if there is a better solution to compare two values. How my setup works:

The signal from all chests for a wagon is fed into an arithmetic combinator with: Each divided by wagon capacity, output A (one letter per combinator).
Wire two "chest letters (e.g. A and B) to two deciders with:
1. If A =< B output A
2. If A > B output B
3. Run the output of the second decider to an arithmetic combinator with: B + 0 output A (just to turn signal B to A).

Use Output of 1. and 3. as first input for the next combinator triple. Continue until you have compared all values and wire the output to the station with set train limit option enabled.

KoblerMan
Fast Inserter
Posts: 185
Joined: Sat Mar 05, 2016 12:59 am
Contact:

### Re: Advanced circuit network conditions for train behavior in 1.1

Ok, so I promptly forgot about this post and happened to check on it again. I know it's months old at this point, but this is a forum, so I consider the wisdom contained within to be timeless for newcomers, and thus worth sharing!
Hildron wrote:
Wed Dec 09, 2020 5:21 pm
I tested your setup for setting the train limit. Unfortunately it doesn't work because while the loop for calculating the average is running, it is outputting higher numbers for some ticks. With how train limit and slot reservation works, this is enough that trains will route to the station even after the correct train limit is set (once a train has reserved a station slot, it will do so even after the train limit is changed).
This is a big problem that I ran into pretty quickly and racked my brain to sort out, but my first mistake is using DaveMcW's design instead of my own (not saying DaveMcW's designs are bad - just the opposite, which is that they're JFM to me). The closest I got to figuring this one out turning down the tick speed all the way and looking at the wires in the moment this happened was realizing that I had crossed some wires in my design, particularly the logic on the inserters and chests for averaging out the load per wagon. The problem here is that I neglected Factorio's limitation of only having 2 colored wires for transferring a signal. Ultimately though, after spending countless hours working on this and not making any progress, I gave up.

I haven't done much testing, but from what I've seen, the few ticks where the train limit bounces around between arbitrarily high numbers is volatile enough that trains don't get called over during those ticks. Maybe this is a bug, or an intentional failsafe to prevent unexpected behavior?
Hildron wrote:
Thu Dec 10, 2020 8:13 am
astroshak wrote:
Thu Dec 10, 2020 2:50 am
Now that I think about it ... it might just be simpler to wire the six chests per cargo wagon together, and output their contents to an arithmetic combinator. Divide the total by the amount that one cargo wagon can hold, and output that to its own distinct signal. Do what was suggested above, and compare them by pairs, outputting the lowest signal. The last signal released could then be used as I use the T signal, when >= 1 enable the station, and set the limit to that number. This would basically be a variation on the OP’s initial situation (pre 1.1) with a bit of computing done near the locomotives.
That's exactly how I finally solved it.
I would love to see a blueprint for this. I could make it myself but if it's already been done, no point in duplication of effort.
burninghey wrote:
Thu Dec 10, 2020 2:50 pm
is there a final bp for this?
Here's my final blueprint for my train stations, updated for 1.1 (WITH the aforementioned issue of high number output for some ticks):

As an added bonus, here's the rail blueprint book I use:

System Specs
OS: Windows 10 Professional 64 Bit
CPU: AMD Ryzen 5 3600X (@~3.8 gHz)
GPU: Nvidia RTX 2080
RAM: 32GB DDR4 (2400)
DRIVES: 2x 1TB NVMe SSD, 1x 6TB HDD

astroshak
Filter Inserter
Posts: 602
Joined: Thu May 10, 2018 9:59 am
Contact:

### Re: Advanced circuit network conditions for train behavior in 1.1

In the time since this thread started, I have come to the realization that using a balancer so that each wagon gets the same amount of material mostly eliminates the need to verify that each wagon chest set has enough for the wagons.

While there may be a small difference, it does not appear to have any real significance or impact on the design. Nowadays I just sum the entire contents of all the wagon chest sets, divide that by the maximum load of the train, and send that to the train stop as signal L. The stop is enabled if L >= 1, and the train stop limit is set to L.

The one potential drawback to this is that trains with more than just two stops will cycle through the other stops if all of these supply stations are turned off. That, however, just IMO indicates a supply issue and not a train or other issue.

Trific
Fast Inserter
Posts: 153
Joined: Thu Dec 31, 2020 7:57 pm
Contact:

### Re: Advanced circuit network conditions for train behavior in 1.1

This problem is similar to a technique I use to balance between stations. The solution I came up with has each input check itself against all the other signals and if its signal is the lowest, passes it on as a distinct signal. Ties for lowest end up passing more than one signal, but since they are distinct, whatever is going to use the result can simply choose any of them.

It uses 2 arithmetic combinators and a decider combinator per input to find the minimum of a set of inputs. The set of inputs (checkmarks on the constant combinators) is transformed into distinct signals (A-D in the blueprint) by the leftmost column of arithmetic combinators and then aggregated onto a power pole. The decider combinators then take these aggregated signals and compare them to the input in their row (the checkmark). It outputs 1 on a given signal (red signal in the blueprint) if its checkmark is less than or equal to all of the aggregated signals. The rightmost arithmetic combinators then multiply the red signal (1 or 0) by the checkmark and again produce a distinct signal, so that only those that are the minimum get passed on. The example in the blueprint has inputs 2, 2, 3, and 4, and produces 2 signals of 2 on the final power pole.

Signals should be corrected to deal with quantities of 0 outside of this circuit.

KoblerMan
Fast Inserter
Posts: 185
Joined: Sat Mar 05, 2016 12:59 am
Contact:

### Re: Advanced circuit network conditions for train behavior in 1.1

As OP, I'm necro'ing this thread because although this post and design are pushing 3 years old, I've still been using it in my factories and continually revisiting and optimizing the design as best I can, and I want to leave on a good note by sharing the latest blueprint for people who still run into this topic.
Hildron wrote:
Wed Dec 09, 2020 5:21 pm
I tested your setup for setting the train limit. Unfortunately it doesn't work because while the loop for calculating the average is running, it is outputting higher numbers for some ticks. With how train limit and slot reservation works, this is enough that trains will route to the station even after the correct train limit is set (once a train has reserved a station slot, it will do so even after the train limit is changed).
Yeah, I found this out the hard way when I was testing the design. I made a debug version and racked my brain trying to figure out what caused this. The best I can figure is either the red or green wire (don't remember which) that connects all of the chests and inserters together also mixes the signal of the ore contents with the A/B/C/D signals. When the minimum signal part of the logic hits that average and ticks over, it sends a bunch of different signals each tick for about 3-5 ticks which can get the train limit to go astronomically high. I kinda gave up trying to fix this because outside of having another color of wire, I can't feasibly prevent this from happening, so it's just a limitation of Factorio. To be fair, this is a very niche issue and I'm willing to ignore it because it's an edge case. I would imagine pre-megabase you will never have so many trains on your network which are constantly pathing to supply and demand stations that it would cause an issue.

Here's my latest blueprint for all of the stations (color coded via the Factorio Cheat Sheet convention):

I called these stations "SmartFlux" because they needed a catchy name, I guess.

System Specs
OS: Windows 10 Professional 64 Bit
CPU: AMD Ryzen 5 3600X (@~3.8 gHz)
GPU: Nvidia RTX 2080
RAM: 32GB DDR4 (2400)
DRIVES: 2x 1TB NVMe SSD, 1x 6TB HDD

KoblerMan
Fast Inserter
Posts: 185
Joined: Sat Mar 05, 2016 12:59 am
Contact:

### Re: Advanced circuit network conditions for train behavior in 1.1

Okay. I updated the SmartFlux trains where I fixed that bug in the logic that calls way too many trains over (turns out it was because I was crossing wires in a place that I shouldn't have and signals were getting mixed up). What lit a fire under my butt to change it was the fact it was happening to me way too often on a late game base I was doing in space exploration.

Enjoy guys!

System Specs
OS: Windows 10 Professional 64 Bit
CPU: AMD Ryzen 5 3600X (@~3.8 gHz)
GPU: Nvidia RTX 2080
RAM: 32GB DDR4 (2400)
DRIVES: 2x 1TB NVMe SSD, 1x 6TB HDD

KoblerMan
Fast Inserter
Posts: 185
Joined: Sat Mar 05, 2016 12:59 am
Contact:

### Re: Advanced circuit network conditions for train behavior in 1.1

Also, here's all my rail blueprints that I use:

And here's my PAX stations:

System Specs
OS: Windows 10 Professional 64 Bit
CPU: AMD Ryzen 5 3600X (@~3.8 gHz)
GPU: Nvidia RTX 2080
RAM: 32GB DDR4 (2400)
DRIVES: 2x 1TB NVMe SSD, 1x 6TB HDD