Skip to content

Commit

Permalink
chore: ensuring simulations are deterministic in tests (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkchouaki authored Nov 26, 2024
1 parent 60520f1 commit 678fbb0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ static public void insertVehicles(Config config, Scenario scenario) {
VehiclesFactory factory = vehicles.getFactory();

VehicleType vehicleType = VehicleUtils.createVehicleType(Id.create("defaultVehicleType", VehicleType.class));
vehicleType.setNetworkMode("car");
vehicles.addVehicleType(vehicleType);
for (Person person : scenario.getPopulation().getPersons().values()) {
Map<String, Id<Vehicle>> personVehicles = new HashMap<>();
Expand Down
50 changes: 35 additions & 15 deletions core/src/test/java/org/eqasim/TestSimulationPipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.*;

import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eqasim.core.analysis.run.RunLegAnalysis;
import org.eqasim.core.analysis.run.RunPublicTransportLegAnalysis;
import org.eqasim.core.analysis.run.RunTripAnalysis;
Expand All @@ -36,18 +34,13 @@
import org.eqasim.core.simulation.vdf.utils.AdaptConfigForVDF;
import org.eqasim.core.standalone_mode_choice.RunStandaloneModeChoice;
import org.eqasim.core.standalone_mode_choice.StandaloneModeChoiceConfigurator;
import org.eqasim.core.tools.ExportActivitiesToShapefile;
import org.eqasim.core.tools.ExportNetworkRoutesToGeopackage;
import org.eqasim.core.tools.ExportNetworkToShapefile;
import org.eqasim.core.tools.ExportPopulationToCSV;
import org.eqasim.core.tools.ExportTransitLinesToShapefile;
import org.eqasim.core.tools.ExportTransitStopsToShapefile;
import org.eqasim.core.tools.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.*;
import org.matsim.contribs.discrete_mode_choice.model.DiscreteModeChoiceTrip;
import org.matsim.contribs.discrete_mode_choice.model.mode_availability.ModeAvailability;
import org.matsim.core.config.CommandLine;
Expand All @@ -57,6 +50,7 @@
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.Controler;
import org.matsim.core.scenario.ScenarioUtils;

import org.matsim.core.utils.misc.CRCChecksum;

import com.google.inject.Inject;
Expand All @@ -73,16 +67,21 @@ public void setUp() throws IOException {

@After
public void tearDown() throws IOException {
FileUtils.deleteDirectory(new File("melun_test"));
//FileUtils.deleteDirectory(new File("melun_test"));
}

private void runMelunSimulation(String configPath, String outputPath) {
runMelunSimulation(configPath, outputPath, null, null);
}

private void runMelunSimulation(String configPath, String outputPath, String inputPlansFile, Integer lastIteration) {
EqasimConfigurator eqasimConfigurator = new EqasimConfigurator();
Config config = ConfigUtils.loadConfig(configPath);
runMelunSimulation(config, outputPath, inputPlansFile, lastIteration);
}

private void runMelunSimulation(Config config, String outputPath, String inputPlansFile, Integer lastIteration) {
EqasimConfigurator eqasimConfigurator = new EqasimConfigurator();

eqasimConfigurator.updateConfig(config);
((ControllerConfigGroup) config.getModules().get(ControllerConfigGroup.GROUP_NAME)).setOutputDirectory(outputPath);
if(inputPlansFile != null) {
Expand Down Expand Up @@ -424,11 +423,32 @@ public void testPipeline() throws Exception {
runCutterV2();
}

@Test
public void testBaseDeterminism() throws Exception {
Logger logger = LogManager.getLogger(TestSimulationPipeline.class);
Config config = ConfigUtils.loadConfig("melun_test/input/config.xml");
runMelunSimulation(config, "melun_test/output_determinism_1", null, 2);

config = ConfigUtils.loadConfig("melun_test/input/config.xml");
runMelunSimulation(config, "melun_test/output_determinism_2", null, 2 );

for(String comparedFile: new String[]{"output_plans.xml.gz"}) {
long firstCrc = CRCChecksum.getCRCFromFile("melun_test/output_determinism_1/" + comparedFile);
long secondCrc = CRCChecksum.getCRCFromFile("melun_test/output_determinism_2/"+comparedFile);
assert firstCrc == secondCrc;
}
}

public void runPopulationRouting() throws CommandLine.ConfigurationException, IOException, InterruptedException {
RunPopulationRouting.main(new String[] {
"--config-path", "melun_test/input/config.xml",
"--output-path", "melun_test/output/routed_population.xml.gz"
"--output-path", "melun_test/output/routed_population.xml"
});
RunPopulationRouting.main(new String[] {
"--config-path", "melun_test/input/config.xml",
"--output-path", "melun_test/output/routed_population_again.xml"
});
assert CRCChecksum.getCRCFromFile("melun_test/output/routed_population.xml") == CRCChecksum.getCRCFromFile("melun_test/output/routed_population_again.xml");
}

public void runStandaloneModeChoice() throws CommandLine.ConfigurationException, IOException, InterruptedException {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<matsim.version>2025.0-PR3483</matsim.version>
<matsim.version>2025.0-PR3568</matsim.version>
</properties>

<distributionManagement>
Expand Down

0 comments on commit 678fbb0

Please sign in to comment.