Skip to content

Commit

Permalink
Generalised diary methods to work with Manchester and Melbourne, upda…
Browse files Browse the repository at this point in the history
…ted route code & writers
  • Loading branch information
CorinStaves committed Dec 18, 2023
1 parent bee42ce commit 036b4a7
Show file tree
Hide file tree
Showing 34 changed files with 599 additions and 1,200 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Creates a directed 2-way edges file (.gpkg) based on the matsim network. Include
This is especially useful for visualisations in which attributes are different in each direction.
Note that the output from this code is only meant for visualisation, it is not to be taken as input anywhere else.

## Trads package (src/main/java/trads)
## Trads package (src/main/java/diary)
This package contains everything related to routing trips from the Greater Manchester TRADS travel survey and collecting attributes to expand the trips dataset.

### RunTradsAnalysis.java
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/accessibility/decay/DecayFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.matsim.core.router.util.TravelTime;
import org.matsim.vehicles.Vehicle;
import routing.disutility.DistanceDisutility;
import trads.DecayFunctionCalculators;
import diary.calculate.DecayFunctionCalculators;
import trip.Purpose;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ cutoff.distance = 1600
#purpose.pair.2 = shopping_food;home

# OPTIONAL: Output TRADS cost data used for beta estimation to file
# trads.cost.output =
# diary.cost.output =
2 changes: 1 addition & 1 deletion src/main/java/census/RunCensusMcRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import routing.disutility.DistanceDisutility;
import routing.disutility.JibeDisutility;
import routing.travelTime.WalkTravelTime;
import trads.calculate.RouteIndicatorCalculator;
import diary.calculate.RouteIndicatorCalculator;
import trip.Trip;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package trads;
package diary;

import gis.GpkgReader;
import org.apache.log4j.Logger;
import org.locationtech.jts.geom.Geometry;
import org.matsim.api.core.v01.TransportMode;
import resources.Resources;
import trads.io.TradsReader;
import io.DiaryReader;
import trip.Purpose;
import trip.Trip;

Expand All @@ -32,7 +32,7 @@ public static void main(String[] args) throws IOException {

// Read in TRADS trips from CSV
logger.info("Reading person micro data from ascii file...");
Set<Trip> trips = TradsReader.readTrips(boundary);
Set<Trip> trips = DiaryReader.readTrips(boundary);

logger.info("Total trips: " + trips.size());
logger.info("Trips in network boundary: " + trips.stream().filter(t -> t.routable(ORIGIN,DESTINATION)).count());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package trads;
package diary;

import com.google.common.collect.Iterables;
import gis.GpkgReader;
import io.ioUtils;
import network.NetworkUtils2;
import org.apache.log4j.Logger;
import org.locationtech.jts.algorithm.Distance;
import org.locationtech.jts.geom.Geometry;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.IdMap;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.network.Network;
import org.matsim.core.router.costcalculators.OnlyTimeDependentTravelDisutility;
import org.matsim.core.router.util.TravelDisutility;
import org.matsim.core.router.util.TravelTime;
import org.matsim.vehicles.Vehicle;
Expand All @@ -22,28 +20,25 @@
import routing.Bicycle;
import routing.disutility.DistanceDisutility;
import routing.travelTime.WalkTravelTime;
import trads.calculate.LinkCorridorCalculator;
import trads.io.TradsReader;
import trip.Purpose;
import diary.calculate.LinkCorridorCalculator;
import io.DiaryReader;
import trip.Trip;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static trip.Place.*;

// Code to calculate route-based corridors between origin-destination pairs.
// NOTE: Feasible for detour factors up to 50%

public class RunLinkCorridor {
public class RunCorridors {

private final static Logger logger = Logger.getLogger(RunLinkCorridor.class);
private final static Logger logger = Logger.getLogger(RunCorridors.class);
private final static char SEP = ',';

public static void main(String[] args) throws IOException, FactoryException {
Expand All @@ -69,7 +64,7 @@ public static void main(String[] args) throws IOException, FactoryException {

// Read in TRADS trips from CSV
logger.info("Reading person micro data from ascii file...");
Set<Trip> trips = TradsReader.readTrips(boundary);//.stream()
Set<Trip> trips = DiaryReader.readTrips(boundary);//.stream()
// .filter(t -> !((t.getEndPurpose().isMandatory() && t.getStartPurpose().equals(Purpose.HOME)) ||
// (t.getStartPurpose().isMandatory() && t.getEndPurpose().equals(Purpose.HOME))))
// .collect(Collectors.toCollection(LinkedHashSet::new));
Expand All @@ -89,15 +84,15 @@ public static void main(String[] args) throws IOException, FactoryException {
veh = null;
} else throw new RuntimeException("Modes other than walk and bike are not supported!");

// Disutility function
TravelDisutility td = new DistanceDisutility();

// Write header
writeHeader(outputCsv);

// Use time disutility
TravelDisutility td = new DistanceDisutility();

// Calculate shortest, fastest, and jibe route
// Calculate corridors in partitions of 1000
for(List<Trip> partition : Iterables.partition(trips,1000)) {
Map<Trip, IdMap<Link,Double>> results = LinkCorridorCalculator.calculate(partition,HOME, DESTINATION, td, veh, modeNetwork, modeNetwork, 1.5);
Map<Trip, IdMap<Link,Double>> results = LinkCorridorCalculator.calculate(partition,ORIGIN, DESTINATION, td, veh, modeNetwork, modeNetwork, 1.25);
writeResults(results,outputCsv);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package trads;
package diary;

import gis.GpkgReader;
import io.ioUtils;
Expand All @@ -23,8 +23,8 @@
import routing.disutility.components.LinkAmbience;
import routing.disutility.components.LinkStress;
import routing.travelTime.WalkTravelTime;
import trads.calculate.LogitDataCalculator;
import trads.io.TradsReader;
import diary.calculate.LogitDataCalculator;
import io.DiaryReader;
import trip.Trip;

import java.io.*;
Expand Down Expand Up @@ -73,7 +73,7 @@ public static void main(String[] args) throws IOException, FactoryException {

// Read in TRADS trips from CSV
logger.info("Reading person micro data from ascii file...");
Set<Trip> trips = TradsReader.readTrips(boundary);
Set<Trip> trips = DiaryReader.readTrips(boundary);

// Filter to only routable trips with chosen mode
Set<Trip> selectedTrips = trips.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package trads;
package diary;

import org.matsim.api.core.v01.TransportMode;
import org.matsim.core.api.experimental.events.EventsManager;
import org.matsim.core.events.EventsUtils;
import org.matsim.core.events.MatsimEventsReader;
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.network.io.MatsimNetworkReader;
import org.matsim.core.router.util.TravelDisutility;
import org.matsim.core.trafficmonitoring.TravelTimeCalculator;
import org.matsim.vehicles.Vehicle;
import org.opengis.referencing.FactoryException;
import resources.Properties;
import resources.Resources;
import gis.GpkgReader;
import network.NetworkUtils2;
Expand All @@ -14,15 +22,14 @@
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.router.costcalculators.FreespeedTravelTimeAndDisutility;
import org.matsim.core.router.util.TravelTime;
import routing.ActiveAttributes;
import routing.Bicycle;
import routing.disutility.DistanceDisutility;
import routing.disutility.JibeDisutility3Fast;
import routing.travelTime.WalkTravelTime;
import trads.calculate.RouteIndicatorCalculator;
import trads.io.TradsCsvWriter;
import trads.io.TradsReader;
import trads.io.TradsRouteWriter;
import diary.calculate.RouteIndicatorCalculator;
import io.TripCsvWriter;
import io.DiaryReader;
import io.TripRouteWriter;
import trip.Trip;

import java.io.*;
Expand All @@ -36,90 +43,93 @@ public class RunRouter {

public static void main(String[] args) throws IOException, FactoryException {

if(args.length != 2) {
if(args.length != 3) {
throw new RuntimeException("Program requires 2 arguments: \n" +
"(0) Properties file \n" +
"(1) Output file");
"(1) Route data output (.csv) \n" +
"(2) Link data output (specify .gpkg OR write \"true\" to include links in csv file) \n");
}

Resources.initializeResources(args[0]);
String outputFile = args[1];

boolean savePath;
if (outputFile.endsWith(".csv")) {
savePath = false;
} else if (outputFile.endsWith(".gpkg")) {
savePath = true;
} else {
throw new RuntimeException("Unrecognised file output suffix. Please use csv or gpkg.");
String outputCsv = args[1];
String outputGpkg = null;
boolean savePath = false;

if(args[2] != null) {
if (args[2].endsWith(".gpkg")) {
outputGpkg = args[2];
savePath = true;
} else {
savePath = Boolean.parseBoolean(args[2]);
}
}

// String transitScheduleFilePath = Resources.instance.getString(Properties.MATSIM_TRANSIT_SCHEDULE);
// String transitNetworkFilePath = Resources.instance.getString(Properties.MATSIM_TRANSIT_NETWORK);


// Set up scenario and config
Config config = ConfigUtils.createConfig();
Bicycle bicycle = new Bicycle(config);
Vehicle bike = bicycle.getVehicle();

// // Create car networks
// logger.info("Creating mode-specific networks...");
// Network networkCar = NetworkUtils.createNetwork();
// new MatsimNetworkReader(networkCar).readFile(Resources.instance.getString(Properties.MATSIM_CAR_NETWORK));
// Network carXy2l = NetworkUtils2.extractXy2LinksNetwork(networkCar, l -> !((boolean) l.getAttributes().getAttribute("motorway")));

// Create active mode networks
Network network = NetworkUtils2.readFullNetwork();
Network networkBike = NetworkUtils2.extractModeSpecificNetwork(network, TransportMode.bike);
Network networkWalk = NetworkUtils2.extractModeSpecificNetwork(network, TransportMode.walk);

// Read Boundary Shapefile
logger.info("Reading boundary shapefile...");
Geometry boundary = GpkgReader.readNetworkBoundary();

// Read in TRADS trips from CSV
logger.info("Reading person micro data from ascii file...");
Set<Trip> trips = TradsReader.readTrips(boundary);//
Set<Trip> trips = DiaryReader.readTrips(boundary);//
// .stream()
// .filter(t -> (t.getMainMode().equals("Car or van passenger") || t.getMainMode().equals("Car or van driver")))
// .collect(Collectors.toCollection(LinkedHashSet::new));

// Create car networks
logger.info("Creating mode-specific networks...");
Network networkCar = NetworkUtils.createNetwork();
new MatsimNetworkReader(networkCar).readFile(Resources.instance.getString(Properties.MATSIM_CAR_NETWORK));
Network carXy2l = NetworkUtils2.extractXy2LinksNetwork(networkCar, l -> !((boolean) l.getAttributes().getAttribute("motorway")));

// Create active mode networks
Network network = NetworkUtils2.readFullNetwork();
Network networkBike = NetworkUtils2.extractModeSpecificNetwork(network, TransportMode.bike);
Network networkWalk = NetworkUtils2.extractModeSpecificNetwork(network, TransportMode.walk);

// // Load public transport data
// String transitScheduleFilePath = Resources.instance.getString(Properties.MATSIM_TRANSIT_SCHEDULE);
// String transitNetworkFilePath = Resources.instance.getString(Properties.MATSIM_TRANSIT_NETWORK);

// Travel time
FreespeedTravelTimeAndDisutility freeSpeed = new FreespeedTravelTimeAndDisutility(config.planCalcScore());
TravelTime ttBikeFast = bicycle.getTravelTimeFast(networkBike,bike);
TravelTime ttBike = bicycle.getTravelTime();
TravelTime ttWalk = new WalkTravelTime();

// // Car freespeed & congested travel time
// String tfgmDemandEvents = Resources.instance.getString(Properties.MATSIM_TFGM_OUTPUT_EVENTS);
// TravelTimeCalculator.Builder builder = new TravelTimeCalculator.Builder(networkCar);
// TravelTimeCalculator congested = builder.build();
// EventsManager events = EventsUtils.createEventsManager();
// events.addHandler(congested);
// (new MatsimEventsReader(events)).readFile(tfgmDemandEvents);
// TravelTime congestedTime = congested.getLinkTravelTimes();
// TravelDisutility congestedDisutility = new OnlyTimeDependentTravelDisutility(congested.getLinkTravelTimes());
// Car freespeed & congested travel time
String tfgmDemandEvents = Resources.instance.getString(Properties.MATSIM_DEMAND_OUTPUT_EVENTS);
TravelTimeCalculator.Builder builder = new TravelTimeCalculator.Builder(networkCar);
TravelTimeCalculator congested = builder.build();
EventsManager events = EventsUtils.createEventsManager();
events.addHandler(congested);
(new MatsimEventsReader(events)).readFile(tfgmDemandEvents);
TravelTime congestedTime = congested.getLinkTravelTimes();
TravelDisutility congestedDisutility = new OnlyTimeDependentTravelDisutility(congested.getLinkTravelTimes());

// CALCULATOR
RouteIndicatorCalculator calc = new RouteIndicatorCalculator(trips);

// // beeline
// calc.beeline("beeline", ORIGIN, DESTINATION);
calc.beeline("beeline", ORIGIN, DESTINATION);

// // car (freespeed only)
// calc.network("car_freespeed", ORIGIN, DESTINATION, null, networkCar, carXy2l, freeSpeed, freeSpeed, null,savePath);
// calc.network("car_congested", ORIGIN, DESTINATION, null, networkCar, carXy2l, congestedDisutility, congestedTime, null,savePath);
// car (freespeed only)
calc.network("car_freespeed", ORIGIN, DESTINATION, null, networkCar, carXy2l, freeSpeed, freeSpeed, null,savePath);
calc.network("car_congested", ORIGIN, DESTINATION, null, networkCar, carXy2l, congestedDisutility, congestedTime, null,savePath);

// bike (shortest and fastest)
calc.network("bike_jibe_day", ORIGIN, DESTINATION, bike, networkBike, networkBike, new JibeDisutility3Fast(networkBike,bike,TransportMode.bike,ttBikeFast,true), ttBike, ActiveAttributes.getJibe4(TransportMode.bike,bike),savePath);
calc.network("bike_jibe_night", ORIGIN, DESTINATION, bike, networkBike, networkBike, new JibeDisutility3Fast(networkBike,bike,TransportMode.bike,ttBikeFast,false), ttBike, ActiveAttributes.getJibe4(TransportMode.bike,bike),savePath);
calc.network("bike_short", ORIGIN, DESTINATION, bike, networkBike, networkBike, new DistanceDisutility(), ttBike, null,savePath);
calc.network("bike_fast", ORIGIN, DESTINATION, bike, networkBike, networkBike, new OnlyTimeDependentTravelDisutility(ttBike), ttBike, null,savePath);

calc.network("walk_jibe_day", ORIGIN, DESTINATION, null, networkWalk, networkWalk, new JibeDisutility3Fast(networkWalk,null,TransportMode.walk,ttWalk,true), ttWalk, ActiveAttributes.getJibe4(TransportMode.walk,null), savePath);
calc.network("walk_jibe_night", ORIGIN, DESTINATION, null, networkWalk, networkWalk, new JibeDisutility3Fast(networkWalk,null,TransportMode.walk,ttWalk,false), ttWalk, ActiveAttributes.getJibe4(TransportMode.walk,null), savePath);
calc.network("walk_short", ORIGIN, DESTINATION, null, networkWalk, networkWalk, new DistanceDisutility(), ttWalk, ActiveAttributes.getJibe4(TransportMode.walk,null),savePath);
calc.network("bike_jibe_day", ORIGIN, DESTINATION, bike, networkBike, networkBike, new JibeDisutility3Fast(networkBike,bike,TransportMode.bike,ttBikeFast,true), ttBike, null,savePath);
calc.network("bike_jibe_night", ORIGIN, DESTINATION, bike, networkBike, networkBike, new JibeDisutility3Fast(networkBike,bike,TransportMode.bike,ttBikeFast,false), ttBike, null,savePath);
calc.network("bike_short", ORIGIN, DESTINATION, bike, networkBike, networkBike, new DistanceDisutility(), ttBikeFast, null,savePath);
calc.network("bike_fast", ORIGIN, DESTINATION, bike, networkBike, networkBike, new OnlyTimeDependentTravelDisutility(ttBikeFast), ttBike, null,savePath);

calc.network("walk_jibe_day", ORIGIN, DESTINATION, null, networkWalk, networkWalk, new JibeDisutility3Fast(networkWalk,null,TransportMode.walk,ttWalk,true), ttWalk, null, savePath);
calc.network("walk_jibe_night", ORIGIN, DESTINATION, null, networkWalk, networkWalk, new JibeDisutility3Fast(networkWalk,null,TransportMode.walk,ttWalk,false), ttWalk, null, savePath);
calc.network("walk_short", ORIGIN, DESTINATION, null, networkWalk, networkWalk, new DistanceDisutility(), ttWalk, null,savePath);
calc.network("walk_fast", ORIGIN, DESTINATION, null, networkWalk, networkWalk, new OnlyTimeDependentTravelDisutility(ttWalk), ttWalk, null,savePath);

// // public transport
Expand All @@ -134,12 +144,14 @@ public static void main(String[] args) throws IOException, FactoryException {
// calc.network("car_hm",HOME,MAIN,null, networkCar, carXy2l, freeSpeed, freeSpeed, null,false);

// Write results
if (outputFile.endsWith(".csv")) {
if (outputCsv != null) {
logger.info("Writing results to csv file...");
TradsCsvWriter.write(trips, outputFile, calc.getAllAttributeNames());
} else {
TripCsvWriter.write(trips, outputCsv, calc.getAllAttributeNames());
}

if (outputGpkg != null) {
logger.info("Writing results to gpkg file...");
TradsRouteWriter.write(trips,outputFile,calc.getAllAttributeNames());
TripRouteWriter.write(trips,network,outputGpkg,false,calc.getAllAttributeNames());
}
}
}
Loading

0 comments on commit 036b4a7

Please sign in to comment.