diff --git a/core/src/main/kotlin/fr/sncf/osrd/stdcm/graph/DelayManager.kt b/core/src/main/kotlin/fr/sncf/osrd/stdcm/graph/DelayManager.kt index e72490f4d61..ae757c9a9ec 100644 --- a/core/src/main/kotlin/fr/sncf/osrd/stdcm/graph/DelayManager.kt +++ b/core/src/main/kotlin/fr/sncf/osrd/stdcm/graph/DelayManager.kt @@ -9,6 +9,7 @@ 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.min /** * This class contains all the methods used to handle delays (how much we can add, how much we need @@ -29,10 +30,12 @@ internal constructor( */ fun minimumDelaysPerOpening( infraExplorerWithNewEnvelope: InfraExplorerWithEnvelope, - startTime: Double, + timeData: TimeData, envelope: Envelope, startOffset: Offset, ): NavigableSet { + val startTime = timeData.earliestReachableTime + val maximumDelay = computeMaximumDelay(timeData) val res = TreeSet() val endOffset = startOffset + fromMeters(envelope.endPos) var time = startTime @@ -44,6 +47,7 @@ internal constructor( endOffset, time, ) + if (time - startTime > maximumDelay) break time += when (availability) { is BlockAvailabilityInterface.Available -> { @@ -58,6 +62,17 @@ 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.timeOfNextConflictAtLocation - data.earliestReachableTime + return min(maxDelayForMaxRunTime, maxDelayWithLocalConflict) + } + /** Returns the start time of the next occupancy for the block */ fun findNextOccupancy( infraExplorer: InfraExplorerWithEnvelope, diff --git a/core/src/main/kotlin/fr/sncf/osrd/stdcm/graph/STDCMEdgeBuilder.kt b/core/src/main/kotlin/fr/sncf/osrd/stdcm/graph/STDCMEdgeBuilder.kt index 4119df66c0d..9075342b437 100644 --- a/core/src/main/kotlin/fr/sncf/osrd/stdcm/graph/STDCMEdgeBuilder.kt +++ b/core/src/main/kotlin/fr/sncf/osrd/stdcm/graph/STDCMEdgeBuilder.kt @@ -132,7 +132,7 @@ internal constructor( private fun getDelaysPerOpening(): Set { return graph.delayManager.minimumDelaysPerOpening( getExplorerWithNewEnvelope()!!, - prevNode.timeData.earliestReachableTime, + prevNode.timeData, envelope!!, startOffset, )