2424# ' print(dir_dist)
2525# ' print(non_dir_dist)
2626
27-
2827get_distance_matrix <- function (Graph ,from ,to ,allcores = FALSE ){
2928 if (any(is.na(from ))) stop(" NAs are not allowed in origin/destination nodes" )
3029 if (any(is.na(to ))) stop(" NAs are not allowed in origin/destination nodes" )
@@ -41,16 +40,33 @@ get_distance_matrix<-function(Graph,from,to,allcores=FALSE){
4140 numWorkers <- parallel :: detectCores()
4241 cl <- parallel :: makeCluster(numWorkers , type = " PSOCK" )
4342 parallel :: clusterEvalQ(cl = cl ,library(" cppRouting" ))
44- chunks <- parallel :: splitIndices(length(from ), ncl = numWorkers )
45- mylist <- lapply(chunks ,function (x ) from_id [x ])
46- # mylist2<-lapply(chunks,function(x) to_id[x])
47- res <- parallel :: clusterMap(cl ,Dijkstra_mat ,dep = mylist ,
48- MoreArgs = list (arr = to_id ,gfrom = Graph $ data $ from ,gto = Graph $ data $ to ,gw = Graph $ data $ dist ,NbNodes = Graph $ nbnode ))
49- parallel :: stopCluster(cl )
43+
44+ if (length(to )< length(from )){
45+ chunks <- parallel :: splitIndices(length(to ), ncl = numWorkers )
46+ mylist <- lapply(chunks ,function (x ) to_id [x ])
47+ # mylist2<-lapply(chunks,function(x) to_id[x])
48+ res <- parallel :: clusterMap(cl ,Dijkstra_mat ,dep = mylist ,
49+ MoreArgs = list (arr = from_id ,gfrom = Graph $ data $ to ,gto = Graph $ data $ from ,gw = Graph $ data $ dist ,NbNodes = Graph $ nbnode ))
50+ parallel :: stopCluster(cl )
51+ }
52+ else {
53+ chunks <- parallel :: splitIndices(length(from ), ncl = numWorkers )
54+ mylist <- lapply(chunks ,function (x ) from_id [x ])
55+ # mylist2<-lapply(chunks,function(x) to_id[x])
56+ res <- parallel :: clusterMap(cl ,Dijkstra_mat ,dep = mylist ,
57+ MoreArgs = list (arr = to_id ,gfrom = Graph $ data $ from ,gto = Graph $ data $ to ,gw = Graph $ data $ dist ,NbNodes = Graph $ nbnode ))
58+ parallel :: stopCluster(cl )
59+ }
60+
5061 res <- do.call(rbind ,res )
5162
5263 }
53- else res <- Dijkstra_mat(Graph $ data [,1 ],Graph $ data [,2 ],Graph $ data [,3 ],Graph $ nbnode ,from_id ,to_id )
64+ else {
65+ if (length(to )< length(from )) res <- Dijkstra_mat(Graph $ data [,2 ],Graph $ data [,1 ],Graph $ data [,3 ],Graph $ nbnode ,to_id ,from_id )
66+ else res <- Dijkstra_mat(Graph $ data [,1 ],Graph $ data [,2 ],Graph $ data [,3 ],Graph $ nbnode ,from_id ,to_id )
67+ }
68+
69+ if (length(to )< length(from )) res <- t(res )
5470
5571 rownames(res )<- from
5672 colnames(res )<- to
0 commit comments