Skip to content

Commit 97009c5

Browse files
authored
Add files via upload
1 parent c2d422b commit 97009c5

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed

R/get_detour.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
get_detour<-function(Graph,from,to,extra=NULL,allcores=FALSE){
2+
if (length(from)!=length(to)) stop("From and to have not the same length")
3+
if (is.null(extra)) stop("No extra cost")
4+
extra<-as.numeric(extra)
5+
if (extra<=0) stop("extra must be positive")
6+
if (any(is.na(cbind(from,to)))) stop("NAs are not allowed in origin/destination nodes")
7+
from<-as.character(from)
8+
9+
to<-as.character(to)
10+
allnodes<-c(from,to)
11+
if (sum(allnodes %in% Graph$dict$ref)<length(allnodes)) stop("Some nodes are not in the graph")
12+
13+
from_id<-Graph$dict$id[match(from,Graph$dict$ref)]
14+
to_id<-Graph$dict$id[match(to,Graph$dict$ref)]
15+
16+
if (allcores==FALSE) res<-Detour(from_id,to_id,Graph$data$from,Graph$data$to,Graph$data$dist,Graph$nbnode,t=extra,Graph$dict$ref)
17+
else {
18+
numWorkers <- parallel::detectCores()
19+
cl <- parallel::makeCluster(numWorkers, type = "PSOCK")
20+
parallel::clusterEvalQ(cl = cl,library("cppRouting"))
21+
chunks <- parallel::splitIndices(length(from), ncl = numWorkers)
22+
mylist<-lapply(chunks,function(x) from_id[x])
23+
mylist2<-lapply(chunks,function(x) to_id[x])
24+
25+
26+
res<-parallel::clusterMap(cl,Detour,dep=mylist,arr=mylist2,
27+
MoreArgs = list(gfrom=Graph$data$from,gto=Graph$data$to,gw=Graph$data$dist,NbNodes=Graph$nbnode,t=extra,dict=Graph$dict$ref))
28+
parallel::stopCluster(cl)
29+
30+
res<-do.call(c,res)
31+
}
32+
33+
names(res)<-paste0(from,"_",to)
34+
return(res)
35+
}
36+

R/get_distance_mat.R

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#' print(dir_dist)
2525
#' print(non_dir_dist)
2626

27-
2827
get_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

Comments
 (0)