Skip to content

Commit d73625f

Browse files
authored
fix: various fixes for vdf (#274)
1 parent 74befe8 commit d73625f

5 files changed

Lines changed: 37 additions & 19 deletions

File tree

core/src/main/java/org/eqasim/core/simulation/vdf/VDFModule.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.eqasim.core.simulation.vdf.travel_time.function.VolumeDelayFunction;
1818
import org.matsim.api.core.v01.network.Network;
1919
import org.matsim.core.config.ConfigGroup;
20+
import org.matsim.core.config.groups.ControllerConfigGroup;
2021
import org.matsim.core.config.groups.QSimConfigGroup;
2122
import org.matsim.core.controler.OutputDirectoryHierarchy;
2223

@@ -52,12 +53,12 @@ protected void installEqasimExtension() {
5253
@Provides
5354
@Singleton
5455
public VDFUpdateListener provideVDFUpdateListener(VDFScope scope, VDFTrafficHandler handler,
55-
VDFTravelTime travelTime, VDFConfigGroup config, OutputDirectoryHierarchy outputHierarchy,
56-
Network network) {
56+
VDFTravelTime travelTime, VDFConfigGroup config, OutputDirectoryHierarchy outputHierarchy, Network network,
57+
ControllerConfigGroup controllerConfig) {
5758
URL inputFile = config.getInputFile() == null ? null
5859
: ConfigGroup.getInputFileURL(getConfig().getContext(), config.getInputFile());
5960
return new VDFUpdateListener(network, scope, handler, travelTime, outputHierarchy, config.getWriteInterval(),
60-
config.getWriteFlowInterval(), inputFile);
61+
config.getWriteFlowInterval(), controllerConfig.getFirstIteration(), inputFile);
6162
}
6263

6364
@Provides

core/src/main/java/org/eqasim/core/simulation/vdf/VDFUpdateListener.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ public class VDFUpdateListener implements IterationEndsListener, StartupListener
3737
private final int writeFlowInterval;
3838
private final URL inputFile;
3939

40+
private final int firstIteration;
41+
4042
private final OutputDirectoryHierarchy outputHierarchy;
4143
private final Network network;
4244

4345
public VDFUpdateListener(Network network, VDFScope scope, VDFTrafficHandler handler, VDFTravelTime travelTime,
44-
OutputDirectoryHierarchy outputHierarchy, int writeInterval, int writeFlowInterval, URL inputFile) {
46+
OutputDirectoryHierarchy outputHierarchy, int writeInterval, int writeFlowInterval, int firstIteration,
47+
URL inputFile) {
4548
this.network = network;
4649
this.scope = scope;
4750
this.handler = handler;
@@ -50,11 +53,17 @@ public VDFUpdateListener(Network network, VDFScope scope, VDFTrafficHandler hand
5053
this.writeFlowInterval = writeFlowInterval;
5154
this.outputHierarchy = outputHierarchy;
5255
this.inputFile = inputFile;
56+
this.firstIteration = firstIteration;
5357
}
5458

5559
@Override
5660
public void notifyIterationEnds(IterationEndsEvent event) {
57-
IdMap<Link, List<Double>> data = handler.aggregate();
61+
// ignore when restarting simulation as "first iteration" will produce
62+
// information that is already in the cache, hence the call to aggregate is just
63+
// to obtain historical flows
64+
boolean ignoreIteration = event.getIteration() == firstIteration && inputFile != null;
65+
66+
IdMap<Link, List<Double>> data = handler.aggregate(ignoreIteration);
5867
scope.verify(data, "Wrong flow format");
5968
travelTime.update(data);
6069

@@ -83,7 +92,9 @@ public void notifyStartup(StartupEvent event) {
8392

8493
handler.getReader().readFile(inputFile);
8594

86-
IdMap<Link, List<Double>> data = handler.aggregate();
95+
// ignore "current iteration" because it does not exist at startup, we just
96+
// aggregate to have consistent travel times
97+
IdMap<Link, List<Double>> data = handler.aggregate(true);
8798
scope.verify(data, "Wrong flow format");
8899
travelTime.update(data, true);
89100

core/src/main/java/org/eqasim/core/simulation/vdf/handlers/VDFHorizonHandler.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,24 @@ public void processEnterLink(double time, Id<Link> linkId) {
6363
}
6464

6565
@Override
66-
public IdMap<Link, List<Double>> aggregate() {
66+
public IdMap<Link, List<Double>> aggregate(boolean ignoreIteration) {
6767
while (state.size() > horizon) {
6868
state.remove(0);
6969
}
7070

7171
logger.info(String.format("Starting aggregation of %d slices", state.size()));
7272

7373
// Make a copy to add to the history
74+
if (!ignoreIteration) {
75+
IdMap<Link, List<Double>> copy = new IdMap<>(Link.class);
7476

75-
IdMap<Link, List<Double>> copy = new IdMap<>(Link.class);
77+
for (Map.Entry<Id<Link>, List<Double>> entry : counts.entrySet()) {
78+
copy.put(entry.getKey(), new ArrayList<>(entry.getValue()));
79+
}
7680

77-
for (Map.Entry<Id<Link>, List<Double>> entry : counts.entrySet()) {
78-
copy.put(entry.getKey(), new ArrayList<>(entry.getValue()));
81+
state.add(copy);
7982
}
8083

81-
state.add(copy);
82-
8384
IdMap<Link, List<Double>> aggregated = new IdMap<>(Link.class);
8485

8586
for (Id<Link> linkId : network.getLinks().keySet()) {

core/src/main/java/org/eqasim/core/simulation/vdf/handlers/VDFInterpolationHandler.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,19 @@ public void processEnterLink(double time, Id<Link> linkId) {
5353
}
5454

5555
@Override
56-
public IdMap<Link, List<Double>> aggregate() {
57-
interpolatedCounts.forEach((id, interpolated) -> {
58-
List<Double> current = currentCounts.get(id);
56+
public IdMap<Link, List<Double>> aggregate(boolean ignoreIteration) {
57+
for (var item : interpolatedCounts.entrySet()) {
58+
List<Double> current = currentCounts.get(item.getKey());
59+
List<Double> interpolated = item.getValue();
5960

6061
for (int i = 0; i < interpolated.size(); i++) {
61-
interpolated.set(i, (1.0 - updateFactor) * interpolated.get(i) + updateFactor * current.get(i));
62+
if (!ignoreIteration) {
63+
interpolated.set(i, (1.0 - updateFactor) * interpolated.get(i) + updateFactor * current.get(i));
64+
}
65+
66+
current.set(i, 0.0);
6267
}
63-
});
68+
}
6469

6570
return interpolatedCounts;
6671
}
@@ -113,7 +118,7 @@ public void writeFile(File outputFile) {
113118
outputStream.writeDouble(scope.getStartTime());
114119
outputStream.writeDouble(scope.getEndTime());
115120
outputStream.writeDouble(scope.getIntervalTime());
116-
outputStream.writeDouble(scope.getIntervals());
121+
outputStream.writeInt(scope.getIntervals());
117122

118123
for (var entry : interpolatedCounts.entrySet()) {
119124
outputStream.writeUTF(entry.getKey().toString());

core/src/main/java/org/eqasim/core/simulation/vdf/handlers/VDFTrafficHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public interface VDFTrafficHandler {
1212
void processEnterLink(double time, Id<Link> linkId);
1313

14-
IdMap<Link, List<Double>> aggregate();
14+
IdMap<Link, List<Double>> aggregate(boolean ignoreIteration);
1515

1616
VDFReaderInterface getReader();
1717

0 commit comments

Comments
 (0)