Skip to content

Commit

Permalink
continued and hopefully fixed bicycle migrant test
Browse files Browse the repository at this point in the history
  • Loading branch information
GregorRyb committed Jul 2, 2024
1 parent c7f1417 commit 861dee1
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 15 deletions.
44 changes: 30 additions & 14 deletions src/main/java/org/matsim/prepare/MigrantBicycleChoiceHandler.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,55 +17,69 @@
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;

public class MigrantBicycleChoiceHandler implements PersonDepartureEventHandler, AfterMobsimListener {
private List<PersonScoreEvent> migrantDepartures;
private Population population;
private List<Id<Person>> migrants;

public MigrantBicycleChoiceHandler(Population population) {
migrantDepartures = new LinkedList<>();
this.population = population;
}


public MigrantBicycleChoiceHandler(List<Id<Person>> listOfMigrants) {
migrants = listOfMigrants;
migrantDepartures = new LinkedList<>();
}

public MigrantBicycleChoiceHandler(String populationPath) {
migrantDepartures = new LinkedList<>();
this.population = PopulationUtils.readPopulation(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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void install() {
@Override
public void install() {
addEventHandlerBinding().toInstance(choiceHandler);
addControlerListenerBinding().toInstance(choiceHandler);
// addControlerListenerBinding().toInstance(choiceHandler);
}
});
controler.run();
Expand Down
66 changes: 66 additions & 0 deletions src/test/java/org/matsim/run/TestMigrationMapper.java
Original file line number Diff line number Diff line change
@@ -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<Id<Person>>();
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();
}

}

0 comments on commit 861dee1

Please sign in to comment.