Skip to content

Commit

Permalink
use population comparison instead of population utils to compare plans
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuehnel committed Apr 30, 2024
1 parent 65b49fb commit 14d007a
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.events.EventsUtils;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.population.routes.PopulationComparison;
import org.matsim.testcases.MatsimTestUtils;
import org.matsim.utils.eventsfilecomparison.ComparisonResult;

Expand All @@ -33,8 +34,8 @@ public class RunEvExampleTest{
Population actual = PopulationUtils.createPopulation( ConfigUtils.createConfig() ) ;
PopulationUtils.readPopulation( actual, utils.getOutputDirectory() + "/output_plans.xml.gz" );

boolean result = PopulationUtils.comparePopulations( expected, actual );
Assertions.assertTrue(result);
PopulationComparison.Result result = PopulationComparison.compare(expected, actual);
Assertions.assertEquals(PopulationComparison.Result.equal, result);
}
{
String expected = utils.getInputDirectory() + "/output_events.xml.gz" ;
Expand Down Expand Up @@ -66,8 +67,8 @@ public class RunEvExampleTest{
Population actual = PopulationUtils.createPopulation( ConfigUtils.createConfig() ) ;
PopulationUtils.readPopulation( actual, utils.getOutputDirectory() + "/output_plans.xml.gz" );

boolean result = PopulationUtils.comparePopulations( expected, actual );
Assertions.assertTrue(result);
PopulationComparison.Result result = PopulationComparison.compare(expected, actual);
Assertions.assertEquals(PopulationComparison.Result.equal, result);
}
{
String expected = utils.getInputDirectory() + "/output_events.xml.gz" ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.events.EventsUtils;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.population.routes.PopulationComparison;
import org.matsim.testcases.MatsimTestUtils;
import org.matsim.utils.eventsfilecomparison.ComparisonResult;

Expand All @@ -33,8 +34,8 @@ void runTest(){
Population actual = PopulationUtils.createPopulation( ConfigUtils.createConfig() ) ;
PopulationUtils.readPopulation( actual, utils.getOutputDirectory() + "/output_plans.xml.gz" );

boolean result = PopulationUtils.comparePopulations( expected, actual );
Assertions.assertTrue( result );
PopulationComparison.Result result = PopulationComparison.compare(expected, actual);
Assertions.assertEquals(PopulationComparison.Result.equal, result);
}
{
String expected = utils.getInputDirectory() + "/output_events.xml.gz" ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.matsim.api.core.v01.population.Population;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.events.EventsUtils;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.population.routes.PopulationComparison;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.examples.ExamplesUtils;
import org.matsim.testcases.MatsimTestUtils;
Expand Down Expand Up @@ -60,9 +62,8 @@ void testMain(){
PopulationUtils.readPopulation( expected, utils.getInputDirectory() + "/output_experienced_plans.xml.gz" );
final Population actual = PopulationUtils.createPopulation( ConfigUtils.createConfig() );
PopulationUtils.readPopulation( actual, utils.getOutputDirectory() + "/output_experienced_plans.xml.gz" );
if(!PopulationUtils.comparePopulations( expected, actual )) {
throw new RuntimeException("Plans file comparison ended with result false");
}
PopulationComparison.Result result = PopulationComparison.compare(expected, actual);
Assertions.assertEquals(PopulationComparison.Result.equal, result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.events.EventsUtils;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.population.routes.PopulationComparison;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.examples.ExamplesUtils;
import org.matsim.testcases.MatsimTestUtils;
Expand Down Expand Up @@ -62,7 +63,8 @@ final void testMain() {
PopulationUtils.readPopulation( expected, utils.getInputDirectory() + "/output_plans.xml.gz" );
final Population actual = PopulationUtils.createPopulation( ConfigUtils.createConfig() );
PopulationUtils.readPopulation( actual, utils.getOutputDirectory() + "/output_plans.xml.gz" );
Assertions.assertTrue(PopulationUtils.comparePopulations( expected, actual ), "Populations are different");
PopulationComparison.Result result = PopulationComparison.compare(expected, actual);
Assertions.assertEquals(PopulationComparison.Result.equal, result);
}


Expand Down
5 changes: 3 additions & 2 deletions contribs/vsp/src/test/java/playground/vsp/ev/UrbanEVIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.events.EventsUtils;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.population.routes.PopulationComparison;
import org.matsim.testcases.MatsimTestUtils;
import org.matsim.utils.eventsfilecomparison.ComparisonResult;

Expand All @@ -32,8 +33,8 @@ void run() {
Population actual = PopulationUtils.createPopulation( ConfigUtils.createConfig() ) ;
PopulationUtils.readPopulation( actual, utils.getOutputDirectory() + "/output_plans.xml.gz" );

boolean result = PopulationUtils.comparePopulations( expected, actual );
Assertions.assertTrue( result );
PopulationComparison.Result result = PopulationComparison.compare(expected, actual);
Assertions.assertEquals(PopulationComparison.Result.equal, result);
}
{
String expected = utils.getInputDirectory() + "/output_events.xml.gz" ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,10 @@ public static double calculateSimilarity(List<Activity> activities1, List<Activi
* this will go unnoticed (this method will just return true or false,
* probably false, except if both Writers have written the exact same text
* until the Exception happens).
*
* @deprecated -- please use {@link org.matsim.core.population.routes.PopulationComparison} instead. nkuehnel, apr'24
*/
@Deprecated
public static boolean equalPopulation(final Population s1, final Population s2) {
try {
try( InputStream inputStream1 = openPopulationInputStream( s1 ) ; InputStream inputStream2 = openPopulationInputStream( s2 ) ){
Expand Down Expand Up @@ -1186,6 +1189,10 @@ public static Population readPopulation( String filename ) {
readPopulation( population, filename );
return population ;
}
/**
* @deprecated -- please use {@link org.matsim.core.population.routes.PopulationComparison} instead. nkuehnel, apr'24
*/
@Deprecated
public static boolean comparePopulations( Population population1, Population population2 ) {
return PopulationUtils.equalPopulation( population1, population2 );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ public enum Result { equal, notEqual }

private static final Logger log = LogManager.getLogger( PopulationComparison.class );

private PopulationComparison(){}

public Result compare( Population population1, Population population2 ){

public static Result compare( Population population1, Population population2 ){
Result result=Result.equal;

Iterator<? extends Person> it1 = population1.getPersons().values().iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.matsim.core.controler.*;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.population.io.PopulationReader;
import org.matsim.core.population.routes.PopulationComparison;
import org.matsim.core.replanning.ReplanningContext;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.testcases.MatsimTestUtils;
Expand Down Expand Up @@ -82,7 +83,9 @@ public boolean invoke() {
}
}, "test", outputDirectoryHierarchy, scenario);
replanPopulation(scenario.getPopulation(), testee);
Assertions.assertTrue(PopulationUtils.equalPopulation(scenario.getPopulation(), originalScenario.getPopulation()));

PopulationComparison.Result result = PopulationComparison.compare(scenario.getPopulation(), originalScenario.getPopulation());
Assertions.assertEquals(PopulationComparison.Result.equal, result);
}

@Test
Expand All @@ -99,7 +102,8 @@ public boolean invoke() {
}
}, "test", outputDirectoryHierarchy, scenario);
replanPopulation(scenario.getPopulation(), testee);
Assertions.assertFalse(PopulationUtils.equalPopulation(scenario.getPopulation(), originalScenario.getPopulation()));
PopulationComparison.Result result = PopulationComparison.compare(scenario.getPopulation(), originalScenario.getPopulation());
Assertions.assertEquals(PopulationComparison.Result.equal, result);
}

private Population loadPopulation(String filename) {
Expand Down
7 changes: 4 additions & 3 deletions matsim/src/test/java/org/matsim/core/router/RoutingIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.population.algorithms.PersonAlgorithm;
import org.matsim.core.population.io.PopulationReader;
import org.matsim.core.population.routes.PopulationComparison;
import org.matsim.core.router.costcalculators.FreespeedTravelTimeAndDisutility;
import org.matsim.core.router.costcalculators.TravelDisutilityFactory;
import org.matsim.core.router.speedy.SpeedyALTFactory;
Expand Down Expand Up @@ -161,12 +162,12 @@ private void doTest(final RouterProvider provider) {
new MatsimNetworkReader(referenceScenario.getNetwork()).readFile(config.network().getInputFile());
new PopulationReader(referenceScenario).readFile(inPlansName);

final boolean isEqual = PopulationUtils.equalPopulation(referenceScenario.getPopulation(), scenario.getPopulation());
if ( !isEqual ) {
PopulationComparison.Result result = PopulationComparison.compare(referenceScenario.getPopulation(), scenario.getPopulation());
if (result == PopulationComparison.Result.notEqual) {
new PopulationWriter(referenceScenario.getPopulation(), scenario.getNetwork()).write(this.utils.getOutputDirectory() + "/reference_population.xml.gz");
new PopulationWriter(scenario.getPopulation(), scenario.getNetwork()).write(this.utils.getOutputDirectory() + "/output_population.xml.gz");
}
Assertions.assertTrue(isEqual, "different plans files.");
Assertions.assertEquals(PopulationComparison.Result.equal, result, "different plans files");
}

private static void calcRoute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.matsim.core.controler.Controler;
import org.matsim.core.gbl.Gbl;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.population.routes.PopulationComparison;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.examples.ExamplesUtils;
Expand Down Expand Up @@ -113,7 +114,6 @@ private void evaluate() throws MalformedURLException {
this.evaluate("plans.xml.gz");
}

private final static Logger LOG = LogManager.getLogger(ReRoutingIT.class);
private void evaluate(String plansFilename) throws MalformedURLException {
Config config = utils.loadConfig(utils.getClassInputDirectory() + "config.xml");
config.network().setInputFile(IOUtils.extendUrl(ExamplesUtils.getTestScenarioURL("berlin"), "network.xml.gz").toString());
Expand All @@ -124,13 +124,12 @@ private void evaluate(String plansFilename) throws MalformedURLException {
Scenario scenario = ScenarioUtils.loadScenario(config);

Gbl.startMeasurement();
final boolean isEqual = PopulationUtils.equalPopulation(referenceScenario.getPopulation(), scenario.getPopulation());
PopulationComparison.Result result = PopulationComparison.compare(referenceScenario.getPopulation(), scenario.getPopulation());
Gbl.printElapsedTime();
if ( !isEqual ) {
if (result == PopulationComparison.Result.notEqual) {
new PopulationWriter(referenceScenario.getPopulation(), scenario.getNetwork()).write(utils.getOutputDirectory() + "/reference_population.xml.gz");
new PopulationWriter(scenario.getPopulation(), scenario.getNetwork()).write(utils.getOutputDirectory() + "/output_population.xml.gz");
}
Assertions.assertTrue(isEqual, "different plans files.");
Assertions.assertEquals(PopulationComparison.Result.equal, result, "different plans file");
}

}

0 comments on commit 14d007a

Please sign in to comment.