Skip to content

Commit e46a85a

Browse files
committed
core: fix node relative time diff calculation
Relative time diff calculation should return the lowest possible relative time diff in the interval made of the possible times at which the train can pass through the node. I.e. it should return 0 if it is possible to add delay for the train to pass at the planned arrival time.
1 parent 322d5f4 commit e46a85a

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

core/src/main/kotlin/fr/sncf/osrd/stdcm/graph/STDCMEdge.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ data class STDCMEdge(
116116
while (currentEdge != null) {
117117
val previousPlannedNode = currentEdge.previousNode
118118
if (previousPlannedNode.plannedTimingData != null) {
119-
return previousPlannedNode.getRelativeTimeDiff(totalDepartureTimeShift)
119+
return previousPlannedNode.getRelativeTimeDiff(
120+
totalDepartureTimeShift,
121+
maximumAddedDelayAfter
122+
)
120123
}
121124
currentEdge = previousPlannedNode.previousEdge
122125
}

core/src/main/kotlin/fr/sncf/osrd/stdcm/graph/STDCMNode.kt

+20-8
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ data class STDCMNode(
5656
override fun compareTo(other: STDCMNode): Int {
5757
val runTimeEstimation = timeSinceDeparture + remainingTimeEstimation
5858
val otherRunTimeEstimation = other.timeSinceDeparture + other.remainingTimeEstimation
59-
val plannedRelativeTimeDiff = getRelativeTimeDiff()
60-
val otherPlannedRelativeTimeDiff = other.getRelativeTimeDiff()
59+
val plannedRelativeTimeDiff = getRelativeTimeDiff(totalPrevAddedDelay, maximumAddedDelay)
60+
val otherPlannedRelativeTimeDiff =
61+
other.getRelativeTimeDiff(other.totalPrevAddedDelay, other.maximumAddedDelay)
6162
// Firstly, minimize the total run time: highest priority node takes the least time to
6263
// complete the path
6364
return if (!areTimesEqual(runTimeEstimation, otherRunTimeEstimation))
@@ -97,23 +98,34 @@ data class STDCMNode(
9798
/**
9899
* Returns the relative time difference between the real arrival time at node and the planned
99100
* arrival time at node, taking into account the tolerance on either side. It takes the lowest
100-
* value between the relative time diff at the current time and the relative time diff at the
101-
* maximum time at which the train can pass on the node.
101+
* value in the window made of [currentTime; currentTimeWithMaxDelayAdded].
102102
*/
103-
fun getRelativeTimeDiff(currentTotalAddedDelay: Double = totalPrevAddedDelay): Double? {
103+
fun getRelativeTimeDiff(currentTotalAddedDelay: Double, currentMaximumDelay: Double): Double? {
104+
// Ex: here, minimum time diff possible is 0.0 => minimum relative time diff will be 0.0.
105+
// before plannedArrival after
106+
// ------------[-----|-----------|----------------|----------]------------
107+
// currentTime currentTimeWithMaxDelay
104108
if (plannedTimingData != null) {
105109
val timeDiff =
106110
getRealTime(currentTotalAddedDelay) - plannedTimingData.arrivalTime.seconds
107111
val relativeTimeDiff = plannedTimingData.getBeforeOrAfterRelativeTimeDiff(timeDiff)
112+
// If time diff is positive, adding delay won't decrease relative time diff: return
113+
// relativeTimeDiff
114+
if (timeDiff >= 0) return relativeTimeDiff
115+
108116
val maxTimeDiff =
109117
min(
110-
getRealTime(maximumAddedDelay) - plannedTimingData.arrivalTime.seconds,
118+
getRealTime(currentMaximumDelay) - plannedTimingData.arrivalTime.seconds,
111119
plannedTimingData.arrivalTimeToleranceAfter.seconds
112120
)
113121
val relativeMaxTimeDiff =
114122
plannedTimingData.getBeforeOrAfterRelativeTimeDiff(maxTimeDiff)
115-
val minRelativeTimeDiff = min(relativeTimeDiff, relativeMaxTimeDiff)
116-
return minRelativeTimeDiff
123+
// If time diff < 0.0 and maxTimeDiff >= 0.0, then we can add delay to make the node
124+
// arrive at planned arrival time: return 0.0
125+
if (maxTimeDiff >= 0.0) return 0.0
126+
127+
// Else, both are < 0.0: return the lowest relative time diff, i.e., relativeMaxTimeDiff
128+
return relativeMaxTimeDiff
117129
}
118130
return null
119131
}

0 commit comments

Comments
 (0)