如何从R并行运行相同的NetLogo仿真?

时间:2019-04-25 13:14:34

标签: r netlogo

我想使用相同的参数运行NetLogo模型的多个实例。我正在使用RNetLogo软件包,并遵循给出的答案here

但是,当我运行代码时,parSapply函数会给我以下错误:

  

checkForRemoteErrors(val)中的错误:一个节点产生了一个错误:   未使用的参数(X [[i]])

我对并行处理是完全陌生的,因此我不确定这是指什么,因为我似乎对所有参数都有选择。

这是我的代码:

#' load the libraries
#' load the parallel package
library(parallel)
library(RNetLogo)

# detect the number of cores available
processors <- detectCores()
processors

# create a cluster
cl <- makeCluster(processors)
cl

# the initialization function
prepro <- function(dummy, gui, nl.path, model.path) {
  library(RNetLogo)
  NLStart(nl.path, gui = gui, nl.jarname = "netlogo-6.0.4.jar")
  NLLoadModel(model.path)
}

#' the function for the model 
simfun <- function(x) {

 #' the reporters for the model 
 vars <- c("ticks", "who", "g" , "sex", "gm_val")
 agents <- "turtles"
 reporters <- sprintf("map [x -> [%s] of x ] sort %s", vars, agents)
 nlogo_ret <- RNetLogo::NLReport(reporters)

  NLCommand("setup")
  run <-
    NLDoReport(
      x,
      "repeat 1000 [go]",
      c("ticks", reporters),
      as.data.frame = T,
      df.col.names = c("ticks", reporters)
    )
}

# the quit function
postpro <- function(x) {
  NLQuit()
}


# set variables for the start up process
# adapt path appropriate (or set an environment variable NETLOGO_PATH)
gui <- TRUE
nl.path <- "C:\\Program Files\\NetLogo 6.0.4\\app"
model.path <-
  "\\trout-migration-full-time-matrix.nlogo" # enter the path for your own computer here 

# load NetLogo in each processor/core
invisible(
  parLapply(
    cl,
    1:processors,
    prepro,
    gui = gui,
    nl.path = nl.path,
    model.path = model.path
  )
)
#' the number of 1000 ticks simulations we want to run
#' sampling takes place every 1000th tick
slice <- 10
print(slice)

result.par <- parSapply(cl, slice, simfun)

我的模型在这里:trout-migration-full-time-matrix.nlogo

0 个答案:

没有答案