Skip to content

Commit 3822dd5

Browse files
authored
Add files via upload
1 parent 640c980 commit 3822dd5

3 files changed

Lines changed: 45 additions & 8 deletions

File tree

R/RcppExports.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ Bidir <- function(dep, arr, gfrom, gto, gw, NbNodes) {
1313
.Call(`_cppRouting_Bidir`, dep, arr, gfrom, gto, gw, NbNodes)
1414
}
1515

16+
NBA <- function(dep, arr, gfrom, gto, gw, NbNodes, lat, lon, k) {
17+
.Call(`_cppRouting_NBA`, dep, arr, gfrom, gto, gw, NbNodes, lat, lon, k)
18+
}
19+
20+
NBA_path <- function(dep, arr, gfrom, gto, gw, NbNodes, lat, lon, k, dict) {
21+
.Call(`_cppRouting_NBA_path`, dep, arr, gfrom, gto, gw, NbNodes, lat, lon, k, dict)
22+
}
23+
1624
Bidir_path <- function(dep, arr, gfrom, gto, gw, NbNodes, dict) {
1725
.Call(`_cppRouting_Bidir_path`, dep, arr, gfrom, gto, gw, NbNodes, dict)
1826
}

R/get_distance_pair.R

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
#' @param Graph An object generated by cppRouting::makegraph() 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. "Dijkstra" for uni-directional Dijkstra, "bi" for bi-directional Dijkstra or "A*".Default to "Dijkstra"
6+
#' @param algorithm character. "Dijkstra" for uni-directional Dijkstra, "bi" for bi-directional Dijkstra, "A*" or "NBA" for New bi-directional A star .Default to "Dijkstra"
77
#' @param constant numeric. Constant to maintain the heuristic function admissible in A* algorithm.
88
#' Default to 1, when cost is expressed in the same unit than coordinates. See details
99
#' @param allcores Logical. If TRUE, all cores are used.
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.
13+
#' In A* algorithms, euclidean distance is used as heuristic function.
1414
#' To understand how A star algorithm work, see https://en.wikipedia.org/wiki/A*_search_algorithm .
1515
#' To understand the importance of constant parameter, see the package description : https://github.com/vlarmet/cppRouting
1616
#'
@@ -31,7 +31,7 @@ get_distance_pair<-function(Graph,from,to,algorithm="Dijkstra",constant=1,allcor
3131

3232

3333
if (!is.null(Graph$coords)){
34-
34+
if (algorithm %in% c("NBA","A*","bi")){
3535
if (algorithm=="A*"){
3636
message("Running A* ...")
3737
if (allcores==FALSE) res<-Astar(from_id,to_id,Graph$data$from,Graph$data$to,Graph$data$dist,Graph$nbnode,Graph$coords[,2],Graph$coords[,3],constant)
@@ -51,6 +51,27 @@ get_distance_pair<-function(Graph,from,to,algorithm="Dijkstra",constant=1,allcor
5151
return(res)
5252
}
5353
}
54+
if (algorithm=="NBA"){
55+
message("Running NBA* ...")
56+
if (allcores==FALSE) res<-NBA(from_id,to_id,Graph$data$from,Graph$data$to,Graph$data$dist,Graph$nbnode,Graph$coords[,2],Graph$coords[,3],constant)
57+
else {
58+
numWorkers <- parallel::detectCores()
59+
cl <- parallel::makeCluster(numWorkers, type = "PSOCK")
60+
parallel::clusterEvalQ(cl = cl,library("cppRouting"))
61+
chunks <- parallel::splitIndices(length(from), ncl = numWorkers)
62+
mylist<-lapply(chunks,function(x) from_id[x])
63+
mylist2<-lapply(chunks,function(x) to_id[x])
64+
65+
66+
res<-parallel::clusterMap(cl,NBA,dep=mylist,arr=mylist2,
67+
MoreArgs = list(gfrom=Graph$data$from,gto=Graph$data$to,gw=Graph$data$dist,NbNodes=Graph$nbnode,lat=Graph$coords$X,lon=Graph$coords$Y,k=constant))
68+
parallel::stopCluster(cl)
69+
res<-c(unlist(res))
70+
return(res)
71+
}
72+
}
73+
74+
5475
if (algorithm=="bi"){
5576
message("Running bidirectional Dijkstra...")
5677
if (allcores==FALSE) res<-Bidir(from_id,to_id,Graph$data$from,Graph$data$to,Graph$data$dist,Graph$nbnode)
@@ -68,8 +89,9 @@ get_distance_pair<-function(Graph,from,to,algorithm="Dijkstra",constant=1,allcor
6889
return(res)
6990
}
7091
}
92+
}
7193

72-
if (!algorithm %in% c("A*","bi")) {
94+
else {
7395
message("Running Dijkstra ...")
7496
if (allcores==FALSE) res<-Dijkstra_early_stop(from_id,to_id,Graph$data$from,Graph$data$to,Graph$data$dist,Graph$nbnode)
7597
else {

R/get_path_pair.R

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
#' @param Graph An object generated by cppRouting::makegraph() function.
44
#' @param from A vector of one or more vertices from which shortest paths are calculated (origin).
55
#' @param to A vector of one or more vertices (destination).
6-
#' @param algorithm character. "Dijkstra" for uni-directional Dijkstra, "bi" for bi-directional Dijkstra or "A*".Default to "Dijkstra"
6+
#' @param algorithm character. "Dijkstra" for uni-directional Dijkstra, "bi" for bi-directional Dijkstra, "A*" or "NBA" for New bi-directional A star .Default to "Dijkstra"
77
#' @param constant numeric. Constant to maintain the heuristic function admissible in A* algorithm.
88
#'Default to 1, when cost is expressed in the same unit than coordinates. See details
99
#' @return List containing shortest path between from and to.
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.
12+
#' In A* algorithms, euclidean distance is used as heuristic function.
1313
#' To understand how A star algorithm work, see https://en.wikipedia.org/wiki/A*_search_algorithm .
1414
#' To understand the importance of constant parameter, see the package description : https://github.com/vlarmet/cppRouting .
1515
#'
@@ -27,19 +27,26 @@ get_path_pair<-function(Graph,from,to,algorithm="Dijkstra",constant=1){
2727

2828

2929
if (!is.null(Graph$coords)){
30-
30+
if (algorithm %in% c("NBA","A*","bi")){
3131
if (algorithm=="A*"){
3232
message("Running A* ...")
3333
res<-Astar_paths(Graph$data$from,Graph$data$to,Graph$data$dist,Graph$nbnode,from_id,to_id,Graph$coords[,2],Graph$coords[,3],constant,Graph$dict$ref)
3434
}
3535

36+
if (algorithm=="NBA"){
37+
message("Running NBA* ...")
38+
res<-NBA_path(from_id,to_id,Graph$data$from,Graph$data$to,Graph$data$dist,Graph$nbnode,Graph$coords[,2],Graph$coords[,3],constant,Graph$dict$ref)
39+
}
40+
41+
3642
if (algorithm=="bi"){
3743
message("Running bidirectional Dijkstra...")
3844
res<-Bidir_path(from_id,to_id,Graph$data$from,Graph$data$to,Graph$data$dist,Graph$nbnode)
3945

46+
}
4047
}
4148

42-
if (!algorithm %in% c("A*","bi")) {
49+
else {
4350
message("Running Dijkstra ...")
4451
res<-Dijkstra_early_stop_path(Graph$data$from,Graph$data$to,Graph$data$dist,Graph$nbnode,from_id,to_id,Graph$dict$ref)
4552

0 commit comments

Comments
 (0)