Skip to content

Commit

Permalink
only use direct teleportation for unknown modes, for pt -> use freesp…
Browse files Browse the repository at this point in the history
…eed factor travel times based on car network if no schedule is provided
  • Loading branch information
nkuehnel committed Dec 6, 2019
1 parent 319c396 commit e70bdf5
Show file tree
Hide file tree
Showing 11 changed files with 544 additions and 465 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 Down Expand Up @@ -123,19 +124,25 @@ 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 @@ -159,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 Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,8 @@ public void update(MatsimData matsimData) {

private void updateSkims() {
logger.info("Updating car and pt skim.");
final MatsimSkimCreator matsimSkimCreator = new MatsimSkimCreator(matsimData);
IndexedDoubleMatrix2D skimCar = matsimSkimCreator.createCarSkim(zones.values());
skimsByMode.put(TransportMode.car, skimCar);
if(config.transit().isUseTransit()) {
IndexedDoubleMatrix2D skimPt = matsimSkimCreator.createPtSkim(zones.values());
skimsByMode.put(TransportMode.pt, skimPt);
} else {
IndexedDoubleMatrix2D skimPt = matsimSkimCreator.createTeleportedSkim(this, TransportMode.pt, zones.values());
skimsByMode.put(TransportMode.pt, skimPt);
}
getPeakSkim(TransportMode.car);
getPeakSkim(TransportMode.pt);
}

private void updateRegionalTravelTimes() {
Expand Down Expand Up @@ -186,11 +178,14 @@ public IndexedDoubleMatrix2D getPeakSkim(String mode) {
skim = matsimSkimCreator.createPtSkim(zones.values());
break;
} else {
logger.warn("No schedule/ network provided for pt.");
logger.warn("No schedule/ network provided for pt. Will use freespeed factor.");
skim = matsimSkimCreator.createFreeSpeedFactorSkim(zones.values(),
config.plansCalcRoute().getModeRoutingParams().get(TransportMode.pt).getTeleportedModeFreespeedFactor());
break;
}
default:
logger.warn("Defaulting to teleportation.");
skim = matsimSkimCreator.createTeleportedSkim(this, mode, zones.values());
skim = matsimSkimCreator.createTeleportedSkim(zones.values(), mode);
}
skimsByMode.put(mode, skim);
logger.info("Obtained skim for mode " + mode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public MovesModelImpl(DataContainer dataContainer, Properties properties, MovesS
@Override
public void setup() {
housingStrategy.setup();
String header = new StringJoiner(",").add("hh").add("oldDdd").add("newDd").toString();
String header = new StringJoiner(",").add("hh").add("oldDdd").add("newDd").add("oldZone").add("newZone").toString();
Path basePath = Paths.get(properties.main.baseDirectory).resolve("scenOutput").resolve(properties.main.scenarioName).resolve("siloResults/relocation");
relocationTracker = new YearByYearCsvModelTracker(basePath, "relocation", header);
}
Expand Down Expand Up @@ -137,22 +137,27 @@ public boolean handleEvent(MoveEvent event) {
return false;
}

final int idOldDd = household.getDwellingId();
// Step 2: Choose new dwelling
int idNewDD = searchForNewDwelling(household);

if (idNewDD > 0) {

// Step 3: Move household
dataContainer.getHouseholdDataManager().saveHouseholdMemento(household);
int oldZoneId = dataContainer.getRealEstateDataManager().getDwelling(idOldDd).getZoneId();
int newZoneId = dataContainer.getRealEstateDataManager().getDwelling(idNewDD).getZoneId();
relocationTracker.trackRecord(new StringJoiner(",")
.add(String.valueOf(hhId))
.add(String.valueOf(household.getDwellingId()))
.add(String.valueOf(idOldDd))
.add(String.valueOf(idNewDD))
.add(String.valueOf(oldZoneId))
.add(String.valueOf(newZoneId))
.toString());
moveHousehold(household, household.getDwellingId(), idNewDD);
moveHousehold(household, idOldDd, idNewDD);
if (hhId == SiloUtil.trackHh) {
SiloUtil.trackWriter.println("Household " + hhId + " has moved to dwelling " +
household.getDwellingId());
idOldDd);
}
return true;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static ModelContainer getModelContainerForMstm(DataContainer dataContaine
MatsimData matsimData = null;
if (config != null) {
final Scenario scenario = ScenarioUtils.loadScenario(config);
matsimData = new MatsimData(config, properties, ZoneConnectorManager.ZoneConnectorMethod.WEIGHTED_BY_POPULATION, dataContainer, scenario.getNetwork(), scenario.getTransitSchedule());
matsimData = new MatsimData(config, properties, ZoneConnectorManager.ZoneConnectorMethod.RANDOM, dataContainer, scenario.getNetwork(), scenario.getTransitSchedule());
}
final SimpleMatsimScenarioAssembler scenarioAssembler = new SimpleMatsimScenarioAssembler(dataContainer, properties);
transportModel = new MatsimTransportModel(dataContainer, config, properties, scenarioAssembler, matsimData);
Expand Down
Loading

0 comments on commit e70bdf5

Please sign in to comment.