Skip to content

Commit 1c7b04f

Browse files
authored
Add files via upload
1 parent ddba3ad commit 1c7b04f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+7190
-6413
lines changed

DESCRIPTION

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ Description: Calculation of distances, shortest paths and isochrones on weighted
1010
bidirectional Dijkstra (Goldberg, Andrew & Fonseca F. Werneck, Renato (2005) <https://pdfs.semanticscholar.org/0761/18dfbe1d5a220f6ac59b4de4ad07b50283ac.pdf>),
1111
A* search (P. E. Hart, N. J. Nilsson et B. Raphael (1968) <doi:10.1109/TSSC.1968.300136>),
1212
new bidirectional A* (Pijls & Post (2009) <http://repub.eur.nl/pub/16100/ei2009-10.pdf>),
13-
Contraction hierarchies (R. Geisberger, P. Sanders, D. Schultes and D. Delling (2008) <doi:10.1007/978-3-540-68552-4_24>).
13+
Contraction hierarchies (R. Geisberger, P. Sanders, D. Schultes and D. Delling (2008) <doi:10.1007/978-3-540-68552-4_24>),
14+
PHAST (D. Delling, A.Goldberg, A. Nowatzyk, R. Werneck (2011) <doi:10.1016/j.jpdc.2012.02.007>).
1415
License: GPL (>= 2)
1516
Imports: Rcpp (>= 1.0.1), RcppParallel, RcppProgress , data.table
1617
LinkingTo: Rcpp, RcppParallel, RcppProgress
1718
SystemRequirements: GNU make, C++11
1819
RoxygenNote: 6.1.1
1920
URL: https://github.com/vlarmet/cppRouting
20-
Suggests:
21-
knitr,
22-
rmarkdown,
23-
igraph
21+
Suggests: knitr, rmarkdown, igraph
2422
VignetteBuilder: knitr
23+
NeedsCompilation: yes
24+
Packaged: 2019-12-20 11:17:10 UTC; vlarmet

R/RcppExports.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ Dijkstra_mat_par <- function(dep, arr, gfrom, gto, gw, NbNodes) {
101101
.Call(`_cppRouting_Dijkstra_mat_par`, dep, arr, gfrom, gto, gw, NbNodes)
102102
}
103103

104+
Phast3 <- function(dep, arr, gfrom, gto, gw, NbNodes) {
105+
.Call(`_cppRouting_Phast3`, dep, arr, gfrom, gto, gw, NbNodes)
106+
}
107+
108+
Phast_par <- function(dep, arr, gfrom, gto, gw, NbNodes) {
109+
.Call(`_cppRouting_Phast_par`, dep, arr, gfrom, gto, gw, NbNodes)
110+
}
111+
104112
Simplify3 <- function(gfrom, gto, gw, NbNodes, loop, keep, iterate, progress) {
105113
.Call(`_cppRouting_Simplify3`, gfrom, gto, gw, NbNodes, loop, keep, iterate, progress)
106114
}

R/get_detour.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ get_detour<-function(Graph,from,to,extra=NULL,keep=NULL,long=FALSE){
7878
}
7979

8080
res<-Detour(from_id,to_id,Graph$data$from,Graph$data$to,Graph$data$dist,Graph$nbnode,t=extra,Graph$dict$ref,to_keep)
81-
81+
8282

8383
if (long){
8484
names(res)<-paste0(from)
@@ -94,5 +94,4 @@ get_detour<-function(Graph,from,to,extra=NULL,keep=NULL,long=FALSE){
9494
return(res)
9595
}
9696

97-
}
98-
97+
}

R/get_distance_mat.R

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
#' @param Graph An object generated by makegraph(), cpp_simplify() or cpp_contract() function.
44
#' @param from A vector of one or more vertices from which distances are calculated (origin).
55
#' @param to A vector of one or more vertices (destination).
6+
#' @param algorithm Character. Only for contracted graph, "mch" for Many to many CH, "phast" for PHAST algorithm
67
#' @param allcores Logical. If TRUE, all cores are used.
78
#' @return Matrix of shortest distances.
89
#' @note If graph is not contracted, get_distance_matrix() recursively perform Dijkstra algorithm for each 'from' nodes.
9-
#' Else, the algorithm is a modified bidirectional search adapted to many to many computation.
10+
#' If graph is contracted, the user has the choice between : \itemize{
11+
#' \item many to many contraction hierarchies (mch) : should be applied if the matrix is square or 'almost' square.
12+
#' \item PHAST (phast) : outperform mch on rectangular matrix
13+
#' }
14+
#' See details in package website : https://github.com/vlarmet/cppRouting/blob/master/README.md
1015
#' @examples
1116
#' #Data describing edges of the graph
1217
#' edges<-data.frame(from_vertex=c(0,0,1,1,2,2,3,4,4),
@@ -25,7 +30,7 @@
2530
#' print(dir_dist)
2631
#' print(non_dir_dist)
2732

28-
get_distance_matrix<-function(Graph,from,to,allcores=FALSE){
33+
get_distance_matrix<-function(Graph,from,to,algorithm="phast",allcores=FALSE){
2934
if (any(is.na(from))) stop("NAs are not allowed in origin/destination nodes")
3035
if (any(is.na(to))) stop("NAs are not allowed in origin/destination nodes")
3136
from<-as.character(from)
@@ -60,23 +65,59 @@ get_distance_matrix<-function(Graph,from,to,allcores=FALSE){
6065
}
6166

6267
if (length(Graph)==5){
63-
if (allcores==TRUE){
64-
65-
if (length(to)< length(from)){
66-
res<-par_Bidir_mat2(to_id,from_id,Graph$data[,2],Graph$data[,1],Graph$data[,3],Graph$nbnode,Graph$rank)
68+
69+
if (algorithm=="mch"){
70+
if (allcores==TRUE){
71+
72+
if (length(to)< length(from)){
73+
res<-par_Bidir_mat2(to_id,from_id,Graph$data[,2],Graph$data[,1],Graph$data[,3],Graph$nbnode,Graph$rank)
74+
75+
}
76+
else {
77+
res<-par_Bidir_mat2(from_id,to_id,Graph$data[,1],Graph$data[,2],Graph$data[,3],Graph$nbnode,Graph$rank)
78+
79+
}
80+
6781

6882
}
6983
else {
70-
res<-par_Bidir_mat2(from_id,to_id,Graph$data[,1],Graph$data[,2],Graph$data[,3],Graph$nbnode,Graph$rank)
71-
84+
if (length(to)< length(from)) res<-Bidir_mat3(to_id,from_id,Graph$data[,2],Graph$data[,1],Graph$data[,3],Graph$nbnode,Graph$rank)
85+
else res<-Bidir_mat3(from_id,to_id,Graph$data[,1],Graph$data[,2],Graph$data[,3],Graph$nbnode,Graph$rank)
7286
}
87+
}
88+
else{
7389

90+
test<-data.frame(id=Graph$dict$id,rank=(Graph$nbnode)-Graph$rank)
7491

92+
if (allcores==TRUE){
93+
94+
if (length(to)< length(from)){
95+
res<-Phast_par(test$rank[to_id+1],
96+
test$rank[from_id+1],
97+
test$rank[match(Graph$data$to,test$id)],
98+
test$rank[match(Graph$data$from,test$id)],
99+
Graph$data[,3],
100+
Graph$nbnode)
101+
102+
}
103+
else {
104+
res<-Phast_par(test$rank[from_id+1],
105+
test$rank[to_id+1],
106+
test$rank[match(Graph$data$from,test$id)],
107+
test$rank[match(Graph$data$to,test$id)],
108+
Graph$data[,3],
109+
Graph$nbnode)
110+
111+
}
112+
113+
114+
}
115+
else {
116+
if (length(to)< length(from)) res<-Phast3(test$rank[to_id+1],test$rank[from_id+1],test$rank[match(Graph$data$to,test$id)],test$rank[match(Graph$data$from,test$id)],Graph$data[,3],Graph$nbnode)
117+
else res<-Phast3(test$rank[from_id+1],test$rank[to_id+1],test$rank[match(Graph$data$from,test$id)],test$rank[match(Graph$data$to,test$id)],Graph$data[,3],Graph$nbnode)
118+
}
75119
}
76-
else {
77-
if (length(to)< length(from)) res<-Bidir_mat3(to_id,from_id,Graph$data[,2],Graph$data[,1],Graph$data[,3],Graph$nbnode,Graph$rank)
78-
else res<-Bidir_mat3(from_id,to_id,Graph$data[,1],Graph$data[,2],Graph$data[,3],Graph$nbnode,Graph$rank)
79-
}
120+
80121

81122
}
82123

R/get_path_pair.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#' @param keep numeric or character. Vertices of interest that will be returned.
99
#' @param long logical. If TRUE, a long data.frame is returned instead of a list.
1010
#' Default to 1, when cost is expressed in the same unit than coordinates. See details
11-
#' @return List containing shortest path nodes between from and to.
11+
#' @return List or a data.frame containing shortest path nodes between from and to.
1212
#' @note 'from' and 'to' must be the same length.
1313
#' @details If the input graph has been contracted by cpp_contract() function, the algorithm is a modified bidirectional search.
1414
#' To perform A* and New Bidirectional A star, projected coordinates should be provided in the Graph object.

R/graphs.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
#' @param df A data.frame or matrix containing 3 columns: from, to, cost. See details.
44
#' @param directed logical. If FALSE, then all edges are duplicated by inverting 'from' and 'to' nodes.
55
#' @param coords Optional. A data.frame or matrix containing all nodes coordinates. Columns order should be 'node_ID', 'X', 'Y'.
6-
#' @return List
6+
#' @return List with two useful attributes for the user : \cr
7+
#'
8+
#' \emph{nbnode} : total number of vertices \cr
9+
#' \emph{dict$ref} : vertices IDs
710
#' @details 'from' and 'to' are character or numeric vector containing nodes IDs.
811
#' 'cost' is a non-negative numeric vector describing the cost (e.g time, distance) between each 'from' and 'to' nodes.
912
#' coords should not be angles (e.g latitude and longitude), but expressed in a projection system.
@@ -30,8 +33,6 @@
3033
#' #Construct graph with coordinates
3134
#' directed_graph2<-makegraph(edges, directed=TRUE, coords=coord)
3235
#'
33-
#'
34-
#'
3536

3637
makegraph<-function(df,
3738
directed=TRUE,

build/vignette.rds

202 Bytes
Binary file not shown.

inst/doc/cpprouting.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## ----setup, include=FALSE------------------------------------------------
2+
knitr::opts_chunk$set(
3+
collapse = TRUE,
4+
comment = "#>"
5+
)
6+

inst/doc/cpprouting.Rmd

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
title: "cppRouting"
3+
author: "Vincent Larmet"
4+
date: "`r Sys.Date()`"
5+
output: rmarkdown::html_vignette
6+
vignette: >
7+
%\VignetteIndexEntry{cppRouting}
8+
%\VignetteEngine{knitr::rmarkdown}
9+
%\VignetteEncoding{UTF-8}
10+
---
11+
12+
```{r setup, include=FALSE}
13+
knitr::opts_chunk$set(
14+
collapse = TRUE,
15+
comment = "#>"
16+
)
17+
```
18+
19+
#Package presentation
20+
21+
`cppRouting` is an `R` package which provide functions to calculate distances, shortest paths and isochrones/isodistances on non-negative weighted graphs.
22+
`cppRouting` is characterized by :
23+
24+
- its ability to work on large road graphs (country/continent scale)
25+
- its large choice of `one-to-one` shortest path algorithms
26+
- its implementation of **contraction hierarchies** algorithm
27+
28+
`cppRouting` is therefore particularly adapted for geographer, or whoever who need to calculate accessibility indicators at large scale.
29+
Most of the functions are written in C++ and use std::priority_queue container from the Standard Template Library.
30+
This package have been made with `Rcpp` and `RcppParallel` packages.
31+
32+
33+
#Main functions
34+
35+
`cppRouting` package provide these functions :
36+
37+
- `get_distance_matrix` : compute distance matrix (between all combinations origin-destination nodes - *one-to-many*),
38+
- `get_distance_pair` : compute distances between origin and destination by pair (*one-to-one*),
39+
- `get_path_pair` : compute shortest paths between origin and destination by pair (*one-to-one*),
40+
- `get_multi_paths` : compute shortest paths between all origin nodes and all destination nodes (*one-to-many*),
41+
- `get_isochrone` : compute isochrones/isodistances with one or multiple breaks.
42+
- `get_detour` : return nodes that are reachable within a fixed additional cost around shortest paths. This function can be useful in producing accessibility indicators.
43+
- `cpp_simplify` : remove non-intersection nodes, duplicated edges and isolated loops in the graph. Graph topology is preserved so distance calculation is faster and remains true. This function can be applied to very large graphs (several millions of nodes).
44+
- `cpp_contract` : contract the graph by applying **contraction hierarchies** algorithm.
45+
46+
47+
###Path algorithms
48+
Path algorithms proposed by the package are :
49+
50+
- **1** uni-directional Dijkstra algorithm,
51+
- **2** bi-directional Dijkstra algorithm,
52+
- **3** uni-directional A* algorithm
53+
- **4** New bi-directional A* algorithm (Piljs & Post, 2009 : see http://repub.eur.nl/pub/16100/ei2009-10.pdf)
54+
- **5** *one-to-one* bi-directional Dijkstra adapted to contraction hierarchies (Geisberger & al., 2008)
55+
- **6** *many-to-many* bi-directional Dijkstra adapted to contraction hierarchies (Geisberger & al., 2008)
56+
- **7** PHAST algorithm (Hardware-accelerated shortest path trees), *one-to-all* algorithm adapted to contraction hierarchies (Delling & al., 2011)
57+
58+
59+
*1*, *2*, *3* and *4* are available for **one-to-one** calculation in `get_distance_pair` and `get_path_pair` functions on a **non-contracted** graph.
60+
In these functions, uni-directional Dijkstra algorithm is stopped when the destination node is reached.
61+
`A*` and `NBA*` are relevant if geographic coordinates of all nodes are provided. Note that coordinates should be expressed in a **projection system**.
62+
To be accurate and efficient, `A*` and `NBA*` algorithms should use an admissible heuristic function (here the Euclidean distance), i.e cost and heuristic function must be expressed in the same unit.
63+
In `cppRouting`, heuristic function `h` for a node (n) is defined such that :
64+
**h(n,d) = ED(n,d) / k**
65+
with *h* the heuristic, *ED* the Euclidean distance, *d* the destination node and a constant *k*.
66+
So in the case where coordinates are expressed in meters and cost is expressed in time, *k* is the maximum speed allowed on the road. By default, constant is 1 and is designed for graphs with cost expressed in the same unit than coordinates (for example in meters).
67+
If coordinates cannot be provided, bi-directional Dijkstra algorithm is the best option in terms of performance.
68+
69+
*5* is used for **one-to-one** calculation in `get_distance_pair` and `get_path_pair` functions on a **contracted** graph.
70+
71+
*1* is used for **one-to-many** calculation in `get_distance_matrix` function on a **non-contracted** graph.
72+
73+
*6* and *7* are available for **one-to-many** calculation in `get_distance_matrix` function on a **contracted** graph.
74+
75+
#Examples and applications using `cppRouting`
76+
77+
see : https://github.com/vlarmet/cppRouting/blob/master/README.md

0 commit comments

Comments
 (0)