Train routing. Completely explained

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
Post Reply
Harkonnen604
Filter Inserter
Filter Inserter
Posts: 285
Joined: Thu Jun 09, 2016 5:56 am
Contact:

Train routing. Completely explained

Post by Harkonnen604 »

Is there any document or probably a thread describing ALL of train routing aspects? Like that a programmer can code algo from. I do not mean proposed builds, I mean the exact algorithms they take, so I can prebuild stuff in my head instead of reverse-engineering it through experimenting (and getting unexpected results along the way).

Example of questions I have vague answers to so far:
1. Does a train reserve its path by the time it departs a station or it adjusts its path while waiting at a signal before junction?
2. Does chain signal expand several chain signals ahead up until regular signal or just to the single next block?
3. If chain signal is put before rail splits two different ways, which directions does it chain-signal to? does it depend on train waiting on a chain signal? how does it interfere with intermittent train path updating (if there is any)?
4. How does exactly one-way tracks work (when signal is placed on only one side), how does train consider during initial pathfinding?
5. Do trains consider other trains on the track during pathfinding, or just signals, or nothing at all?

So an exact algo is deeply wanted. or a link. youtube videos and threads here describe only some particular cases, but a lot of important details still remain unclear to me.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3699
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Train routing. Completely explained

Post by DaveMcW »

F4 -> always -> show_rail_paths

Harkonnen604
Filter Inserter
Filter Inserter
Posts: 285
Joined: Thu Jun 09, 2016 5:56 am
Contact:

Re: Train routing. Completely explained

Post by Harkonnen604 »

That simplifies reverse-engineering, thank you :) but the real algo is still much appreciated

Shokubai
Filter Inserter
Filter Inserter
Posts: 470
Joined: Mon May 02, 2016 3:17 pm
Contact:

Re: Train routing. Completely explained

Post by Shokubai »

Harkonnen604 wrote: 1. Does a train reserve its path by the time it departs a station or it adjusts its path while waiting at a signal before junction?
2. Does chain signal expand several chain signals ahead up until regular signal or just to the single next block?
3. If chain signal is put before rail splits two different ways, which directions does it chain-signal to? does it depend on train waiting on a chain signal? how does it interfere with intermittent train path updating (if there is any)?
4. How does exactly one-way tracks work (when signal is placed on only one side), how does train consider during initial pathfinding?
5. Do trains consider other trains on the track during pathfinding, or just signals, or nothing at all?
1) At each signal (chains are special case)
2) Multiple chains in a row the train must lock up the whole chain...it will not decide to stop at a chain in the middle of a group of chains.
3) GENERALLY, shortest path. If a train can go two ways and there is a train in the FIRST block after the Y then the train will go the other way. If a train is farther down the Y branch the train may go the same way as the first train if the path is shortest. You can help yourself at Y's (assuming they go to the same place) by making your first block AFTER the chain signal a longer than normal one. This will force more trains both ways.
4) No idea what you are looking for here.
5) No

and p.s. trains do not really do path-finding other than to verify they can reach a destination and determine shortest route. Path-finding in Factorio is just "I know where I am going but what do I do at this block.

Harkonnen604
Filter Inserter
Filter Inserter
Posts: 285
Joined: Thu Jun 09, 2016 5:56 am
Contact:

Re: Train routing. Completely explained

Post by Harkonnen604 »

Thank you :) that explains most things. I'll experiment more to see if any questions remain.

Frightning
Filter Inserter
Filter Inserter
Posts: 807
Joined: Fri Apr 29, 2016 5:27 pm
Contact:

Re: Train routing. Completely explained

Post by Frightning »

Train pathing looks ahead to the next regular signal in the direction it wants to head, and checks if that block is available. If it is, the train will reserve it (and now the block is yellow anticipating the train soon to occupy it). Path-finding is dynamic, and it will adjust based on new information, but since it only looks one regular signal ahead (through any number of chain signals), it won't account for traffic beyond the block defined beginning at the aforementioned regular signal that the train is looking ahead to.

As for 1-way vrs 2-way rail. The way that is handled is via signalling, if signals are placed symmetrically (of either type, including combination of regular and chain) then the associated blocks are seen as 2-way, otherwise they are only 1-way. (Note that improperly positioning a pair of signals will break the 2-way nature of the blocks that begin at the given signal pair, and trains won't be able to actually path through that location).

Keeping all of the above in mind makes proper signalling not that hard to figure out, you just need to think about what ways trains will enter and leave crossings, and consider subdividing long stretches of track into a few blocks with regular signals so that multiple trains can be moving through it at once. Also paying attention to 1-way and 2-way track (and any transitions between them).

Loewchen
Global Moderator
Global Moderator
Posts: 8285
Joined: Wed Jan 07, 2015 5:53 pm
Contact:

Re: Train routing. Completely explained

Post by Loewchen »

Harkonnen604 wrote: 1. Does a train reserve its path by the time it departs a station or it adjusts its path while waiting at a signal before junction?
[...]
4. How does exactly one-way tracks work (when signal is placed on only one side), how does train consider during initial pathfinding?
5. Do trains consider other trains on the track during pathfinding, or just signals, or nothing at all?
1. A train does reserve all blocks in its stopping distance on the chosen route. The stopping distance is dependent on velocity, length and number of locomotives.
4. Only the correct direction is considered a viable path.
5. Train path finding in general considers length of a path and calculates penalties for occupied blocks and occupied stations. The penalty for occupied blocks considers the length and distance of the block, the one for stations is dependent on the remaining waiting time of the occupying train.

Greetings Loewchen

Jupiter
Fast Inserter
Fast Inserter
Posts: 174
Joined: Thu Jun 23, 2016 2:38 pm
Contact:

Re: Train routing. Completely explained

Post by Jupiter »

Loewchen wrote:5. Train path finding in general considers length of a path and calculates penalties for occupied blocks and occupied stations. The penalty for occupied blocks considers the length and distance of the block, the one for stations is dependent on the remaining waiting time of the occupying train.
I'm not entirely sure but this system has some weird consequences. Let's say you have a rather long block that is occupied. The penalty the block gives by being occupied is (at least proportional to) 2*length of block/distance from start (as per https://www.factorio.com/blog/post/fff-68). So every tile of this block gives the same 'amount of penalty' to the path.

Now you decide to break the block up in 2 equal pieces by placing a signal in the middle. The first (closest to starting point) block gives exactly half the penalty as the whole block did because its distance to the start is the same as for the large block, only its length is halved. The second block, however, gives LESS than half the penalty. This is because its distance from the starting point to this block is bigger.

This means that a higher signal density on a certain track means that it is more likely to be chosen by trains.

This system is also the cause of the problems of a loop-based system. This is, I think, because a train can lower the penalty that a certain occupied block gives by planning a detour via a loop somewhere before approaching the occupied block, thereby increasing the distance from the block to the start so it gives less penalty. This is just my suspicion so please don't quote me on this :D .

Jürgen Erhard
Filter Inserter
Filter Inserter
Posts: 298
Joined: Sun Jun 12, 2016 11:29 pm
Contact:

Re: Train routing. Completely explained

Post by Jürgen Erhard »

Harkonnen604 wrote:
Fri Jun 24, 2016 11:12 am
Is there any document or probably a thread describing ALL of train routing aspects? Like that a programmer can code algo from. I do not mean proposed builds, I mean the exact algorithms they take, so I can prebuild stuff in my head instead of reverse-engineering it through experimenting (and getting unexpected results along the way).

Example of questions I have vague answers to so far:
1. Does a train reserve its path by the time it departs a station or it adjusts its path while waiting at a signal before junction?
2. Does chain signal expand several chain signals ahead up until regular signal or just to the single next block?
3. If chain signal is put before rail splits two different ways, which directions does it chain-signal to? does it depend on train waiting on a chain signal? how does it interfere with intermittent train path updating (if there is any)?
4. How does exactly one-way tracks work (when signal is placed on only one side), how does train consider during initial pathfinding?
5. Do trains consider other trains on the track during pathfinding, or just signals, or nothing at all?

So an exact algo is deeply wanted. or a link. youtube videos and threads here describe only some particular cases, but a lot of important details still remain unclear to me.
I wonder why we never got an *OFFICIAL* answer. Every answer here is <expletive deleted> because NOT OFFICIAL. Yes, you may *THINK* you know, but what decades of gaming have taught me is that people are full of shit, basically. For example, how many WoW players are convinced to this very day that a Rabbit's Foot gives luck? It's most pronounced in MMOs ("If you do X, your chances for Y increase/decrease" is a myth that's found in all of them)

SoShootMe
Filter Inserter
Filter Inserter
Posts: 472
Joined: Mon Aug 03, 2020 4:16 pm
Contact:

Re: Train routing. Completely explained

Post by SoShootMe »

Jürgen Erhard wrote:
Sat Apr 10, 2021 11:16 am
I wonder why we never got an *OFFICIAL* answer.
An official answer belongs in the wiki, which does have a fair bit of information.

I think a complete answer would be very complex, and mostly not needed. But there's the Wiki Talk forum if you think there's something important missing.

Loewchen
Global Moderator
Global Moderator
Posts: 8285
Joined: Wed Jan 07, 2015 5:53 pm
Contact:

Re: Train routing. Completely explained

Post by Loewchen »

The algorithm is public.

Post Reply

Return to “Gameplay Help”