[kovarex] [0.17.66] Rail segment with stations on both ends adds penalty as for one

This subforum contains all the issues which we already resolved.
Post Reply
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2250
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

[kovarex] [0.17.66] Rail segment with stations on both ends adds penalty as for one

Post by boskid »

Going by this:
https://wiki.factorio.com/Railway/Train_path_finding wrote:When the path includes a train stop that is not the destination -> Add a penalty of 2000.
i found some weird cases where this statement does not hold exactly as i would expect.

Here is my testing setup:
testing setup
In this setup i can put up to 5 train stations on upper triange segment that will be counted once when path goes from "R" station (it is right one) to "L" station (left one), and up to 3 train stations on each side on this vertical segment between triangle and loop. Since all distances are less than 2000 i will be speaking of path costs in terms of "stations". My observations go towards stations put on this vertical path. If i say path costs N stations through vertical segment, that means that when in upper triangle segment there are N stations, path will go by them, but when there are N+1 stations in upper triangle segment, path will go through vertical part and by loop.

Case 1 - single station (cost 2, as expected)
pfsc-case-1.png
pfsc-case-1.png (65.99 KiB) Viewed 2104 times
This is simple, reference case. Path goes by this station twice. Measured cost is "2 stations" [that is, 3 stations in upper triangle segment are required to force path to go by vertical section]

Case 2 - two stations on same side (cost 4 as expected)
pfsc-case-2.png
pfsc-case-2.png (53 KiB) Viewed 2104 times
Here as expected, cost is 4 stations because path goes twice by each station

Case 3 - two stations not aligned on opposite sides (cost 2, expected 4)
pfsc-case-3.png
pfsc-case-3.png (74.68 KiB) Viewed 2104 times
Here things are getting interesting. Cost here is 2 meaning that either: each station is counted as 1, or one of them is not counted while other counts as 2. Expected cost would be 4

Case 4 - two stations aligned on opposite sides (cost 2, expected 4)
pfsc-case-4.png
pfsc-case-4.png (72.4 KiB) Viewed 2104 times
Expected cost is 4

Case 5 - two stations on opposite sides + extra one below (cost 4, expected 6)
pfsc-case-5.png
pfsc-case-5.png (64.65 KiB) Viewed 2104 times
Expected cost is 6. Because it is combination of cases 2 and 4 i guess that lower station costs 2, upper right station costs 2 and upper left station costs 0

Case 6 - two stations not aligned on opposite sides, inner segment split by rail signals (cost 4 as expected)
pfsc-case-6.png
pfsc-case-6.png (63.69 KiB) Viewed 2104 times
This is related to case 4. However because there are signals in between of stations, now each station costs 2 as expected giving total of 4

Based on all of above, i guess there is something wrong with how stations passed twice count towards path cost.

-- edit:
Case 7a (cost 2, expected 4) [well, same as case 3]
pfsc-case-7-cost-2.png
pfsc-case-7-cost-2.png (65.18 KiB) Viewed 2101 times
Case 7b (cost 4 as expected)
pfsc-case-7b-cost-4.png
pfsc-case-7b-cost-4.png (80.67 KiB) Viewed 2101 times
-- edit:
By doing more tests i guess it is not that "train station adds 2000 penalty", but in fact, rail segment that contains at least one station adds 2000 penalty. Rail segment can have at most 2 train stations, one on each end because train stations will split rails into segments. Going by this explanation, lets analyze all above cases:
- case 1: station connects to rail segment in between of station and lower merge point. This segment is twice in path and cost is 2
- case 2: first station is connected to segment in between of stations, second station is connected to segment in between of lower station and loop merge. 2 segments containing stations, each passed twice: cost 4 as expected
- case 3: both station will connect to same segment which is in between stations. This segment is passed two times co real cost is 2
- case 4: this is minimal setup for case 3. Stations on opposite sides will force single rail entity to be single segment with two stations (one on each end), This is why real cost is 2 (only one segment with stations, passed twice)
- case 5: upper pair of stations create short (1 straight rail) segment with two stations (this is +2 for cost) and lower station connects to segment going from lower train station up to merge of loop
- case 6: because signals split rail segments into two, there are two rail segments, each with only one station. 2 rail segments with station counting twice gives cost of 4
- case 7a and 7b: because of relative station positions, 7a gives 1 segment with both stations on its ends while 7b gives 2 segments with 1 station each

Because of this, solution is quite easy: if rail segment contains two stations (one on each end), count it as 4000 penalty (that is 2000*numberOfStationsAssociatedWithThisRailSegment)

-- edit:
Based on https://gist.github.com/Rseding91/c0d4d ... 3f6c6a8fe6, fix would be to change this:

Code: Select all

if (currentSegment->frontStop || currentSegment->backStop)
    costFromStart += TrainPathFinder::constants.trainStopPenalty;
into this:

Code: Select all

if (currentSegment->frontStop)
    costFromStart += TrainPathFinder::constants.trainStopPenalty;
if (currentSegment->backStop)
    costFromStart += TrainPathFinder::constants.trainStopPenalty;
-- edit:
Changed subject from "[0.17.66] Inconsistent train station cost when path goes twice" into "[0.17.66] Rail segment with stations on both ends adds penalty as for one"

kovarex
Factorio Staff
Factorio Staff
Posts: 8078
Joined: Wed Feb 06, 2013 12:00 am
Contact:

Re: [kovarex] [0.17.66] Rail segment with stations on both ends adds penalty as for one

Post by kovarex »

This is not a bug but wanted behaviour, the penalty is only in the direction of the station.

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2250
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: [kovarex] [0.17.66] Rail segment with stations on both ends adds penalty as for one

Post by boskid »

kovarex wrote:
Tue Sep 10, 2019 11:07 am
This is not a bug but wanted behaviour, the penalty is only in the direction of the station.
This is not true. Station in opposite direction will also count towards penalty.

Going with what you said, using save from below, train should choose upper path (0 stations on same direction) but chooses bottom one instead:
75107-station-penalty.zip
(266.88 KiB) Downloaded 79 times
75107-station-penalty.png
75107-station-penalty.png (436.91 KiB) Viewed 1927 times
When counting this as "rail segments with at least one station" this fits what i described: upper path has cost of 3 stations (3 segments with 1 station each), but lower path has cost of 2 stations (2 segments, each with 2 stations but not counting more than one)

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

Re: [kovarex] [0.17.66] Rail segment with stations on both ends adds penalty as for one

Post by mrvn »

boskid wrote:
Tue Sep 10, 2019 11:11 am
kovarex wrote:
Tue Sep 10, 2019 11:07 am
This is not a bug but wanted behaviour, the penalty is only in the direction of the station.
This is not true. Station in opposite direction will also count towards penalty.

Going with what you said, using save from below, train should choose upper path (0 stations on same direction) but chooses bottom one instead:
75107-station-penalty.zip
75107-station-penalty.png

When counting this as "rail segments with at least one station" this fits what i described: upper path has cost of 3 stations (3 segments with 1 station each), but lower path has cost of 2 stations (2 segments, each with 2 stations but not counting more than one)
Huh? On each path all the stations are in a single segment. There are no signals between the station. Going by "rail segments with at least one station" the upper path it shorter and has a station. Should have been taken.

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2250
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: [kovarex] [0.17.66] Rail segment with stations on both ends adds penalty as for one

Post by boskid »

mrvn wrote:
Tue Sep 10, 2019 12:38 pm
Huh? On each path all the stations are in a single segment. There are no signals between the station. Going by "rail segments with at least one station" the upper path it shorter and has a station. Should have been taken.
There is subtle difference between "block" and "segment". Segment is pure sector of rails only and has two ends. Block consists of multiple segments connected with merge points and many stations as long as there is path without rail signals that would split them in half.
In upper leg there is one block (in between signals) but 4 segments (signal-station[1TS], station-station[1TS], station-station[1TS], station-signal)
In lower leg there is one block (in between signals) but 5 segments (signal-station, station-station[2TS], station-station, station-station[2TS], station-signal)
Upper leg has 3 segments, each with 1 TrainStation on some of its end. Lower leg has 2 segments containing stations (1 on each end since they are on opposite sides)
segments-and-blocks.gif
segments-and-blocks.gif (276.25 KiB) Viewed 1902 times

Post Reply

Return to “Resolved Problems and Bugs”