[0.17.74] Train pathfinding creates a looped path, self-destructs the train

Bugs that are actually features.
mnvoronin
Burner Inserter
Burner Inserter
Posts: 5
Joined: Sun Oct 06, 2019 5:36 am
Contact:

[0.17.74] Train pathfinding creates a looped path, self-destructs the train

Post by mnvoronin »

Just got this issue today with the long train (50 wagons+locos total)
Train chooses to go full circle through the roundabout intersection instead of going other way, hits itself and crashes.
trainloop1.png
trainloop1.png (340.07 KiB) Viewed 2814 times
trainloop2.png
trainloop2.png (327.25 KiB) Viewed 2814 times
There's two alternative paths - one to bypass the circle (turn left at that circle, though it passes through the station this way), and the other is go right off the mining outpost.

The intersection is signalled correctly as far as I can see. Here's a zoom-in with blocks:
trainloop3.png
trainloop3.png (4.71 MiB) Viewed 2814 times
Save file: https://drive.google.com/file/d/1jLhKPW ... sp=sharing
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 4446
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: [0.17.74] Train pathfinding creates a looped path, self-destructs the train

Post by boskid »

Not a bug

https://wiki.factorio.com/Railway/Train_path_finding
Since there is station "MegaRail Outpost Resupply e" that adds penalty, train will not choose outer track down if there is path with less cost. In this case, there is: using inner track down. However your roundabout is made so it is not possible to enter from outer rail and exit from inner rail without doing full circle. Train pathfinding does not check for collisions based on train length and there is exception for signaling that allows train to go by red signal if block is reserved for same train (to handle cases where train enters same block twice, https://factorio.com/blog/post/fff-299) so everything you described is "as designed"

-- edit:
As for going right: there are stations on both tracks (like pair "Outpost Resupply" + "Arty Point NW 8", or pair "Outpost Resupply" + "Arty Point NW 7") that made right path even worse to choose than going left and down.
mnvoronin
Burner Inserter
Burner Inserter
Posts: 5
Joined: Sun Oct 06, 2019 5:36 am
Contact:

Re: [0.17.74] Train pathfinding creates a looped path, self-destructs the train

Post by mnvoronin »

Train pathfinding does not check for collisions based on train length
It should, really. Or at least add a penalty for crossing its own path, maybe proportional to train length to encourage it to take detour even if it goes through the station.
But I understand it might not be easy to code and the impact is pretty low with an easy workaround in my case - I just added bowties after round intersections so that train can change tracks easily.

Maybe add a note that pathfinding considers the train to be one wagon long to the page you referenced? That way it's a documented feature :)
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 4446
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: [0.17.74] Train pathfinding creates a looped path, self-destructs the train

Post by boskid »

Its not as simple as "add penalty for crossing its own path" since pathfinding is not about "here is candidate path, lets compute its penalty - oh there is crossing within length, add penalty - oh now its worse that current best, skip". Pathfinding is more like expanding from every open node (like train splits) to connected other node keeping count of current penalty to given node.

Pathfinding is working fast because of optimal substructure, that is if path A->B->C->D is shortest from A to D, then path B->C is shortest path from B to C (if it would not, you subsitute better subpath B->X->C into initial path A->B->X->C->D to get shorter path and this is against initial assumption of path being optimal).

Trying to implement collision check would mean that train could go from A->B without collisions, from B->C without collisions, but now if train is long, path A->B->C would be invalid (assuming partial paths crosses somewhere) or with higher cost than sum of subpaths and it would be possible that detour would be better than sum of 2 parts of optimal paths - now problem would not have optimal substructure and so huge performance penalty would apply here.
smcpeak
Burner Inserter
Burner Inserter
Posts: 17
Joined: Tue Dec 05, 2017 7:50 am
Contact:

Re: [0.17.74] Train pathfinding creates a looped path, self-destructs the train

Post by smcpeak »

Since my 133314 was marked as a duplicate of this issue (which I don't dispute), let me summarize here the essence of what I said there:
  • This is a bug. A train should never cross a red signal.
  • The existing replies here (77759) do not adequately justify the current unsafe behavior.
  • This bug makes building large bases a fool's errand because the game will randomly cause my trains to crash despite correct signaling.
computeraddict
Filter Inserter
Filter Inserter
Posts: 431
Joined: Sat Oct 07, 2023 6:44 am
Contact:

Re: [0.17.74] Train pathfinding creates a looped path, self-destructs the train

Post by computeraddict »

smcpeak wrote: Fri Apr 10, 2026 3:44 am [*] This bug makes building large bases a fool's errand because the game will randomly cause my trains to crash despite correct signaling.
It's not random. Simply don't build loops shorter than your longest train.

Even without the collision failure mode, loops like this would cause a one-train deadlock with the head of the train waiting at a signal that will never clear.
Loewchen
Global Moderator
Global Moderator
Posts: 10724
Joined: Wed Jan 07, 2015 5:53 pm
Contact:

Re: [0.17.74] Train pathfinding creates a looped path, self-destructs the train

Post by Loewchen »

This is not a bug, make a feature request.
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 4446
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: [0.17.74] Train pathfinding creates a looped path, self-destructs the train

Post by boskid »

There are no easy solutions to make trains pathfinder not find paths that would cross themselves given certain train length (this would cause massive performance penalty and logic complexity due to all corner cases like repathing during travel to not collide with train, not even with positions train currently is when pathfinding happens).

Trains passing red signals is not considered a bug: signal colors are mostly meaningless for trains, what is important is if a rail block after the signal is reserved or can be reserved by the train entering it, and in this situation where train crosses a red signal it is because rail block after the signal is reserved by the train itself so it enters its own territory.

Given the constraints, i think trains colliding into themselves is funnier solution than letting train just get stuck silently on a signal and be unable to move forward without manual reaction.

Still not a bug.
smcpeak
Burner Inserter
Burner Inserter
Posts: 17
Joined: Tue Dec 05, 2017 7:50 am
Contact:

Re: [0.17.74] Train pathfinding creates a looped path, self-destructs the train

Post by smcpeak »

Feature request: 133322.
Post Reply

Return to “Not a bug”