我試圖在筆記本上使用並行處理在R中運行NetLogo模擬(使用RNetLogo軟件包)。我試圖用3個(即0,25和50)不同的「最小間隔」值來評估「女性的餵食」。對於每個「最小分離」值,我想重複模擬10次。我可以使用lapply
正確運行所有內容,但我遇到了parLapply
問題。我剛開始使用「parallel」包,所以我確定它是語法中的一些東西。使用parLapply運行NetLogo模型遇到問題
#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
工作。或者,我想我可以修改parLapply
中的res
,它的作用是讓列表的長度爲max_sep
,而不是30.但是,我似乎無法做到這一點。任何幫助將非常感激!
在此先感謝。
謝謝Steve!這就說得通了。現在就開始工作了。 – user2359494