Skip to content

Commit

Permalink
fix: various fixes for vdf (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhoerl authored Nov 2, 2024
1 parent 74befe8 commit d73625f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.eqasim.core.simulation.vdf.travel_time.function.VolumeDelayFunction;
import org.matsim.api.core.v01.network.Network;
import org.matsim.core.config.ConfigGroup;
import org.matsim.core.config.groups.ControllerConfigGroup;
import org.matsim.core.config.groups.QSimConfigGroup;
import org.matsim.core.controler.OutputDirectoryHierarchy;

Expand Down Expand Up @@ -52,12 +53,12 @@ protected void installEqasimExtension() {
@Provides
@Singleton
public VDFUpdateListener provideVDFUpdateListener(VDFScope scope, VDFTrafficHandler handler,
VDFTravelTime travelTime, VDFConfigGroup config, OutputDirectoryHierarchy outputHierarchy,
Network network) {
VDFTravelTime travelTime, VDFConfigGroup config, OutputDirectoryHierarchy outputHierarchy, Network network,
ControllerConfigGroup controllerConfig) {
URL inputFile = config.getInputFile() == null ? null
: ConfigGroup.getInputFileURL(getConfig().getContext(), config.getInputFile());
return new VDFUpdateListener(network, scope, handler, travelTime, outputHierarchy, config.getWriteInterval(),
config.getWriteFlowInterval(), inputFile);
config.getWriteFlowInterval(), controllerConfig.getFirstIteration(), inputFile);
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ public class VDFUpdateListener implements IterationEndsListener, StartupListener
private final int writeFlowInterval;
private final URL inputFile;

private final int firstIteration;

private final OutputDirectoryHierarchy outputHierarchy;
private final Network network;

public VDFUpdateListener(Network network, VDFScope scope, VDFTrafficHandler handler, VDFTravelTime travelTime,
OutputDirectoryHierarchy outputHierarchy, int writeInterval, int writeFlowInterval, URL inputFile) {
OutputDirectoryHierarchy outputHierarchy, int writeInterval, int writeFlowInterval, int firstIteration,
URL inputFile) {
this.network = network;
this.scope = scope;
this.handler = handler;
Expand All @@ -50,11 +53,17 @@ public VDFUpdateListener(Network network, VDFScope scope, VDFTrafficHandler hand
this.writeFlowInterval = writeFlowInterval;
this.outputHierarchy = outputHierarchy;
this.inputFile = inputFile;
this.firstIteration = firstIteration;
}

@Override
public void notifyIterationEnds(IterationEndsEvent event) {
IdMap<Link, List<Double>> data = handler.aggregate();
// ignore when restarting simulation as "first iteration" will produce
// information that is already in the cache, hence the call to aggregate is just
// to obtain historical flows
boolean ignoreIteration = event.getIteration() == firstIteration && inputFile != null;

IdMap<Link, List<Double>> data = handler.aggregate(ignoreIteration);
scope.verify(data, "Wrong flow format");
travelTime.update(data);

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

handler.getReader().readFile(inputFile);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,24 @@ public void processEnterLink(double time, Id<Link> linkId) {
}

@Override
public IdMap<Link, List<Double>> aggregate() {
public IdMap<Link, List<Double>> aggregate(boolean ignoreIteration) {
while (state.size() > horizon) {
state.remove(0);
}

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

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

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

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

state.add(copy);

IdMap<Link, List<Double>> aggregated = new IdMap<>(Link.class);

for (Id<Link> linkId : network.getLinks().keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,19 @@ public void processEnterLink(double time, Id<Link> linkId) {
}

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

for (int i = 0; i < interpolated.size(); i++) {
interpolated.set(i, (1.0 - updateFactor) * interpolated.get(i) + updateFactor * current.get(i));
if (!ignoreIteration) {
interpolated.set(i, (1.0 - updateFactor) * interpolated.get(i) + updateFactor * current.get(i));
}

current.set(i, 0.0);
}
});
}

return interpolatedCounts;
}
Expand Down Expand Up @@ -113,7 +118,7 @@ public void writeFile(File outputFile) {
outputStream.writeDouble(scope.getStartTime());
outputStream.writeDouble(scope.getEndTime());
outputStream.writeDouble(scope.getIntervalTime());
outputStream.writeDouble(scope.getIntervals());
outputStream.writeInt(scope.getIntervals());

for (var entry : interpolatedCounts.entrySet()) {
outputStream.writeUTF(entry.getKey().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public interface VDFTrafficHandler {
void processEnterLink(double time, Id<Link> linkId);

IdMap<Link, List<Double>> aggregate();
IdMap<Link, List<Double>> aggregate(boolean ignoreIteration);

VDFReaderInterface getReader();

Expand Down

0 comments on commit d73625f

Please sign in to comment.