Skip to content

Commit

Permalink
feat: vdf sparse horizon handler - file writing and reading (#278)
Browse files Browse the repository at this point in the history
* feat: Using VDFSparseHorizonHandler by default

* fix: file writing and reading

* feat: towards testing VDFSparseHorizonHandler VS VDFHorizonHandler

* chore: cleanup

* feat: comparing output events of VDFHorizonHandler and VDFSparseHorizonHandler

---------

Co-authored-by: tkchouaki <tarek.chouaki@irt-systemx.fr>
  • Loading branch information
tkchouaki and tkchouaki authored Nov 14, 2024
1 parent 9055683 commit 05f84a8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public IdMap<Link, List<Double>> aggregate(boolean ignoreIteration) {
}

if (total > 0.0) {
LinkState linkState = newState.get(entry.getKey());
LinkState linkState = new LinkState(new ArrayList<>(), new ArrayList<>());
newState.put(entry.getKey(), linkState);

int timeIndex = 0;
Expand Down Expand Up @@ -211,6 +211,9 @@ public void readFile(URL inputFile) {
state.add(slice);

int sliceLinkCount = inputStream.readInt();

logger.info(String.format("Slice %d/%d, Reading %d link states", sliceIndex+1, slices, sliceLinkCount));

for (int sliceLinkIndex = 0; sliceLinkIndex < sliceLinkCount; sliceLinkIndex++) {
int linkIndex = inputStream.readInt();
int linkStateSize = inputStream.readInt();
Expand Down Expand Up @@ -256,20 +259,28 @@ public void writeFile(File outputFile) {
outputStream.writeUTF(linkIds.get(linkIndex).toString());
}

logger.info(String.format("About to write %d slices", state.size()));

for (int sliceIndex = 0; sliceIndex < state.size(); sliceIndex++) {
IdMap<Link, LinkState> slice = state.get(sliceIndex);
outputStream.writeInt(slice.size());

int sliceLinkIndex = 0;
for (Id<Link> linkId : linkIds) {
LinkState linkState = slice.get(linkId);
if(linkState == null) {
continue;
}
outputStream.writeInt(linkIds.indexOf(linkId));
outputStream.writeInt(linkState.count.size());

for (int i = 0; i < linkState.count.size(); i++) {
outputStream.writeInt(linkState.time.get(i));
outputStream.writeDouble(linkState.count.get(i));
}
sliceLinkIndex += 1;
}
assert sliceLinkIndex == slice.size();
}

outputStream.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.eqasim.core.simulation.vdf.utils;

import org.eqasim.core.components.config.EqasimConfigGroup;
import org.eqasim.core.simulation.EqasimConfigurator;
import org.eqasim.core.simulation.vdf.VDFConfigGroup;
import org.eqasim.core.simulation.vdf.engine.VDFEngineConfigGroup;
Expand All @@ -25,9 +24,8 @@ public static void adaptConfigForVDF(Config config, boolean engine) {
VDFConfigGroup.getOrCreate(config).setWriteInterval(1);
VDFConfigGroup.getOrCreate(config).setWriteFlowInterval(1);

// VDF: Set capacity factor instead (We retrieve it form the Eqasim config group)
EqasimConfigGroup eqasimConfigGroup = (EqasimConfigGroup) config.getModules().get(EqasimConfigGroup.GROUP_NAME);
VDFConfigGroup.getOrCreate(config).setCapacityFactor(eqasimConfigGroup.getSampleSize());
VDFConfigGroup vdfConfigGroup = VDFConfigGroup.getOrCreate(config);
vdfConfigGroup.setHandler(VDFConfigGroup.HandlerType.SparseHorizon);

if(engine) {
// VDF Engine: Add config group
Expand Down
17 changes: 17 additions & 0 deletions core/src/test/java/org/eqasim/TestSimulationPipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@

import com.google.inject.Inject;
import com.google.inject.Provider;
import org.matsim.utils.eventsfilecomparison.ComparisonResult;
import org.matsim.utils.eventsfilecomparison.EventsFileComparator;

public class TestSimulationPipeline {

Expand Down Expand Up @@ -378,6 +380,7 @@ public void testTransitWithAbstractAccess() throws CommandLine.ConfigurationExce
}

public void runVdf() throws CommandLine.ConfigurationException, IOException, InterruptedException {
// This one will use the SparseHorizon handler
AdaptConfigForVDF.main(new String[] {
"--input-config-path", "melun_test/input/config.xml",
"--output-config-path", "melun_test/input/config_vdf.xml",
Expand All @@ -388,6 +391,20 @@ public void runVdf() throws CommandLine.ConfigurationException, IOException, Int

runMelunSimulation("melun_test/input/config_vdf.xml", "melun_test/output_vdf");


// We force this one to use the legacy horizon handler
AdaptConfigForVDF.main(new String[] {
"--input-config-path", "melun_test/input/config.xml",
"--output-config-path", "melun_test/input/config_vdf_horizon.xml",
"--engine", "true",
"--config:eqasim:vdf_engine.generateNetworkEvents", "true",
"--config:eqasim:vdf.handler", "Horizon"
});

runMelunSimulation("melun_test/input/config_vdf_horizon.xml", "melun_test/output_vdf_horizon");

assert EventsFileComparator.compare("melun_test/output_vdf_horizon/output_events.xml.gz", "melun_test/output_vdf/output_events.xml.gz").equals(ComparisonResult.FILES_ARE_EQUAL);

RunStandaloneModeChoice.main(new String[]{
"--config-path", "melun_test/input/config_vdf.xml",
"--config:standaloneModeChoice.outputDirectory", "melun_test/output_mode_choice_vdf",
Expand Down

0 comments on commit 05f84a8

Please sign in to comment.