dopar%和%for each%无法用于gmapsdistance
我目前正在尝试使用gmapsdistance
package(drive Mode = bicycling
)应用并行化,以加快包含循环的
<强>依次。如果我将的计算。代码> 1
origin和100
目的地 x 的每个值。我有500个x值对应于500对1
onement和100
目的地,我需要为这500对计算gmapsdistance
gmapsdistance
直接应用于所有500对1
onect> onect> arount> 100 目标,我经常达到Google Maps API施加的限制。因此,我决定使用1
Origin和100
目的地的每个单独的 gmapsdistance 使用 for 循环。根据我的初步检查,在我看来,通过40-60个元素分解$ 100 $的目的地将是加快元素的最佳方法。
问题:我如何正确应用和%dopar%
以加快1
onect> onement and <%代码> 100 目的地?我尝试了以下代码,但是foreach%的%似乎根本无法完成计算gmapsdistance
的工作。
origin <- "30.458788+-91.66276"
destination
n.dest <- length(destination)
res <- list()
library(doParallel)
ncores <- detectCores()-8
registerDoParallel(ncores)
result <- foreach(j=seq(n.dest/40+1), .combine = rbind,
.packages = "gmapsdistance", .multicombine = TRUE)
%dopar% {
j.start <- (j-1)*40+1
j.end <- min(j*40, n.dest)
dest.tmp <- destination[j.start:j.end]
matrix <- gmapsdistance(origin, destination,
mode='bicycling', shape="long",
key = [key to Google Maps API here])
#if(length(dest.tmp)==1){
# res[[j]] <- (matrix$Distance)/1609
#} else {
# matrix$Distance$Distance <- #as.vector(matrix$Distance$Distance)/1609
# res[[j]] <- matrix$Distance$Distance
}
}
示例数据
destination <-
"31.458788+-90.66276"
"32.458788+-90.66276"
"30.457391+-90.637622"
"31.40022+-91.007789"
"32.400216+-91.007899"
"31.745092+-90.72207"
"30.355628+-91.039009"
"32.755678+-90.715828"
"31.752829+-90.724454"
"32.758509+-90.715734"
"30.758526+-90.715734"
"31.762113+-90.71588"
"31.34873+-91.043439"
"30.527764+-90.21993"
"31.525427+-90.215216"
"32.526959+-90.213706"
"30.504474+-90.183939"
"31.875694+-90.393612"
"30.867884+-90.394896"
"31.865573+-90.393505"
"30.486356+-89.759238"
"32.189036+-90.575873"
"31.188247+-90.581814"
"31.785027+-89.843494"
"32.78316+-89.831685"
"30.78316+-89.831685"
"31.765321+-89.824013"
"32.765321+-89.824013"
"30.605845+-90.067735"
"31.605022+-90.067732"
"30.603925+-90.067727"
"31.599866+-90.067711"
"31.599554+-90.067711"
"31.59933+-90.067711"
"32.88156+-91.48726"
"31.17727+-91.913525"
"30.476405+-90.153457"
"31.322707+-89.950904"
"32.621621+-91.80243"
"30.624892+-91.802716"
"31.633465+-91.801092"
"31.638366+-91.802947"
"31.309983+-89.927821"
"30.309662+-89.927826"
"30.03933+-89.579129"
"31.31023+-89.923957"
"31.309487+-89.923367"
"30.309488+-89.923119"
"31.045959+-89.573218"
"30.517189+-90.099222"
"30.441837+-90.149984"
"30.442301+-90.150707"
"30.440868+-90.149794"
"32.312126+-89.91667"
"30.439829+-90.146798"
"30.34343+-90.83843"
"30.3423+-90.837489"
"30.340156+-90.83728"
"30.343552+-90.84245"
"30.349831+-90.881005"
"30.341989+-90.840575"
"30.341655+-90.841115"
"30.343312+-90.848024"
"30.3405+-90.84298"
"30.432235+-90.140964"
"30.50413+-90.092005"
"30.34301+-90.850283"
"30.329542+-90.325884"
"30.328089+-90.325728"
"30.328336+-90.325734"
"30.428608+-90.153354"
"30.328893+-90.323734"
"30.329036+-90.323154"
"30.324194+-90.325706"
"30.329507+-90.317416"
"30.325438+-90.328082"
"30.385365+-90.17449"
"30.3217+-90.319121"
"30.385387+-90.173534"
"30.409225+-90.140713"
"30.409225+-90.140713"
"30.408096+-90.1407"
"31.408096+-90.1407"
"30.403206+-90.142447"
"30.399693+-90.143573"
"30.40238+-90.137045"
"30.402334+-90.137005"
"30.395251+-90.145992"
"30.318924+-90.873704"
"30.317151+-90.874869"
"30.315461+-90.879247"
"30.395372+-90.142445"
"30.383176+-90.147925"
"30.380337+-90.149091"
"30.389491+-90.145018"
"30.333701+-90.176533"
"30.303498+-90.889794"
"30.372756+-90.14738"
"30.300538+-90.189769"
"30.290752+-90.264155"
"30.301205+-90.185661"
I am currently trying to apply parallelization with the gmapsdistance
package (driving mode = bicycling
) to speed up the computation of the for
loop that contains 1
origin and 100
destinations per each value of X. I have 500 values of X corresponding to 500 pairs of 1
origin and 100
destinations, and I need to compute the gmapsdistance
for these 500 pairs sequentially. If I apply the gmapsdistance
function directly to all 500 pairs of 1
origin and 100
destination, I often hit the limit imposed by Google Maps API. Thus, I decided to compute gmapsdistance
of each individual set of 1
origin and 100
destinations using for
loop. Based on my initial inspection, it seems to me that breaking down the $100$ destinations by batches of 40-60 elements would be the best way to speed it up.
Question: How do I correctly apply foreach
and %dopar%
to speed up the computation of 1
origin and 100
destinations? I tried with the following code but the %foreach% seems not to do its job of computing the gmapsdistance
at all.
origin <- "30.458788+-91.66276"
destination
n.dest <- length(destination)
res <- list()
library(doParallel)
ncores <- detectCores()-8
registerDoParallel(ncores)
result <- foreach(j=seq(n.dest/40+1), .combine = rbind,
.packages = "gmapsdistance", .multicombine = TRUE)
%dopar% {
j.start <- (j-1)*40+1
j.end <- min(j*40, n.dest)
dest.tmp <- destination[j.start:j.end]
matrix <- gmapsdistance(origin, destination,
mode='bicycling', shape="long",
key = [key to Google Maps API here])
#if(length(dest.tmp)==1){
# res[[j]] <- (matrix$Distance)/1609
#} else {
# matrix$Distance$Distance <- #as.vector(matrix$Distance$Distance)/1609
# res[[j]] <- matrix$Distance$Distance
}
}
Sample Data
destination <-
"31.458788+-90.66276"
"32.458788+-90.66276"
"30.457391+-90.637622"
"31.40022+-91.007789"
"32.400216+-91.007899"
"31.745092+-90.72207"
"30.355628+-91.039009"
"32.755678+-90.715828"
"31.752829+-90.724454"
"32.758509+-90.715734"
"30.758526+-90.715734"
"31.762113+-90.71588"
"31.34873+-91.043439"
"30.527764+-90.21993"
"31.525427+-90.215216"
"32.526959+-90.213706"
"30.504474+-90.183939"
"31.875694+-90.393612"
"30.867884+-90.394896"
"31.865573+-90.393505"
"30.486356+-89.759238"
"32.189036+-90.575873"
"31.188247+-90.581814"
"31.785027+-89.843494"
"32.78316+-89.831685"
"30.78316+-89.831685"
"31.765321+-89.824013"
"32.765321+-89.824013"
"30.605845+-90.067735"
"31.605022+-90.067732"
"30.603925+-90.067727"
"31.599866+-90.067711"
"31.599554+-90.067711"
"31.59933+-90.067711"
"32.88156+-91.48726"
"31.17727+-91.913525"
"30.476405+-90.153457"
"31.322707+-89.950904"
"32.621621+-91.80243"
"30.624892+-91.802716"
"31.633465+-91.801092"
"31.638366+-91.802947"
"31.309983+-89.927821"
"30.309662+-89.927826"
"30.03933+-89.579129"
"31.31023+-89.923957"
"31.309487+-89.923367"
"30.309488+-89.923119"
"31.045959+-89.573218"
"30.517189+-90.099222"
"30.441837+-90.149984"
"30.442301+-90.150707"
"30.440868+-90.149794"
"32.312126+-89.91667"
"30.439829+-90.146798"
"30.34343+-90.83843"
"30.3423+-90.837489"
"30.340156+-90.83728"
"30.343552+-90.84245"
"30.349831+-90.881005"
"30.341989+-90.840575"
"30.341655+-90.841115"
"30.343312+-90.848024"
"30.3405+-90.84298"
"30.432235+-90.140964"
"30.50413+-90.092005"
"30.34301+-90.850283"
"30.329542+-90.325884"
"30.328089+-90.325728"
"30.328336+-90.325734"
"30.428608+-90.153354"
"30.328893+-90.323734"
"30.329036+-90.323154"
"30.324194+-90.325706"
"30.329507+-90.317416"
"30.325438+-90.328082"
"30.385365+-90.17449"
"30.3217+-90.319121"
"30.385387+-90.173534"
"30.409225+-90.140713"
"30.409225+-90.140713"
"30.408096+-90.1407"
"31.408096+-90.1407"
"30.403206+-90.142447"
"30.399693+-90.143573"
"30.40238+-90.137045"
"30.402334+-90.137005"
"30.395251+-90.145992"
"30.318924+-90.873704"
"30.317151+-90.874869"
"30.315461+-90.879247"
"30.395372+-90.142445"
"30.383176+-90.147925"
"30.380337+-90.149091"
"30.389491+-90.145018"
"30.333701+-90.176533"
"30.303498+-90.889794"
"30.372756+-90.14738"
"30.300538+-90.189769"
"30.290752+-90.264155"
"30.301205+-90.185661"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论