Question about "smart" trains that run on demand requests

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
Post Reply
halenrauch
Manual Inserter
Manual Inserter
Posts: 1
Joined: Tue Jul 31, 2018 2:51 am
Contact:

Question about "smart" trains that run on demand requests

Post by halenrauch »

Hello everyone,

I have a question regarding trains that run with "demand". What does this mean? my entire rail network has red wire on it, at every station. stations are of two kind: supplier, demander. every demand station is disabled unless it is low on a resource, iron for example. If station is low on iron, then it will raise a signal "Iron-1" (increment the count). It means there is one station low on iron. Supplier is a huge station and has multiple trains ready to go but waiting unless a demand is active. every train has path of "Supplier->Demand" to the specific resource, two stations only. When a station is low on resource (lets say %25 percent) then supplier releases trains until station is %80 full (SR latch), in other words until the demand is 0 again. supplier stations have chain signals at every track and they connect to main rail network via a single out controlled by a regular train signal (which is controlled by the demand) so trains stay at supplier stations until a demand is active.

This works fine as a concept and I have a working simple example (have not built full system yet and started a new game due to other reasons)

My question/problem starts when I try to add a "second" same resource supplier AND also try to balance the multiple demander inputs.

First I tried to limit the count of trains going to the demand station to balance the suppliers as well (not every train should rush to the demander). But it became meaningless as it takes multiple trains to fill a 48 steel chest station resource (4wagon with double side filling). I assume this is not a problem now.

Second issue: synchronizing the suppliers. I want to do something like both suppliers should send only limited count trains just to make sure supplier does not run out of resource (one train per demander for example). how to count trains that is send out for that resource? is there a way to balance trains of suppliers?

Third issue: a general one actually. assume a second demander station raises a flag. so two stations are demanding iron. how can I balance the trains sent out to the demander stations in a (not perfectly is acceptable) balanced approach? I assume the trains will go to the closest one and wait until the demand station's current train leaves instead of going to the other demander or is it just opposite?

Solution is supposed to be in vanilla circuit network without special mods that add smart trains/stations etc.

Any ideas?

Thanks

DaleStan
Filter Inserter
Filter Inserter
Posts: 368
Joined: Mon Jul 09, 2018 2:40 am
Contact:

Re: Question about "smart" trains that run on demand requests

Post by DaleStan »

halenrauch wrote:Solution is supposed to be in vanilla circuit network without special mods that add smart trains/stations etc.
Ah. Make it hard, why don't you. LTN would have been perfect for this.

I'd change the stations to report how many trains they want, instead of just "I want at least one train". The simplest way to do this is to change the combinator driven by the SR latch to report the number of trains needed to fill the chests to the desired level, instead of just reporting 1. More elegant is to calculate how many trains are needed based on the available space in the chests.
You'll also need one or two counters, and those need pulsed inputs. Either set the requesters to pulse, or convert the rising edge to a pulse at each supplier.[0] Use that pulse to count how many lifetime trains have been requested. Also count how many lifetime trains have been released. It's tempting to report released trains by pulsing a negative number into the counter, but you can't achieve both perfect supply and perfect balance if you do that. With two counters, use the green wire for supply counting, or multiplex the supply and demand counters onto the same wire[1].
I don't think overflow is a reasonable fear; one train per tick for 2 billion ticks is just over a year. Also an enormous number of both trains and stations. If you want something that will work forever anyway, add a circuit that subtracts (a multiple of) the number of supply stations from the counters whenever the counters get too large. (2520 is the smallest number that's a multiple of all numbers from 1 to 10, so you could use that, and probably never have to change that circuit. If you're planning on more suppliers, 360,360 gets you 1-15, and 232,792,560 gets you 1-22.)
halenrauch wrote:how to count trains that is send out for that resource?
I'd set each train stop at the suppliers to "read train contents", and use a falling-edge converter (much like the rising-edge converter in [0]) to pulse 1 (not "input value", in this case) of whatever cargo type is on the train when the train leaves the station (and the stop output, therefore, falls to 0). There are other solutions involving reading the status of rail signals, but then you have to Just Know what sort cargo is on that train.

Now you have the tools you need.

  • For perfect balance and perfect supply: If there are any unfulfilled requests, add one to (total supply count)%(number of supply stations) to determine which station's turn it is to fulfill that request. That station releases one train, and pulses one into the appropriate counter. If, for example, there are 5 supply stations, 124 lifetime supplied trains, and any trains requested, station 5 will release 1 train, since 124 mod 5 is 4. Several ticks later, station 1 will release a train, assuming there is still demand for that resource. In this scenario, all suppliers must supply all cargos. In the other ones, things may be slower than desired, but I think it'll all work out in the end.
  • For perfect balance but imperfect supply: If the number of unfulfilled requests is greater than the number of supply stations, each station sends one train. (And also pulses into the appropriate counter.) Otherwise, no stations send any trains. Repeat as needed.
  • For imperfect balance but perfect supply: Stations 1 through (number of unfulfilled requests) supply one train. This will be "all stations", if the number of unfulfilled requests is larger than the number of supply stations. The rest, if applicable supply nothing. Repeat as needed.
If the demand stations remain enabled until they've received exactly the required number of trains, and trains headed to one demand station can always re-path to any other, this should work.
halenrauch wrote:I assume the trains will go to the closest one
Trains will go to the closest enabled station with the specified name, but "closest" is defined by the pathfinder, which includes penalties for trains currently on the intermediate rails and trains that are stopped at stations. The "trains headed to one demand station can always re-path to any other" part is required since a train may have chosen the "wrong" station when it left. That station was too popular, and is now full, so it's disabled itself, and the train now needs to find a path from wherever it happens to be to its new destination.

[0] I think you can construct a circuit that outputs (Iron Signal from tick T)-(Iron Signal from tick T-1), unless that number is negative, with three combinators. Place an anti-backfeed (Each+0->Each, for example), and connect its red output to an Each*-1. Join the green outputs of both and feed that to an Each>0-to-Each combinator. I've haven't constructed this for real, though.
[1] My muxer/demuxer is here viewtopic.php?f=193&t=61722. It works in vanilla, but it is dirty; the multiplexing control signals show up on the outputs of the demuxers and on the inputs of the muxers. viewtopic.php?f=193&t=19594 is another option. It reports clean outputs, but it may not work without mods. If you multiplex, you cannot pulse at the requester stations.

mrvn
Smart Inserter
Smart Inserter
Posts: 5709
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Question about "smart" trains that run on demand requests

Post by mrvn »

Trains go to the cheapest station. The cost of a station rises with the distance, with signals along the path (especially red ones) and stations it needs to cross. Also weather the station has a train present or not. But when 2 trains start at the same time it's likely they go to the same station. Nothing you can do about that.

What you should do is disable stations with enough items buffered. That way no train will go there. You can also disable stations with a train present that are nearly full (chest + train content > target). No need to mess around with signals on that end.

So the remaining problem is sending out only as many trains as needed. As the other reply explains that is a matter of counting trains. I would suggest a slightly different method though than perfect counting.

At each station have a counter (each + 0 with output connected to input). On the input also add the number of trains the station needs. If the counter is > 0 then pulse 1 on the green wire and set -1800 (amount is subject to how long trains take to arrive) on the counters input. subtract the counter value from the counter when the station has enough items (reset it to 0).

Globally have a counter that sums up the green wire. Then at the supplier send a train when the global counter has item > 0 and subtract one.

With multiple suppliers you can send trains if item > 1, >2, >3, ... or put a tick count on the green wire too and use tick % 600 == 0, 60, 120, 180, ... to have each supplier send trains a second apart.

Post Reply

Return to “Gameplay Help”