Skip to content

Commit

Permalink
core: stdcm: limit the loop where we look for possible delays
Browse files Browse the repository at this point in the history
We would keep looking for different "openings" even very far in the
future. This limits both conflict detection calls and the early steps of
edge building.

Signed-off-by: Eloi Charpentier <[email protected]>
  • Loading branch information
eckter committed Dec 19, 2024
1 parent 5d127e0 commit 8a3622d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
20 changes: 19 additions & 1 deletion core/src/main/kotlin/fr/sncf/osrd/stdcm/graph/DelayManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import fr.sncf.osrd.utils.units.Distance.Companion.fromMeters
import fr.sncf.osrd.utils.units.Offset
import fr.sncf.osrd.utils.units.meters
import java.util.*
import kotlin.math.max
import kotlin.math.min

/**
* This class contains all the methods used to handle delays (how much we can add, how much we need
Expand All @@ -29,14 +31,17 @@ internal constructor(
*/
fun minimumDelaysPerOpening(
infraExplorerWithNewEnvelope: InfraExplorerWithEnvelope,
startTime: Double,
timeData: TimeData,
envelope: Envelope,
startOffset: Offset<Block>,
): NavigableSet<Double> {
val startTime = timeData.earliestReachableTime
val maximumDelay = computeMaximumDelay(timeData)
val res = TreeSet<Double>()
val endOffset = startOffset + fromMeters(envelope.endPos)
var time = startTime
while (java.lang.Double.isFinite(time)) {
if (time - startTime > maximumDelay) break
val availability =
getLastBlockAvailability(
infraExplorerWithNewEnvelope,
Expand All @@ -58,6 +63,19 @@ internal constructor(
return res
}

/**
* Compute how much delay we may add here. Prevents the generation of edges very far in the
* future that would necessarily be discarded.
*/
private fun computeMaximumDelay(data: TimeData): Double {
val maxExtraRunTime = maxRunTime - data.totalRunningTime
val maxDelayForMaxRunTime = data.maxDepartureDelayingWithoutConflict + maxExtraRunTime
val maxDelayWithLocalConflict =
data.maxDepartureDelayingWithoutConflict + data.timeOfNextConflictAtLocation -
data.earliestReachableTime
return max(0.0, min(maxDelayForMaxRunTime, maxDelayWithLocalConflict))
}

/** Returns the start time of the next occupancy for the block */
fun findNextOccupancy(
infraExplorer: InfraExplorerWithEnvelope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ internal constructor(
private fun getDelaysPerOpening(): Set<Double> {
return graph.delayManager.minimumDelaysPerOpening(
getExplorerWithNewEnvelope()!!,
prevNode.timeData.earliestReachableTime,
prevNode.timeData,
envelope!!,
startOffset,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import io.opentelemetry.instrumentation.annotations.WithSpan
import java.time.Duration
import java.time.Instant
import java.util.*
import kotlin.Double.Companion.POSITIVE_INFINITY
import org.slf4j.Logger
import org.slf4j.LoggerFactory

Expand Down Expand Up @@ -272,7 +273,7 @@ class STDCMPathfinding(
earliestReachableTime = startTime,
maxDepartureDelayingWithoutConflict = maxDepartureDelay,
departureTime = startTime,
timeOfNextConflictAtLocation = 0.0,
timeOfNextConflictAtLocation = POSITIVE_INFINITY,
totalRunningTime = 0.0,
stopTimeData = listOf(),
maxFirstDepartureDelaying = maxDepartureDelay,
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/kotlin/fr/sncf/osrd/stdcm/graph/TimeData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ data class TimeData(
var maxDepartureDelayingWithoutConflict = maxDepartureDelayingWithoutConflict
val nextEarliestReachableTime =
earliestReachableTime + extraTravelTime + (extraStopTime ?: 0.0)
var timeOfNextConflict = timeOfNextConflictAtLocation
if (maxAdditionalStopTime != null)
timeOfNextConflict = nextEarliestReachableTime + maxAdditionalStopTime
if (extraStopTime != null) {
val stopDataCopy = newStopData.toMutableList()
stopDataCopy.add(
Expand All @@ -108,6 +111,7 @@ data class TimeData(
maxDepartureDelayingWithoutConflict = maxDepartureDelayingWithoutConflict,
maxFirstDepartureDelaying =
min(maxFirstDepartureDelaying, (maxAdditionalStopTime ?: POSITIVE_INFINITY)),
timeOfNextConflictAtLocation = timeOfNextConflict,
)
}

Expand Down

0 comments on commit 8a3622d

Please sign in to comment.