Skip to content

Commit

Permalink
Merge pull request #4 from matsim-scenarios/addGladbeckUtils
Browse files Browse the repository at this point in the history
Add gladbeck utils
  • Loading branch information
GregorRyb authored May 8, 2023
2 parents 0b0c057 + c358340 commit 946f7a6
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/main/java/org/matsim/prepare/AssignPersonAttributes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.matsim.prepare;

import org.locationtech.jts.geom.prep.PreparedGeometry;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.Population;
import org.matsim.application.analysis.HomeLocationFilter;
import org.matsim.application.options.ShpOptions;
import org.matsim.core.gbl.MatsimRandom;
import org.matsim.run.RunGladbeckScenario;
import org.matsim.utils.gis.shp2matsim.ShpGeometryUtils;
import org.opengis.feature.simple.SimpleFeature;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Collection;
import java.util.List;

public class AssignPersonAttributes {

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

public static void assigningDifferentCitizenship(Scenario scenario, ShpOptions shp) {

for (Person p: scenario.getPopulation().getPersons().values()) {
if (shp != null) {
HomeLocationFilter homeLocationFilter = new HomeLocationFilter(shp, scenario.getConfig().global().getCoordinateSystem(), scenario.getPopulation());
if (homeLocationFilter.test(p) == true) {
log.info("person p is resident in Gladbeck" + p);
double random = MatsimRandom.getRandom().nextDouble();
if (random <= GladbeckUtils.getShareOfDiffrentCitizenship()) {
log.info("set person " + p + "to different citizenship");
GladbeckUtils.setPersonToDifferentCitizenship(p);
}
}
} else {
log.warn("no shape file defined assigning attribute to everyone in the population");
double random = MatsimRandom.getRandom().nextDouble();
if (random <= GladbeckUtils.getShareOfDiffrentCitizenship()) {
log.info("set person " + p + "to different citizenship");
GladbeckUtils.setPersonToDifferentCitizenship(p);
}
}


}
}
}
21 changes: 21 additions & 0 deletions src/main/java/org/matsim/prepare/GladbeckUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.matsim.prepare;

import org.matsim.api.core.v01.population.Person;



public class GladbeckUtils{
private GladbeckUtils(){} // do not instantiate


public static void setPersonToDifferentCitizenship(Person person) {
person.getAttributes().putAttribute("citizenship", "diffrent");
}

public static double getShareOfDiffrentCitizenship() {
// this number is derived from: respos/shared-svn/projects/GlaMoBi/data/sozio-demographischen_Daten/2023-18-04_Auswertung_Staatsangehoerigkeiten.xlsx
return 0.19;
}


}
11 changes: 11 additions & 0 deletions src/main/java/org/matsim/run/RunGladbeckScenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.Controler;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.prepare.AssignPersonAttributes;
import org.matsim.prepare.ScenarioCutOut;
import org.matsim.run.policies.KlimaTaler;
import org.matsim.run.policies.SchoolRoadsClosure;
Expand Down Expand Up @@ -52,6 +53,9 @@ public class RunGladbeckScenario extends RunMetropoleRuhrScenario {
@CommandLine.Option(names = "--simplePtFlat", defaultValue = "false", description = "measures to allow everyone to have free pt")
private boolean simplePtFlat;

@CommandLine.Option(names = "--cyclingCourse", defaultValue = "false", description = "measures to increase the ")
boolean cyclingCourse;

@CommandLine.Option(names = "--klimaTaler", defaultValue = "0.0", description = "amount of money to give to a person to use pt, walk and bike")
double klimaTalerMoneyAmount;

Expand Down Expand Up @@ -116,6 +120,12 @@ protected void prepareScenario(Scenario scenario) {
listOfSchoolLinks.add(Id.createLinkId("481471120002r"));
new SchoolRoadsClosure().closeSchoolLinks(listOfSchoolLinks, scenario.getNetwork(), 800, 1700);
}

if (cyclingCourse == true) {
log.info("adding diffrent citizenships to the agents");
AssignPersonAttributes.assigningDifferentCitizenship(scenario, shp);
}

}

@Override
Expand All @@ -124,6 +134,7 @@ protected void prepareControler(Controler controler) {
//controler.getConfig().vspExperimental().setVspDefaultsCheckingLevel(VspExperimentalConfigGroup.VspDefaultsCheckingLevel.abort);

if (klimaTalerMoneyAmount != 0.0) {
log.info("add Klima taler with money amount: " + klimaTalerMoneyAmount);
KlimaTaler klimaTaler = new KlimaTaler(controler.getScenario().getConfig().plansCalcRoute().getBeelineDistanceFactors().get(TransportMode.walk), controler.getScenario().getNetwork(), klimaTalerMoneyAmount);
addKlimaTaler(controler, klimaTaler);
}
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/org/matsim/run/TestPersonAttributes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.matsim.run;

import org.junit.Rule;
import org.junit.Test;
import org.locationtech.jts.util.Assert;
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.OutputDirectoryHierarchy;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.examples.ExamplesUtils;
import org.matsim.prepare.AssignPersonAttributes;
import org.matsim.testcases.MatsimTestUtils;

public class TestPersonAttributes {

@Rule
public MatsimTestUtils utils = new MatsimTestUtils();

@Test
public final void runTestPersonAttributes() {
String inputPath = String.valueOf(ExamplesUtils.getTestScenarioURL("equil-mixedTraffic"));
Config config = ConfigUtils.loadConfig(inputPath + "config-with-mode-vehicles.xml");
config.controler().setLastIteration(0);
config.controler().setOutputDirectory("output/TestPersonAttributes/");
config.global().setNumberOfThreads(1);
config.qsim().setNumberOfThreads(1);
config.controler().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists);

Scenario scenario = ScenarioUtils.loadScenario(config);
AssignPersonAttributes.assigningDifferentCitizenship(scenario, null);

scenario.getPopulation().getPersons();
int count = 0;
for (Person p: scenario.getPopulation().getPersons().values()) {
if( p.getAttributes().getAttribute("citizenship") != null) {
count++;
}
}
int correctNr = (int) Math.floor(scenario.getPopulation().getPersons().size() * 0.19);
Assert.equals(correctNr, count);
}
}

0 comments on commit 946f7a6

Please sign in to comment.