|
3 | 3 | #' @param Graph An object generated by makegraph(), cpp_simplify() or cpp_contract() function.
|
4 | 4 | #' @param from A vector of one or more vertices from which distances are calculated (origin).
|
5 | 5 | #' @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 |
6 | 7 | #' @param allcores Logical. If TRUE, all cores are used.
|
7 | 8 | #' @return Matrix of shortest distances.
|
8 | 9 | #' @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 |
10 | 15 | #' @examples
|
11 | 16 | #' #Data describing edges of the graph
|
12 | 17 | #' edges<-data.frame(from_vertex=c(0,0,1,1,2,2,3,4,4),
|
|
25 | 30 | #' print(dir_dist)
|
26 | 31 | #' print(non_dir_dist)
|
27 | 32 |
|
28 |
| -get_distance_matrix<-function(Graph,from,to,allcores=FALSE){ |
| 33 | +get_distance_matrix<-function(Graph,from,to,algorithm="phast",allcores=FALSE){ |
29 | 34 | if (any(is.na(from))) stop("NAs are not allowed in origin/destination nodes")
|
30 | 35 | if (any(is.na(to))) stop("NAs are not allowed in origin/destination nodes")
|
31 | 36 | from<-as.character(from)
|
@@ -60,23 +65,59 @@ get_distance_matrix<-function(Graph,from,to,allcores=FALSE){
|
60 | 65 | }
|
61 | 66 |
|
62 | 67 | 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 | + |
67 | 81 |
|
68 | 82 | }
|
69 | 83 | 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) |
72 | 86 | }
|
| 87 | + } |
| 88 | + else{ |
73 | 89 |
|
| 90 | + test<-data.frame(id=Graph$dict$id,rank=(Graph$nbnode)-Graph$rank) |
74 | 91 |
|
| 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 | + } |
75 | 119 | }
|
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 | + |
80 | 121 |
|
81 | 122 | }
|
82 | 123 |
|
|
0 commit comments