Skip to content

Commit

Permalink
clean up matsim transport model, pull matsim accessiblity into matsim…
Browse files Browse the repository at this point in the history
… package, factor out accessibility related code into its separate class
  • Loading branch information
nkuehnel committed Dec 6, 2019
1 parent 1652b72 commit 319c396
Show file tree
Hide file tree
Showing 15 changed files with 247 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public final class MatsimData {
private final Properties properties;
private Config config;

private Network carNetwork;
private Network ptNetwork;
private final Network carNetwork;
private final Network ptNetwork;
private final TransitSchedule schedule;

private TransitSchedule schedule;
private RaptorParameters raptorParameters;
private DefaultRaptorParametersForPerson parametersForPerson;
private LeastCostRaptorRouteSelector routeSelector;
Expand All @@ -50,10 +50,11 @@ public final class MatsimData {

public MatsimData(Config config, Properties properties,
ZoneConnectorManager.ZoneConnectorMethod method,
DataContainer dataContainer) {
DataContainer dataContainer, Network network, TransitSchedule schedule) {
this.config = config;
this.raptorParameters = RaptorUtils.createParameters(config);
this.properties = properties;
this.schedule = schedule;
final Collection<Zone> zones = dataContainer.getGeoData().getZones().values();
switch (method) {
case RANDOM:
Expand All @@ -67,39 +68,37 @@ public MatsimData(Config config, Properties properties,
default:
throw new RuntimeException("No valid zone connector method defined!");
}

TransportModeNetworkFilter filter = new TransportModeNetworkFilter(network);

Set<String> car = Sets.newHashSet(TransportMode.car);
Set<String> pt = Sets.newHashSet(TransportMode.pt);

Network carNetwork = NetworkUtils.createNetwork();
filter.filter(carNetwork, car);

Network ptNetwork = NetworkUtils.createNetwork();
filter.filter(ptNetwork, pt);

this.carNetwork = carNetwork;
this.ptNetwork = ptNetwork;
}

ZoneConnectorManager getZoneConnectorManager() {
return zoneConnectorManager;
}

Network getCarNetwork() {
public Network getCarNetwork() {
return carNetwork;
}

Network getPtNetwork() {
return ptNetwork;
}

public void update(Network network, TransitSchedule schedule,
TravelDisutility travelDisutility, TravelTime travelTime) {
public void update(TravelDisutility travelDisutility, TravelTime travelTime) {
this.travelDisutility = travelDisutility;
this.travelTime = travelTime;
this.schedule = schedule;

TransportModeNetworkFilter filter = new TransportModeNetworkFilter(network);

Set<String> car = Sets.newHashSet(TransportMode.car);
Set<String> pt = Sets.newHashSet(TransportMode.pt);

Network carNetwork = NetworkUtils.createNetwork();
filter.filter(carNetwork, car);

Network ptNetwork = NetworkUtils.createNetwork();
filter.filter(ptNetwork, pt);

this.carNetwork = carNetwork;
this.ptNetwork = ptNetwork;

this.leastCostPathCalculatorFactory = new FastAStarLandmarksFactory(properties.main.numberOfThreads);

Expand All @@ -118,7 +117,6 @@ public void update(Network network, TransitSchedule schedule,
null);
routeSelector = new LeastCostRaptorRouteSelector();
}

}

MultiNodePathCalculator createMultiNodePathCalculator() {
Expand Down Expand Up @@ -176,4 +174,7 @@ RaptorParameters getRaptorParameters() {
return raptorParameters;
}

public TransitSchedule getSchedule() {
return schedule;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,26 @@


import de.tum.bgu.msm.container.DataContainer;
import de.tum.bgu.msm.data.Zone;
import de.tum.bgu.msm.data.accessibility.MatsimAccessibility;
import de.tum.bgu.msm.data.household.HouseholdUtil;
import de.tum.bgu.msm.data.travelTimes.SkimTravelTimes;
import de.tum.bgu.msm.data.travelTimes.TravelTimes;
import de.tum.bgu.msm.matsim.accessibility.MatsimAccessibility;
import de.tum.bgu.msm.models.transportModel.TransportModel;
import de.tum.bgu.msm.properties.Properties;
import de.tum.bgu.msm.properties.modules.TransportModelPropertiesModule;
import org.apache.log4j.Logger;
import org.locationtech.jts.geom.Geometry;
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.Network;
import org.matsim.api.core.v01.network.Node;
import org.matsim.contrib.accessibility.AccessibilityAttributes;
import org.matsim.contrib.accessibility.AccessibilityConfigGroup;
import org.matsim.contrib.accessibility.AccessibilityModule;
import org.matsim.contrib.dvrp.trafficmonitoring.TravelTimeUtils;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.config.groups.FacilitiesConfigGroup;
import org.matsim.core.controler.Controler;
import org.matsim.core.controler.ControlerDefaults;
import org.matsim.core.controler.OutputDirectoryHierarchy;
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.router.FastAStarLandmarksFactory;
import org.matsim.core.router.TripRouterFactoryBuilderWithDefaults;
import org.matsim.core.router.util.TravelDisutility;
import org.matsim.core.router.util.TravelTime;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.core.utils.geometry.CoordUtils;
import org.matsim.facilities.*;
import org.matsim.pt.transitSchedule.api.TransitSchedule;
import org.opengis.feature.simple.SimpleFeature;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

/**
* @author dziemke, nkuehnel
Expand All @@ -74,18 +53,14 @@ public final class MatsimTransportModel implements TransportModel {

private final MatsimData matsimData;
private final MatsimTravelTimes internalTravelTimes;
private Scenario scenario;

private final DataContainer dataContainer;

private ActivityFacilities zoneRepresentativeCoords;
private MatsimAccessibility accessibility;

private MatsimScenarioAssembler scenarioAssembler;

public MatsimTransportModel(DataContainer dataContainer, Config matsimConfig,
Properties properties, MatsimAccessibility accessibility,
ZoneConnectorManager.ZoneConnectorMethod method, MatsimScenarioAssembler scenarioAssembler) {
Properties properties, MatsimScenarioAssembler scenarioAssembler,
MatsimData matsimData) {
this.dataContainer = Objects.requireNonNull(dataContainer);
this.initialMatsimConfig = Objects.requireNonNull(matsimConfig,
"No initial matsim config provided to SiloModel class!");
Expand All @@ -96,32 +71,15 @@ public MatsimTransportModel(DataContainer dataContainer, Config matsimConfig,
} else {
this.internalTravelTimes = new MatsimTravelTimes(matsimConfig);
}
this.matsimData = new MatsimData(matsimConfig, properties, method, dataContainer);
this.matsimData = matsimData;
this.scenarioAssembler = scenarioAssembler;

this.properties = properties;
this.accessibility = accessibility;
}

@Override
public void setup() {
scenario = ScenarioUtils.loadScenario(initialMatsimConfig);
Network network = scenario.getNetwork();
internalTravelTimes.initialize(dataContainer.getGeoData(), matsimData);

logger.warn("Finding coordinates that represent a given zone.");
zoneRepresentativeCoords = FacilitiesUtils.createActivityFacilities();
ActivityFacilitiesFactory aff = new ActivityFacilitiesFactoryImpl();
Map<Integer, Zone> zoneMap = dataContainer.getGeoData().getZones();
for (int zoneId : zoneMap.keySet()) {
Geometry geometry = (Geometry) zoneMap.get(zoneId).getZoneFeature().getDefaultGeometry();
Coord centroid = CoordUtils.createCoord(geometry.getCentroid().getX(), geometry.getCentroid().getY());
Node nearestNode = NetworkUtils.getNearestNode(network, centroid); // TODO choose road of certain category
Coord coord = CoordUtils.createCoord(nearestNode.getCoord().getX(), nearestNode.getCoord().getY());
ActivityFacility activityFacility = aff.createActivityFacility(Id.create(zoneId, ActivityFacility.class), coord);
zoneRepresentativeCoords.addActivityFacility(activityFacility);
}

if (properties.transportModel.matsimInitialEventsFile == null) {
runTransportModel(properties.main.startYear);
} else {
Expand Down Expand Up @@ -163,10 +121,6 @@ private void runTransportModel(int year) {

final Controler controler = new Controler(assembledScenario);

if (accessibility != null) {
setupAccessibility(assembledScenario, controler);
}

controler.run();
logger.warn("Running MATSim transport model for year " + year + " finished.");

Expand All @@ -190,83 +144,18 @@ private void finalizeConfig(Config config, int year) {
config.vspExperimental().setWritingOutputEvents(true);
}

private void setupAccessibility(Scenario scenario, Controler controler) {
// Opportunities
Map<Integer, Integer> populationMap = HouseholdUtil.getPopulationByZoneAsMap(dataContainer);
Map<Id<ActivityFacility>, Integer> zonePopulationMap = new TreeMap<>();
for (int zoneId : populationMap.keySet()) {
zonePopulationMap.put(Id.create(zoneId, ActivityFacility.class), populationMap.get(zoneId));
}
final ActivityFacilities opportunities = scenario.getActivityFacilities();
int i = 0;
for (ActivityFacility activityFacility : zoneRepresentativeCoords.getFacilities().values()) {
activityFacility.getAttributes().putAttribute(AccessibilityAttributes.WEIGHT, zonePopulationMap.get(activityFacility.getId()));
opportunities.addActivityFacility(activityFacility);
i++;
}
logger.warn(i + " facilities added as opportunities.");

SiloMatsimUtils.determineExtentOfFacilities(zoneRepresentativeCoords);

scenario.getConfig().facilities().setFacilitiesSource(FacilitiesConfigGroup.FacilitiesSource.setInScenario);
// End opportunities

// Accessibility settings
AccessibilityConfigGroup acg = ConfigUtils.addOrGetModule(scenario.getConfig(), AccessibilityConfigGroup.class);
acg.setMeasuringPointsFacilities(zoneRepresentativeCoords);
//
Map<Id<ActivityFacility>, Geometry> measurePointGeometryMap = new TreeMap<>();
Map<Integer, SimpleFeature> zoneFeatureMap = new HashMap<>();
for (Zone zone : dataContainer.getGeoData().getZones().values()) {
zoneFeatureMap.put(zone.getId(), zone.getZoneFeature());
}

for (Integer zoneId : zoneFeatureMap.keySet()) {
SimpleFeature feature = zoneFeatureMap.get(zoneId);
Geometry geometry = (Geometry) feature.getDefaultGeometry();
measurePointGeometryMap.put(Id.create(zoneId, ActivityFacility.class), geometry);
}
acg.setMeasurePointGeometryProvision(AccessibilityConfigGroup.MeasurePointGeometryProvision.fromShapeFile);
acg.setMeasurePointGeometryMap(measurePointGeometryMap);

acg.setTileSize_m(1000); // TODO This is only a dummy value here
//
acg.setAreaOfAccessibilityComputation(AccessibilityConfigGroup.AreaOfAccesssibilityComputation.fromFacilitiesObject);
acg.setUseOpportunityWeights(true);
acg.setWeightExponent(Properties.get().accessibility.alphaAuto); // TODO Need differentiation for different modes
logger.warn("Properties.get().accessibility.alphaAuto = " + Properties.get().accessibility.alphaAuto);
acg.setAccessibilityMeasureType(AccessibilityConfigGroup.AccessibilityMeasureType.rawSum);
// End accessibility settings
// Accessibility module

AccessibilityModule module = new AccessibilityModule();
module.addFacilityDataExchangeListener(accessibility);
controler.addOverridingModule(module);
// End accessibility module
}

/**
* @param eventsFile
*/
private void replayFromEvents(String eventsFile) {
Scenario scenario = ScenarioUtils.loadScenario(initialMatsimConfig);
TravelTime travelTime = TravelTimeUtils.createTravelTimesFromEvents(scenario, eventsFile);
TravelDisutility travelDisutility = ControlerDefaults.createDefaultTravelDisutilityFactory(scenario).createTravelDisutility(travelTime);
TripRouterFactoryBuilderWithDefaults builder = new TripRouterFactoryBuilderWithDefaults();
builder.setLeastCostPathCalculatorFactory(new FastAStarLandmarksFactory(properties.main.numberOfThreads));
builder.setTravelTime(travelTime);
builder.setTravelDisutility(travelDisutility);
updateTravelTimes(travelTime, travelDisutility);
}


private void updateTravelTimes(TravelTime travelTime, TravelDisutility disutility) {
Network network = scenario.getNetwork();
TransitSchedule schedule = null;
if (scenario.getConfig().transit().isUseTransit()) {
schedule = scenario.getTransitSchedule();
}
matsimData.update(network, schedule, disutility, travelTime);
matsimData.update(disutility, travelTime);
internalTravelTimes.update(matsimData);
final TravelTimes mainTravelTimes = dataContainer.getTravelTimes();

Expand All @@ -279,5 +168,4 @@ private void updateTravelTimes(TravelTime travelTime, TravelDisutility disutilit
dataContainer.getGeoData().getZones().values());
}
}

}
Loading

0 comments on commit 319c396

Please sign in to comment.