Title: | Provides an Interface to the Agent-Based Modelling Platform 'NetLogo' |
---|---|
Description: | Interface to use and access Wilensky's 'NetLogo' (Wilensky 1999) from R using either headless (no GUI) or interactive GUI mode. Provides functions to load models, execute commands, and get values from reporters. Mostly analogous to the 'NetLogo' 'Mathematica' Link <https://github.com/NetLogo/Mathematica-Link>. |
Authors: | Jan C. Thiele |
Maintainer: | Jan C. Thiele <[email protected]> |
License: | GPL-2 |
Version: | 1.0-4 |
Built: | 2024-11-22 06:23:12 UTC |
Source: | https://github.com/r-forge/rnetlogo |
Interface to use and access Wilensky's NetLogo (Wilensky 1999) from R (R Core Team 2014) using either headless (no GUI) or interactive GUI mode. Provides functions to load models, execute commands, and get values from reporters. Mostly analogous to the NetLogo Mathematica Link https://github.com/NetLogo/Mathematica-Link.
Package: | RNetLogo |
Type: | Package |
Version: | 1.0-4 |
Date: | 2017-06-10 |
License: | GNU GPL v2 |
LazyLoad: | yes |
Start by creating a NetLogo instance by using NLStart
. Then load a model with the function
NLLoadModel
and then use commands and reporters to do what you like.
It is possible to use NetLogo 3D. Just set the is3d
argument in NLStart
to TRUE
. This functionality is experimental. All RNetLogo functions should work in NetLogo 3D as they do in conventional 2D NetLogo except NLSetPatches
, which is not implemented to work with NetLogo 3D properly. NLSetPatchSet
delivers a similar functionality usable also with NetLogo 3D but uses a data.frame instead of a matrix.
Note for MAC users: If you want to run RNetLogo in headless mode (without GUI, i.e. setting argument gui=FALSE
in NLStart
) you have to disable AWT before loading the package. Just execute Sys.setenv(NOAWT=1)
before executing library(RNetLogo)
.
If you want to run RNetLogo in GUI mode you have to start it from the JGR application (see https://cran.r-project.org/package=JGR and the note at http://groups.yahoo.com/group/netlogo-users/message/14817). It can be necessary to run Sys.setenv(NOAWT=1)
before loading the JGR package and run Sys.unsetenv("NOAWT")
before starting JGR via JGR()
.
Note for Linux users: If you want to run RNetLogo in GUI mode you should start RNetLogo from JGR (see https://cran.r-project.org/package=JGR).
Note for Windows 32-bit users: Starting RNetLogo (in GUI mode) on 32-bit Windows (not 64-bit Windows running in 32-bit mode) may fail in R version 2.15.2 and 2.15.3 (see description here: https://stat.ethz.ch/pipermail/r-devel/2013-January/065576.html). The reason could be the increased C stack size in 2.15.2 and 2.15.3. If you execute Cstack_info()
you can see how large the C stack size is. The problem seems to be resolved with 3.0.0.
A workaround is to use R 2.15.1 or 3.x or to start RNetLogo from JGR (see https://cran.r-project.org/package=JGR) or RStudio (see http://www.rstudio.com/).
If you want to increase the Java Heap Space and set other parameters of the Java Virtual Machine (JVM) see notes at NLStart
.
See the tutorial published as Thiele (2014) for an introduction. Example codes for all functions can be found in the folder "examples" in the installation path of the package. For performance notes see the vignette "performanceNotes.pdf" and for an introduction how to run RNetLogo in parallel on multicore computers or clusters/grids see the vignette "parallelProcessing.pdf".
Jan C. Thiele <[email protected]>
For NetLogo see http://ccl.northwestern.edu/netlogo.
For R Extension for NetLogo see http://r-ext.sourceforge.net/.
For Rserve Extension for NetLogo see http://rserve-ext.sourceforge.net/.
The RNetLogo package is analogous to (and inspired by) the NetLogo Mathematica Link https://github.com/NetLogo/Mathematica-Link.
Thiele, J. (2014) R Marries NetLogo: Introduction to the RNetLogo Package. Journal of Statistical Software 58(2) 1-41. http://www.jstatsoft.org/v58/i02/
Thiele, J., Kurth, W., Grimm, V. (2012) RNetLogo: An R Package for Running and Exploring Individual-Based Models Implemented in NetLogo. Methods in Ecology and Evolution 3(3) 480-483.
R Core Team (2014) R: A Language and Environment for Statistical. R Foundation for Statistical Computing.Vienna, Austria. https://www.r-project.org.
Wilensky, U. (1999) NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern University. Evanston, IL.
NLStart,
NLLoadModel,
NLQuit,
rJava package
## Not run: library(RNetLogo) nl.path <- "C:/Program Files/NetLogo 6.0/app" nl.jarname <- "netlogo-6.0.0.jar" NLStart(nl.path, nl.jarname=nl.jarname) model.path <- "/models/Sample Models/Earth Science/Fire.nlogo" NLLoadModel(paste(nl.path,model.path,sep="")) NLCommand("setup") NLDoCommand(10, "go") burned <- NLReport("burned-trees") print(burned) NLQuit() ## End(Not run)
## Not run: library(RNetLogo) nl.path <- "C:/Program Files/NetLogo 6.0/app" nl.jarname <- "netlogo-6.0.0.jar" NLStart(nl.path, nl.jarname=nl.jarname) model.path <- "/models/Sample Models/Earth Science/Fire.nlogo" NLLoadModel(paste(nl.path,model.path,sep="")) NLCommand("setup") NLDoCommand(10, "go") burned <- NLReport("burned-trees") print(burned) NLQuit() ## End(Not run)
NLCommand
executes a NetLogo command (submitted as a string)
in the (submitted) NetLogo instance.
NLCommand(..., nl.obj=NULL)
NLCommand(..., nl.obj=NULL)
... |
An undefined number of strings with the NetLogo command(s) to be executed.
Vectors, lists and data.frames will be represented as NetLogo lists. To set a NetLogo list you can write |
nl.obj |
(optional) A string identifying a reference to a NetLogo instance created with |
The command can be anything which can be submitted from the NetLogo Command Center.
A command has no return value! If you want to return a value from NetLogo use NLReport
and other report functions.
No return value.
Jan C. Thiele <[email protected]>
NLDoCommand
,
NLDoCommandWhile
,
NLReport
## Not run: NLStart("C:/Program Files/NetLogo 6.0/app") NLCommand("create-turtles 10") ## End(Not run)
## Not run: NLStart("C:/Program Files/NetLogo 6.0/app") NLCommand("create-turtles 10") ## End(Not run)
NLDfToList
pushes the values of a data.frame into NetLogo lists.
The column names of the data.frame are used as names for the NetLogo lists
(but the lists must already exist in the current NetLogo model).
NLDfToList(in.data.frame, nl.obj=NULL)
NLDfToList(in.data.frame, nl.obj=NULL)
in.data.frame |
The data.frame to fill the NetLogo lists. |
nl.obj |
(optional) A string identifying a reference to a NetLogo instance created with |
Remember: There must be lists in the NetLogo model with the names of the columns of the submitted data.frame.
No return value.
Jan C. Thiele <[email protected]>
NLDoCommand
,
NLDoCommandWhile
,
NLReport
## Not run: NLStart("C:/Program Files/NetLogo 6.0/app") df1 <- data.frame(x=c(1,2,3,4),y=c(5,6,7,8)) # the current NetLogo model must have two variables ('x' and 'y') # add the variables NLSourceFromString("globals [x y]", append.model=FALSE) # set the variables to the data.frame NLDfToList(df1) ## End(Not run)
## Not run: NLStart("C:/Program Files/NetLogo 6.0/app") df1 <- data.frame(x=c(1,2,3,4),y=c(5,6,7,8)) # the current NetLogo model must have two variables ('x' and 'y') # add the variables NLSourceFromString("globals [x y]", append.model=FALSE) # set the variables to the data.frame NLDfToList(df1) ## End(Not run)
NLDoCommand
executes a NetLogo command (submitted as a string)
in the submitted NetLogo instance more than one time. It works like NLCommand
.
NLDoCommand(iterations, ..., nl.obj=NULL)
NLDoCommand(iterations, ..., nl.obj=NULL)
iterations |
An integer defining the number of times the command is executed. |
... |
An undefined number of string(s) with the NetLogo command(s) to be executed. See NLCommand for details. |
nl.obj |
(optional) A string identifying a reference to a NetLogo instance created with |
This function is used to execute a command more than one time. It is usually used to call a procedure (e.g. "go") for a defined number of times.
No return value.
Jan C. Thiele <[email protected]>
NLCommand
,
NLDoCommandWhile
,
NLReport
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) model.path <- "/models/Sample Models/Earth Science/Fire.nlogo" NLLoadModel(paste(nl.path,model.path,sep="")) NLCommand("setup") NLDoCommand(10, "go") ## End(Not run)
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) model.path <- "/models/Sample Models/Earth Science/Fire.nlogo" NLLoadModel(paste(nl.path,model.path,sep="")) NLCommand("setup") NLDoCommand(10, "go") ## End(Not run)
TRUE
.
NLDoCommandWhile
function executes a NetLogo command (submitted as a string)
in the submitted NetLogo instance more than one time. It works like NLCommand
but will
be repeated as long as the reporter returns TRUE
.
NLDoCommandWhile(condition, ..., max.minutes=10, nl.obj=NULL)
NLDoCommandWhile(condition, ..., max.minutes=10, nl.obj=NULL)
condition |
A string with a NetLogo conditional reporter. |
... |
An undefined number of string(s) with the NetLogo command(s) to be executed. See NLCommand for details. |
max.minutes |
(optional) If |
nl.obj |
(optional) A string identifying a reference to a NetLogo instance created with |
This function is used to execute a command for more than one time. It can be used, for example, to run a simulation (by calling "go") while a variable is below some limit.
The condition is evaluated before the submitted commands are executed. If the condition is FALSE
at the first evaluation, the commands will never be executed.
Attention: Make sure that the condition switches from TRUE
to FALSE
sometime,
otherwise you will run an endless loop (which is stopped after 10 minutes by default, see argument max.minutes
).
No return value.
Jan C. Thiele <[email protected]>
NLCommand
,
NLDoCommandWhile
,
NLReport
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) model.path <- "/models/Sample Models/Earth Science/Fire.nlogo" NLLoadModel(paste(nl.path,model.path,sep="")) NLCommand("setup") NLDoCommandWhile("burned-trees < 500", "go") ## End(Not run)
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) model.path <- "/models/Sample Models/Earth Science/Fire.nlogo" NLLoadModel(paste(nl.path,model.path,sep="")) NLCommand("setup") NLDoCommandWhile("burned-trees < 500", "go") ## End(Not run)
NLDoReport
executes a NetLogo command (submitted as a string)
in the NetLogo instance for more than one time, and executes the reporter after each iteration.
It works like a combination of NLReport
and NLDoCommand
.
NLDoReport(iterations, command, reporter, as.data.frame=FALSE, df.col.names=NULL, nl.obj=NULL)
NLDoReport(iterations, command, reporter, as.data.frame=FALSE, df.col.names=NULL, nl.obj=NULL)
iterations |
An integer defining how many times the command is repeated. |
command |
A string with the NetLogo command to be executed. |
reporter |
A string containing a NetLogo reporter.
This argument can also be an R vector containing multiple strings with different NetLogo reporters (separated by commas), like |
as.data.frame |
(optional) If |
df.col.names |
(optional) If |
nl.obj |
(optional) A string identifying a reference to a NetLogo instance created with |
This function is used to execute a command more than one time and report a value or a number of values after each iteration. It is often used to call a procedure (e.g. "go") for a defined number of times and save the value of a state variable each time.
A list/nested list or data.frame with the value(s) of the reporter after each execution of the command.
Jan C. Thiele <[email protected]>
NLDoCommand
,
NLReport
,
NLDoReportWhile
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) model.path <- "/models/Sample Models/Earth Science/Fire.nlogo" NLLoadModel(paste(nl.path,model.path,sep="")) NLCommand("setup") burned10 <- NLDoReport(10, "go", "burned-trees") initburned10 <- NLDoReport(10, "go", c("initial-trees","burned-trees"), as.data.frame=TRUE, df.col.names=c("initial","burned")) str(initburned10) ## End(Not run)
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) model.path <- "/models/Sample Models/Earth Science/Fire.nlogo" NLLoadModel(paste(nl.path,model.path,sep="")) NLCommand("setup") burned10 <- NLDoReport(10, "go", "burned-trees") initburned10 <- NLDoReport(10, "go", c("initial-trees","burned-trees"), as.data.frame=TRUE, df.col.names=c("initial","burned")) str(initburned10) ## End(Not run)
TRUE
.
NLDoReportWhile
function executes a NetLogo command (submitted as a string)
more than one time and executes the reporter after each iteration.
It works like NLDoReport
but will be repeated while the conditional reporter returns TRUE
.
NLDoReportWhile(condition, command, reporter, as.data.frame=FALSE, df.col.names=NULL, max.minutes=10, nl.obj=NULL)
NLDoReportWhile(condition, command, reporter, as.data.frame=FALSE, df.col.names=NULL, max.minutes=10, nl.obj=NULL)
condition |
A string with a NetLogo conditional reporter. |
command |
A string with the NetLogo command to be executed. |
reporter |
A string containing a NetLogo reporter.
This argument can also be an R vector containing multiple strings with different NetLogo reporters (separated by commas), like |
as.data.frame |
(optional) If |
df.col.names |
(optional) If |
max.minutes |
(optional) If |
nl.obj |
(optional) A string identifying a reference to a NetLogo instance created with |
This function executes a command more than one time and reports a value or a number of values after
each iteration. It is usually used to call a procedure (e.g. "go") while a variable is below a boundary value and save the value of a state variable each time.
Attention: Make sure that the condition switches from TRUE
to FALSE
sometime,
otherwise you will run an endless loop (which is stopped after 10 minutes by default, see argument max.minutes
).
A list/nested list with the value(s) of the reporter after each execution of the command.
Jan C. Thiele <[email protected]>
NLDoCommandWhile
,
NLReport
,
NLDoReport
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) model.path <- "/models/Sample Models/Earth Science/Fire.nlogo" NLLoadModel(paste(nl.path,model.path,sep="")) NLCommand("setup") burnedLower2200 <- NLDoReportWhile("burned-trees < 2200", "go", "burned-trees") str(burnedLower2200) ## End(Not run)
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) model.path <- "/models/Sample Models/Earth Science/Fire.nlogo" NLLoadModel(paste(nl.path,model.path,sep="")) NLCommand("setup") burnedLower2200 <- NLDoReportWhile("burned-trees < 2200", "go", "burned-trees") str(burnedLower2200) ## End(Not run)
NLGetAgentSet
is an easy way to access variable value(s) of one or more agent(s) (in a sorted way) by specifying the name of the agent or the name of an agentset containing the agents. An agent is a turtle, breed, patch, or link. An agentset is a collection of agents.
NLGetAgentSet(agent.var, agentset, as.data.frame=TRUE, agents.by.row=FALSE, as.vector=FALSE, nl.obj=NULL)
NLGetAgentSet(agent.var, agentset, as.data.frame=TRUE, agents.by.row=FALSE, as.vector=FALSE, nl.obj=NULL)
agent.var |
A string or vector/list of strings with the variable names of the agent(s). |
agentset |
A string specifying the agent or agentset to be queried. |
as.data.frame |
(optional) If |
agents.by.row |
(optional) This argument has an effect only in combination with |
as.vector |
(optional) Set this argument to |
nl.obj |
(optional) A string identifying a reference to a NetLogo instance created with |
It's possible to use all variables of an agent, which can be found in NetLogo's Agent Monitors.
It isn't possible to get values from different types of agents (i.e. turtles, patches, links) with one
call of NLGetAgentSet
.
Returns a data.frame (optional a list) with the variable value(s) of an agent/agents of an agentset.
One row for each agent and one column for each agent variable.
The result is sorted in the same manner as using sort agentset
in NetLogo, i.e. turtles are sorted by their who
variable and patches from upper left to lower right.
Jan C. Thiele <[email protected]>
NLReport
,
NLGetPatches
,
NLGetGraph
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) # NLLoadModel(...) NLCommand("create-turtles 10") colors <- NLGetAgentSet(c("who","xcor","ycor","color"), "turtles with [who < 5]") str(colors) # or as a list (slightly faster): colors.list <- NLGetAgentSet(c("who","xcor","ycor","color"), "turtles with [who < 5]", as.data.frame=FALSE) str(colors.list) # or as a list with one list element for each agent # (very slow!, not recommended especially for large agentsets) colors.list2 <- NLGetAgentSet(c("who","xcor","ycor","color"), "turtles with [who < 5]", as.data.frame=FALSE, agents.by.row=TRUE) str(colors.list2) # getting the ends of links is a little bit more tricky, because they store only the # reference to the turtles and turtles cannot directly be requested. # A way to go is: # create some links NLCommand("ask turtles [ create-links-with n-of 2 other turtles ]") link.test <- NLGetAgentSet(c("[who] of end1","[who] of end2"),"links") str(link.test) ## End(Not run)
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) # NLLoadModel(...) NLCommand("create-turtles 10") colors <- NLGetAgentSet(c("who","xcor","ycor","color"), "turtles with [who < 5]") str(colors) # or as a list (slightly faster): colors.list <- NLGetAgentSet(c("who","xcor","ycor","color"), "turtles with [who < 5]", as.data.frame=FALSE) str(colors.list) # or as a list with one list element for each agent # (very slow!, not recommended especially for large agentsets) colors.list2 <- NLGetAgentSet(c("who","xcor","ycor","color"), "turtles with [who < 5]", as.data.frame=FALSE, agents.by.row=TRUE) str(colors.list2) # getting the ends of links is a little bit more tricky, because they store only the # reference to the turtles and turtles cannot directly be requested. # A way to go is: # create some links NLCommand("ask turtles [ create-links-with n-of 2 other turtles ]") link.test <- NLGetAgentSet(c("[who] of end1","[who] of end2"),"links") str(link.test) ## End(Not run)
NLGetGraph
converts a set of NetLogo Link agents into an igraph graph object (see package igraph for details on graph objects).
NLGetGraph(link.agentset="links", nl.obj=NULL)
NLGetGraph(link.agentset="links", nl.obj=NULL)
link.agentset |
(optional) A string defining an agentset of NetLogo Links. Default is |
nl.obj |
(optional) A string identifying a reference to a NetLogo instance created with |
Saves a link network in a graph object of package igraph for network analysis.
Returns a graph object of package igraph.
Jan C. Thiele <[email protected]>
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) model.path <- "/models/Sample Models/Networks/Preferential Attachment.nlogo" NLLoadModel(paste(nl.path,model.path,sep="")) NLCommand("setup") NLDoCommand(4, "go") graph1 <- NLGetGraph() plot(graph1, layout=layout.kamada.kawai, vertex.label=V(graph1)$name, vertex.shape="rectangle", vertex.size=20, asp=FALSE) ## End(Not run)
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) model.path <- "/models/Sample Models/Networks/Preferential Attachment.nlogo" NLLoadModel(paste(nl.path,model.path,sep="")) NLCommand("setup") NLDoCommand(4, "go") graph1 <- NLGetGraph() plot(graph1, layout=layout.kamada.kawai, vertex.label=V(graph1)$name, vertex.shape="rectangle", vertex.size=20, asp=FALSE) ## End(Not run)
NLGetPatches
is an easy way to access variables of all patches (default) or of
a subset of patches.
NLGetPatches(patch.var, patchset="patches", as.matrix=FALSE, as.data.frame=TRUE, patches.by.row=FALSE, as.vector=FALSE, nl.obj=NULL)
NLGetPatches(patch.var, patchset="patches", as.matrix=FALSE, as.data.frame=TRUE, patches.by.row=FALSE, as.vector=FALSE, nl.obj=NULL)
patch.var |
A string or vector/list of strings with the names of patch variables to report. |
patchset |
(optional) A string defining which patches to request. By default, values of all patches are returned. |
as.matrix |
(optional) If this variable is |
as.data.frame |
(optional) If |
patches.by.row |
(optional) This argument has an effect only in combination with |
as.vector |
(optional) Set this argument to |
nl.obj |
(optional) A string identifying a reference to a NetLogo instance created with |
It's possible to use all the variables of a patch, which can be found in NetLogo's Agent Monitors.
Returns a data.frame (optional a list) with the variable value(s) of a patch/patches of a patchset.
One row for each patch and one column for each patch variable.
The result is sorted (like using sort patchset
in NetLogo), e.g. patches are sorted from upper left to lower right.
Jan C. Thiele <[email protected]>
NLReport
,
NLGetAgentSet
,
NLGetGraph
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) # NLLoadModel(...) allpatches <- NLGetPatches(c("pxcor","pycor","pcolor")) str(allpatches) # only a subset of patches subsetpatches <- NLGetPatches(c("pxcor","pycor","pcolor"), "patches with [pxcor < 5]") str(subsetpatches) # or as a list (slightly faster): colors.list <- NLGetPatches(c("pxcor","pycor","pcolor"), "patches with [pxcor < 5]", as.data.frame=FALSE) str(colors.list) # or as a list with one list element for each patch # (very slow!, not recommended especially for large patchsets) colors.list2 <- NLGetPatches(c("pxcor","pycor","pcolor"), "patches with [pxcor < 5]", as.data.frame=FALSE, patches.by.row=TRUE) str(colors.list2) ## End(Not run)
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) # NLLoadModel(...) allpatches <- NLGetPatches(c("pxcor","pycor","pcolor")) str(allpatches) # only a subset of patches subsetpatches <- NLGetPatches(c("pxcor","pycor","pcolor"), "patches with [pxcor < 5]") str(subsetpatches) # or as a list (slightly faster): colors.list <- NLGetPatches(c("pxcor","pycor","pcolor"), "patches with [pxcor < 5]", as.data.frame=FALSE) str(colors.list) # or as a list with one list element for each patch # (very slow!, not recommended especially for large patchsets) colors.list2 <- NLGetPatches(c("pxcor","pycor","pcolor"), "patches with [pxcor < 5]", as.data.frame=FALSE, patches.by.row=TRUE) str(colors.list2) ## End(Not run)
NLLoadModel
loads a model (*.nlogo file) into the submitted NetLogo instance.
NLLoadModel(model.path, nl.obj=NULL)
NLLoadModel(model.path, nl.obj=NULL)
model.path |
A string containing either the absolute path to the model file (*.nlogo file) or a relative path to the model file starting from the NetLogo installation directory specified in |
nl.obj |
(optional) A string identifying a reference to a NetLogo instance created with |
No return value.
Jan C. Thiele <[email protected]>
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) model.path <- "/models/Sample Models/Earth Science/Fire.nlogo" absolute.model.path <- paste(nl.path,model.path,sep="") NLLoadModel(absolute.model.path) relative.model.path <- "models/Sample Models/Earth Science/Fire.nlogo" NLLoadModel(relative.model.path) ## End(Not run)
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) model.path <- "/models/Sample Models/Earth Science/Fire.nlogo" absolute.model.path <- paste(nl.path,model.path,sep="") NLLoadModel(absolute.model.path) relative.model.path <- "models/Sample Models/Earth Science/Fire.nlogo" NLLoadModel(relative.model.path) ## End(Not run)
Quits the NetLogo workspace and closes the GUI window (if started with GUI).
NLQuit(nl.obj=NULL, all=FALSE)
NLQuit(nl.obj=NULL, all=FALSE)
nl.obj |
(optional) A string identifying a reference to the NetLogo instance defined in |
all |
(optional) A boolean variable: If |
No return value.
please note that you will not be ask to save changes when closing NetLogo.
Furthermore, there is currently no way to kill a NetLogo instance with GUI completely. After executing NLQuit
on a GUI instance, you can't run NLStart
again.
You have to quit your R session first and start a new one.
The reason is that NetLogo quits via System.exit (and has no functionality to quit all threads manually) but executing System.exit will terminate the whole JVM which will also terminate rJava and finally R. But there is a trick to run RNetLogo in GUI mode multiple times described in the document parallelProcessing.pdf
in directory parallelProcessing
in the installation directory of the package.
It can happen that some memory is not released although you have executed NLQuit
, because shutting down the running JVM via rJava and unloading the required libraries is not possible.
Therefore, it is a good idea to start a new R session if possible when you load a new model.
Jan C. Thiele <[email protected]>
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) NLQuit() ## End(Not run)
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) NLQuit() ## End(Not run)
NLReport
reports NetLogo data back to R.
NLReport(reporter, nl.obj=NULL)
NLReport(reporter, nl.obj=NULL)
reporter |
A string containing a NetLogo reporter. (Or a vector of strings.) |
nl.obj |
(optional) A string identifying a reference to a NetLogo instance created with |
Every reporter (commands which return a value) that can be called in the NetLogo Command Center
can be called with NLReport
.
A vector of length one if only one value is returned. Otherwise it is a list or, if necessary, a nested list with the reported values.
Jan C. Thiele <[email protected]>
NLDoReport
,
NLDoReportWhile
,
NLGetPatches
,
NLGetAgentSet
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) model.path <- "/models/Sample Models/Earth Science/Fire.nlogo" NLLoadModel(paste(nl.path,model.path,sep="")) NLCommand("setup") NLDoCommand(10, "go") noburned <- NLReport("burned-trees") str(noburned) ## End(Not run)
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) model.path <- "/models/Sample Models/Earth Science/Fire.nlogo" NLLoadModel(paste(nl.path,model.path,sep="")) NLCommand("setup") NLDoCommand(10, "go") noburned <- NLReport("burned-trees") str(noburned) ## End(Not run)
NLSetAgentSet
is an easy way to set the variable value(s) of one or more agent(s) (by specifying the name of the agent or the name of an agentset containing the agents) to the value(s) of a data.frame or vector.
NLSetAgentSet(agentset, input, var.name=NULL, nl.obj=NULL)
NLSetAgentSet(agentset, input, var.name=NULL, nl.obj=NULL)
agentset |
A string specifying the agent or agentset for which values should be changed. |
input |
A data.frame or vector.
If a data.frame, it must have one column with the corresponding agent variable name for each agent variable to be set and one row for each agent.
The rows have to be sorted in the order NetLogo is processing the agentset with |
var.name |
If |
nl.obj |
(optional) A string identifying a reference to a NetLogo instance created with |
The agent variable values contained as columns in the input data.frame are changed. The columns of the data.frame have to be named exactly like the
agent variable which should get the values. The rows have to be sorted as NetLogo would process the agentset using the sort
reporter.
No return value.
Jan C. Thiele <[email protected]>
NLSetPatches
,
NLGetAgentSet
,
NLGetGraph
,
NLDfToList
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) # NLLoadModel(...) ag <- NLGetAgentSet(c("xcor","ycor"),"turtles") ag2 <- data.frame(xcor=ag$xcor, ycor=ag$xcor) NLSetAgentSet("turtles", ag2) ## End(Not run)
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) # NLLoadModel(...) ag <- NLGetAgentSet(c("xcor","ycor"),"turtles") ag2 <- data.frame(xcor=ag$xcor, ycor=ag$xcor) NLSetAgentSet("turtles", ag2) ## End(Not run)
NLSetPatches
is an easy way to set the values of all patches to the values of a matrix.
NLSetPatches(patch.var, in.matrix, nl.obj=NULL)
NLSetPatches(patch.var, in.matrix, nl.obj=NULL)
patch.var |
The name of the patch variable to set. |
in.matrix |
A matrix that represents the NetLogo world (has the same dimensions). |
nl.obj |
(optional) A string identifying a reference to a NetLogo instance created with |
The matrix must have the same x- and y-dimensions as the NetLogo world, indices beginning with (1,1).
The upper-left cell (1,1) of the matrix represents the upper-left patch of the NetLogo world, no matter where the origin of the NetLogo world is set.
This function is not available when running NetLogo 3D. Use NLSetPatchSet
instead.
No return value.
Jan C. Thiele <[email protected]>
NLReport
,
NLGetAgentSet
,
NLGetGraph
,
NLDfToList
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) m1 <- matrix(1:1089 , 33) NLSetPatches("pcolor", m1) ## End(Not run)
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) m1 <- matrix(1:1089 , 33) NLSetPatches("pcolor", m1) ## End(Not run)
NLSetPatchSet
is an easy way to set the variable value of one or more patch(es) to the value(s) of a data.frame.
NLSetPatchSet(patch.var, input, nl.obj=NULL)
NLSetPatchSet(patch.var, input, nl.obj=NULL)
patch.var |
This argument gives the name of the patch variable as a string which should be set to the values of the third (for NetLogo 2D) or fourth column (for NetLogo 3D) of the data.frame submitted in |
input |
A data.frame with columns giving the coordinates of a patch and the values for the patch variable to be changed. For conventional 2D NetLogo there has to be a pxcor and a pycor column , for NetLogo 3D there has to be a pxcor, a pycor, and a pzcor column. name of the column that contains the new values for the patch variable has to be equal to the argument |
nl.obj |
(optional) A string identifying a reference to a NetLogo instance created with |
This function is used to update one patch variable for patches identified by their pxcor, pycor (and pzcor in case of NetLogo 3D) values based on values given in a data.frame.
No return value.
Jan C. Thiele <[email protected]>
NLSetPatches
,
NLGetAgentSet
,
NLGetGraph
,
NLDfToList
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) # NLLoadModel(...) # for NetLogo 2D: input <- NLGetPatches(c("pxcor","pycor","pcolor")) str(input) # for NetLogo 3D: input <- NLGetPatches(c("pxcor","pycor","pzcor","pcolor")) str(input) input$pcolor <- floor(abs(rnorm(nrow(input))*100)) patch.var <- "pcolor" NLSetPatchSet(patch.var, input) ## End(Not run)
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) # NLLoadModel(...) # for NetLogo 2D: input <- NLGetPatches(c("pxcor","pycor","pcolor")) str(input) # for NetLogo 3D: input <- NLGetPatches(c("pxcor","pycor","pzcor","pcolor")) str(input) input$pcolor <- floor(abs(rnorm(nrow(input))*100)) patch.var <- "pcolor" NLSetPatchSet(patch.var, input) ## End(Not run)
NLSourceFromString
is a way to create/append a NetLogo model's source code dynamically from R.
NLSourceFromString(..., append.model=TRUE, nl.obj=NULL)
NLSourceFromString(..., append.model=TRUE, nl.obj=NULL)
... |
An undefined number of strings containing NetLogo model source code to be printed into the procedures tab.
Line breaks within a string can be represented as |
append.model |
(optional) Determines whether existing code in the procedures tab (i.e. a loaded model) will be appended by the new code or will be replaced. By default, all existing code will be appended. |
nl.obj |
(optional) A string identifying a reference to a NetLogo instance created with |
This function only works with NetLogo instances with GUI. It doesn't work in headless mode.
No return value.
Jan C. Thiele <[email protected]>
NLReport
,
NLGetAgentSet
,
NLGetGraph
,
NLDfToList
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) setup <- "to setup\n ca\n crt 10\nend \n" go <- "to go\n ask turtles [\n set xcor random-xcor\n set ycor random-ycor\n ]\nend \n" reporter1 <- "to-report noturtles\n report count turtles\n end \n" NLSourceFromString(setup,go,reporter1, append.model=FALSE) NLCommand("setup") NLCommand("go") noturtles <- NLReport("noturtles") print(noturtles) ## End(Not run)
## Not run: nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) setup <- "to setup\n ca\n crt 10\nend \n" go <- "to go\n ask turtles [\n set xcor random-xcor\n set ycor random-ycor\n ]\nend \n" reporter1 <- "to-report noturtles\n report count turtles\n end \n" NLSourceFromString(setup,go,reporter1, append.model=FALSE) NLCommand("setup") NLCommand("go") noturtles <- NLReport("noturtles") print(noturtles) ## End(Not run)
NLStart
creates a new instance of NetLogo in either headless (without the Graphical User Interface)
or GUI mode.
NLStart(nl.path, gui=TRUE, nl.obj=NULL, is3d=FALSE, nl.jarname='netlogo-6.0.0.jar')
NLStart(nl.path, gui=TRUE, nl.obj=NULL, is3d=FALSE, nl.jarname='netlogo-6.0.0.jar')
nl.path |
An absolute path to your NetLogo installation (the folder where the NetLogo.jar is) starting from the root. On Windows, for example, something like "C:/Program Files/NetLogo 6.0/app". |
gui |
(optional) A boolean value: if |
nl.obj |
(optional) A string which is used to identify the created NetLogo instance reference internally (in |
is3d |
(optional) A boolean value: if |
nl.jarname |
(optional) The name of the NetLogo jar file. Since NetLogo 6.0 the jar file includes the version number. Default value is netlogo-6.0.0.jar. For other version the name has to be set here. |
You can start multiple instances of NetLogo in headless mode and store each in another
variable (using nl.obj
) but it is not possible to start multiple instances in GUI mode.
(It would result in an crash of R since there is no way to detach the Java Virtual Machine via rJava.) But there is a trick to run RNetLogo in GUI mode multiple times described in the document parallelProcessing.pdf
in directory parallelProcessing
in the installation directory of the package.
Note for Mac OS users: If you want to run RNetLogo in headless mode (without GUI, i.e. setting argument gui=FALSE
) you have to disable AWT before loading the package. Just execute Sys.setenv(NOAWT=1)
before executing library(RNetLogo)
.
If you want to run RNetLogo in GUI mode you have to start it from the JGR application (see https://cran.r-project.org/package=JGR and the note at http://groups.yahoo.com/group/netlogo-users/message/14817). It can be necessary to run Sys.setenv(NOAWT=1)
before loading the JGR package and run Sys.unsetenv("NOAWT")
before starting JGR via JGR()
.
Note for Linux users: If you want to run RNetLogo in GUI mode you should start RNetLogo in the JGR application (see https://cran.r-project.org/package=JGR).
Note for Windows 32-bit users: Starting RNetLogo (in GUI mode) on 32-bit Windows (not 64-bit Windows running in 32-bit mode) may fail in R version 2.15.2 and 2.15.3 (see description here: https://stat.ethz.ch/pipermail/r-devel/2013-January/065576.html). The reason could be the increased C stack size in 2.15.2 and 2.15.3. If you execute Cstack_info()
you can see how large the C stack size is. The problem seems to be resolved with 3.0.0.
A workaround is to use R 2.15.1 or 3.x or to start RNetLogo from JGR (see https://cran.r-project.org/package=JGR) or RStudio (see http://www.rstudio.com/).
Avoid manually changing the working directory of R, because NetLogo needs to have the working directory pointed to its installation path. As
the R working directory and the Java working directory depend on each other, changing the R working directory can result in unexpected behavior of NetLogo.
Therefore, you should use absolute paths for I/O processes in R instead of submitting setwd(...)
.
Note that the RNetLogo package changes the working directory
automatically when loading NetLogo and changes back to the former working directory closing the last active instance of NetLogo with NLQuit
.
As mentioned in NLQuit
, it is currently not possible to quit NetLogo completely.
If you want to specify options for the underlying Java Virtual Machine (JVM), like increasing the Java Heap Space for large models, execute options(java.parameters="...")
before loading the RNetLogo package with library(RNetLogo)
or require(RNetLogo)
. For increasing the Java Heap Space it can be options(java.parameters="-Xmx1024m")
, for example. Use a vector of strings for setting multiple options, for example options(java.parameters=c("-server","-Xmx1300m"))
.
See also
http://ccl.northwestern.edu/netlogo/docs/faq.html#howbig and rJava manual.
See the directory examples
in the installation directory of the package for example codes to all RNetLogo functions.
See Thiele (2014) (also included in directory tutorial
in the installation directory of the package) for a step-by-step usage tutorial.
See the vignette performanceNotes.pdf
for performance notes.
See the vignette parallelProcessing.pdf
on how to run RNetLogo on multiple processors/clusters in parallel.
No return value.
It's not possible to run multiple instances of NetLogo in GUI mode! Closing NetLogo from the
NetLogo Window is blocked, because it would quit the whole R process. To close the NetLogo
call NLQuit
.
If you use the headless mode you should first load a model with NLLoadModel
before executing other commands or reporters. In GUI mode you can execute commands and reporters already with the initial empty model without loading a specific one.
Jan C. Thiele <[email protected]>
Thiele, J. (2014) R Marries NetLogo: Introduction to the RNetLogo Package. Journal of Statistical Software 58(2) 1-41. http://www.jstatsoft.org/v58/i02/
## Not run: library(RNetLogo) nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) NLCommand("create-turtles 10") noturtles <- NLReport("count turtles") print(noturtles) # create a second NetLogo instance in headless mode (= without GUI) # stored in a variable nlheadless1 <- "nlheadless1" NLStart(nl.path, gui=F, nl.obj=nlheadless1) model.path <- "/models/Sample Models/Earth Science/Fire.nlogo" NLLoadModel(paste(nl.path,model.path,sep=""), nl.obj=nlheadless1) NLCommand("setup", nl.obj=nlheadless1) burned1 <- NLDoReport(20, "go", c("ticks","burned-trees"), as.data.frame=TRUE,df.col.names=c("tick","burned"), nl.obj=nlheadless1) print(burned1) # create a third NetLogo instance in headless mode (= without GUI) # with explicit name of stored object nlheadless2 <- "nlheadless2" NLStart(nl.path, gui=F, nl.obj=nlheadless2) model.path <- "/models/Sample Models/Earth Science/Fire.nlogo" NLLoadModel(paste(nl.path,model.path,sep=""), nl.obj=nlheadless2) NLCommand("setup", nl.obj=nlheadless2) burned2 <- NLDoReport(10, "go", c("ticks","burned-trees"), as.data.frame=TRUE,df.col.names=c("tick","burned"), nl.obj=nlheadless2) print(burned2) ## End(Not run)
## Not run: library(RNetLogo) nl.path <- "C:/Program Files/NetLogo 6.0/app" NLStart(nl.path) NLCommand("create-turtles 10") noturtles <- NLReport("count turtles") print(noturtles) # create a second NetLogo instance in headless mode (= without GUI) # stored in a variable nlheadless1 <- "nlheadless1" NLStart(nl.path, gui=F, nl.obj=nlheadless1) model.path <- "/models/Sample Models/Earth Science/Fire.nlogo" NLLoadModel(paste(nl.path,model.path,sep=""), nl.obj=nlheadless1) NLCommand("setup", nl.obj=nlheadless1) burned1 <- NLDoReport(20, "go", c("ticks","burned-trees"), as.data.frame=TRUE,df.col.names=c("tick","burned"), nl.obj=nlheadless1) print(burned1) # create a third NetLogo instance in headless mode (= without GUI) # with explicit name of stored object nlheadless2 <- "nlheadless2" NLStart(nl.path, gui=F, nl.obj=nlheadless2) model.path <- "/models/Sample Models/Earth Science/Fire.nlogo" NLLoadModel(paste(nl.path,model.path,sep=""), nl.obj=nlheadless2) NLCommand("setup", nl.obj=nlheadless2) burned2 <- NLDoReport(10, "go", c("ticks","burned-trees"), as.data.frame=TRUE,df.col.names=c("tick","burned"), nl.obj=nlheadless2) print(burned2) ## End(Not run)