Skip to content

Commit 290f554

Browse files
authored
Add files via upload
1 parent 682f3ed commit 290f554

33 files changed

+203
-177
lines changed

R/get_distance_pair.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#' @return Vector of shortest distances.
1111
#' @note 'from' and 'to' must be the same length.
1212
#' @details To perform A*, projected coordinates should be provided in the Graph object.
13+
#' In A* algorithm, euclidean distance is used as heuristic function.
1314
#' To understand how A star algorithm work, see https://en.wikipedia.org/wiki/A*_search_algorithm .
1415
#' To understand the importance of constant parameter, see the package description : https://github.com/vlarmet/cppRouting
1516
#'
@@ -22,10 +23,8 @@ get_distance_pair<-function(Graph,from,to,algorithm="Dijkstra",constant=1,allcor
2223

2324
if (any(is.na(cbind(from,to)))) stop("NAs are not allowed in origin/destination nodes")
2425
from<-as.character(from)
25-
to<-as.character(to)
26-
allnodes<-c(from,to)
27-
if (sum(allnodes %in% Graph$dict$ref)<length(allnodes)) stop("Some nodes are not in the graph")
2826
from_id<-Graph$dict$id[match(from,Graph$dict$ref)]
27+
to<-as.character(to)
2928
to_id<-Graph$dict$id[match(to,Graph$dict$ref)]
3029

3130

R/get_isochrone.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ get_isochrone<-function(Graph,from,lim,setdif=FALSE){
1414

1515
if (any(is.na(from))) stop("NAs are not allowed in origin nodes")
1616
from<-as.character(from)
17-
if (sum(from %in% Graph$dict$ref)<length(from)) stop("Some nodes are not in the graph")
1817
from_id<-Graph$dict$id[match(from,Graph$dict$ref)]
1918
lim<-as.numeric(lim)
2019
if (any(is.na(lim))) stop("NAs are not allowed in cost value(s)")

R/get_multi_paths.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
#' @note Be aware that if 'from' and 'to' have consequent size, output will require much memory space.
99

1010
get_multi_paths<-function(Graph,from,to){
11-
if (any(is.na(from))) stop("NAs are not allowed in origin/destination nodes")
12-
if (any(is.na(to))) stop("NAs are not allowed in origin/destination nodes")
11+
if (any(is.na(cbind(from,to)))) stop("NAs are not allowed in origin/destination nodes")
1312

1413

1514
from<-as.character(from)

R/get_path_pair.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#' @return List containing shortest path between from[i] and to[i].
1010
#' @note 'from' and 'to' must be the same length.
1111
#' @details To perform A*, projected coordinates should be provided in the Graph object.
12+
#' In A* algorithm, euclidean distance is used as heuristic function.
1213
#' To understand how A star algorithm work, see https://en.wikipedia.org/wiki/A*_search_algorithm .
1314
#' To understand the importance of constant parameter, see the package description : https://github.com/vlarmet/cppRouting .
1415
#'
@@ -20,10 +21,8 @@ get_path_pair<-function(Graph,from,to,algorithm="Dijkstra",constant=1){
2021

2122
if (any(is.na(cbind(from,to)))) stop("NAs are not allowed in origin/destination nodes")
2223
from<-as.character(from)
23-
to<-as.character(to)
24-
allnodes<-c(from,to)
25-
if (sum(allnodes %in% Graph$dict$ref)<length(allnodes)) stop("Some nodes are not in the graph")
2624
from_id<-Graph$dict$id[match(from,Graph$dict$ref)]
25+
to<-as.character(to)
2726
to_id<-Graph$dict$id[match(to,Graph$dict$ref)]
2827

2928

man/get_distance_pair.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_path_pair.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.Rmd

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ Data has to be a 3 columns data.frame or matrix containing from, to and a cost/d
4646

4747
The choice between Dijkstra and A* algorithm is available for `get_distance_pair` and `get_path_pair`. In these functions, Dijkstra algorithm is stopped when the destination node is reached.
4848
A* is relevant if geographic coordinates of all nodes are provided. Note that coordinates should be expressed in a projection system.
49-
To be accurate and efficient, `A*` algorithm should use an admissible heuristic function (see https://en.wikipedia.org/wiki/A*_search_algorithm), e.g the cost and geographic coordinates must be expressed in the same unit.
49+
To be accurate and efficient, `A*` algorithm should use an admissible heuristic function (here the Euclidean distance), e.g cost and heuristic function must be expressed in the same unit.
5050
In `cppRouting`, heuristic function `h` is defined such that : h(xi,yi,xdestination,ydestination)/k, with a constant k; 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.
5151

5252
By default, constant is 1 and is designed for graphs with cost expressed in the same unit than coordinates (e.g meters).
5353

54-
55-
###Let's see the benefit of the A* algorithm with the french road network :
54+
##Examples
55+
###Prepare data
5656
```{r pressure, echo=TRUE,message=FALSE}
5757
library(cppRouting)
5858
library(dplyr)
@@ -74,7 +74,7 @@ coord<-read.csv("coordinates.csv",colClasses = c("character","numeric","numeric"
7474
#Head of road network data
7575
head(roads)
7676
```
77-
###Head of coordinates data
77+
####Head of coordinates data
7878
```{r , echo=TRUE,message=FALSE}
7979
head(coord)
8080
```
@@ -85,8 +85,8 @@ head(coord)
8585
#Instantiate a graph with coordinates
8686
graph<-makegraph(roads,directed = T,coords = coord)
8787
```
88-
89-
###Run Dijkstra algorithm for finding minimum cost between pairs of nodes
88+
###Distances by pairs between nodes
89+
####Using Dijkstra algorithm
9090
```{r,echo=TRUE}
9191
#Generate 2000 random origin and destination nodes
9292
origin<-sample(roads$from,2000)
@@ -105,7 +105,7 @@ pair_dijkstra_par<-get_distance_pair(graph,origin,destination,allcores = TRUE)
105105
)
106106
107107
```
108-
###Run A* algorithm
108+
####Using A* algorithm
109109
Coordinates are defined in meters and max speed is 110km/h; so for the heuristic function to be admissible, the constant equal 110/0.06 :
110110
```{r,echo=TRUE}
111111
#A* single node
@@ -119,12 +119,14 @@ pair_astar_par<-get_distance_pair(graph,origin,destination,algorithm = "A*",cons
119119
)
120120
```
121121

122-
A* is the fastest one and the output is the same.
122+
####Output
123123

124124
```{r,echo=TRUE}
125125
head(cbind(pair_dijkstra,pair_astar,pair_dijkstra_par,pair_astar_par))
126126
```
127-
##Compute isochrones
127+
128+
129+
###Compute isochrones
128130
Let's compute isochrones around Dijon city
129131
```{r,echo=TRUE,message=FALSE,warning=FALSE}
130132
#Compute isochrones
@@ -147,8 +149,8 @@ poly2<-st_transform(poly2,"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
147149
dijon=get_map(location=c(lon=5.041140,lat=47.323025),zoom=7, source="google",maptype = "toner-2010")
148150
#Plot the map
149151
p<-ggmap(dijon)+
150-
geom_sf(data=poly2,aes(fill=time),alpha=.5,inherit.aes = FALSE)+
151-
scale_fill_brewer(palette = "RdBu")+
152+
geom_sf(data=poly2,aes(fill=time),alpha=.8,inherit.aes = FALSE)+
153+
scale_fill_brewer(palette = "YlOrRd")+
152154
labs(fill="Minutes")+
153155
ggtitle("Isochrones around Dijon")+
154156
theme(axis.text.x = element_blank(),
@@ -291,6 +293,10 @@ system.time(
291293
test_cpp<-get_distance_matrix(graph,origin,destination,allcores = FALSE)
292294
)
293295
```
296+
####Ouput
297+
```{r,echo=TRUE}
298+
head(cbind(test_igraph[,1],test_dodgr[,1],test_cpp[,1]))
299+
```
294300

295301
###Distance matrix : parallel
296302
```{r,echo=TRUE,warning=FALSE}
@@ -305,7 +311,7 @@ test_cpp<-get_distance_matrix(graph,origin,destination,allcores = TRUE)
305311
)
306312
```
307313

308-
##Benchmarking on shortest paths by pairs
314+
##Benchmark on computing shortest paths by pairs
309315
```{r,echo=TRUE,warning=FALSE}
310316
#Sampling 500 random origin/destination nodes
311317
origin<-sample(unique(roads$from),500,replace = F)

readme.md

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,15 @@ Main functions
4545

4646
The choice between Dijkstra and A\* algorithm is available for `get_distance_pair` and `get_path_pair`. In these functions, Dijkstra algorithm is stopped when the destination node is reached.
4747
A\* is relevant if geographic coordinates of all nodes are provided. Note that coordinates should be expressed in a projection system.
48-
To be accurate and efficient, `A*` algorithm should use an admissible heuristic function (see <https://en.wikipedia.org/wiki/A*_search_algorithm>), e.g the cost and geographic coordinates must be expressed in the same unit.
48+
To be accurate and efficient, `A*` algorithm should use an admissible heuristic function (here the Euclidean distance), e.g cost and heuristic function must be expressed in the same unit.
4949
In `cppRouting`, heuristic function `h` is defined such that : h(xi,yi,xdestination,ydestination)/k, with a constant k; 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.
5050

5151
By default, constant is 1 and is designed for graphs with cost expressed in the same unit than coordinates (e.g meters).
5252

53-
### Let's see the benefit of the A\* algorithm with the french road network :
53+
Examples
54+
--------
55+
56+
### Prepare data
5457

5558
``` r
5659
library(cppRouting)
@@ -82,7 +85,7 @@ head(roads)
8285
## 5 4 113129 4.9680000
8386
## 6 5 4 1.6680000
8487

85-
### Head of coordinates data
88+
#### Head of coordinates data
8689

8790
``` r
8891
head(coord)
@@ -103,7 +106,9 @@ head(coord)
103106
graph<-makegraph(roads,directed = T,coords = coord)
104107
```
105108

106-
### Run Dijkstra algorithm for finding minimum cost between pairs of nodes
109+
### Distances by pairs between nodes
110+
111+
#### Using Dijkstra algorithm
107112

108113
``` r
109114
#Generate 2000 random origin and destination nodes
@@ -120,7 +125,7 @@ pair_dijkstra<-get_distance_pair(graph,origin,destination)
120125
## Running Dijkstra ...
121126

122127
## user system elapsed
123-
## 56.36 0.02 57.24
128+
## 60.49 0.77 61.84
124129

125130
``` r
126131
#Benchmarks parallel
@@ -133,9 +138,9 @@ pair_dijkstra_par<-get_distance_pair(graph,origin,destination,allcores = TRUE)
133138
## Running Dijkstra ...
134139

135140
## user system elapsed
136-
## 71.52 0.08 19.59
141+
## 77.55 1.54 20.98
137142

138-
### Run A\* algorithm
143+
#### Using A\* algorithm
139144

140145
Coordinates are defined in meters and max speed is 110km/h; so for the heuristic function to be admissible, the constant equal 110/0.06 :
141146

@@ -149,7 +154,7 @@ pair_astar<-get_distance_pair(graph,origin,destination,algorithm = "A*",constant
149154
## Running A* ...
150155

151156
## user system elapsed
152-
## 31.37 1.93 33.82
157+
## 32.99 2.03 35.32
153158

154159
``` r
155160
#A* parallel
@@ -161,24 +166,23 @@ pair_astar_par<-get_distance_pair(graph,origin,destination,algorithm = "A*",cons
161166
## Running A* ...
162167

163168
## user system elapsed
164-
## 43.90 0.75 11.97
169+
## 47.74 4.79 13.98
165170

166-
A\* is the fastest one and the output is the same.
171+
#### Output
167172

168173
``` r
169174
head(cbind(pair_dijkstra,pair_astar,pair_dijkstra_par,pair_astar_par))
170175
```
171176

172177
## pair_dijkstra pair_astar pair_dijkstra_par pair_astar_par
173-
## [1,] 347.4129 347.4129 347.4129 347.4129
174-
## [2,] 331.9925 331.9925 331.9925 331.9925
175-
## [3,] 196.9234 196.9234 196.9234 196.9234
176-
## [4,] 427.4617 427.4617 427.4617 427.4617
177-
## [5,] 196.4124 196.4124 196.4124 196.4124
178-
## [6,] 313.8013 313.8013 313.8013 313.8013
178+
## [1,] 427.5181 427.5181 427.5181 427.5181
179+
## [2,] 175.5770 175.5770 175.5770 175.5770
180+
## [3,] 383.0222 383.0222 383.0222 383.0222
181+
## [4,] 240.5373 240.5373 240.5373 240.5373
182+
## [5,] 114.4894 114.4894 114.4894 114.4894
183+
## [6,] 382.0717 382.0717 382.0717 382.0717
179184

180-
Compute isochrones
181-
------------------
185+
### Compute isochrones
182186

183187
Let's compute isochrones around Dijon city
184188

@@ -203,8 +207,8 @@ poly2<-st_transform(poly2,"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
203207
dijon=get_map(location=c(lon=5.041140,lat=47.323025),zoom=7, source="google",maptype = "toner-2010")
204208
#Plot the map
205209
p<-ggmap(dijon)+
206-
geom_sf(data=poly2,aes(fill=time),alpha=.5,inherit.aes = FALSE)+
207-
scale_fill_brewer(palette = "RdBu")+
210+
geom_sf(data=poly2,aes(fill=time),alpha=.8,inherit.aes = FALSE)+
211+
scale_fill_brewer(palette = "YlOrRd")+
208212
labs(fill="Minutes")+
209213
ggtitle("Isochrones around Dijon")+
210214
theme(axis.text.x = element_blank(),
@@ -345,7 +349,7 @@ system.time(
345349
```
346350

347351
## user system elapsed
348-
## 92.93 0.11 93.70
352+
## 92.08 0.01 93.24
349353

350354
``` r
351355
#dodgr
@@ -366,7 +370,7 @@ test_dodgr<-dodgr_dists(graph=data.frame(roads2),from=origin,to=destination,para
366370
```
367371

368372
## user system elapsed
369-
## 89.75 0.08 90.28
373+
## 90.96 0.12 92.15
370374

371375
``` r
372376
#cppRouting
@@ -376,7 +380,21 @@ test_cpp<-get_distance_matrix(graph,origin,destination,allcores = FALSE)
376380
```
377381

378382
## user system elapsed
379-
## 59.76 0.06 60.61
383+
## 62.51 0.34 63.67
384+
385+
#### Ouput
386+
387+
``` r
388+
head(cbind(test_igraph[,1],test_dodgr[,1],test_cpp[,1]))
389+
```
390+
391+
## [,1] [,2] [,3]
392+
## 65497 298.6589 298.6589 298.6589
393+
## 31007 494.4791 494.4791 494.4791
394+
## 90120 471.7311 471.7311 471.7311
395+
## 173218 168.3926 168.3926 168.3926
396+
## 133255 264.4111 264.4111 264.4111
397+
## 207610 301.4288 301.4288 301.4288
380398

381399
### Distance matrix : parallel
382400

@@ -388,7 +406,7 @@ test_dodgr<-dodgr_dists(graph=data.frame(roads2),from=origin,to=destination,para
388406
```
389407

390408
## user system elapsed
391-
## 134.83 0.89 41.32
409+
## 129.23 1.33 34.44
392410

393411
``` r
394412
#cppRouting
@@ -398,10 +416,10 @@ test_cpp<-get_distance_matrix(graph,origin,destination,allcores = TRUE)
398416
```
399417

400418
## user system elapsed
401-
## 76.25 0.10 21.98
419+
## 80.90 0.67 20.98
402420

403-
Benchmarking on shortest paths by pairs
404-
---------------------------------------
421+
Benchmark on computing shortest paths by pairs
422+
----------------------------------------------
405423

406424
``` r
407425
#Sampling 500 random origin/destination nodes
@@ -414,7 +432,7 @@ test_dodgr<-dodgr_paths(graph=data.frame(roads2),from=origin,to=destination,pair
414432
```
415433

416434
## user system elapsed
417-
## 572.20 20.48 599.70
435+
## 554.38 20.50 580.83
418436

419437
``` r
420438
#cppRouting
@@ -426,7 +444,7 @@ test_cpp<-get_path_pair(graph,origin,destination,algorithm = "A*",constant=110/0
426444
## Running A* ...
427445

428446
## user system elapsed
429-
## 8.30 0.04 8.35
447+
## 8.44 0.01 8.49
430448

431449
### Test similarity of the first travel
432450

@@ -435,13 +453,13 @@ test_cpp<-get_path_pair(graph,origin,destination,algorithm = "A*",constant=110/0
435453
length(test_dodgr[[1]][[1]])
436454
```
437455

438-
## [1] 227
456+
## [1] 196
439457

440458
``` r
441459
length(test_cpp[[1]])
442460
```
443461

444-
## [1] 227
462+
## [1] 196
445463

446464
``` r
447465
#Setdiff
-6.09 KB
Loading
-9 Bytes
Loading

0 commit comments

Comments
 (0)