Skip to content

Commit

Permalink
Merge pull request #269 from msmobility/cleanUpMatsimTransportModel
Browse files Browse the repository at this point in the history
cleanUpMatsimTransportModel
  • Loading branch information
mergify[bot] authored Dec 6, 2019
2 parents 0bf442e + 3a6df65 commit fc96a6a
Show file tree
Hide file tree
Showing 24 changed files with 788 additions and 648 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.matsim.core.network.algorithms.TransportModeNetworkFilter;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.router.*;
import org.matsim.core.router.costcalculators.FreespeedTravelTimeAndDisutility;
import org.matsim.core.router.util.LeastCostPathCalculator;
import org.matsim.core.router.util.LeastCostPathCalculatorFactory;
import org.matsim.core.router.util.TravelDisutility;
Expand All @@ -32,10 +33,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 +51,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 +69,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,26 +118,31 @@ public void update(Network network, TransitSchedule schedule,
null);
routeSelector = new LeastCostRaptorRouteSelector();
}

}

MultiNodePathCalculator createMultiNodePathCalculator() {
return (MultiNodePathCalculator) multiNodeFactory.createPathCalculator(carNetwork, travelDisutility, travelTime);
}

MultiNodePathCalculator createFreeSpeedMultiNodePathCalculator() {
FreespeedTravelTimeAndDisutility freespeed = new FreespeedTravelTimeAndDisutility(config.planCalcScore());
return (MultiNodePathCalculator) multiNodeFactory.createPathCalculator(carNetwork, freespeed, freespeed);
}

TripRouter createTripRouter() {

final RoutingModule networkRoutingModule = DefaultRoutingModules.createPureNetworkRouter(
TransportMode.car, PopulationUtils.getFactory(), carNetwork, leastCostPathCalculatorFactory.createPathCalculator(carNetwork, travelDisutility, travelTime));
final RoutingModule teleportationRoutingModule = DefaultRoutingModules.createTeleportationRouter(
TransportMode.walk, PopulationUtils.getFactory(), config.plansCalcRoute().getOrCreateModeRoutingParams(TransportMode.walk));
final RoutingModule ptRoutingModule;

if (schedule != null) {
if (schedule != null && config.transit().isUseTransit()) {
final RoutingModule teleportationRoutingModule = DefaultRoutingModules.createTeleportationRouter(
TransportMode.walk, PopulationUtils.getFactory(), config.plansCalcRoute().getOrCreateModeRoutingParams(TransportMode.walk));
final SwissRailRaptor swissRailRaptor = createSwissRailRaptor(RaptorStaticConfig.RaptorOptimization.OneToOneRouting);
ptRoutingModule = new SwissRailRaptorRoutingModule(swissRailRaptor, schedule, ptNetwork, teleportationRoutingModule);
} else {
ptRoutingModule = teleportationRoutingModule;
ptRoutingModule = DefaultRoutingModules.createPseudoTransitRouter(TransportMode.pt, PopulationUtils.getFactory(), carNetwork,
leastCostPathCalculatorFactory.createPathCalculator(carNetwork, travelDisutility, travelTime), config.plansCalcRoute().getOrCreateModeRoutingParams(TransportMode.pt));
}

TripRouter.Builder bd = new TripRouter.Builder(config);
Expand All @@ -161,8 +166,13 @@ LeastCostPathCalculator createLeastCostPathCalculator() {
return leastCostPathCalculatorFactory.createPathCalculator(carNetwork, travelDisutility, travelTime);
}

SwissRailRaptorData getRaptorData(RaptorStaticConfig.RaptorOptimization optimitzaion) {
switch (optimitzaion) {
RoutingModule getTeleportationRouter(String mode) {
return DefaultRoutingModules.createTeleportationRouter(
mode, PopulationUtils.getFactory(), config.plansCalcRoute().getOrCreateModeRoutingParams(mode));
}

SwissRailRaptorData getRaptorData(RaptorStaticConfig.RaptorOptimization optimization) {
switch (optimization) {
case OneToAllRouting:
return raptorDataOneToAll;
case OneToOneRouting:
Expand All @@ -176,4 +186,7 @@ RaptorParameters getRaptorParameters() {
return raptorParameters;
}

public TransitSchedule getSchedule() {
return schedule;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.network.Network;
import org.matsim.api.core.v01.network.Node;
import org.matsim.api.core.v01.population.Leg;
import org.matsim.api.core.v01.population.PlanElement;
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.router.ImaginaryNode;
import org.matsim.core.router.InitialNode;
import org.matsim.core.router.MultiNodeDijkstra;
import org.matsim.core.router.MultiNodePathCalculator;
import org.matsim.core.router.*;
import org.matsim.core.utils.geometry.CoordUtils;
import org.matsim.facilities.ActivityFacilitiesFactory;
import org.matsim.facilities.ActivityFacilitiesFactoryImpl;
Expand Down Expand Up @@ -57,7 +56,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 Expand Up @@ -162,7 +161,7 @@ public IndexedDoubleMatrix2D createPtSkim(Collection<Zone> zones) {
return skim;
}

public IndexedDoubleMatrix2D createTeleportedSkim(TravelTimes travelTimes, String mode, Collection<Zone> zones) {
public IndexedDoubleMatrix2D createTeleportedSkim(Collection<Zone> zones, String mode) {

final int partitionSize = (int) ((double) zones.size() / (Properties.get().main.numberOfThreads)) + 1;
Iterable<List<Zone>> partitions = Iterables.partition(zones, partitionSize);
Expand All @@ -173,10 +172,72 @@ public IndexedDoubleMatrix2D createTeleportedSkim(TravelTimes travelTimes, Strin
for (final List<Zone> partition : partitions) {
executor.addTaskToQueue(() -> {
try {
TravelTimes copy = travelTimes.duplicate();
final RoutingModule teleportationRouter = matsimData.getTeleportationRouter(mode);
for (Zone origin : partition) {
for (Zone destination : zones) {
Coord originCoord = matsimData.getZoneConnectorManager().getCoordsForZone(origin).get(0);
Coord destinationCoord = matsimData.getZoneConnectorManager().getCoordsForZone(destination).get(0);

ActivityFacilitiesFactoryImpl activityFacilitiesFactory = new ActivityFacilitiesFactoryImpl();
Facility fromFacility = ((ActivityFacilitiesFactory) activityFacilitiesFactory).createActivityFacility(Id.create(1, ActivityFacility.class), originCoord);
Facility toFacility = ((ActivityFacilitiesFactory) activityFacilitiesFactory).createActivityFacility(Id.create(2, ActivityFacility.class), destinationCoord);
final double peakHour_s = Properties.get().transportModel.peakHour_s;
List<? extends PlanElement> planElements = teleportationRouter.calcRoute(fromFacility, toFacility, peakHour_s, null);
double arrivalTime = peakHour_s;

if (!planElements.isEmpty()) {
final Leg lastLeg = (Leg) planElements.get(planElements.size() - 1);
arrivalTime = lastLeg.getDepartureTime() + lastLeg.getTravelTime();
}

double time = arrivalTime - peakHour_s;

//convert to minutes
time /= 60.;
skim.setIndexed(origin.getZoneId(), destination.getZoneId(), time);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
});
}
executor.execute();
assignIntrazonals(5, Float.MAX_VALUE, 0.66f, skim);
return skim;
}

public IndexedDoubleMatrix2D createFreeSpeedFactorSkim(Collection<Zone> zones, double factor) {
final int partitionSize = (int) ((double) zones.size() / (Properties.get().main.numberOfThreads)) + 1;
Iterable<List<Zone>> partitions = Iterables.partition(zones, partitionSize);

IndexedDoubleMatrix2D skim = new IndexedDoubleMatrix2D(zones, zones);
Network carNetwork = matsimData.getCarNetwork();
ConcurrentExecutor<Void> executor = ConcurrentExecutor.fixedPoolService(Properties.get().main.numberOfThreads);
for (final List<Zone> partition : partitions) {
executor.addTaskToQueue(() -> {
try {
MultiNodePathCalculator calculator = matsimData.createFreeSpeedMultiNodePathCalculator();
Set<InitialNode> toNodes = new HashSet<>();
for (Zone zone : zones) {
for (Coord coord : matsimData.getZoneConnectorManager().getCoordsForZone(zone)) {
Node originNode = NetworkUtils.getNearestNode(carNetwork, coord);
toNodes.add(new InitialNode(originNode, 0., 0.));
}
}

ImaginaryNode aggregatedToNodes = MultiNodeDijkstra.createImaginaryNode(toNodes);

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) {
double travelTime = copy.getTravelTime(origin, destination, Properties.get().transportModel.peakHour_s, mode);
Node destinationNode = NetworkUtils.getNearestNode(carNetwork, matsimData.getZoneConnectorManager().getCoordsForZone(destination).get(0));
double travelTime = calculator.constructPath(originNode, destinationNode, Properties.get().transportModel.peakHour_s).travelTime;

//adjust by factor
travelTime *= factor;

//convert to minutes
travelTime /= 60.;
Expand Down
Loading

0 comments on commit fc96a6a

Please sign in to comment.