Skip to content

Commit

Permalink
add tests for all parkingSearch strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
rewertvsp committed Nov 30, 2023
1 parent 38ad994 commit 12cd0e0
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,141 @@
* *
* *********************************************************************** */

/**
*
*/
package org.matsim.contrib.parking.run;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.Population;
import org.matsim.contrib.parking.parkingsearch.ParkingSearchStrategy;
import org.matsim.contrib.parking.parkingsearch.RunParkingSearchExample;
import org.matsim.contrib.parking.parkingsearch.sim.ParkingSearchConfigGroup;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.events.EventsUtils;
import org.matsim.core.population.PopulationUtils;
import org.matsim.testcases.MatsimTestUtils;
import org.matsim.utils.eventsfilecomparison.EventsFileComparator;

/**
* @author jbischoff
*
* @author jbischoff
*/
public class RunParkingSearchScenarioIT {
@Rule public MatsimTestUtils utils = new MatsimTestUtils() ;
@Rule
public MatsimTestUtils utils = new MatsimTestUtils();

@Test
public void testRunParkingBenesonStrategy() {
try {
String configFile = "./src/main/resources/parkingsearch/config.xml";
Config config = ConfigUtils.loadConfig(configFile, new ParkingSearchConfigGroup());
config.controller().setLastIteration(0);
config.controller().setOutputDirectory(utils.getOutputDirectory());

ParkingSearchConfigGroup configGroup = (ParkingSearchConfigGroup) config.getModules().get(ParkingSearchConfigGroup.GROUP_NAME);
configGroup.setParkingSearchStrategy(ParkingSearchStrategy.Benenson);

new RunParkingSearchExample().run(config, false);

} catch (Exception e) {
e.printStackTrace();
Assert.fail("something went wrong");
}
}

@Test
public void testRunParking() {
public void testRunParkingRandomStrategy() {
String configFile = "./src/main/resources/parkingsearch/config.xml";
Config config = ConfigUtils.loadConfig(configFile, new ParkingSearchConfigGroup());
config.controller().setLastIteration(0);
config.controller().setOutputDirectory( utils.getOutputDirectory() );
config.controller().setOutputDirectory(utils.getOutputDirectory());

ParkingSearchConfigGroup configGroup = (ParkingSearchConfigGroup) config.getModules().get(ParkingSearchConfigGroup.GROUP_NAME);
configGroup.setParkingSearchStrategy(ParkingSearchStrategy.Benenson);
configGroup.setParkingSearchStrategy(ParkingSearchStrategy.Random);

try {
new RunParkingSearchExample().run(config, false);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("something went wrong");
}
}

@Test
public void testRunParkingDistanceMemoryStrategy() {
try {
String configFile = "./src/main/resources/parkingsearch/config.xml";
Config config = ConfigUtils.loadConfig(configFile, new ParkingSearchConfigGroup());
config.controller().setLastIteration(0);
config.controller().setOutputDirectory(utils.getOutputDirectory());

ParkingSearchConfigGroup configGroup = (ParkingSearchConfigGroup) config.getModules().get(ParkingSearchConfigGroup.GROUP_NAME);
configGroup.setParkingSearchStrategy(ParkingSearchStrategy.DistanceMemory);

new RunParkingSearchExample().run(config, false);
{
Population expected = PopulationUtils.createPopulation(ConfigUtils.createConfig());
PopulationUtils.readPopulation(expected, utils.getInputDirectory() + "/output_plans.xml.gz");

Population actual = PopulationUtils.createPopulation(ConfigUtils.createConfig());
PopulationUtils.readPopulation(actual, utils.getOutputDirectory() + "/output_plans.xml.gz");

for (Id<Person> personId : expected.getPersons().keySet()) {
double scoreReference = expected.getPersons().get(personId).getSelectedPlan().getScore();
double scoreCurrent = actual.getPersons().get(personId).getSelectedPlan().getScore();
Assert.assertEquals("Scores of person=" + personId + " are different", scoreReference, scoreCurrent, MatsimTestUtils.EPSILON);
}

}
{
String expected = utils.getInputDirectory() + "/output_events.xml.gz";
String actual = utils.getOutputDirectory() + "/output_events.xml.gz";
EventsFileComparator.Result result = EventsUtils.compareEventsFiles(expected, actual);
Assert.assertEquals(EventsFileComparator.Result.FILES_ARE_EQUAL, result);
}
} catch (Exception e) {
e.printStackTrace();
Assert.fail("something went wrong");
}
}

@Test
public void testRunParkingNearestParkingSpotStrategy() {
try {
String configFile = "./src/main/resources/parkingsearch/config.xml";
Config config = ConfigUtils.loadConfig(configFile, new ParkingSearchConfigGroup());
config.controller().setLastIteration(0);
config.controller().setOutputDirectory(utils.getOutputDirectory());

ParkingSearchConfigGroup configGroup = (ParkingSearchConfigGroup) config.getModules().get(ParkingSearchConfigGroup.GROUP_NAME);
configGroup.setParkingSearchStrategy(ParkingSearchStrategy.NearestParkingSpot);

new RunParkingSearchExample().run(config, false);
{
Population expected = PopulationUtils.createPopulation(ConfigUtils.createConfig());
PopulationUtils.readPopulation(expected, utils.getInputDirectory() + "/output_plans.xml.gz");

Population actual = PopulationUtils.createPopulation(ConfigUtils.createConfig());
PopulationUtils.readPopulation(actual, utils.getOutputDirectory() + "/output_plans.xml.gz");

new RunParkingSearchExample().run(config,false);
for (Id<Person> personId : expected.getPersons().keySet()) {
double scoreReference = expected.getPersons().get(personId).getSelectedPlan().getScore();
double scoreCurrent = actual.getPersons().get(personId).getSelectedPlan().getScore();
Assert.assertEquals("Scores of person=" + personId + " are different", scoreReference, scoreCurrent, MatsimTestUtils.EPSILON);
}

}
{
String expected = utils.getInputDirectory() + "/output_events.xml.gz";
String actual = utils.getOutputDirectory() + "/output_events.xml.gz";
EventsFileComparator.Result result = EventsUtils.compareEventsFiles(expected, actual);
Assert.assertEquals(EventsFileComparator.Result.FILES_ARE_EQUAL, result);
}
} catch (Exception e) {
e.printStackTrace();
Assert.fail("something went wrong");
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 12cd0e0

Please sign in to comment.