From 861dee1d44a32bc8081fc56b65d5f1a65575dec5 Mon Sep 17 00:00:00 2001 From: GregorRyb Date: Tue, 2 Jul 2024 16:33:39 +0200 Subject: [PATCH] continued and hopefully fixed bicycle migrant test --- .../prepare/MigrantBicycleChoiceHandler.java | 44 +++++++++---- .../MigrantBicycleChoiceHandlerTest.java | 2 +- .../org/matsim/run/TestMigrationMapper.java | 66 +++++++++++++++++++ 3 files changed, 97 insertions(+), 15 deletions(-) create mode 100644 src/test/java/org/matsim/run/TestMigrationMapper.java diff --git a/src/main/java/org/matsim/prepare/MigrantBicycleChoiceHandler.java b/src/main/java/org/matsim/prepare/MigrantBicycleChoiceHandler.java index fefcb41..46eb0b8 100644 --- a/src/main/java/org/matsim/prepare/MigrantBicycleChoiceHandler.java +++ b/src/main/java/org/matsim/prepare/MigrantBicycleChoiceHandler.java @@ -1,10 +1,12 @@ package org.matsim.prepare; +import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.TransportMode; import org.matsim.api.core.v01.events.PersonDepartureEvent; import org.matsim.api.core.v01.events.PersonMoneyEvent; import org.matsim.api.core.v01.events.PersonScoreEvent; import org.matsim.api.core.v01.events.handler.PersonDepartureEventHandler; +import org.matsim.api.core.v01.population.Person; import org.matsim.api.core.v01.population.Population; import org.matsim.core.config.Config; import org.matsim.core.config.ConfigUtils; @@ -15,7 +17,9 @@ import org.matsim.core.events.EventsUtils; import org.matsim.core.gbl.MatsimRandom; import org.matsim.core.population.PopulationUtils; +import org.matsim.core.utils.misc.Time; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Random; @@ -23,12 +27,19 @@ public class MigrantBicycleChoiceHandler implements PersonDepartureEventHandler, AfterMobsimListener { private List migrantDepartures; private Population population; + private List> migrants; public MigrantBicycleChoiceHandler(Population population) { migrantDepartures = new LinkedList<>(); this.population = population; } + + public MigrantBicycleChoiceHandler(List> listOfMigrants) { + migrants = listOfMigrants; + migrantDepartures = new LinkedList<>(); + } + public MigrantBicycleChoiceHandler(String populationPath) { migrantDepartures = new LinkedList<>(); this.population = PopulationUtils.readPopulation(populationPath); @@ -36,34 +47,39 @@ public MigrantBicycleChoiceHandler(String populationPath) { @Override public void handleEvent(PersonDepartureEvent personDepartureEvent) { - // Do not do anything, if this Person is not a migrant + /* // Do not do anything, if this Person is not a migrant if( !population.getPersons().get(personDepartureEvent.getPersonId()).getAttributes().getAsMap().containsKey("subpopulation") || !population.getPersons().get(personDepartureEvent.getPersonId()).getAttributes().getAttribute("subpopulation").equals("migrant") ){ return; } - - // Add to departures of migrants - Random rand = MatsimRandom.getRandom(); - if(personDepartureEvent.getLegMode().equals("bicycle") && rand.nextInt(1000) < 1000) new PersonMoneyEvent(personDepartureEvent.getTime(), personDepartureEvent.getPersonId(), 1000); //TODO what is kind?; set rand-value to 400 +*/ + if(personDepartureEvent.getLegMode().equals("bicycle") && migrants.contains(personDepartureEvent.getPersonId())) { + migrantDepartures.add(new PersonScoreEvent(personDepartureEvent.getTime(), personDepartureEvent.getPersonId(), -10000.0, "punishment_for_cycling")); + } // TODO TransportMode.bike } - @Override public void notifyAfterMobsim(AfterMobsimEvent afterMobsimEvent) { - for(PersonScoreEvent e : migrantDepartures){ - // TODO Check if this will actually influence the choice - // TODO Check what would be a useful value - // TODO Gibt es keine direktere Möglichkeit? Kann ich das Event nicht einfach irgendwie überschreiben? - afterMobsimEvent.getServices().getEvents().processEvent(e); + for(PersonScoreEvent personScoreEvent : migrantDepartures) { + afterMobsimEvent.getServices().getEvents().processEvent(personScoreEvent); } - migrantDepartures.clear(); + + + + } + + @Override + public void reset(int iteration) { + PersonDepartureEventHandler.super.reset(iteration); } - public static void main(String[] args) { + + +/* public static void main(String[] args) { //MigrantBicycleChoiceHandler choiceHandler = new MigrantBicycleChoiceHandler(); - } + }*/ // @Override // public void notifyReplanning(ReplanningEvent replanningEvent) { diff --git a/src/test/java/org/matsim/prepare/MigrantBicycleChoiceHandlerTest.java b/src/test/java/org/matsim/prepare/MigrantBicycleChoiceHandlerTest.java index 3cb0de8..c6099df 100644 --- a/src/test/java/org/matsim/prepare/MigrantBicycleChoiceHandlerTest.java +++ b/src/test/java/org/matsim/prepare/MigrantBicycleChoiceHandlerTest.java @@ -103,7 +103,7 @@ public void install() { @Override public void install() { addEventHandlerBinding().toInstance(choiceHandler); - addControlerListenerBinding().toInstance(choiceHandler); + // addControlerListenerBinding().toInstance(choiceHandler); } }); controler.run(); diff --git a/src/test/java/org/matsim/run/TestMigrationMapper.java b/src/test/java/org/matsim/run/TestMigrationMapper.java new file mode 100644 index 0000000..97c9913 --- /dev/null +++ b/src/test/java/org/matsim/run/TestMigrationMapper.java @@ -0,0 +1,66 @@ +package org.matsim.run; + +import org.junit.Rule; +import org.junit.Test; +import org.matsim.analysis.personMoney.PersonMoneyEventsAnalysisModule; +import org.matsim.api.core.v01.Id; +import org.matsim.api.core.v01.Scenario; +import org.matsim.api.core.v01.population.Person; +import org.matsim.core.config.Config; +import org.matsim.core.config.ConfigUtils; +import org.matsim.core.controler.AbstractModule; +import org.matsim.core.controler.Controler; +import org.matsim.core.controler.OutputDirectoryHierarchy; +import org.matsim.core.scenario.ScenarioUtils; +import org.matsim.examples.ExamplesUtils; +import org.matsim.prepare.MigrantBicycleChoiceHandler; +import org.matsim.prepare.MigrantMapper; +import org.matsim.testcases.MatsimTestUtils; + +import java.util.ArrayList; +import java.util.List; + + +public class TestMigrationMapper { + + + @Rule + public MatsimTestUtils utils = new MatsimTestUtils(); + + @Test + public final void runTestMigrationMapper() { + String inputPath = String.valueOf(ExamplesUtils.getTestScenarioURL("equil-mixedTraffic")); + Config config = ConfigUtils.loadConfig(inputPath + "config-with-mode-vehicles.xml"); + config.controler().setLastIteration(3); + config.controler().setOutputDirectory(utils.getOutputDirectory()); + config.global().setNumberOfThreads(1); + config.qsim().setNumberOfThreads(1); + config.controler().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists); + Scenario scenario = ScenarioUtils.loadScenario(config); + Person person = scenario.getPopulation().getPersons().get(Id.createPersonId("10")); + //TODO use migrant mapper class? + //TODO I do + person.getAttributes().putAttribute("migrant", "true"); + + scenario.getPopulation().getPersons().clear(); + scenario.getPopulation().addPerson(person); + Controler controler = new Controler(scenario); + + List migrants = new ArrayList>(); + migrants.add(person.getId()); + + MigrantBicycleChoiceHandler migrantBicycleChoiceHandler = new MigrantBicycleChoiceHandler(migrants); + + controler.addOverridingModule(new AbstractModule() { + @Override + public void install() { + addEventHandlerBinding().toInstance(migrantBicycleChoiceHandler); + addControlerListenerBinding().toInstance(migrantBicycleChoiceHandler); + new PersonMoneyEventsAnalysisModule(); + } + }); + + controler.run(); + } + +}