Skip to content

Commit

Permalink
Updated skim matrix calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
CorinStaves committed Nov 29, 2024
1 parent 88d76da commit 28ef9e3
Showing 1 changed file with 88 additions and 22 deletions.
110 changes: 88 additions & 22 deletions src/main/java/skim/RunSkims.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
package skim;

import estimation.RouteAttribute;
import gis.GpkgReader;
import io.OmxWriter;
import network.NetworkUtils2;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.network.Network;
import org.matsim.core.api.experimental.events.EventsManager;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
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.costcalculators.FreespeedTravelTimeAndDisutility;
import org.matsim.core.router.costcalculators.OnlyTimeDependentTravelDisutility;
import org.matsim.core.router.util.TravelDisutility;
import org.matsim.core.router.util.TravelTime;
import org.matsim.core.trafficmonitoring.TravelTimeCalculator;
import org.matsim.vehicles.Vehicle;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.referencing.FactoryException;
import resources.Properties;
import resources.Resources;
import routing.Bicycle;
import routing.Gradient;
import routing.disutility.DistanceDisutility;
import routing.disutility.JibeDisutility2;
import routing.disutility.JibeDisutility4;
import routing.disutility.components.JctStress;
import routing.disutility.components.LinkAmbience;
import routing.disutility.components.LinkStress;
import routing.travelTime.WalkTravelTime;

import java.io.File;
Expand All @@ -28,6 +38,8 @@

public class RunSkims {

public static final String FILE_PATH_PREFIX = "intervention/cycling/";

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

if(args.length != 2) {
Expand All @@ -50,6 +62,16 @@ public static void main(String[] args) throws IOException, FactoryException {
Network networkCar = NetworkUtils2.extractModeSpecificNetwork(networkCarInput, TransportMode.car);
Network carXy2l = NetworkUtils2.extractXy2LinksNetwork(networkCar, l -> !((boolean) l.getAttributes().getAttribute("motorway")));

// 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());

// Create active mode networks
Network network = NetworkUtils2.readFullNetwork();
Network networkWalk = NetworkUtils2.extractModeSpecificNetwork(network, TransportMode.walk);
Expand All @@ -66,34 +88,78 @@ public static void main(String[] args) throws IOException, FactoryException {
// Initiate custom disutility
SkimCalculator calc = new SkimCalculator(features);

// CAR MATRICES
// Car skims
calc.calculate("dist",networkCar,carXy2l,freespeed,new DistanceDisutility(),null);
calc.calculate("time",networkCar,carXy2l,freespeed,freespeed,null);
OmxWriter.createOmxSkimMatrix("skimCar.omx",calc.getResults(),calc.getId2index());
calc.calculate("free",networkCar,carXy2l,freespeed,freespeed,null);
calc.calculate("congested",networkCar,carXy2l,congestedTime,congestedDisutility,null);
OmxWriter.createOmxSkimMatrix(FILE_PATH_PREFIX + "skimCar.omx",calc.getResults(),calc.getId2index());
calc.clearResults();

// BIKE MATRICES
TravelDisutility tdBikeCommute = new JibeDisutility2(networkBike,bike,"bike",ttBike,
66.8,0.,6.3,0.);
TravelDisutility tdBikeDiscretionary = new JibeDisutility2(networkBike,bike,"bike",ttBike,
63.45,0.,1.59,0.);

calc.calculate("commute",networkBike,networkBike,ttBike,tdBikeCommute,bike);
calc.calculate("discretionary",networkBike,networkBike,ttBike,tdBikeDiscretionary,bike);
// Bike skims
calc.calculate("dist",networkBike,networkBike,ttBike,new DistanceDisutility(),bike);
OmxWriter.createOmxSkimMatrix("skimBike.omx",calc.getResults(),calc.getId2index());
calc.calculate("time",networkBike,networkBike,ttBike,new OnlyTimeDependentTravelDisutility(ttBike),bike);
OmxWriter.createOmxSkimMatrix(FILE_PATH_PREFIX + "skimBike.omx",calc.getResults(),calc.getId2index());

// Walk skims
calc.calculate("dist",networkWalk,networkWalk,ttWalk,new DistanceDisutility(),null);
calc.calculate("time",networkWalk,networkWalk,ttWalk,new OnlyTimeDependentTravelDisutility(ttWalk),null);
OmxWriter.createOmxSkimMatrix(FILE_PATH_PREFIX + "skimWalk.omx",calc.getResults(),calc.getId2index());

// PURPOSE-SPECIFIC MATRICES, FOR IMPLEMENTING IN MITO
// Bike attributes
List<RouteAttribute> bikeAttributes = new ArrayList<>();
bikeAttributes.add(new RouteAttribute("grad", l -> Math.max(Math.min(Gradient.getGradient(l),0.5),0.)));
bikeAttributes.add(new RouteAttribute("stressLink", l -> LinkStress.getStress(l,TransportMode.bike)));

// Walk attributes
List<RouteAttribute> walkAttributes = new ArrayList<>();
walkAttributes.add(new RouteAttribute("vgvi", l -> Math.max(0.,0.81 - LinkAmbience.getVgviFactor(l))));
walkAttributes.add(new RouteAttribute("speed", l -> Math.min(1.,l.getFreespeed() / 22.35)));
walkAttributes.add(new RouteAttribute("stressJct", l -> JctStress.getStressProp(l,TransportMode.walk)));

// Home-based Work (HBW)
TravelDisutility tdBikeHBW = new JibeDisutility4(networkBike,bike,"bike",ttBike,bikeAttributes, new double[] {35.9032908,2.3084587});
TravelDisutility tdBikeHBW_f = new JibeDisutility4(networkBike,bike,"bike",ttBike,bikeAttributes, new double[] {35.9032908,2.3084587 + 2.7762033});
TravelDisutility tdWalkHBW = new JibeDisutility4(networkWalk,null,"walk",ttWalk,walkAttributes, new double[] {0.3307472,0,4.9887390});
calc.calculate("bike",networkBike,networkBike,ttBike,tdBikeHBW,bike);
calc.calculate("bike_female",networkBike,networkBike,ttBike,tdBikeHBW_f,bike);
calc.calculate("walk",networkWalk,networkWalk,ttWalk,tdWalkHBW,null);
OmxWriter.createOmxSkimMatrix(FILE_PATH_PREFIX + "HBW.omx",calc.getResults(),calc.getId2index());
calc.clearResults();

// Home-based Education (HBE)
TravelDisutility tdBikeHBE = new JibeDisutility4(networkBike,bike,"bike",ttBike,bikeAttributes, new double[] {0,4.3075357});
TravelDisutility tdWalkHBE = new JibeDisutility4(networkWalk,null,"walk",ttWalk,walkAttributes, new double[] {0,0,1.0037846});
calc.calculate("bike",networkBike,networkBike,ttBike,tdBikeHBE,bike);
calc.calculate("walk",networkWalk,networkWalk,ttWalk,tdWalkHBE,null);
OmxWriter.createOmxSkimMatrix(FILE_PATH_PREFIX + "HBE.omx",calc.getResults(),calc.getId2index());
calc.clearResults();

// WALK MATRICES
TravelDisutility tdWalkCommute = new JibeDisutility2(networkWalk,null,"walk",ttWalk,
0.,0.,0.,4.27);
TravelDisutility tdWalkDiscretionary = new JibeDisutility2(networkWalk,null,"walk",ttWalk,
0.,0.62,0.,14.34);
// Home-based Discretionary (HBD)
TravelDisutility tdBikeHBD = new JibeDisutility4(networkBike,bike,"bike",ttBike,bikeAttributes, new double[] {57.0135325,1.2411983});
TravelDisutility tdBikeHBD_c = new JibeDisutility4(networkBike,bike,"bike",ttBike,bikeAttributes, new double[] {57.0135325,1.2411983 + 6.4243251});
TravelDisutility tdWalkHBD = new JibeDisutility4(networkWalk,null,"walk",ttWalk,walkAttributes, new double[] {0.7789561,0.4479527,5.8219067});
TravelDisutility tdWalkHBD_c = new JibeDisutility4(networkWalk,null,"walk",ttWalk,walkAttributes, new double[] {0.7789561,0.4479527 + 2.0418898,5.8219067});
TravelDisutility tdWalkHBD_o = new JibeDisutility4(networkWalk,null,"walk",ttWalk,walkAttributes, new double[] {0.7789561,0.4479527 + 0.3715017,5.8219067});
calc.calculate("bike",networkBike,networkBike,ttBike,tdBikeHBD,bike);
calc.calculate("bike_child",networkBike,networkBike,ttBike,tdBikeHBD_c,bike);
calc.calculate("walk",networkWalk,networkWalk,ttWalk,tdWalkHBD,null);
calc.calculate("walk_child",networkWalk,networkWalk,ttWalk,tdWalkHBD_c,null);
calc.calculate("walk_elderly",networkWalk,networkWalk,ttWalk,tdWalkHBD_o,null);
OmxWriter.createOmxSkimMatrix(FILE_PATH_PREFIX + "HBD.omx",calc.getResults(),calc.getId2index());
calc.clearResults();

calc.calculate("commute",networkWalk,networkWalk,ttWalk,tdWalkCommute,null);
calc.calculate("discretionary",networkWalk,networkWalk,ttWalk,tdWalkDiscretionary,null);
calc.calculate("dist",networkWalk,networkWalk,ttWalk,new DistanceDisutility(),null);
OmxWriter.createOmxSkimMatrix("skimWalk.omx",calc.getResults(),calc.getId2index());
// Home-based Accompany (HBA)
TravelDisutility tdWalkHBA = new JibeDisutility4(networkWalk,null,"walk",ttWalk,walkAttributes, new double[] {0.6908324,0,0});
calc.calculate("walk",networkWalk,networkWalk,ttWalk,tdWalkHBA,null);
OmxWriter.createOmxSkimMatrix(FILE_PATH_PREFIX + "HBA.omx",calc.getResults(),calc.getId2index());
calc.clearResults();

// Non home-based other (NHBO)
TravelDisutility tdWalkNHBO = new JibeDisutility4(networkWalk,null,"walk",ttWalk,walkAttributes, new double[] {0,3.4485883,0});
calc.calculate("walk",networkWalk,networkWalk,ttWalk,tdWalkNHBO,null);
OmxWriter.createOmxSkimMatrix(FILE_PATH_PREFIX + "NHBO.omx",calc.getResults(),calc.getId2index());
calc.clearResults();

}
}

0 comments on commit 28ef9e3

Please sign in to comment.