Skip to content

Commit

Permalink
Merge pull request #264 from msmobility/cllorca_branch
Browse files Browse the repository at this point in the history
simple mode choice
  • Loading branch information
mergify[bot] authored Dec 2, 2019
2 parents 6d1201c + ed643aa commit 59334e9
Show file tree
Hide file tree
Showing 47 changed files with 147,922 additions and 147,051 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package de.tum.bgu.msm.matsim;

import de.tum.bgu.msm.data.travelTimes.TravelTimes;
import org.matsim.api.core.v01.Scenario;
import org.matsim.core.config.Config;

public interface MatsimScenarioAssembler {

Scenario assembleScenario(Config initialMatsimConfig, int year);
Scenario assembleScenario(Config initialMatsimConfig, int year, TravelTimes travelTimes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public IndexedDoubleMatrix2D createCarSkim(Collection<Zone> zones) {

ImaginaryNode aggregatedToNodes = MultiNodeDijkstra.createImaginaryNode(toNodes);

for (Zone origin : partition) {
for (Zone origin : partition) {
Node originNode = NetworkUtils.getNearestNode(carNetwork, matsimData.getZoneConnectorManager().getCoordsForZone(origin).get(0));
calculator.calcLeastCostPath(originNode, aggregatedToNodes, Properties.get().transportModel.peakHour_s, null, null);
for (Zone destination : zones) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,15 @@ public void endSimulation() {

private void runTransportModel(int year) {
logger.warn("Running MATSim transport model for year " + year + ".");

Scenario assembledScenario = scenarioAssembler.assembleScenario(initialMatsimConfig, year);
Scenario assembledScenario;
TravelTimes travelTimes = dataContainer.getTravelTimes();
if (year == properties.main.baseYear){
//if using the SimpleCommuteModeChoiceScenarioAssembler, we need some intial travel times (this will use an unlodaded network)
TravelTime myTravelTime = SiloMatsimUtils.getAnEmptyNetworkTravelTime();
TravelDisutility myTravelDisutility = SiloMatsimUtils.getAnEmptyNetworkTravelDisutility();
updateTravelTimes(myTravelTime, myTravelDisutility);
}
assembledScenario = scenarioAssembler.assembleScenario(initialMatsimConfig, year, travelTimes);

ConfigUtils.setVspDefaults(assembledScenario.getConfig());
finalizeConfig(assembledScenario.getConfig(), year);
Expand Down Expand Up @@ -268,4 +275,5 @@ private void updateTravelTimes(TravelTime travelTime, TravelDisutility disutilit
dataContainer.getGeoData().getZones().values());
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.tum.bgu.msm.matsim;

import de.tum.bgu.msm.container.DataContainer;
import de.tum.bgu.msm.data.Location;
import de.tum.bgu.msm.data.MicroLocation;
import de.tum.bgu.msm.data.Region;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@
import de.tum.bgu.msm.data.job.Job;
import de.tum.bgu.msm.data.job.JobDataManager;
import de.tum.bgu.msm.data.person.Occupation;
import de.tum.bgu.msm.data.person.Person;
import de.tum.bgu.msm.properties.Properties;
import de.tum.bgu.msm.utils.SiloUtil;
import org.apache.log4j.Logger;
import org.locationtech.jts.geom.Coordinate;
import org.matsim.api.core.v01.Coord;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.population.Activity;
import org.matsim.api.core.v01.population.Plan;
import org.matsim.api.core.v01.population.Population;
import org.matsim.api.core.v01.population.PopulationFactory;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.population.*;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.config.groups.PlanCalcScoreConfigGroup.ActivityParams;
import org.matsim.core.config.groups.VspExperimentalConfigGroup.VspDefaultsCheckingLevel;
import org.matsim.core.controler.OutputDirectoryHierarchy.OverwriteFileSetting;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.router.util.TravelDisutility;
import org.matsim.core.router.util.TravelTime;
import org.matsim.core.utils.collections.Tuple;
import org.matsim.facilities.ActivityFacilities;
import org.matsim.facilities.ActivityFacility;
import org.matsim.vehicles.Vehicle;

import java.util.Collection;
import java.util.Map;
Expand Down Expand Up @@ -88,4 +88,28 @@ public static void determineExtentOfFacilities(ActivityFacilities activityFacili
}
LOG.info("Extent of facilities is: xmin = " + xmin + "; xmax = " + xmax + "; ymin = " + ymin + "; ymax = " + ymax);
}

static TravelTime getAnEmptyNetworkTravelTime(){
return new TravelTime() {
@Override
public double getLinkTravelTime(Link link, double time, Person person, Vehicle vehicle) {
return link.getLength() / link.getFreespeed();
}
};
}

static TravelDisutility getAnEmptyNetworkTravelDisutility(){
return new TravelDisutility() {
@Override
public double getLinkTravelDisutility(Link link, double time, Person person, Vehicle vehicle) {
return link.getLength() / link.getFreespeed();
}

@Override
public double getLinkMinimumTravelDisutility(Link link) {
return link.getLength() / link.getFreespeed();
}
};

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package de.tum.bgu.msm.matsim;

import de.tum.bgu.msm.container.DataContainer;
import de.tum.bgu.msm.data.Location;
import de.tum.bgu.msm.data.Region;
import de.tum.bgu.msm.data.TravelTimesWrapper;
import de.tum.bgu.msm.data.Zone;
import de.tum.bgu.msm.data.dwelling.Dwelling;
import de.tum.bgu.msm.data.dwelling.RealEstateDataManager;
import de.tum.bgu.msm.data.household.Household;
import de.tum.bgu.msm.data.household.HouseholdDataManager;
import de.tum.bgu.msm.data.job.Job;
import de.tum.bgu.msm.data.job.JobDataManager;
import de.tum.bgu.msm.data.person.Occupation;
import de.tum.bgu.msm.data.person.Person;
import de.tum.bgu.msm.data.travelTimes.SkimTravelTimes;
import de.tum.bgu.msm.data.travelTimes.TravelTimes;
import de.tum.bgu.msm.models.modeChoice.CommuteModeChoice;
import de.tum.bgu.msm.models.modeChoice.CommuteModeChoiceMapping;
import de.tum.bgu.msm.properties.Properties;
import de.tum.bgu.msm.util.matrices.IndexedDoubleMatrix2D;
import de.tum.bgu.msm.utils.SiloUtil;
import de.tum.bgu.msm.utils.TravelTimeUtil;
import org.apache.log4j.Logger;
import org.locationtech.jts.geom.Coordinate;
import org.matsim.api.core.v01.Coord;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.population.Activity;
import org.matsim.api.core.v01.population.Plan;
import org.matsim.api.core.v01.population.Population;
import org.matsim.api.core.v01.population.PopulationFactory;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.config.groups.PlanCalcScoreConfigGroup;
import org.matsim.core.config.groups.VspExperimentalConfigGroup;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.router.util.TravelTime;
import org.matsim.core.scenario.MutableScenario;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.vehicles.Vehicle;

public class SimpleCommuteModeChoiceMatsimScenarioAssembler implements MatsimScenarioAssembler {

private final static Logger logger = Logger.getLogger(SimpleMatsimScenarioAssembler.class);

private final DataContainer dataContainer;
private CommuteModeChoice commuteModeChoice;

private final Properties properties;

public SimpleCommuteModeChoiceMatsimScenarioAssembler(DataContainer dataContainer, Properties properties, CommuteModeChoice commuteModeChoice) {
this.dataContainer = dataContainer;
this.commuteModeChoice = commuteModeChoice;
this.properties = properties;
}

private Population generateDemand(TravelTimes travelTimes) {
logger.info("Starting creating a MATSim population.");

HouseholdDataManager householdDataManager = dataContainer.getHouseholdDataManager();

Population matsimPopulation = PopulationUtils.createPopulation(ConfigUtils.createConfig());
PopulationFactory matsimPopulationFactory = matsimPopulation.getFactory();

JobDataManager jobDataManager = dataContainer.getJobDataManager();

RealEstateDataManager realEstateDataManager = dataContainer.getRealEstateDataManager();

for(Household household: householdDataManager.getHouseholds()) {
if (SiloUtil.getRandomNumberAsDouble() > properties.transportModel.matsimScaleFactor) {
// e.g. if scalingFactor = 0.01, there will be a 1% chance that the loop is not
// continued in the next step, i.e. that the person is added to the population
continue;
}
Dwelling dwelling = realEstateDataManager.getDwelling(household.getDwellingId());
CommuteModeChoiceMapping commuteModeChoiceMapping = commuteModeChoice.assignCommuteModeChoice(
dwelling, travelTimes, household);

for(Person person: household.getPersons().values()) {
if (person.getOccupation() != Occupation.EMPLOYED || person.getJobId() == -2) { // i.e. person does not work
continue;
}
if(commuteModeChoiceMapping.getMode(person).mode.equals(TransportMode.car)) {

Coordinate dwellingCoordinate;
if (dwelling != null && dwelling.getCoordinate() != null) {
dwellingCoordinate = dwelling.getCoordinate();
} else {
dwellingCoordinate = dataContainer.getGeoData().getZones().get(dwelling.getZoneId()).getRandomCoordinate(SiloUtil.getRandomObject());
}
Coord dwellingCoord = new Coord(dwellingCoordinate.x, dwellingCoordinate.y);

Job job = jobDataManager.getJobFromId(person.getJobId());
Coordinate jobCoordinate;
if (job != null && job.getCoordinate() != null) {
jobCoordinate = job.getCoordinate();
} else {
jobCoordinate = dataContainer.getGeoData().getZones().get(job.getZoneId()).getRandomCoordinate(SiloUtil.getRandomObject());
}
Coord jobCoord = new Coord(jobCoordinate.x, jobCoordinate.y);

org.matsim.api.core.v01.population.Person matsimPerson =
matsimPopulationFactory.createPerson(Id.create(person.getId(), org.matsim.api.core.v01.population.Person.class));
matsimPopulation.addPerson(matsimPerson);

Plan matsimPlan = matsimPopulationFactory.createPlan();
matsimPerson.addPlan(matsimPlan);

Activity activity1 = matsimPopulationFactory.createActivityFromCoord("home", dwellingCoord);
Integer departureTime = defineDepartureFromHome(job);
activity1.setEndTime(departureTime);
matsimPlan.addActivity(activity1);
matsimPlan.addLeg(matsimPopulationFactory.createLeg(TransportMode.car));

Activity activity2 = matsimPopulationFactory.createActivityFromCoord("work", jobCoord);
activity2.setEndTime(defineWorkEndTime(job, departureTime));
matsimPlan.addActivity(activity2);
matsimPlan.addLeg(matsimPopulationFactory.createLeg(TransportMode.car));

Activity activity3 = matsimPopulationFactory.createActivityFromCoord("home", dwellingCoord);

matsimPlan.addActivity(activity3);
}
}

}
logger.info("Finished creating a MATSim population.");
return matsimPopulation;
}

/**
* Defines departure time from home. Note that it actually tries to use job start times if defined. Otherwise
* randomly draws from a normal distribution around the peak hour with 1 hour standard deviation.
*/
private Integer defineDepartureFromHome(Job job) {
return job.getStartTimeInSeconds().orElse(Math.max(0, (int) (properties.transportModel.peakHour_s + SiloUtil.getRandomObject().nextGaussian() * 3600)));
}

/**
* Defines departure time from work. Note that it actually tries to use job duration times if defined. Otherwise
* randomly draws from a normal distribution with mean of 8 hours with 1 hour standard deviation. The duration
* is then added to the job starting time.
*/
private int defineWorkEndTime(Job job, int departureTime) {
return departureTime + job.getWorkingTimeInSeconds().orElse(Math.max(0, (int) (8*3600 + SiloUtil.getRandomObject().nextGaussian() * 3600)));
}

@Override
public Scenario assembleScenario(Config initialMatsimConfig, int year, TravelTimes travelTimes) {
Config config = createMatsimConfig(initialMatsimConfig);

// if(dataContainer.getTravelTimes() instanceof SkimTravelTimes) {
//
// }
// if(year == properties.main.startYear) {
// logger.info("Referring to the simple MATSim scenario assembler for the base year as no travel times are available yet.");
// return new SimpleMatsimScenarioAssembler(dataContainer, properties).assembleScenario(initialMatsimConfig, year);
// } else {
// travelTimes = dataContainer.getTravelTimes();
// }


MutableScenario scenario = (MutableScenario) ScenarioUtils.loadScenario(config);
Population population = generateDemand(travelTimes);
scenario.setPopulation(population);

return scenario;
}

private Config createMatsimConfig(Config initialConfig) {
logger.info("Stating creating a MATSim config.");
Config config = ConfigUtils.loadConfig(initialConfig.getContext());
config.qsim().setFlowCapFactor(properties.transportModel.matsimScaleFactor);
config.qsim().setStorageCapFactor(properties.transportModel.matsimScaleFactor);

// TODO Add some switch here like "autoGenerateSimplePlans" or similar...
PlanCalcScoreConfigGroup.ActivityParams homeActivity = new PlanCalcScoreConfigGroup.ActivityParams("home");
homeActivity.setTypicalDuration(12*60*60);
config.planCalcScore().addActivityParams(homeActivity);

PlanCalcScoreConfigGroup.ActivityParams workActivity = new PlanCalcScoreConfigGroup.ActivityParams("work");
workActivity.setTypicalDuration(8*60*60);
config.planCalcScore().addActivityParams(workActivity);

config.vspExperimental().setVspDefaultsCheckingLevel(VspExperimentalConfigGroup.VspDefaultsCheckingLevel.warn);

logger.info("Finished creating a MATSim config.");
return config;
}



}


Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import de.tum.bgu.msm.data.job.JobDataManager;
import de.tum.bgu.msm.data.person.Occupation;
import de.tum.bgu.msm.data.person.Person;
import de.tum.bgu.msm.data.travelTimes.TravelTimes;
import de.tum.bgu.msm.properties.Properties;
import de.tum.bgu.msm.utils.SiloUtil;
import org.apache.log4j.Logger;
Expand Down Expand Up @@ -42,7 +43,7 @@ public SimpleMatsimScenarioAssembler(DataContainer dataContainer, Properties pro
this.scalingFactor = properties.transportModel.matsimScaleFactor;
}

private Population generateDemand() {
private Population generateDemand(TravelTimes travelTimes) {
logger.info("Starting creating a MATSim population.");

HouseholdDataManager householdDataManager = dataContainer.getHouseholdDataManager();
Expand Down Expand Up @@ -130,11 +131,11 @@ private Population generateDemand() {
}

@Override
public Scenario assembleScenario(Config initialMatsimConfig, int year) {
public Scenario assembleScenario(Config initialMatsimConfig, int year, TravelTimes travelTimes) {
Config config = createMatsimConfig(initialMatsimConfig);

MutableScenario scenario = (MutableScenario) ScenarioUtils.loadScenario(config);
Population population = generateDemand();
Population population = generateDemand(travelTimes);
scenario.setPopulation(population);

return scenario;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ public static ZoneConnectorManager createWeightedZoneConnectors(
for (Dwelling dwelling : dwellings) {
final Coordinate coordinate = dwelling.getCoordinate();
final Household household = householdData.getHouseholdFromId(dwelling.getResidentId());
int weight = 1;
if(household!=null) {
int weight = household.getHhSize();
weight += household.getHhSize();
xSum += (weight * coordinate.x);
ySum += (weight * coordinate.y);
weightedCount += weight;
}
weightedCount += weight;
}
double avgX = xSum / weightedCount;
double avgY = ySum / weightedCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public MitoMatsimScenarioAssembler(DataContainer dataContainer,
}

@Override
public Scenario assembleScenario(Config initialMatsimConfig, int year) {
public Scenario assembleScenario(Config initialMatsimConfig, int year, TravelTimes travelTimes) {

logger.info(" Running travel demand model MITO for the year " + year);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public TravelTimes getDelegate() {
private final Properties properties;
private final GeoData geoData;

public TravelTimesWrapper(TravelTimes travelTimes, Properties properties, GeoData geoData){
public TravelTimesWrapper(TravelTimes travelTimes, Properties properties, GeoData geoData) {
delegate = travelTimes;
this.properties = properties;
this.geoData = geoData;
Expand Down Expand Up @@ -55,7 +55,7 @@ public TravelTimes duplicate() {

@Override
public void setup() {
if (delegate instanceof SkimTravelTimes){
if (delegate instanceof SkimTravelTimes) {
updateSkims(properties.main.startYear);
}
}
Expand All @@ -78,7 +78,7 @@ public void endSimulation() {
}

private void updateSkims(int year) {
if(properties.transportModel.transportModelIdentifier != TransportModelPropertiesModule.TransportModelIdentifier.MATSIM) {
if (properties.transportModel.transportModelIdentifier == TransportModelPropertiesModule.TransportModelIdentifier.NONE) {
TravelTimeUtil.updateCarSkim((SkimTravelTimes) delegate, year, properties);
TravelTimeUtil.updateTransitSkim((SkimTravelTimes) delegate, year, properties);
((SkimTravelTimes) delegate).updateRegionalTravelTimes(geoData.getRegions().values(), geoData.getZones().values());
Expand Down
Loading

0 comments on commit 59334e9

Please sign in to comment.