Skip to content

Commit

Permalink
Merge pull request #46 from BauhausLuftfahrt/travelTimesEnhancement, c…
Browse files Browse the repository at this point in the history
…loses #47

-Added distance and links path to car and distance to pt
  • Loading branch information
RRothfeld authored Jan 29, 2020
2 parents 6846bc8 + 3f62d71 commit f228a13
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 178 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ List of all current features provided by MATSim-UAM with the version of feature
## Versions and Change Log

### v2.0
Travel time scripts:
- Replaced provision of, e.g., network and transit schedule via programme arguments to retrieval from config file
- Added distance and links path list to CalculateCarTravelTimes
- Added distance and route information to CalculatePTTravelTimes

Updates the extension to MATSim version 11 and current DVRP version. Major changes includes:
- UAMQSimPlugin is substituted by UAMQSimModule
- A new fleet is provided every new iteration by using UAMFleetData
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package net.bhl.matsim.uam.analysis.traveltimes;

import net.bhl.matsim.uam.analysis.traveltimes.utils.ConfigSetter;
import net.bhl.matsim.uam.analysis.traveltimes.utils.ThreadCounter;
import net.bhl.matsim.uam.analysis.traveltimes.utils.TripItem;
import net.bhl.matsim.uam.analysis.traveltimes.utils.TripItemReader;
import net.bhl.matsim.uam.config.UAMConfigGroup;
import org.apache.log4j.Logger;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.network.Network;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.Injector;
import org.matsim.core.network.NetworkUtils;
Expand Down Expand Up @@ -45,17 +46,17 @@ public class RunCalculateCarTravelTimes {
private static ArrayBlockingQueue<LeastCostPathCalculator> carRouters = new ArrayBlockingQueue<>(processes);

public static void main(String[] args) throws Exception {
System.out.println("ARGS: base-network.xml* networkEventsChangeFile.xml* tripsCoordinateFile.csv* outputfile-name.csv*");
System.out.println(
"ARGS: config.xml* tripsCoordinateFile.csv* outputfile-name.csv*");
System.out.println("(* required)");

// ARGS
int j = 0;
String networkInput = args[j++];
String networkEventsChangeFile = args[j++];
String configInput = args[j++];
String tripsInput = args[j++];
String outputPath = args[j];

Config config = ConfigSetter.createCarConfig(networkInput, networkEventsChangeFile);
Config config = ConfigUtils.loadConfig(configInput, new UAMConfigGroup());
Scenario scenario = ScenarioUtils.createScenario(config);
ScenarioUtils.loadScenario(scenario);
Network network = scenario.getNetwork();
Expand Down Expand Up @@ -84,7 +85,7 @@ public void install() {
LeastCostPathCalculatorFactory pathCalculatorFactory = injector
.getInstance(LeastCostPathCalculatorFactory.class); // AStarLandmarksFactory

//Provide routers
// Provide routers
for (int i = 0; i < processes; i++) {
carRouters.add(pathCalculatorFactory.createPathCalculator(networkCar, travelDisutility, travelTime));
}
Expand Down Expand Up @@ -127,7 +128,8 @@ public static void write(String outputPath, List<TripItem> trips) throws IOExcep
writer.write(String.join(",",
new String[]{String.valueOf(trip.origin.getX()), String.valueOf(trip.origin.getY()),
String.valueOf(trip.destination.getX()), String.valueOf(trip.destination.getY()),
String.valueOf(trip.departureTime), String.valueOf(trip.travelTime)})
String.valueOf(trip.departureTime), String.valueOf(trip.travelTime),
String.valueOf(trip.distance), trip.description})
+ "\n");
}

Expand All @@ -137,24 +139,18 @@ public static void write(String outputPath, List<TripItem> trips) throws IOExcep

private static String formatHeader() {
return String.join(",", new String[]{"origin_x", "origin_y", "destination_x", "destination_y",
"departure_time", "travel_time"});
"departure_time", "travel_time", "distance", "description"});
}

private static double estimateTravelTime(Link from, Link to, double departureTime, Network carNetwork,
LeastCostPathCalculator pathCalculator) {
if (carNetwork.getLinks().get(from.getId()) != null)
from = carNetwork.getLinks().get(from.getId());
else
private static Path estimatePath(Link from, Link to, double departureTime, Network carNetwork,
LeastCostPathCalculator pathCalculator) {
if (carNetwork.getLinks().get(from.getId()) == null)
from = NetworkUtils.getNearestLinkExactly(carNetwork, from.getCoord());

if (carNetwork.getLinks().get(to.getId()) != null)
to = carNetwork.getLinks().get(to.getId());
else
if (carNetwork.getLinks().get(to.getId()) == null)
to = NetworkUtils.getNearestLinkExactly(carNetwork, to.getCoord());

Path path = pathCalculator.calcLeastCostPath(from.getFromNode(), to.getToNode(), departureTime, null,
null);
return path.travelTime;
return pathCalculator.calcLeastCostPath(from.getFromNode(), to.getToNode(), departureTime, null, null);
}

static class CarTravelTimeCalculator implements Runnable {
Expand Down Expand Up @@ -184,7 +180,19 @@ public void run() {
Link to = NetworkUtils.getNearestLink(networkCar, trip.destination);

try {
trip.travelTime = estimateTravelTime(from, to, trip.departureTime, networkCar, plcpccar);
Path path = estimatePath(from, to, trip.departureTime, networkCar, plcpccar);
double distance = 0;
StringBuilder linksList = new StringBuilder();
for (Link link : path.links) {
if (distance != 0)
linksList.append("->");
distance += link.getLength();
linksList.append("[link:").append(link.getId().toString()).append("]");
}

trip.distance = distance;
trip.travelTime = path.travelTime;
trip.description = linksList.toString();
} catch (NullPointerException e) {
// Do nothing; failed trip will show as null in results.
}
Expand All @@ -197,5 +205,4 @@ public void run() {
threadCounter.deregister();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package net.bhl.matsim.uam.analysis.traveltimes;

import ch.sbb.matsim.routing.pt.raptor.*;
import net.bhl.matsim.uam.analysis.traveltimes.utils.ConfigSetter;
import net.bhl.matsim.uam.analysis.traveltimes.utils.ThreadCounter;
import net.bhl.matsim.uam.analysis.traveltimes.utils.TripItem;
import net.bhl.matsim.uam.analysis.traveltimes.utils.TripItemReader;
import net.bhl.matsim.uam.config.UAMConfigGroup;
import org.apache.log4j.Logger;
import org.matsim.api.core.v01.Scenario;
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.api.core.v01.population.Leg;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.router.LinkWrapperFacility;
import org.matsim.core.router.RoutingModule;
Expand Down Expand Up @@ -47,31 +48,30 @@ public class RunCalculatePTTravelTimes {

public static void main(String[] args) throws Exception {
System.out.println(
"ARGS: base-network.xml* transitScheduleFile.xml* tripsCoordinateFile.csv* outputfile-name*");
"ARGS: config.xml* tripsCoordinateFile.csv* outputfile-name*");
System.out.println("(* required)");

// ARGS
int j = 0;
String networkInput = args[j++];
String transitScheduleInput = args[j++];
String configInput = args[j++];
String tripsInput = args[j++];
String outputPath = args[j];

// READ NETWORK
Config config = ConfigSetter.createPTConfig(networkInput, transitScheduleInput);
Config config = ConfigUtils.loadConfig(configInput, new UAMConfigGroup());
Scenario scenario = ScenarioUtils.createScenario(config);
ScenarioUtils.loadScenario(scenario);
Network network = scenario.getNetwork();

RaptorStaticConfig raptorStaticConfig = ConfigSetter.createRaptorConfig(config);
RaptorStaticConfig raptorStaticConfig = RaptorUtils.createStaticConfig(config);
SwissRailRaptorData data = SwissRailRaptorData.create(scenario.getTransitSchedule(), raptorStaticConfig,
network);

//Provide routers
for (int i = 0; i < processes; i++) {
Map<String, RoutingModule> router = new HashMap<>();
router.put(TransportMode.pt, new TeleportationRoutingModule(TransportMode.pt,
scenario.getPopulation().getFactory(),0,1.5));
scenario.getPopulation().getFactory(), 0, 1.5));
ptRouters.add(new SwissRailRaptor(data, new DefaultRaptorParametersForPerson(config),
new LeastCostRaptorRouteSelector(),
new DefaultRaptorStopFinder(null, new DefaultRaptorIntermodalAccessEgress(), router)));
Expand All @@ -93,7 +93,7 @@ public static void main(String[] args) throws Exception {
while (threadCounter.getProcesses() >= processes - 1)
Thread.sleep(200);

es.execute(new PTTravelTimeCalculator(threadCounter, network, config, trip, network, scenario));
es.execute(new PTTravelTimeCalculator(threadCounter, network, trip));
counter++;
}
es.shutdown();
Expand All @@ -116,7 +116,8 @@ public static void write(String outputPath, List<TripItem> trips) throws IOExcep
writer.write(String.join(",",
new String[]{String.valueOf(trip.origin.getX()), String.valueOf(trip.origin.getY()),
String.valueOf(trip.destination.getX()), String.valueOf(trip.destination.getY()),
String.valueOf(trip.departureTime), String.valueOf(trip.travelTime)})
String.valueOf(trip.departureTime), String.valueOf(trip.travelTime),
String.valueOf(trip.distance), trip.description})
+ "\n");
}

Expand All @@ -126,40 +127,20 @@ public static void write(String outputPath, List<TripItem> trips) throws IOExcep

private static String formatHeader() {
return String.join(",", new String[]{"origin_x", "origin_y", "destination_x", "destination_y",
"departure_time", "travel_time"});
}

private static double estimateTravelTime(Link from, Link to, double departureTime, TransitRouter router) {
List<Leg> legs = router.calcRoute(new LinkWrapperFacility(from), new LinkWrapperFacility(to), departureTime,
null);
double time = 0;
boolean onlyWalk = true;
for (Leg leg : legs) {
if (onlyWalk)
onlyWalk = leg.getMode().contains(TransportMode.walk) ? true : false;
time += leg.getTravelTime();
}
return onlyWalk ? -1 * time : time;
"departure_time", "travel_time", "distance", "description"});
}

static class PTTravelTimeCalculator implements Runnable {

private TripItem trip;
private Network network;
private Config config;
private ThreadCounter threadCounter;
private Network networkCar;
private Scenario scenario;
private TransitRouter transitRouter;

PTTravelTimeCalculator(ThreadCounter threadCounter, Network network, Config config, TripItem trip,
Network networkCar, Scenario scenario) {
PTTravelTimeCalculator(ThreadCounter threadCounter, Network network, TripItem trip) {
this.trip = trip;
this.network = network;
this.config = config;
this.threadCounter = threadCounter;
this.networkCar = networkCar;
this.scenario = scenario;
}

@Override
Expand All @@ -176,7 +157,25 @@ public void run() {
Link to = NetworkUtils.getNearestLink(network, trip.destination);

try {
trip.travelTime = estimateTravelTime(from, to, trip.departureTime, transitRouter);
List<Leg> legs = transitRouter.calcRoute(new LinkWrapperFacility(from),
new LinkWrapperFacility(to), trip.departureTime, null);
double time = 0;
double distance = 0;
StringBuilder routeList = new StringBuilder();
for (Leg leg : legs) {
if (time != 0)
routeList.append("->");
time += leg.getTravelTime();
distance += leg.getRoute().getDistance();
routeList.append("[mode:").append(leg.getMode()).append("]");
routeList.append("[start:").append(leg.getRoute().getStartLinkId()).append("]");
routeList.append("[end:").append(leg.getRoute().getEndLinkId()).append("]");
routeList.append("[time:").append(leg.getTravelTime()).append("]");
routeList.append("[distance:").append(leg.getRoute().getDistance()).append("]");
}
trip.travelTime = time;
trip.distance = distance;
trip.description = routeList.toString();
} catch (NullPointerException e) {
// Do nothing; failed trip will show as null in results.
}
Expand All @@ -189,5 +188,4 @@ public void run() {
threadCounter.deregister();
}
}

}
Loading

0 comments on commit f228a13

Please sign in to comment.