Page 1 of 1

Train station limit using circuits - deal with problem of decrease while loading

Posted: Wed Feb 17, 2021 10:07 pm
by JayS
So I have station limits set on my ore mines so that the number of trains requested is no more than the ore available to fill a train. I use 1-4 trains (holds 8k ore), so if a mine has 17k ore available in the loading chests, and a capacity for 2 trains, it will request two trains. (If it could only hold 1 train, then it maxes at 1). This uses the pretty standard circuit of dividing by 8k then maxing that with the station specific limit.

But there is a problem - say a mine has 16k ore (enough for exactly 2 trains) and 1 train becomes available at a depot to collect ore. This train gets dispatched and starts loading. Then the amount of ore decreases so there is only enough in the chests to fully load one train (e.g. we are down to only 15.9k ore). Now imagine that another train becomes available at a depot. This train won't be dispatched until the other train leaves the mine because the train limit has decreased to 1.

A solution to this is to read the train contents and send them back into the input that counts the amount in storage, so instead it is the chests+train contents. This way the limit will still be 2 until the train actively loading leaves (at which point it becomes 1), and another train can be dispatched even while the first is loading.

The only issue I could see with this is if there is some sort of race-condition with the limit system, where the limits are still at 2 after the first train leaves for a brief instant (maybe due to latency with the circuit calculations). So the limit would be 2 with two trains, the first train leaves, and for a break instant before the limit decreases to 1, a third train is allowed to make a reservation to go to this mine even though there is only enough ore for 1 train.

I guess I am looking for discussion of this issue, if you think it is possible to happen. Or if this optimization is even necessary. I will probably test the described scenario sometime this week and see what happens.

Re: Train station limit using circuits - deal with problem of decrease while loading

Posted: Wed Feb 17, 2021 10:47 pm
by JayS
It seems like a simple fix to the potential race condition (TBD if it is even a problem, will test later) would be to subtract 1 from the train limit once the train is almost full, e.g. if it has more than 7.9k ore or so. Leave enough room at the top so that this can propagate through the circuit system before the train finishes filling up and leaves.

This would still allow a train to be dispatched while another is loading, even if loading has caused the amount in the chests to decrease.

Re: Train station limit using circuits - deal with problem of decrease while loading

Posted: Thu Feb 18, 2021 12:22 am
by astroshak
Personally, I’m not concerned by this behavior. I try to set up multiple mines, enough that there is more capacity for loading trains than I have trains on the track.

If the nearby mine goes from 2 to 1 train limit, either there has yet to be a train dispatched, and the second train needing ore goes to another mine, or the second train has already been dispatched and should not be affected by the limit reduction. Of course, if it somehow is, it will find a different station to go to.

Re: Train station limit using circuits - deal with problem of decrease while loading

Posted: Thu Feb 18, 2021 12:30 am
by JayS
Update - I have completed testing and it does appear this race condition is a problem. See these two gifs:

The problem: https://gfycat.com/dearbelovedfugu

The solution: https://gfycat.com/wickedneatgnatcatcher

All I did was add a decider combinator taking the train's contents and outputting that value if the contents are not close to full (for uranium, less than 30 items).

For a 1-4 train for my base, I plan on using a condition of less than 7.5k items since 6x4 stack inserters can carry 288 items. This way the condition will activate before the train is filled. (If the condition were closer to 8k than 288, say 7.8k, then the condition would only become invalid on the final pass of the inserters, once the train is full, defeating their purpose).

Here is a blueprint for those curious:

Just the circuit: https://pastebin.com/jPgDa1nq

Circuit with train stop: https://pastebin.com/pgfpC9F0

Note that it is important that you use a seperate color for feeding the train contents back in than for setting the train limit, otherwise the signals get mixed.

Re: Train station limit using circuits - deal with problem of decrease while loading

Posted: Thu Feb 18, 2021 12:31 am
by JayS
astroshak wrote: Thu Feb 18, 2021 12:22 am Personally, I’m not concerned by this behavior. I try to set up multiple mines, enough that there is more capacity for loading trains than I have trains on the track.

If the nearby mine goes from 2 to 1 train limit, either there has yet to be a train dispatched, and the second train needing ore goes to another mine, or the second train has already been dispatched and should not be affected by the limit reduction. Of course, if it somehow is, it will find a different station to go to.
Yes, I suppose it really won't have an effect on a base's throughput. But I still enjoy making sure the small details of my train network are exactly how I want them. Trains are probably my favorite part of this game.

Re: Train station limit using circuits - deal with problem of decrease while loading

Posted: Thu Feb 18, 2021 2:32 am
by NotRexButCaesar
JayS wrote: Thu Feb 18, 2021 12:31 am Trains are probably my favorite part of this game.
Factorio: model train table that I can afford.