Skip to content

Commit 444b5e9

Browse files
committed
core: stdcm: memoize simulateBlock
Add computed block envelopes to STDCMGraph. Only compute block envelopes which have not been already computed.
1 parent 8775fea commit 444b5e9

File tree

5 files changed

+54
-13
lines changed

5 files changed

+54
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package fr.sncf.osrd.stdcm.graph;
2+
3+
public record BlockSimulationParameters(Integer blockId, double initialSpeed, long start, Long stop) {
4+
}

core/src/main/java/fr/sncf/osrd/stdcm/graph/STDCMEdgeBuilder.java

+4-8
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import fr.sncf.osrd.envelope.Envelope;
44
import fr.sncf.osrd.sim_infra.impl.BlockInfraImplKt;
5-
import java.util.ArrayList;
65
import java.util.Collection;
7-
import java.util.Comparator;
86
import java.util.List;
97
import java.util.Objects;
108
import java.util.Set;
@@ -163,17 +161,15 @@ STDCMEdge findEdgeSameNextOccupancy(double timeNextOccupancy) {
163161
/** Returns the envelope to be used for the new edges */
164162
private Envelope getEnvelope() {
165163
if (envelope == null)
166-
envelope = STDCMSimulations.simulateBlock(
164+
envelope = graph.stdcmSimulations.simulateBlock(
167165
graph.rawInfra,
168166
graph.blockInfra,
169-
blockId,
170-
startSpeed,
171-
startOffset,
172167
graph.rollingStock,
173168
graph.comfort,
174169
graph.timeStep,
175-
STDCMUtils.getStopOnBlock(graph, blockId, startOffset, waypointIndex),
176-
graph.tag
170+
graph.tag,
171+
new BlockSimulationParameters(blockId, startSpeed, startOffset,
172+
STDCMUtils.getStopOnBlock(graph, blockId, startOffset, waypointIndex))
177173
);
178174
return envelope;
179175
}

core/src/main/java/fr/sncf/osrd/stdcm/graph/STDCMGraph.java

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class STDCMGraph implements Graph<STDCMNode, STDCMEdge> {
2929
public final RollingStock rollingStock;
3030
public final RollingStock.Comfort comfort;
3131
public final double timeStep;
32+
STDCMSimulations stdcmSimulations;
3233
final List<STDCMStep> steps;
3334
final DelayManager delayManager;
3435
final AllowanceManager allowanceManager;
@@ -55,6 +56,7 @@ public STDCMGraph(
5556
this.rollingStock = rollingStock;
5657
this.comfort = comfort;
5758
this.timeStep = timeStep;
59+
this.stdcmSimulations = new STDCMSimulations();
5860
this.steps = steps;
5961
this.delayManager = new DelayManager(minScheduleTimeStart, maxRunTime, blockAvailability, this);
6062
this.allowanceManager = new AllowanceManager(this);

core/src/main/java/fr/sncf/osrd/stdcm/graph/STDCMSimulations.java

+39
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,19 @@
2828
import fr.sncf.osrd.stdcm.BacktrackingEnvelopeAttr;
2929
import fr.sncf.osrd.train.RollingStock;
3030
import fr.sncf.osrd.utils.units.Distance;
31+
import java.util.HashMap;
3132
import java.util.List;
3233
import java.util.Map;
3334

3435
/** This class contains all the methods used to simulate the train behavior. */
3536
public class STDCMSimulations {
37+
38+
HashMap<BlockSimulationParameters, Envelope> simulatedEnvelopes;
39+
40+
public STDCMSimulations() {
41+
this.simulatedEnvelopes = new HashMap<>();
42+
}
43+
3644
/** Create an EnvelopeSimContext instance from the blocks and extra parameters.
3745
* offsetFirstBlock is in millimeters. */
3846
static EnvelopeSimContext makeSimContext(
@@ -49,6 +57,37 @@ static EnvelopeSimContext makeSimContext(
4957
return EnvelopeSimContextBuilder.build(rollingStock, envelopePath, timeStep, comfort);
5058
}
5159

60+
/** Returns the corresponding envelope if the block's envelope has already been computed in simulatedEnvelopes,
61+
* otherwise computes the matching envelope and adds it to the STDCMGraph. **/
62+
public Envelope simulateBlock(
63+
RawSignalingInfra rawInfra,
64+
BlockInfra blockInfra,
65+
RollingStock rollingStock,
66+
RollingStock.Comfort comfort,
67+
double timeStep,
68+
String trainTag,
69+
BlockSimulationParameters blockParams
70+
) {
71+
if (simulatedEnvelopes.containsKey(blockParams)) {
72+
return simulatedEnvelopes.get(blockParams);
73+
} else {
74+
var simulatedEnvelope = simulateBlock(
75+
rawInfra,
76+
blockInfra,
77+
blockParams.blockId(),
78+
blockParams.initialSpeed(),
79+
blockParams.start(),
80+
rollingStock,
81+
comfort,
82+
timeStep,
83+
blockParams.stop(),
84+
trainTag
85+
);
86+
simulatedEnvelopes.put(blockParams, simulatedEnvelope);
87+
return simulatedEnvelope;
88+
}
89+
}
90+
5291
/**
5392
* Returns an envelope matching the given block. The envelope time starts when the train enters the block.
5493
* stopPosition specifies the position at which the train should stop, may be null (no stop)

core/src/main/java/fr/sncf/osrd/stdcm/preprocessing/implementation/BlockAvailabilityLegacyAdapter.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class BlockAvailabilityLegacyAdapter implements BlockAvailabilityInterfac
2020

2121
/** Simple record used to group together a block and the offset of its start on the given path */
2222
private record BlockWithOffset(
23-
Integer blockID,
23+
Integer blockId,
2424
Long pathOffset
2525
){}
2626

@@ -73,7 +73,7 @@ Unavailable findMinimumDelay(
7373
double minimumDelay = 0;
7474
long conflictOffset = 0;
7575
for (var blockWithOffset : getBlocksInRange(blocks, startOffset, endOffset)) {
76-
for (var unavailableSegment : unavailableSpace.get(blockWithOffset.blockID)) {
76+
for (var unavailableSegment : unavailableSpace.get(blockWithOffset.blockId)) {
7777
var trainInBlock = getTimeTrainInBlock(
7878
unavailableSegment,
7979
blockWithOffset,
@@ -109,7 +109,7 @@ Unavailable findMinimumDelay(
109109
minimumDelay += recursiveDelay.duration;
110110
}
111111
var pathLength = blocks.stream()
112-
.mapToLong(block -> blockInfra.getBlockLength(block.blockID))
112+
.mapToLong(block -> blockInfra.getBlockLength(block.blockId))
113113
.sum();
114114
conflictOffset = Math.max(0, Math.min(pathLength, conflictOffset));
115115
return new Unavailable(minimumDelay, conflictOffset);
@@ -127,7 +127,7 @@ Available findMaximumDelay(
127127
double maximumDelay = Double.POSITIVE_INFINITY;
128128
double timeOfNextOccupancy = Double.POSITIVE_INFINITY;
129129
for (var blockWithOffset : getBlocksInRange(blocks, startOffset, endOffset)) {
130-
for (var block : unavailableSpace.get(blockWithOffset.blockID)) {
130+
for (var block : unavailableSpace.get(blockWithOffset.blockId)) {
131131
var timeTrainInBlock = getTimeTrainInBlock(
132132
block,
133133
blockWithOffset,
@@ -157,7 +157,7 @@ private List<BlockWithOffset> getBlocksInRange(
157157
return blocks.stream()
158158
.filter(r -> r.pathOffset() < end)
159159
.filter(r ->
160-
r.pathOffset() + blockInfra.getBlockLength(r.blockID) > start)
160+
r.pathOffset() + blockInfra.getBlockLength(r.blockId) > start)
161161
.toList();
162162
}
163163

0 commit comments

Comments
 (0)