从R并行运行NetLogo模拟 [英] Running NetLogo simulation in parallel from R

查看:135
本文介绍了从R并行运行NetLogo模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何并行运行以下NetLogo仿真.

How would it be possible to run the following the NetLogo simulations in parallel.

library(RNetLogo)
path.to.NetLogo <- "C:/Program Files (x86)/NetLogo 5.1.0" #change this path to your Netlogo directory
NLStart(path.to.NetLogo, nl.version=5)
#open specific model from NetLogo then.    

while(i < 0.123)
{

NLCommand("set beta-exit", i)
NLCommand("setup");
a=NLReport("count inboxturtles with [exit = true]");
NLCommand ("go");
e=((NLReport("total-time"))/a)

i=i+0.009;
}

考虑一下,这句话:

NLCommand ("go");

需要最多的时间来执行,应该并行运行.我希望能以某种方式做到这一点而无需打开NetLogo的多个实例.

requires the most time to execute and should be run in parallel. I hoping to do it somehow without opening multiple instance of NetLogo.

使问题更清晰:

前提::Behavior Space并行运行NetLogo模拟.

Premise: Behaviour Space runs NetLogo simulations in parallel.

目标:要使用从R开始的同一NetLogo实例并并行运行while循环的仿真.

Objective: To use the same NetLogo instance started from R and run the simulations of while loop in parallel.

推荐答案

我假设您要进行实验,更改参数beta-exit的值,并并行使用计算机上的所有可用内核.从R中开始,这意味着打开同一NetLogo模型的多个实例,每个实例都在不同的内核上运行(这与您声明的目标略有不同).

I assume you want to run an experiment, varying the value of your parameter beta-exit and using all available cores on your computer in parallel. From R this means opening multiple instances of the same NetLogo model, each running on a different core (which is slightly different from your stated objective).

RNetLogo软件包的创建者Jan Thiele实际上已经为此写了一个小插图(

Jan Thiele, the creator of the RNetLogo-package, has actually written a vignette about this (Link).

在您的情况下,仅更改一个参数,他的示例代码应该正是您想要的.这是针对您的问题的一些改编:

In your case, varying only one parameter, his example-code should be exactly what you want. Here it is with some adaptations for your question:

gui <- TRUE
nl.path <- "C:/Program Files (x86)/NetLogo 5.1.0"
model.path <- "C:/..."

2.辅助功能:

## To start NetLogo and open desired model
prepro <- function(gui, nl.path, model.path) {
  library(RNetLogo)
  NLStart(nl.path, gui=gui)
  NLLoadModel(model.path)
}

## simulation function
simfun <- function(i_value) {
  NLCommand("set beta-exit", i_value)
  NLCommand("setup")
  a <- NLReport("count inboxturtles with [exit = true]")
  NLCommand ("go")
  e <- (NLReport("total-time"))/a
  ret <- data.frame(count = a, time = e)
  return(ret)
}

## To close NetLogo
postpro <- function(x) {
  NLQuit()
}

3.设置并行计算:

library(parallel)
processors <- detectCores()
cl <- makeCluster(processors, outfile="./log.txt") 
# Logfile in working directory, oftentimes helpful as there is no console output

## Extension: If you define your own functions that are to be called 
## from within the simulation, they need to be made known to each of the cores
clusterExport(cl, list("own_function1", "own_function1")) 

## load NetLogo on each core
invisible(parLapply(cl, 1:processors, prepro, gui=gui, 
                    nl.path=nl.path, model.path=model.path))

## re-set working directory for each cluster (relevant for logfile).
## There's probably a more elegant way to do this, but it gets the job done.
clusterEvalQ(cl, setwd("C:/DESIRED_WD"))

4.运行并行仿真:

## create vector of beta-exit values
i <- seq(0.006, 0.123, 0.009)

## run simulations
result.par <- parSapply(cl, i, simfun)

5.退出NetLogo并停止群集:

invisible(parLapply(cl, 1:processors, postpro))
stopCluster(cl)

您可能还想在中检出其他用于并行计算的功能.可以用来代替parSapply()的snow-package .

You might also want to check out other functions for parallel computing in the snow-package that can be used instead of parSapply().

这篇关于从R并行运行NetLogo模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆