我正在尝试使用笔记本电脑上的并行处理在 R 中运行 NetLogo 模拟(使用 RNetLogo 包)。我正在尝试使用 3 个(即 0、25 和 50)个不同的“最小分离”值来评估“女性的 t 喂养”。对于每个“最小分离”值,我想复制模拟 10 次。我可以正确运行所有内容,lapply
但我在使用parLapply
. 我刚刚开始使用包“parallel”,所以我确定它是语法中的东西。
#Set up clusters for parallel
processors <- detectCores()
cl <- makeCluster(processors)
#Simulation
sim3 <- function(min_sep) {
NLCommand("set minimum-separation ", min_sep, "setup")
ret <- NLDoReport(720, "go", "[t-feeding] of females", as.data.frame=TRUE)
tot <- sum(ret[,1])
return(tot)
}
#Replicate simulations 10 times using lapply and create boxplots. This one works.
rep.sim3 <- function(min_sep, rep) {
return(
lapply(min_sep, function(min_sep) {
replicate(rep, sim3(min_sep))
})
)
}
d <- seq(0,50,25)
res <- rep.sim3(d,10)
boxplot(res,names=d, xlab="Minimum Separation", ylab="Time spent feeding")
#Replicate simulations 10 times using parLapply. This one does not work.
rep.sim3 <- function(min_sep, rep) {
return(
parLapply(cl, min_sep, function(min_sep) {
replicate(rep, sim3(min_sep))
})
)
}
d <- seq(0,50,25)
res <- rep.sim3(d,10)
# Error in checkForRemoteErrors(val) : 3 nodes produced errors; first error: could not find function "sim3"
#Replicate simulations 10 times using parLapply. This one does work but creates a list of the wrong length and therefore the boxplot cannot be plotted correctly.
rep.sim3 <- function(min_sep, rep) {
return(
parLapply(cl, replicate(rep, d), sim3))
}
d <- seq(0,50,25)
res <- rep.sim3(d,10)
理想情况下,我想做第一个parLapply
工作。或者,我想我可以修改有效res
的parLapply
列表,使列表的长度max_sep
不是 30。但是,我似乎不能这样做。任何帮助将非常感激!
提前致谢。