Page 1 of 1

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

Posted: Sat Nov 09, 2019 4:26 am
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 1792 times
trainloop2.png
trainloop2.png (327.25 KiB) Viewed 1792 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 1792 times
Save file: https://drive.google.com/file/d/1jLhKPW ... sp=sharing

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

Posted: Sat Nov 09, 2019 6:29 am
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.

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

Posted: Sat Nov 09, 2019 9:53 am
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 :)

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

Posted: Sat Nov 09, 2019 10:14 am
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.