i found some weird cases where this statement does not hold exactly as i would expect.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.

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) 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) 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) 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) Expected cost is 4

Case 5 - two stations on opposite sides + extra one below (cost 4, expected 6) 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) 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] Case 7b (cost 4 as expected) -- 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;
```

Code: Select all

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

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"