--- title: 'Trade-offs between travel time and monetary cost' date: "`r Sys.Date()`" output: rmarkdown::html_vignette abstract: "This vignette shows how to use the `pareto_frontier()` function to examine the trade-offs between travel time and monetary cost in travel time matrices in r5r." urlcolor: blue vignette: > %\VignetteIndexEntry{Trade-offs between travel time and monetary cost} %\VignetteEngine{knitr::rmarkdown} \usepackage[utf8]{inputenc} bibliography: references.json --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", eval = identical(tolower(Sys.getenv("NOT_CRAN")), "true"), out.width = "100%" ) ``` # 1. Introduction Routing models usually find either the fastest or the cheapest route. Accounting for both time and monetary cost at once is hard, because minimizing trip duration and minimizing cost are competing objectives [@conway2019getting]. To address this problem, `r5r`'s `pareto_frontier()` returns the most efficient combinations of travel time and monetary cost between origin/destination pairs. This vignette shows, with a reproducible example, how to use it and interpret its results. ## 2. What the `pareto_frontier` means. Imagine a trip from A to B with several route alternatives (figure below): * Walking is the **cheapest** option but takes 50 minutes. * Bus + subway is the **fastest**: 15 minutes for R\$ 8. * In between: * a single bus: R\$ 3, 35 min; * two buses with one transfer: R\$ 5, 29 min; * walking to the subway: R\$ 6, 20 min. These options form the **Pareto frontier** of routes from A to B: no route is both faster and cheaper than any route on the frontier. On the frontier, a trip cannot be made faster without costing more, nor cheaper without taking longer. ```{r, echo = FALSE, fig.width=7, fig.height=4} library(ggplot2) # data.frame df <- structure(list(option = c(1, 2, 3, 4, 5), modes = c("Walk", "Bus","Bus + Bus", "Subway", "Bus + Subway"), time = c(50, 35, 29, 20, 15), cost = c(0, 3, 5, 6, 8)), class = "data.frame", row.names = c(NA, -5L)) # figure ggplot(data=df, aes(x=cost, y=time, label = modes)) + geom_step(linetype = "dashed") + geom_point() + geom_text(color='gray30', hjust = -.2, nudge_x = 0.05, angle = 45) + labs(title='Pareto frontier of alternative routes from A to B', subtitle = 'Hypothetical example') + scale_x_continuous(name="Travel cost (BRL)", breaks=seq(0,12,3)) + scale_y_continuous(name="Travel time (minutes)", breaks=seq(0,60,10)) + coord_cartesian(xlim = c(0,14), ylim = c(0, 60)) + theme_classic() ```
The frontier shows the time-cost trade-offs that public transport passengers face. It also supports cumulative-opportunity accessibility metrics with both time and cost cutoffs (e.g. the number of jobs reachable within 40 minutes and R\$ 5) [@conway2019getting]. Let's see a couple concrete examples showing how `r5r` can calculate the Pareto frontier for multiple origins. ## 3. Demonstration of `pareto_frontier()`. ### 3.1 Build routable transport network with `build_network()` We use the Porto Alegre (Brazil) sample data included in `r5r`. ```{r, message = FALSE} # increase Java memory old_options <- options(java.parameters = "-Xmx2G") # load libraries library(r5r) library(data.table) library(ggplot2) library(dplyr) # build a routable transport network with r5r data_path <- system.file("extdata/poa", package = "r5r") r5r_network <- build_network(data_path) # routing inputs mode <- c('walk', 'transit') max_trip_duration <- 90 # minutes # load origin/destination points of interest points <- fread(file.path(data_path, "poa_points_of_interest.csv")) ``` ### 3.2 Set up the fare structure `R5` computes the monetary cost of each route from the fare rules of the public transport system. In Porto Alegre: * A bus ticket costs R\$ 4.80. * A second bus ride adds R\$ 2.40; subsequent bus rides cost the full R\$ 4.80. * A train ticket costs R\$ 4.50, with unlimited train rides as long as the passenger does not leave the stations. * The integrated bus + train fare has a 10% discount, totalling R\$ 8.37. A fare structure with these rules ships with `r5r`, so we simply read it. The [fare structure vignette](https://ipea.github.io/r5r/articles/fare_structure.html) shows how to build one step by step. ```{r} fare_structure <- r5r::read_fare_structure(file.path(data_path, "fares/fares_poa.zip")) ``` ### 3.3 Calculating a `pareto_frontier()`. We compute the Pareto frontier from all origins to all destinations with these monetary cost cutoffs: * R\$ 1.00: walking only * R\$ 4.50: a train trip * R\$ 4.80: a single bus trip * R\$ 7.20: bus + bus * R\$ 8.37: bus + train ```{r} departure_datetime <- as.POSIXct("13-05-2019 14:00:00", format = "%d-%m-%Y %H:%M:%S") prtf <- pareto_frontier( r5r_network, origins = points, destinations = points, mode = c("WALK", "TRANSIT"), departure_datetime = departure_datetime, fare_structure = fare_structure, fare_cutoffs = c(1, 4.5, 4.8, 7.20, 8.37), progress = TRUE ) head(prtf) ``` For illustration, the Pareto frontiers between the Farrapos train station and (a) the Praia de Belas shopping mall and (b) the Moinhos hospital: ```{r, echo = FALSE, fig.width=7, fig.height=4, warning=FALSE} # select origin and destinations pf2 <- dplyr::filter(prtf, to_id == 'farrapos_station' & from_id %in% c('moinhos_de_vento_hospital', 'praia_de_belas_shopping_center')) # recode modes pf2[, modes := fcase(monetary_cost == 1, 'Walk', monetary_cost == 4.5, 'Train', monetary_cost == 4.8, 'Bus', monetary_cost == 7.2, 'Bus + Bus', monetary_cost == 8.37, 'Bus + Train')] # plot ggplot(data=pf2, aes(x=monetary_cost, y=travel_time, color=from_id, label = modes)) + geom_step(linetype = "dashed") + geom_point() + geom_text(color='gray30', hjust = -.2, nudge_x = 0.05, angle = 45) + labs(title='Pareto frontier of alternative routes from Farrapos station to:', subtitle = 'Praia de Belas shopping mall and Moinhos hospital', color='Destination') + scale_x_continuous(name="Travel cost ($)", breaks=seq(0,12,2)) + scale_y_continuous(name="Travel time (minutes)", breaks=seq(0,120,20)) + coord_cartesian(xlim = c(0,12), ylim = c(0, 120)) + theme_classic() + theme(legend.position=c(.2,0.8)) ``` ### Cleaning up after usage Stop the network and run Java's garbage collector to free the memory it used: ```{r, message = FALSE} r5r::stop_r5(r5r_network) rJava::.jgc(R.gc = TRUE) options(old_options) ``` If you have any suggestions or want to report an error, please visit [the package GitHub page](https://github.com/ipea/r5r). ## References