@@ -50,7 +50,7 @@ In `cppRouting`, heuristic function `h` is defined such that : h(xi,yi,xdestinat
5050
5151By 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+ ### Let's see the benefit of the A\* algorithm with the french road network :
5454
5555``` r
5656library(cppRouting )
@@ -80,14 +80,28 @@ head(roads)
8080 ## 5 4 113129 4.9680000
8181 ## 6 5 4 1.6680000
8282
83+ ### Head of coordinates data
84+
85+ ``` r
86+ head(coord )
87+ ```
88+
89+ ## ID X Y
90+ ## 1 0 805442.8 6458384
91+ ## 2 1 552065.9 6790520
92+ ## 3 2 556840.2 6790475
93+ ## 4 3 554883.7 6790020
94+ ## 5 4 548345.2 6791000
95+ ## 6 5 547141.3 6790434
96+
8397### Instantiate the graph
8498
8599``` r
86100# Instantiate a graph with coordinates
87101graph <- makegraph(roads ,directed = T ,coords = coord )
88102```
89103
90- ### Dijkstra algorithm
104+ ### Run Dijkstra algorithm for finding minimum cost between pairs of nodes
91105
92106``` r
93107# Generate 2000 random origin and destination nodes
@@ -104,7 +118,7 @@ pair_dijkstra<-get_distance_pair(graph,origin,destination)
104118 ## Running Dijkstra ...
105119
106120 ## user system elapsed
107- ## 55.91 0.00 56.34
121+ ## 54.69 0.00 54.79
108122
109123``` r
110124# Benchmarks parallel
@@ -117,9 +131,9 @@ pair_dijkstra_par<-get_distance_pair(graph,origin,destination,allcores = TRUE)
117131 ## Running Dijkstra ...
118132
119133 ## user system elapsed
120- ## 70.06 0.07 18.46
134+ ## 70.31 0.00 17.97
121135
122- ### A\* algorithm
136+ ### Run A\* algorithm
123137
124138Coordinates are defined in meters and max speed is 110km/h; so for the heuristic function to be admissible, the constant equal 110/0.06 :
125139
@@ -133,7 +147,7 @@ pair_astar<-get_distance_pair(graph,origin,destination,algorithm = "A*",constant
133147 ## Running A* ...
134148
135149 ## user system elapsed
136- ## 30.30 2.54 33.18
150+ ## 30.42 2.28 32.79
137151
138152``` r
139153# A* parallel
@@ -145,7 +159,7 @@ pair_astar_par<-get_distance_pair(graph,origin,destination,algorithm = "A*",cons
145159 ## Running A* ...
146160
147161 ## user system elapsed
148- ## 43.93 0.58 11.67
162+ ## 44.38 0.72 11.65
149163
150164A\* is the fastest one and the output is the same.
151165
@@ -154,12 +168,12 @@ head(cbind(pair_dijkstra,pair_astar,pair_dijkstra_par,pair_astar_par))
154168```
155169
156170 ## pair_dijkstra pair_astar pair_dijkstra_par pair_astar_par
157- ## [1,] 191.4190 191.4190 191.4190 191.4190
158- ## [2,] 453.8487 453.8487 453.8487 453.8487
159- ## [3,] 228.9699 228.9699 228.9699 228.9699
160- ## [4,] 275.5419 275.5419 275.5419 275.5419
161- ## [5,] 265.9542 265.9542 265.9542 265.9542
162- ## [6,] 433.7142 433.7142 433.7142 433.7142
171+ ## [1,] 462.4514 462.4514 462.4514 462.4514
172+ ## [2,] 114.1351 114.1351 114.1351 114.1351
173+ ## [3,] 238.4923 238.4923 238.4923 238.4923
174+ ## [4,] 490.4173 490.4173 490.4173 490.4173
175+ ## [5,] 218.5715 218.5715 218.5715 218.5715
176+ ## [6,] 482.1238 482.1238 482.1238 482.1238
163177
164178Applications
165179============
@@ -193,7 +207,7 @@ df$ratio<-df$NB_D201/df$pop
193207### Second step
194208
195209``` r
196- # Isochrone around each commune with time limit of 15 minutes
210+ # Isochrone around each commune with time limit of 15 minutes (few seconds to compute)
197211iso2 <- get_isochrone(graph ,from = ndcom $ id_noeud ,lim = 15 )
198212# Convert list to long data frame
199213df2 <- stack(setNames(iso2 , seq_along(iso2 )))
@@ -221,7 +235,7 @@ p<-ggplot()+
221235p
222236```
223237
224- ![ ] ( readme_files/figure-markdown_github/unnamed-chunk-7 -1.png )
238+ ![ ] ( readme_files/figure-markdown_github/unnamed-chunk-8 -1.png )
225239
226240Application 2 : Calculate the minimum travel time to the closest maternity ward in France
227241-----------------------------------------------------------------------------------------
@@ -237,7 +251,7 @@ The shortest travel time is computed with the `cppRouting` function `get_distanc
237251We compute travel time from all commune nodes to all maternity ward nodes (e.g ~ 36000\* 400 distances).
238252
239253``` r
240- # Distance matrix
254+ # Distance matrix (around 10 minutes to compute)
241255dists <- get_distance_matrix(graph ,
242256 from = ndcom $ id_noeud ,
243257 to = ndcom $ id_noeud [ndcom $ com %in% maternity $ CODGEO ],
@@ -263,7 +277,7 @@ p<-ggplot()+
263277p
264278```
265279
266- ![ ] ( readme_files/figure-markdown_github/unnamed-chunk-10 -1.png )
280+ ![ ] ( readme_files/figure-markdown_github/unnamed-chunk-11 -1.png )
267281
268282Benchmark with other R packages
269283===============================
@@ -285,12 +299,12 @@ destination<-sample(unique(roads$from),1000,replace = F)
285299graph_igraph <- graph_from_data_frame(roads ,directed = TRUE )
286300
287301system.time(
288- test_igraph <- distances(graph_igraph ,origin ,to = destination ,weights = E(graph_igraph )$ weight )
302+ test_igraph <- distances(graph_igraph ,origin ,to = destination ,weights = E(graph_igraph )$ weight , mode = " out " )
289303)
290304```
291305
292306 ## user system elapsed
293- ## 120.42 0.07 121.51
307+ ## 86.48 0.06 86.69
294308
295309``` r
296310# dodgr
@@ -311,7 +325,7 @@ test_dodgr<-dodgr_dists(graph=data.frame(roads2),from=origin,to=destination,para
311325```
312326
313327 ## user system elapsed
314- ## 87.57 0.08 88 .27
328+ ## 85.02 0.08 85 .27
315329
316330``` r
317331# cppRouting
@@ -321,7 +335,7 @@ test_cpp<-get_distance_matrix(graph,origin,destination,allcores = FALSE)
321335```
322336
323337 ## user system elapsed
324- ## 54.97 0.03 55.38
338+ ## 54.35 0.02 54.45
325339
326340### Distance matrix : parallel
327341
@@ -333,7 +347,7 @@ test_dodgr<-dodgr_dists(graph=data.frame(roads2),from=origin,to=destination,para
333347```
334348
335349 ## user system elapsed
336- ## 125.91 0.61 33.93
350+ ## 118.82 0.36 31.42
337351
338352``` r
339353# cppRouting
@@ -343,7 +357,7 @@ test_cpp<-get_distance_matrix(graph,origin,destination,allcores = TRUE)
343357```
344358
345359 ## user system elapsed
346- ## 70.85 0.03 18.28
360+ ## 69.43 0.00 17.73
347361
348362Benchmarking on shortest paths by pairs
349363---------------------------------------
@@ -359,7 +373,7 @@ test_dodgr<-dodgr_paths(graph=data.frame(roads2),from=origin,to=destination,pair
359373```
360374
361375 ## user system elapsed
362- ## 534.82 20.57 557.50
376+ ## 524.96 18.16 544.13
363377
364378``` r
365379# cppRouting
@@ -371,7 +385,7 @@ test_cpp<-get_path_pair(graph,origin,destination,algorithm = "A*",constant=110/0
371385 ## Running A* ...
372386
373387 ## user system elapsed
374- ## 7.90 0.00 7.91
388+ ## 7.88 0.01 7.90
375389
376390### Test similarity of the first travel
377391
@@ -380,13 +394,13 @@ test_cpp<-get_path_pair(graph,origin,destination,algorithm = "A*",constant=110/0
380394length(test_dodgr [[1 ]][[1 ]])
381395```
382396
383- ## [1] 274
397+ ## [1] 324
384398
385399``` r
386400length(test_cpp [[1 ]])
387401```
388402
389- ## [1] 274
403+ ## [1] 324
390404
391405``` r
392406# Setdiff
0 commit comments