@@ -56,8 +56,9 @@ data class STDCMNode(
56
56
override fun compareTo (other : STDCMNode ): Int {
57
57
val runTimeEstimation = timeSinceDeparture + remainingTimeEstimation
58
58
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)
61
62
// Firstly, minimize the total run time: highest priority node takes the least time to
62
63
// complete the path
63
64
return if (! areTimesEqual(runTimeEstimation, otherRunTimeEstimation))
@@ -97,23 +98,34 @@ data class STDCMNode(
97
98
/* *
98
99
* Returns the relative time difference between the real arrival time at node and the planned
99
100
* 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].
102
102
*/
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
104
108
if (plannedTimingData != null ) {
105
109
val timeDiff =
106
110
getRealTime(currentTotalAddedDelay) - plannedTimingData.arrivalTime.seconds
107
111
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
+
108
116
val maxTimeDiff =
109
117
min(
110
- getRealTime(maximumAddedDelay ) - plannedTimingData.arrivalTime.seconds,
118
+ getRealTime(currentMaximumDelay ) - plannedTimingData.arrivalTime.seconds,
111
119
plannedTimingData.arrivalTimeToleranceAfter.seconds
112
120
)
113
121
val relativeMaxTimeDiff =
114
122
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
117
129
}
118
130
return null
119
131
}
0 commit comments