RNetLogo: 1. Linking R with NetLogo

Agent based modeling is a simulation approach, that studies social systems from the complex adaptive systems prospective. Usage of ABM is limited to research, although I have already seen some private companies using it for profit.

NetLogo simulation
NetLogo simulation

The most popular simulation platform is NetLogo, which uses functional programming language called Logo. Unfortunately, NetLogo itself doesn't support parallel computing and that's shame, because ABM is resource intensive. Actually, there is one workaround that I know of.

Prerequsities:

  • R: Statistical programming language that can be obtained on https://www.r-project.org/. I recommend downloading IDE called Rstudio
  • NetLogo
  • R library "parallel": provides parallel computing in R
  • R library "RNetLogo": provides interface to use NetLogo from R, reference

Note: One more reason why I use RNetLogo library is the appearance of NetLogo plots and statistical output (it is not suitable for publication)

library("RNetLogo") 
nl.path <- "C:/Program Files/NetLogo 5.3.1/app/" # the path to NetLogo.jar
NLStart(nl.path, gui=FALSE) # true - if you want to run NetLogo with GUI
model.path <- "C:/Program Files/NetLogo 5.3.1/app/models/model.nlogo" # path to your .nlogo model
# set up parameters and initialize model
NLCommand("set parameter1", toString(1.00))
NLCommand("set parameter2", toString(2.00))
NLCommand("setup")
# and run model for 10 000 ticks.
nOfTicks <- 10000
NLCommand(nOfTicks, "go")

This was basic example where I didn't use parallel runs. In the next example I'll provide working code for running NetLogo in parallel.

library(parallel)
processors <- detectCores() # quantity of cores you want to use
cl <- makeCluster(processors)

nl.path <- "C:/Program Files/NetLogo 5.3.1/app/" # the path to NetLogo.jar
model.path <- "C:/Program Files/NetLogo 5.3.1/app/models/model.nlogo" # path to your .nlogo model

# function that starts NetLogo and loads model
preprocessing <- function(dummy, gui, nl.path, model.path) {
  library("RNetLogo")
  NLStart(nl.path, gui=gui)
  NLLoadModel(model.path)
}

simulationFunction <- function(id) {
  NLCommand("set param1", toString(1))
  NLCommand("setup")
  NLDoCommand(10000,"go")
}

# function that quits NetLogo after simulation
postprocessing <- function(x){
  NLQuit()
}

#start simulation
invisible(parLapply(cl, 1:processors, prepro, gui=gui, nl.path=nl.path, model.path=model.path))

result.par <- parSapply(cl, id, simulationFunction)

# stop simulation
invisible(parLapply(cl, 1:processors, postpro) 

stopCluster(cl)

Note: I hope that comments are informative enough and you won't have any problems with running your model. If you have any questions, please write it below in the comments, I will answer as soon possible.

 

 

3 thoughts on “RNetLogo: 1. Linking R with NetLogo”

  1. Hi Matej,
    very nice example, thanks a lot for sharing.
    I am not sure, but maybe on line 27 you should change "prepro" to "preprocessing".
    Thanks, BR
    Gabee.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.