From 05f84a8e6d627d82bfc389f863903e079cc3c8e3 Mon Sep 17 00:00:00 2001 From: Tarek Chouaki Date: Thu, 14 Nov 2024 10:17:10 +0100 Subject: [PATCH] feat: vdf sparse horizon handler - file writing and reading (#278) * 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 --- .../vdf/handlers/VDFSparseHorizonHandler.java | 13 ++++++++++++- .../simulation/vdf/utils/AdaptConfigForVDF.java | 6 ++---- .../java/org/eqasim/TestSimulationPipeline.java | 17 +++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/eqasim/core/simulation/vdf/handlers/VDFSparseHorizonHandler.java b/core/src/main/java/org/eqasim/core/simulation/vdf/handlers/VDFSparseHorizonHandler.java index 20de195de..2e8eb5d5d 100644 --- a/core/src/main/java/org/eqasim/core/simulation/vdf/handlers/VDFSparseHorizonHandler.java +++ b/core/src/main/java/org/eqasim/core/simulation/vdf/handlers/VDFSparseHorizonHandler.java @@ -87,7 +87,7 @@ public IdMap> 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; @@ -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(); @@ -256,12 +259,18 @@ 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 slice = state.get(sliceIndex); outputStream.writeInt(slice.size()); + int sliceLinkIndex = 0; for (Id linkId : linkIds) { LinkState linkState = slice.get(linkId); + if(linkState == null) { + continue; + } outputStream.writeInt(linkIds.indexOf(linkId)); outputStream.writeInt(linkState.count.size()); @@ -269,7 +278,9 @@ public void writeFile(File outputFile) { outputStream.writeInt(linkState.time.get(i)); outputStream.writeDouble(linkState.count.get(i)); } + sliceLinkIndex += 1; } + assert sliceLinkIndex == slice.size(); } outputStream.close(); diff --git a/core/src/main/java/org/eqasim/core/simulation/vdf/utils/AdaptConfigForVDF.java b/core/src/main/java/org/eqasim/core/simulation/vdf/utils/AdaptConfigForVDF.java index dfb0ea555..ac63f6df3 100644 --- a/core/src/main/java/org/eqasim/core/simulation/vdf/utils/AdaptConfigForVDF.java +++ b/core/src/main/java/org/eqasim/core/simulation/vdf/utils/AdaptConfigForVDF.java @@ -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; @@ -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 diff --git a/core/src/test/java/org/eqasim/TestSimulationPipeline.java b/core/src/test/java/org/eqasim/TestSimulationPipeline.java index 472ca926b..e8f75d857 100644 --- a/core/src/test/java/org/eqasim/TestSimulationPipeline.java +++ b/core/src/test/java/org/eqasim/TestSimulationPipeline.java @@ -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 { @@ -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", @@ -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",