Giter VIP home page Giter VIP logo

nlrx's Introduction

nlrx

R build status Codecov test coverage lifecycle CRAN status ropensci DOI:10.1111/2041-210X.13286

The nlrx package provides tools to setup and execute NetLogo simulations from R. NetLogo is a free, open-source and cross-platform modelling environment for simulating natural and social phenomena. NetLogo focuses on implementation of agent-based and spatially explicit simulation models, although system dynamics models are supported as well. NetLogo is developed and maintained at the Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL. More details on NetLogo itself are available online: NetLogo online documentation

NetLogo comes with the built-in experiment tool Behavior Space that allows to setup and execute model simulations with different settings and parameter variations and to collect model output. This experiment tool can be executed via command line in combination with an XML file that contains the experiment specifications, such as runtime, variables, output measurements, stop conditions, and more. One limitation of Behavior Space is, that it only supports full-factorial parameter designs, which may not be appropriate for complex model analyses. Furthermore, Behavior Space experiment specifications are stored within the NetLogo file and are not easily accessible from R. However, in many cases it is useful to store such specifications along with the model output and analyses results in order to enable fully reproducible model analyses.

The nlrx package utilizes the commandline functionality of Behavior Space to execute NetLogo simulations directly from R. Instead of defining experiments within NetLogo Behavior Space, experiments are defined in R using the class objects of the nlrx package. These class objects hold all the information that is needed to run these experiments remotely from R, such as path to NetLogo installation folder, path to the model file and the experiment specifications itself. nlrx provides useful helper functions to generate parameter input matrices from parameter range definitions that cover a wide range of parameter exploration approaches. By storing all relevant information on simulation experiments, including the output of the model simulations in one class object, experiments can be easily stored and shared.

In summary, the nlrx package uses a similar structure as NetLogos Behavior Space but offers more flexibility and additional tools for running reproducible complex model analyses directly from R.

Publication

Further information on the package functionality and detailed code examples can be found in our accompanying publication: Salecker J, Sciaini M, Meyer KM, Wiegand K. The nlrx r package: A next-generation framework for reproducible NetLogo model analyses. Methods Ecol Evol. 2019;2041-210X. https://doi.org/10.1111/2041-210X.13286.

Get citation information for nlrx in R doing citation(package = 'nlrx').

Prerequirements

NetLogo

In order to use the nlrx package, NetLogo (>=5.3.1) needs to be installed on the system that is used to execute model simulations (local/remote). For remote execution, NetLogo needs to be installed on remote machines as well. The nlrx package provides a utility function (download_netlogo()) that can be used to download and unzip (only unix systems) a specified NetLogo version to a local folder. For windows machines, the downloaded file needs to be executed in order to install NetLogo on the local system. If you are running MacOS, please use the Linux tar.gz version of NetLogo (either from the NetLogo Homepage or by using the download_netlogo() function). The dmg version from the NetLogo homepage is not compatible with nlrx.

All code snippets on this homepage should be compatible with Netlogo <= 6.2.2. In version 6.3.0, the folder structure of NetLogo was slightly updated, thus the modelpath in the code snippets need to be adjusted accordingly (the "app/" folder needs to be removed from the modelpath).

Java

Because NetLogo is executed in a Java virtual machine, Java needs to be installed on the local/remote system as well. We recommend the Oracle Java SE Development Kit 8 or the openjdk. While the nlrx package might work without setting the Java system path explicitly, we recommend to make sure that JAVA_HOME points to the correct Java installation of the system.

Installation

You can install the released version of nlrx from CRAN with:

install.packages("nlrx")

And the development version from GitHub with:

# install.packages("remotes")
remotes::install_github("ropensci/nlrx")

Get started

General information that is needed to run NetLogo simulations remotely, such as path to the NetLogo installation folder is stored within a nl class object. Nested within this nl class are the classes experiment and simdesign. The experiment class stores all experiment specifications. After attaching a valid experiment, a simdesign class object can be attached to the nl class object, by using one of the simdesign helper functions. These helper functions create different parameter input matrices from the experiment variable definitions that can then be executed by the run_nl_one() and run_nl_all() functions. The nested design allows to store everything related to the experiment within one R object. Additionally, different simdesign helper functions can be applied to the same nl object in order to repeat the same experiment with different parameter exploration methods (simdesigns).

Step by step application example

The “Wolf Sheep Predation” model from the NetLogo models library is used to present a basic example on how to setup and run NetLogo model simulations from R.

Step 1: Create a nl object:

The nl object holds all information on the NetLogo version, a path to the NetLogo directory with the defined version, a path to the model file, and the desired memory for the java virtual machine. Depending on the operation system, paths to NetLogo and the model need to be adjusted.

library(nlrx)
# Windows default NetLogo installation path (adjust to your needs!):
netlogopath <- file.path("C:/Program Files/NetLogo 6.0.3")
modelpath <- file.path(netlogopath, "app/models/Sample Models/Biology/Wolf Sheep Predation.nlogo")
outpath <- file.path("C:/out")
# Unix default NetLogo installation path (adjust to your needs!):
netlogopath <- file.path("/home/NetLogo 6.0.3")
modelpath <- file.path(netlogopath, "app/models/Sample Models/Biology/Wolf Sheep Predation.nlogo")
outpath <- file.path("/home/out")

nl <- nl(nlversion = "6.0.3",
         nlpath = netlogopath,
         modelpath = modelpath,
         jvmmem = 1024)

Step 2: Attach an experiment

The experiment object is organized in a similar fashion as NetLogo Behavior Space experiments. It contains all information that is needed to generate a simulation parameter matrix and to execute the NetLogo simulations. Details on the specific slots of the experiment class can be found in the package documentation (?experiment) and the “Advanced configuration” vignette.

nl@experiment <- experiment(expname="wolf-sheep",
                            outpath=outpath,
                            repetition=1,
                            tickmetrics="true",
                            idsetup="setup",
                            idgo="go",
                            runtime=50,
                            evalticks=seq(40,50),
                            metrics=c("count sheep", "count wolves", "count patches with [pcolor = green]"),
                            variables = list('initial-number-sheep' = list(min=50, max=150, qfun="qunif"),
                                             'initial-number-wolves' = list(min=50, max=150, qfun="qunif")),
                            constants = list("model-version" = "\"sheep-wolves-grass\"",
                                             "grass-regrowth-time" = 30,
                                             "sheep-gain-from-food" = 4,
                                             "wolf-gain-from-food" = 20,
                                             "sheep-reproduce" = 4,
                                             "wolf-reproduce" = 5,
                                             "show-energy?" = "false"))

Step 3: Attach a simulation design

While the experiment defines the variables and specifications of the model, the simulation design creates a parameter input table based on these model specifications and the chosen simulation design method. nlrx provides a bunch of different simulation designs, such as full-factorial, latin-hypercube, sobol, morris and eFast (see “Simdesign Examples” vignette for more information on simdesigns). All simdesign helper functions need a properly defined nl object with a valid experiment design. Each simdesign helper also allows to define a number of random seeds that are randomly generated and can be used to execute repeated simulations of the same parameter matrix with different random-seeds (see “Advanced configuration” vignette for more information on random-seed and repetition management). A simulation design is attached to a nl object by using one of the simdesign helper functions:

nl@simdesign <- simdesign_lhs(nl=nl,
                               samples=100,
                               nseeds=3,
                               precision=3)

Step 4: Run simulations

All information that is needed to run the simulations is now stored within the nl object. The run_nl_one() function allows to run one specific simulation from the siminput parameter table. The run_nl_all() function runs a loop over all simseeds and rows of the parameter input table siminput. The loops are constructed in a way that allows easy parallelisation, either locally or on remote HPC machines (see “Advanced configuration” vignette for more information on parallelisation). Before running your simulations you might want to check your current nl object setup. eval_variables_constants(nl) evaluates if the defined variables and constants are correctly defined and are consistent with the attached model. print(nl) prints a complete summary of the provided nl object including checkmarks that might help to indicate potential problems.

# Evaluate nl object:
eval_variables_constants(nl)
print(nl)

# Run all simulations (loop over all siminputrows and simseeds)
results <- run_nl_all(nl)

Step 5: Attach results to nl and run analysis

nlrx provides method specific analysis functions for each simulation design. Depending on the chosen design, the function reports a tibble with aggregated results or sensitivity indices. In order to run the analyze_nl function, the simulation output has to be attached to the nl object first. The simdesign class within the nl object provides a slot for attaching output results (simoutput). An output results tibble can be attached to this slot by using the simdesign setter function setsim(nl, "simoutput"). After attaching the simulation results, these can also be written to the defined outpath of the experiment object.

# Attach results to nl object:
setsim(nl, "simoutput") <- results

# Write output to outpath of experiment within nl
write_simoutput(nl)

# Do further analysis:
analyze_nl(nl)

Meta

  • Please report any issues or bugs.
  • License: GPL3
  • Get citation information for nlrx in R doing citation(package = 'nlrx')
  • We are very open to contributions - if you are interested check Contributing.
    • Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

nlrx's People

Contributors

actions-user avatar annakrystalli avatar bischrob avatar bitbacchus avatar jeroen avatar maelle avatar marcosci avatar nldoc avatar nowosad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

nlrx's Issues

Output not parsed correctly

Hello,

after successfully running sensitivity analysis with the Wolf Sheep predation model, I am attempting to do the same for my own NetLogo model.

My model's output variables are not parsed correctly, since all the values are marked as NA.

After reading this issue: #41 I though it could be the character type in Netlogo, but everything appears to be a numeric and not a list. I am new to netlogo, so I was wondering: is there a way to check it directly? Also, with behaviour space the output is parsed correctly. Any other idea?

Why `nlrx` takes soooooo long to run on my computer?

Hereafter is my codes:

# https://stackoverflow.com/a/59768990/16802797

library(nlrx)
# Windows default NetLogo installation path (adjust to your needs!):
netlogopath <- file.path("C:/Program Files/NetLogo 6.2.0")
modelpath <- file.path(netlogopath, "app/models/Sample Models/Earth Science/Fire.nlogo")
outpath <- file.path(".")
nl <- nl(nlversion = "6.2.0",
         nlpath = netlogopath,
         modelpath = modelpath,
         jvmmem = 1024)

## Example 1: simdesign_distinct
nl@experiment <- experiment(expname="fire",
                            outpath=outpath,
                            repetition=1,
                            tickmetrics="true",
                            idsetup="setup",
                            idgo="go",
                            runtime=0, 
                            metrics=c("ifelse-value (initial-trees > 0) [(burned-trees / initial-trees) * 100][0]"),
                            variables = list('density' = list(values=seq(0, 99, 1))),
                            constants = list())

#### use nseeds = 10 to simulate over 10 different random seeds (replicates)
nl@simdesign <- simdesign_distinct(nl, nseeds = 10)

library(future)
plan(multisession)
tictoc::tic()
results <- progressr::with_progress(run_nl_all(nl))
tictoc::toc()

setsim(nl, "simoutput") <- results

The codes took almost two hours to run in my computer with Intel(R) Xeon(R) CPU E3-1505M v6 @ 3.00GHz and 32G RAM.

But if I used netlogo's BehaviorSpace to do the same job, it finished in 1 minute! Below is the xml setting I distill from the saved .nlogo file.

<experiments>
  <experiment name="fire_ex" repetitions="10" runMetricsEveryStep="true">
    <setup>setup</setup>
    <go>go</go>
    <metric>ifelse-value (initial-trees &gt; 0) [(burned-trees / initial-trees) * 100][0]</metric>
    <steppedValueSet variable="density" first="0" step="1" last="99"/>
  </experiment>
</experiments>

Something wrong? Thank you!

sessioninfo::session_info()
# - Session info -------------------------------------------------------------------------
#   setting  value                         
# version  R version 4.1.1 (2021-08-10)  
# os       Windows 10 x64                
# system   x86_64, mingw32               
# ui       RStudio                       
# language (EN)                          
# collate  Chinese (Simplified)_China.936
# ctype    Chinese (Simplified)_China.936
# tz       Asia/Taipei                   
# date     2021-09-25                    
# 
# - Packages -----------------------------------------------------------------------------
#   package     * version date       lib source        
# assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.1.1)
# cli           3.0.1   2021-07-17 [1] CRAN (R 4.1.1)
# codetools     0.2-18  2020-11-04 [2] CRAN (R 4.1.1)
# crayon        1.4.1   2021-02-08 [1] CRAN (R 4.1.1)
# DBI           1.1.1   2021-01-15 [1] CRAN (R 4.1.1)
# digest        0.6.27  2020-10-24 [1] CRAN (R 4.1.1)
# dplyr         1.0.7   2021-06-18 [1] CRAN (R 4.1.1)
# ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.1.1)
# fansi         0.5.0   2021-05-25 [1] CRAN (R 4.1.1)
# furrr         0.2.3   2021-06-25 [1] CRAN (R 4.1.1)
# future      * 1.22.1  2021-08-25 [1] CRAN (R 4.1.1)
# generics      0.1.0   2020-10-31 [1] CRAN (R 4.1.1)
# globals       0.14.0  2020-11-22 [1] CRAN (R 4.1.1)
# glue          1.4.2   2020-08-27 [1] CRAN (R 4.1.1)
# lifecycle     1.0.0   2021-02-15 [1] CRAN (R 4.1.1)
# listenv       0.8.0   2019-12-05 [1] CRAN (R 4.1.1)
# magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.1.1)
# nlrx        * 0.4.3   2021-09-20 [1] CRAN (R 4.1.1)
# parallelly    1.28.1  2021-09-09 [1] CRAN (R 4.1.1)
# pillar        1.6.2   2021-07-29 [1] CRAN (R 4.1.1)
# pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.1.1)
# progressr     0.9.0   2021-09-24 [1] CRAN (R 4.1.1)
# purrr         0.3.4   2020-04-17 [1] CRAN (R 4.1.1)
# R6            2.5.1   2021-08-19 [1] CRAN (R 4.1.1)
# rlang         0.4.11  2021-04-30 [1] CRAN (R 4.1.1)
# rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.1.1)
# sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.1.1)
# tibble        3.1.4   2021-08-25 [1] CRAN (R 4.1.1)
# tictoc        1.0.1   2021-04-19 [1] CRAN (R 4.1.1)
# tidyselect    1.1.1   2021-04-30 [1] CRAN (R 4.1.1)
# utf8          1.2.2   2021-07-24 [1] CRAN (R 4.1.1)
# vctrs         0.3.8   2021-04-29 [1] CRAN (R 4.1.1)
# withr         2.4.2   2021-04-18 [1] CRAN (R 4.1.1)
# 
# [1] C:/Users/admin/Documents/R/win-library/4.1
# [2] C:/Program Files/R/R-4.1.1/library

Simulations without seed via nlrx

Hi, is it possible to run simulations without setting any seeds via the nlrx functions? I tried several setups but none of the setups and twists has allowed me to keep the seed I set in the NetLogo code. The reasoning is that I vary the landscape together with the seed (e.g. landscape i with seed i for several setups) and thus would need to specify it on my own.

Error in util_gather_results(nl, outfile, seed, siminputrow) : **Output file is empty - simulation aborted due to a runtime error! Make sure that parameter value definitions of the experiment are valid and the model code is running properly!

I have an issue
If the following code is run (R 3.5.3 x64, Windows, nlrx version 0.4.1), then the simulation fails with a difficult-to-interpret error message.

Exception in thread "main" Expected a literal value. at position 0 in
at org.nlogo.core.Fail$.exception(Fail.scala:27)
at org.nlogo.core.Fail$.exception(Fail.scala:25)
at org.nlogo.core.Fail$.exception(Fail.scala:23)
at org.nlogo.parse.LiteralParser.readLiteralPrefix(LiteralParser.scala:83)
at org.nlogo.parse.LiteralParser.getLiteralValue(LiteralParser.scala:33)
at org.nlogo.parse.CompilerUtilities$.$anonfun$readFromString$3(CompilerUtilities.scala:22)
at org.nlogo.parse.CompilerUtilities$.$anonfun$numberOrElse$1(CompilerUtilities.scala:37)
at scala.util.Either$RightProjection.getOrElse(Either.scala:665)
at org.nlogo.parse.CompilerUtilities$.numberOrElse(CompilerUtilities.scala:36)
at org.nlogo.parse.CompilerUtilities$.readFromString(CompilerUtilities.scala:22)
at org.nlogo.fileformat.LabLoader.$anonfun$readProtocolElement$4(LabLoader.scala:70)
at scala.collection.TraversableLike$WithFilter.$anonfun$map$2(TraversableLike.scala:742)
at scala.collection.Iterator.foreach(Iterator.scala:941)
at scala.collection.Iterator.foreach$(Iterator.scala:941)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1429)
at scala.collection.IterableLike.foreach(IterableLike.scala:74)
at scala.collection.IterableLike.foreach$(IterableLike.scala:73)
at scala.collection.AbstractIterable.foreach(Iterable.scala:56)
at scala.collection.TraversableLike$WithFilter.map(TraversableLike.scala:741)
at org.nlogo.fileformat.LabLoader.readEnumeratedValueSetElement$1(LabLoader.scala:66)
at org.nlogo.fileformat.LabLoader.$anonfun$readProtocolElement$5(LabLoader.scala:77)
at scala.collection.immutable.List.flatMap(List.scala:338)
at org.nlogo.fileformat.LabLoader.valueSets$1(LabLoader.scala:74)
at org.nlogo.fileformat.LabLoader.readProtocolElement(LabLoader.scala:94)
at org.nlogo.fileformat.LabLoader.$anonfun$apply$1(LabLoader.scala:45)
at scala.collection.immutable.List.map(List.scala:286)
at org.nlogo.fileformat.LabLoader.apply(LabLoader.scala:45)
at org.nlogo.fileformat.LabLoader.apply(LabLoader.scala:30)
at org.nlogo.fileformat.LabFormat.load(NLogoLabFormat.scala:37)
at org.nlogo.fileformat.LabFormat.load$(NLogoLabFormat.scala:35)
at org.nlogo.fileformat.NLogoLabFormat.load(NLogoLabFormat.scala:40)
at org.nlogo.headless.BehaviorSpaceCoordinator$.$anonfun$selectProtocol$1(BehaviorSpaceCoordinator.scala:34)
at scala.Option.map(Option.scala:163)
at org.nlogo.headless.BehaviorSpaceCoordinator$.selectProtocol(BehaviorSpaceCoordinator.scala:32)
at org.nlogo.headless.Main$.runExperiment(Main.scala:23)
at org.nlogo.headless.Main$.$anonfun$main$1(Main.scala:12)
at org.nlogo.headless.Main$.$anonfun$main$1$adapted(Main.scala:12)
at scala.Option.foreach(Option.scala:274)
at org.nlogo.headless.Main$.main(Main.scala:12)
at org.nlogo.headless.Main.main(Main.scala)
Error in util_gather_results(nl, outfile, seed, siminputrow) :
Output file is empty - simulation aborted due to a runtime error!
Make sure that parameter value definitions of the experiment are valid and the model code is running properly!

Can you help me ?
You'll find in attachment nl@experiment and nl@simdesign

nl@experiment <- experiment(expname="Sobol_v-5-4",
outpath=outpath,
repetition=1,
tickmetrics="false",
idsetup="setup",
idgo="go",
runtime=100,
#stopcond = "not any? turtle",
evalticks=seq(0,1000),
metrics=c("count turtles with [infected?]"),#, "infected-at-tick"),
variables = list('initial-people' = list(min=100, max=2000, qfun="qunif"),
'init-infectors-percentage' = list (min=0, max=100, qfun="qunif")
# 'average-delay' = list (min=0, max=32, qfun="qunif"),
# 'mask-wearing-percentage'= list (min=0, max=100, qfun="qunif"),
# 'infection-chance' = list (min=0, max=100, qfun="qunif"),
# 'movement-at-breaks' = list (min=0, max=100, qfun="qunif"),
# 'free-moving-agents' = list(c(values= "true","false")),
# 'mask-filter' = list(c(values=0,0.95,0.99)),
),
constants = list("max-ticks" = 100,
"infection-chance" = 100,
"average-delay" = 32,
"incubation-time" = 320,
"mask-filter" = 0,
"mask-wearing-percentage" = 0,
"asymptomatic-ratio" = "DP",
"free-moving-agents" = "true",
"movement-at-breaks" = 20,
"end-simulation-infectors" = "true",
"recovery-chance" = 100))

nl@simdesign <- simdesign_sobol(nl = nl,
samples = 1000,
sobolorder = 2,
sobolnboot = 10,
sobolconf = 0.95,
nseeds = 6,
precision = 3)

Error loading native library: Unable to load library 'msvcr120'

I am getting the following error, which I believe to be with nlrx, because the model will run in NetLogo now that the R extension from NetLogo issues have been worked out. The r.jar and jna-4.2.2.jar files are both there.

nlrx is running the simulation in Netlogo which uses a nonlinear solver from GAMS in R to choose activities. I have successfully used the model in a linux environment.

C:\Users\zejas\AppData\Roaming\NetLogo\6.1\extensions\r>ls
GPL.txt  jna-4.2.2.jar  models  r.jar  user.properties

image

> utils::sessionInfo() 
R version 3.5.3 (2019-03-11) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 
Running under: Windows 10 x64 (build 17763)  

Matrix products: default  

locale: [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C                           LC_TIME=English_United States.1252      

attached base packages: [1] stats     graphics  grDevices utils     datasets  methods   base       

other attached packages: [1] nlrx_0.2.0           RevoUtils_11.0.3     RevoUtilsMath_11.0.0  

loaded via a namespace (and not attached):  [1] Rcpp_1.0.1       XML_3.98-1.19    crayon_1.3.4     dplyr_0.8.0.1    assertthat_0.2.1 R6_2.4.0          [7] magrittr_1.5     pillar_1.3.1     miscTools_0.6-22 rlang_0.3.4      rstudioapi_0.10  tools_3.5.3      [13] readr_1.3.1      glue_1.3.1       purrr_0.3.2      hms_0.4.2        compiler_3.5.3   pkgconfig_2.0.2  [19] tidyselect_0.2.5 tibble_2.1.1
>



> results <- run_nl_one(nl, seed=1,siminputrow = 1)
Error loading native library: Unable to load library 'msvcr120': Native library (win32-x86-64/msvcr120.dll) not found in resource path ([file:/C:/Users/zejas/AppData/Roaming/NetLogo/6.1/extensions/r/r.jar, file:/C:/Users/zejas/AppData/Roaming/NetLogo/6.1/extensions/r/jna-4.2.2.jar, file:/C:/Users/zejas/AppData/Roaming/NetLogo/6.1/extensions/r/r.jar])
java.lang.UnsatisfiedLinkError: Unable to load library 'msvcr120': Native library (win32-x86-64/msvcr120.dll) not found in resource path ([file:/C:/Users/zejas/AppData/Roaming/NetLogo/6.1/extensions/r/r.jar, file:/C:/Users/zejas/AppData/Roaming/NetLogo/6.1/extensions/r/jna-4.2.2.jar, file:/C:/Users/zejas/AppData/Roaming/NetLogo/6.1/extensions/r/r.jar])
	at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:277)
	at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:403)
	at com.sun.jna.Library$Handler.<init>(Library.java:147)
	at com.sun.jna.Native.loadLibrary(Native.java:502)
	at com.sun.jna.Native.loadLibrary(Native.java:481)
	at org.nlogo.extension.r.Entry.<clinit>(Entry.java:100)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at java.lang.Class.newInstance(Class.java:442)
	at org.nlogo.workspace.JarLoader.extensionClassManager(JarLoader.scala:48)
	at org.nlogo.workspace.ExtensionManager.$anonfun$importExtension$8(ExtensionManager.scala:159)
	at scala.Option.getOrElse(Option.scala:138)
	at org.nlogo.workspace.ExtensionManager.importExtension(ExtensionManager.scala:157)
	at org.nlogo.parse.StructureParser$.$anonfun$parsingWithExtensions$1(StructureParser.scala:74)
	at org.nlogo.parse.StructureParser$.$anonfun$parsingWithExtensions$1$adapted(StructureParser.scala:68)
	at scala.collection.immutable.List.foreach(List.scala:392)
	at org.nlogo.parse.StructureParser$.parsingWithExtensions(StructureParser.scala:68)
	at org.nlogo.parse.StructureParser$.parseSources(StructureParser.scala:33)
	at org.nlogo.parse.NetLogoParser.basicParse(NetLogoParser.scala:17)
	at org.nlogo.parse.NetLogoParser.basicParse$(NetLogoParser.scala:15)
	at org.nlogo.parse.FrontEnd$.basicParse(FrontEnd.scala:10)
	at org.nlogo.parse.FrontEndMain.frontEnd(FrontEnd.scala:26)
	at org.nlogo.parse.FrontEndMain.frontEnd$(FrontEnd.scala:25)
	at org.nlogo.parse.FrontEnd$.frontEnd(FrontEnd.scala:10)
	at org.nlogo.compile.CompilerMain$.compile(CompilerMain.scala:43)
	at org.nlogo.compile.Compiler.compileProgram(Compiler.scala:54)
	at org.nlogo.headless.HeadlessModelOpener.openFromModel(HeadlessModelOpener.scala:50)
	at org.nlogo.headless.HeadlessWorkspace.openModel(HeadlessWorkspace.scala:539)
	at org.nlogo.headless.HeadlessWorkspace.open(HeadlessWorkspace.scala:506)
	at org.nlogo.headless.Main$.newWorkspace$1(Main.scala:18)
	at org.nlogo.headless.Main$.runExperiment(Main.scala:21)
	at org.nlogo.headless.Main$.$anonfun$main$1(Main.scala:12)
	at org.nlogo.headless.Main$.$anonfun$main$1$adapted(Main.scala:12)
	at scala.Option.foreach(Option.scala:274)
	at org.nlogo.headless.Main$.main(Main.scala:12)
	at org.nlogo.headless.Main.main(Main.scala)

model without parameters

Currently it is not possible to run a NetLogo model which does not have at least one parameter on the interface, because the simdesign functions will throw an error if no variables or constants have been defined. However, in some cases it might make sense to run such models with nlrx even if no parameters are changed (at least for simdesign_simple()).
Thus, for simdesign simple we should print a warning but no error to allow running a model without parameter definitions.

Update the write_simoutput function

The function write_simoutput is working but it prints a warning:

The path argument of write_csv() is deprecated as of readr 1.4.0.
Please use the file argument instead.
This warning is displayed once every 8 hours.
Call lifecycle::last_warnings() to see where this warning was generated.

Running multiple simulations at once option (?)

Hello,

I've searched online, including in the documentation, but I've yet to find an answer to my question. In NetLogo, when running an experiment, the user is prompted with the question of how many simulations NetLogo should run at once (I usually go with 4 for my laptop, for example). I'm not seeing this as an option for the nlrx package, and was curious as to whether or not it is possible, given that NetLogo clearly offers such functionality?

macOS NetLogo dmg version

NetLogo can be downloaded for macOS from the NetLogo homepage. They provide a *.dmg file for MacOS. However, we recommend using the tar.gz on linux as well beacause the dmg version is currently not fully compatible with nlrx.

We may need to discuss if we want to stick with this and add this recommendation to the documentation of the package (or maybe add some automated warnings if the dmg file structure is detected).

Alternatively, we could revise nlrx and check if we can add compatability for the macOS dmg version as well

Problems running nlrx on an HPC

Hello there,

I was using nlrx on my local mashine, as well as on an linux HPC. Everything was working fine, despite getting a bunch of warning messages:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.openide.util.lookup.implspi.ActiveQueue$Impl (file:/work3/jonas/netlogo/NetLogo%206.1.1/app/extensions/.bundled/nw/gephi-toolkit-0.8.2.jar) to field java.lang.ref.ReferenceQueue.lock
WARNING: Please consider reporting this to the maintainers of org.openide.util.lookup.implspi.ActiveQueue$Impl
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

Then suddently, and in the middle of a larger job, there were problems on the HPC. The individual jobs didnt finish and I got these reappearing warning messages (every 30 sec):

Nov 29, 2021 11:19:10 AM java.util.prefs.FileSystemPreferences syncWorld
WARNING: Couldn't flush user prefs: java.util.prefs.BackingStoreException: Couldn't get file lock.
Nov 29, 2021 11:19:40 AM java.util.prefs.FileSystemPreferences syncWorld
WARNING: Couldn't flush user prefs: java.util.prefs.BackingStoreException: Couldn't get file lock.
Nov 29, 2021 11:20:10 AM java.util.prefs.FileSystemPreferences syncWorld
WARNING: Couldn't flush user prefs: java.util.prefs.BackingStoreException: Couldn't get file lock.

I cannot figgure out what the problem is, since it seems to be an issue with java and not R. I set JAVA_HOME path, but this is pretty much all I can do, since I dont know java. Do you have any idea what the issue could be?

Here are the first lines of my sessionInfo():

R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.1 LTS

This is the openjdk version and the JAVA_HOME path I am using:

openjdk version "11.0.11" 2021-04-20

/usr/lib/jvm/java-11-openjdk-amd64

Let me know if you more sessionInfo output or other information.

Thanks a lot in advance!

Issues in Setting up Sample Experiment on Manjaro Linux

See end for version numbers of everything and the input code.

I am attempting to set up an initial demo experiment (the one provided in the documentation), and am running into the following error when attempting to get the result:

"cp: cannot stat '~/.netlogo/NetLogo 6.1.1/netlogo-headless.sh': No such file or directory
sed: can't read /tmp/RtmpcV04os/netlogo-headless36d4c209e3dbf.sh: No such file or directory
sed: can't read /tmp/RtmpcV04os/netlogo-headless36d4c209e3dbf.sh: No such file or directory
sh: /tmp/RtmpcV04os/netlogo-headless36d4c209e3dbf.sh: No such file or directory
Error in util_gather_results(nl, outfile, seed, siminputrow) : 
  Temporary output file /tmp/RtmpcV04os/nlrx5493_136d4c7ab3eec4.csvnot found. On unix systems this can happen if the default system temp folder is used.
                Try reassigning the default temp folder for this R session (unixtools package).
In addition: Warning message:
In system(NLcall, wait = TRUE) : error in running command"

Given that I am running R 4.0.0, I cannot us Unixtools to fix the temp path. For note, if I copy-paste the path for netlogo-headless.sh into my file manager, it works perfectly. I have attempted this with NetLogo 6.0.4, 6.1.1, and 5.3.1, and none of them seem to work. Is there anything recommended that I can do?


Input Code:

library(nlrx)

netlogopath <- file.path("~/.netlogo/NetLogo 6.1.1")
modelpath <- file.path(netlogopath, "app/models/Sample Models/Biology/Wolf Sheep Predation.nlogo")
outpath <- file.path("/home/out")
nl <- nl(nlversion = "6.0.3",
        nlpath = netlogopath,
        modelpath = modelpath,
        jvmmem = 1024)
nl@experiment <- experiment(expname="wolf-sheep",
                           outpath=outpath,
                           repetition=1,
                           tickmetrics="true",
                           idsetup="setup",
                           idgo="go",
                           runtime=50,
                           evalticks=seq(40,50),
                           metrics=c("count sheep", "count wolves", "count patches with [pcolor = green]"),
                           variables = list('initial-number-sheep' = list(min=50, max=150, qfun="qunif"),
                                            'initial-number-wolves' = list(min=50, max=150, qfun="qunif")),
                           constants = list("model-version" = "\"sheep-wolves-grass\"",
                                            "grass-regrowth-time" = 30,
                                            "sheep-gain-from-food" = 4,
                                            "wolf-gain-from-food" = 20,
                                            "sheep-reproduce" = 4,
                                            "wolf-reproduce" = 5,
                                            "show-energy?" = "false"))
nl@simdesign <- simdesign_lhs(nl=nl,
                             samples=100,
                             nseeds=3,
                             precision=3)
results <- run_nl_all(nl = nl)

OS:
DISTRIB_ID=ManjaroLinux
DISTRIB_RELEASE=20.0
DISTRIB_CODENAME=Lysia
DISTRIB_DESCRIPTION="Manjaro Linux"

R Version:
platform x86_64-pc-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
status
major 4
minor 0.0
year 2020
month 04
day 24
svn rev 78286
language R
version.string R version 4.0.0 (2020-04-24)
nickname Arbor Day

unnest_simoutput() Error when first row of tibble contains only NA for that breed

When measuring metrics.turtles, it can happen that none of these turtles actually exist.
Usually, a row with NA is created and stored as list in the output tibble.

However, when these data are unnested by unnest_simoutput(), the data types of the first entry in the tibble are used for determining the data type of the new columns. If these are "NA" there are problems coercing those together with numerics that may appear in later rows.

Example:

  • Note the "ask wolves [die]" setup command and the "if (ticks = 5) [create-wolves 1]" go command.
  • This results in having a NA row for wolve metrics in the first row of the output tibble and a row with valid measurements in row 5 of the output tibble
  • As said, this will throw an error for unnest_simoutput()
  • Changing the go command to "if (ticks = 0) [create-wolves 1]" will work, because there will be numeric measurements in the firs trow of the output tibble. Even if there are NA rows later on.
library(nlrx)
# Windows default NetLogo installation path (adjust to your needs!):
netlogopath <- file.path("C:/Program Files/NetLogo 6.0.3")
modelpath <- file.path(netlogopath, "app/models/Sample Models/Biology/Wolf Sheep Predation.nlogo")
outpath <- file.path("C:/out")
# Unix default NetLogo installation path (adjust to your needs!):
netlogopath <- file.path("/home/NetLogo 6.0.3")
modelpath <- file.path(netlogopath, "app/models/Sample Models/Biology/Wolf Sheep Predation.nlogo")
outpath <- file.path("/home/out")

nl <- nl(nlversion = "6.0.3",
         nlpath = netlogopath,
         modelpath = modelpath,
         jvmmem = 1024)

nl@experiment <- experiment(expname="wolf-sheep",
                            outpath=outpath,
                            repetition=1,
                            tickmetrics="true",
                            idsetup=c("setup", "ask wolves [die]"),
                            idgo=c("go", "if (ticks = 5) [create-wolves 1]"),
                            runtime=50,
                            metrics=c("count sheep", "count wolves", "count patches with [pcolor = green]"),
                            metrics.turtles = list("wolves" = c("who")),
                            variables = list('initial-number-sheep' = list(min=50, max=150, qfun="qunif"),
                                             'initial-number-wolves' = list(min=50, max=150, qfun="qunif")),
                            constants = list("model-version" = "\"sheep-wolves-grass\"",
                                             "grass-regrowth-time" = 30,
                                             "sheep-gain-from-food" = 4,
                                             "wolf-gain-from-food" = 20,
                                             "sheep-reproduce" = 4,
                                             "wolf-reproduce" = 5,
                                             "show-energy?" = "false"))

nl@simdesign <- simdesign_simple(nl=nl, nseeds=1)

results <- run_nl_all(nl)
setsim(nl, "simoutput") <- results
results.spatial <- unnest_simoutput(nl)

Java requirements, JAVA_HOME, JRE path setting unclear

Dear @nldoc,

Excited to use nlrx. It looks like the best way to connect R and NetLogo yet. I am having trouble getting it to work, and the problem seems to be the java version, or failing to connect to the JRE (java runtime environment). I have:

  • MacOS 10.14.6
  • R 3.6.1
  • NetLogo 6.1.0
  • Java 1.8.0_221 (Installed in "Library/Internet Plug-Ins")
  • Java 1.6.0 (I think this is "Apple Java", in Library/Java/JavaVirtualMachines)

I use the code:

netlogopath <- file.path("/Applications/NetLogo 6.1.0")
modelpath <- file.path(netlogopath, "/Applications/NetLogo 6.1.0/models/Sample Models/Biology/Wolf Sheep Predation.nlogo")

nl <- nl(nlversion = "6.1.0", nlpath = netlogopath, modelpath = modelpath, jvmmem = 1024)

nl@experiment <- experiment(expname="wolf-sheep", outpath="~/Desktop/", repetition=1, tickmetrics="true", idsetup="setup", idgo="go", idfinal=NA_character_, idrunnum=NA_character_, runtime=50, evalticks=seq(40,50), metrics=c("count sheep", "count wolves", "count patches with [pcolor = green]"), variables = list(), constants = list("initial-number-sheep" = 20, "initial-number-wolves" = 20, "model-version" = "\"sheep-wolves-grass\"", "grass-regrowth-time" = 30, "sheep-gain-from-food" = 4, "wolf-gain-from-food" = 20, "sheep-reproduce" = 4, "wolf-reproduce" = 5, "show-energy?" = "false"))

nl@simdesign <- simdesign_simple(nl=nl, nseeds=3)

results <- run_nl_all(nl)

And I get this error:

JAVA_HOME undefined, using java from path. For control over exact java version, set JAVA_HOME Exception in thread "main" java.lang.UnsupportedClassVersionError: org/nlogo/headless/Main : Unsupported major.minor version 52.0

How do I set JAVA_HOME, as it is not a package variable, apparently? Thanks!

tests

Todo:

  • Generate sample data
    • Rerun all sample data simulations (updated classes)
    • Create one sample data with spatial metrics (turtles, patches)
  • Implement tests (sew below)

Tests

  • class_init.R
    • already started to write tests (test_class_init.R) - these tests worked on my local PC but not appveyor and travis
  • analyze_nl.R
    • write_simoutput() -> use a sample data object, write output to tempfile and load it back into R
  • class_setget.R
    • tests for all setter and getter functions
  • export_nl.R / import_nl.R
    • use a nl object from test data, pack it up together with a folder from the git repository and check if the zip file is created
    • unpack the zip file in a temp folder and check that the files match, delete everything in the tempdir
  • get_nl_spatial.R
    • create sample data with spatial metrics, then write tests for this sample data
  • util_eval.R
    • util_eval_variables() and util_eval_variables_distinct() - straightforward tests
  • util_runnl.R
    • util_cleanup() -> create a temp file with .csv, .xml, .bat, .sh extension and clean the folder then check if all files have been properly deleted
    • util_gather_results(), .util_clean_metrics_patches(), .util_clean_metrics_turtles() -> we need a "behavior space output file" to test this function - can be either tested together with tests, that require Netlogo (probably not on CRAN) or we add a small behaviorspace output file to the repository to run this test

Other

  • Fix travis for MacOS!

Parallelisation fails under the implicit futures operator

Issue

The issue is that (at least under some conditions) parallelisation fails when using the implicit futures operator (%<-%). However, parallelisation succeeds when using a normal assignment operator with the future library and a multisession plan.

Code producing the issue (R 3.5.3, nlrx 0.2.0, Windows 10)

nl@experiment <- experiment(
  expname = "wolf-sheep",
  outpath = outpath,
  repetition = 1,
  tickmetrics = "false",
  idsetup = "setup",
  idgo = "go",
  runtime=10,
#  evalticks=seq(40,50),
  metrics=c("count sheep", "count wolves", "count patches with [pcolor = green]"),
  metrics.turtles=list("turtles"= c("energy")),
  variables = list(
    "initial-number-sheep" = list(min=50, max=150, qfun="qunif"),
    "initial-number-wolves" = list(min=50, max=150, qfun="qunif")),
  constants = list(
    "model-version" = "\"sheep-wolves-grass\"",
    "grass-regrowth-time" = 30,
    "sheep-gain-from-food" = 4,
    "wolf-gain-from-food" = 20,
    "sheep-reproduce" = 4,
    "wolf-reproduce" = 5,
    "show-energy?" = "false"))

nl@simdesign <- simdesign_lhs(
  nl=nl,
  samples=20,
  nseed=1,
  precision=3
)

library(future)

plan(multisession)

results  %<-%  run_nl_all(nl = nl, split = 5)

results.df <- results

Reason to suspect that parallelisation fails

The simulation runs slowly and inspection of (windows) system processes shows multiple R background processes but only 1 JVM instance (see attached screenshot).

Example Screenshot

Workaround

If, instead of...

library(future)

plan(multisession)

results  %<-%  run_nl_all(nl = nl, split = 5)

results.df <- results

...the following is used (note that the implicit futures operator is not used)...

library(future)

plan(multisession)

results.df <- run_nl_all(nl = nl, split = 5)

... then parallelisation succeeds and is visible as the initialisation of 5 JVM processes. The speed with which the set of simulated runs are executed improves accordingly.

Exception error when running sensitivity analysis with nlrx

Hello,

I want to use an sensitivity analysis on the model I build in NetLogo but I always get the following error message in R:

Exception` in thread "main" Expected a literal value. at position 0 in 
	at org.nlogo.core.Fail$.exception(Fail.scala:27)
	at org.nlogo.core.Fail$.exception(Fail.scala:25)
	at org.nlogo.core.Fail$.exception(Fail.scala:23)
	at org.nlogo.parse.LiteralParser.readLiteralPrefix(LiteralParser.scala:83)
	at org.nlogo.parse.LiteralParser.getLiteralValue(LiteralParser.scala:33)
	at org.nlogo.parse.CompilerUtilities$.$anonfun$readFromString$3(CompilerUtilities.scala:22)
	at org.nlogo.parse.CompilerUtilities$.$anonfun$numberOrElse$1(CompilerUtilities.scala:37)
	at scala.util.Either$RightProjection.getOrElse(Either.scala:666)
	at org.nlogo.parse.CompilerUtilities$.numberOrElse(CompilerUtilities.scala:36)
	at org.nlogo.parse.CompilerUtilities$.readFromString(CompilerUtilities.scala:22)
	at org.nlogo.fileformat.LabLoader.$anonfun$readProtocolElement$4(LabLoader.scala:70)
	at scala.collection.TraversableLike$WithFilter.$anonfun$map$2(TraversableLike.scala:739)
	at scala.collection.Iterator.foreach(Iterator.scala:929)
	at scala.collection.Iterator.foreach$(Iterator.scala:929)
	at scala.collection.AbstractIterator.foreach(Iterator.scala:1417)
	at scala.collection.IterableLike.foreach(IterableLike.scala:71)
	at scala.collection.IterableLike.foreach$(IterableLike.scala:70)
	at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
	at scala.collection.TraversableLike$WithFilter.map(TraversableLike.scala:738)
	at org.nlogo.fileformat.LabLoader.readEnumeratedValueSetElement$1(LabLoader.scala:66)
	at org.nlogo.fileformat.LabLoader.$anonfun$readProtocolElement$5(LabLoader.scala:77)
	at scala.collection.immutable.List.flatMap(List.scala:335)
	at org.nlogo.fileformat.LabLoader.valueSets$1(LabLoader.scala:74)
	at org.nlogo.fileformat.LabLoader.readProtocolElement(LabLoader.scala:94)
	at org.nlogo.fileformat.LabLoader.$anonfun$apply$1(LabLoader.scala:45)
	at scala.collection.immutable.List.map(List.scala:283)
	at org.nlogo.fileformat.LabLoader.apply(LabLoader.scala:45)
	at org.nlogo.fileformat.LabLoader.apply(LabLoader.scala:30)
	at org.nlogo.fileformat.LabFormat.load(NLogoLabFormat.scala:37)
	at org.nlogo.fileformat.LabFormat.load$(NLogoLabFormat.scala:35)
	at org.nlogo.fileformat.NLogoLabFormat.load(NLogoLabFormat.scala:40)
	at org.nlogo.headless.BehaviorSpaceCoordinator$.$anonfun$selectProtocol$1(BehaviorSpaceCoordinator.scala:34)
	at scala.Option.map(Option.scala:146)
	at org.nlogo.headless.BehaviorSpaceCoordinator$.selectProtocol(BehaviorSpaceCoordinator.scala:32)
	at org.nlogo.headless.Main$.runExperiment(Main.scala:23)
	at org.nlogo.headless.Main$.$anonfun$main$1(Main.scala:12)
	at org.nlogo.headless.Main$.$anonfun$main$1$adapted(Main.scala:12)
	at scala.Option.foreach(Option.scala:257)
	at org.nlogo.headless.Main$.main(Main.scala:12)
	at org.nlogo.headless.Main.main(Main.scala)
Fehler in util_gather_results(nl, outfile, seed, siminputrow) : 
  Output file is empty - simulation aborted due to a runtime error!
         Make sure that parameter value definitions of the experiment are valid and the model code is running properly!

This even happens with your example of the wolf-sheep model:

#set paths
netlogopath <- file.path("/Applications/NetLogo 6.0.3")
modelpath <- file.path("/Applications/NetLogo 6.0.3/models/Sample Models/Biology/Wolf Sheep Predation.nlogo")
outpath <- file.path("/Users/christian/Schreibtisch")

#attach paths, set available memory for JAVA, set NetLogo version
nl <- nl(nlversion = "6.0.3",
         nlpath = netlogopath,
         modelpath = modelpath,
         jvmmem = 1024)

nl@experiment <- experiment(expname="wolf-sheep",
                            outpath=outpath,
                            repetition=1,
                            tickmetrics="true",
                            idsetup="setup",
                            idgo="go",
                            idfinal=NA_character_,
                            idrunnum=NA_character_,
                            runtime=50,
                            evalticks=seq(40,50),
                            metrics=c("count sheep", "count wolves", "count patches with [pcolor = green]"),
                            variables = list('initial-number-sheep' = list(min=50, max=150, qfun="qunif"),
                                             'initial-number-wolves' = list(min=50, max=150, qfun="qunif")),
                            constants = list("model-version" = "\"sheep-wolves-grass\"",
                                             "grass-regrowth-time" = 30,
                                             "sheep-gain-from-food" = 4,
                                             "wolf-gain-from-food" = 20,
                                             "sheep-reproduce" = 4,
                                             "wolf-reproduce" = 5,
                                             "show-energy?" = "false"))


nl@simdesign <- simdesign_eFast(nl=nl,
                                samples=10,
                                nseeds=3)

setsim(nl, "simoutput") <- run_nl_all(nl)

I dont really know about programming so maybe the solution to this is very trivial but I would be very thankful for your help!

code review issues

Documentation

README

Add More details on package functionality, in particular

  • (J) Add small section berfore Installation with general information and links to NetLogo online resources

  • (J) Add information that Java jdk 1.8 needs to be installed and java path needs to be set correctly

  • (J) Add information that NetLogo needs to be installed (link to the download_netlogo function)

  • (J) Add information which NetLogo versions are supported

  • (J) Clearer statement of why the package is needed (check Ropensci onboarding submission text)

    • This can also be added to the description and GetStarted vignette
    • ** In my opinion, the description already makes clear why the package is needed **

  • Once readme is final -> Copy to GetStarted vignette and to the roxygen header of the nlrx-package.R file

Vignettes / examples

Code Examples

  • Examples assume that users run windows, instead we could set the relevant path variables at the start of the examples and have unix and windows default paths; in the following examples we can then use these variables
  • add library(nlrx) to code examples after installation code snippet
  • add library(future) to code examples before %<% is used
  • library(ggplot2) needs to be called in the examples for spatial output
  • gganimate needs a renderer installed - add comment for installing gifski and png package -> add to spatial output examples
  • Add another code example with a different model where heading is important (flocking?) and how to translate netlogo heading (degree scale) to ggplot

Vignettes

  • Simdesigns vignette: add more descriptive information on what the simdesigns do
  • Remove technical details (such as S4 classes) from the GetStarted/Readme sections
  • experiment: variables that are not defined in constants or variables will have their default NetLogo value (add to all relevant sections)
  • Add more information on parallelisation to further notes (locally parallelisation over seeds - split function)

Documentation

  • Example code sections are wrapped in \dontrun{} -> remove for functions that are independent of NetLogo (e.g. class getter/setter functions, ...)
  • Package title in titlecase
  • Description should not start with "The nlrx package ...." (CRAN requirement)
  • update R-dependency to R >= 3.3 (lhs package and sf require this)

Class constructor

  • nl constructor: explain more detailed what experiment and simdesign are and link to their help pages
  • experiment constructor: more detail on information that is stored within the class
  • simdesign constructor: design types should also be listed in description, not just stated as examples. Include links to simdesign_helper functions

Analyze_nl

  • analyze_nl:
    • fullstop missing in value section
    • it is currently not clear what the function does exactly because it depends on the chosen simdesign. This could be stated more clearly, with links to the analyze functions.
  • analyze_simple is empty, either remove or add something useful such as a warning message: "'no analysis can take place if a simple design is chosen"
  • simdesign_simple: The fact that currently no analyze_nl function is available should be mentioned

Other

  • run_nl_one and run_nl_all have the same title and description -> revise
  • download_netlogo: replace "Charatcer string with..." with "path to folder"
  • util_eval_: help files are not very informative - comments of these functions could be recylced to create more useful documentation of these functions

Package functionality

Error messages

  • when attaching an experiment, add error message if netlogo variable is defined in variables and constants list (eval_experiment?)
  • If a parameter is wrong, after running the simulations, the results are going to be empty but no warning or other message will be shown before you try to assign in the results, and will get an error 'replacement has 1 row, data has 0'. It would be helful if there was a hint to the user to check the parameters in the experiment should be given.

Spatial output

  • line 86: get_nl_spatial runs but prints a very bad message, a repetiting (a lot of times) 'no non-missing arguments to min; returning Infno non-missing arguments to max; returning -Inf', which I don't know where it comes from. With another model without patches information the message does not appear.
  • line 137: for results_spatial_tibble, all metrics.turtles are returned NA. I think it comes from the full_join of ddplyr, which only preserves the grouping of x (so pathces) and the not matching values are ignored and replaced with NA. So the turtles cannot be plotted afterwards, getting error 'object 'turtles_x' not found'

Write_simoutput

  • add an option (function input) to reassign the default output path of the nl object with another user-defined folder
  • add an error message if the directory does not exist, with a hint that it needs to be created first

Other

  • define .initClasses and simdesign-class as internal functions
  • util_runnl: There is some dead commented-out code at the end (lines 500 - 650).
  • load-model-parameters: The name report-model-parameters would be more representative of this function.
  • How does the package responds to runtime errors of the actual netlogo model? I think it inputs the error message in the object and doesn't give any warnings. Maybe you can try to catch some main runtime errors of netlogo and print a warning. Aditionally, if the netlogo model is broken the results will run forever (or at least as much patience I had :P ); maybe you could add something to break the function and show a warning message to check the netlogo model. Or state that the package will not respond to problems in netlogo, so the user can always have this in mind.
  • Add information that for optimizations run_nl_dyn need to be used instead of run_nl_all/one

update clustermq example

Once the new version 0.4.2 has been released to cran, we can update the clustermq example in the advanced configuration vignette. The run_nl_one function no supports saving results as rds, so this can be removed from the simfun.

Help wanted about memory issues

I'm trying to perform a Sobol analysis of Netlogo results, using NLRX and future.
I tried to use two different windows machines (the first 96 GB and 20 Cores, the latter 32 Gb and 16 cores).

I set up the experiment and defined a "future" simulation plan.
However I'm experiencing memory issues.
Even with low samples (500) the memory space exhausted (but the machine has 96 GB on board.

Can you help me, please ?

CODE

Sys.setenv(JAVA_HOME = "c:/Program Files/Java/jdk-15.0.2/")
netlogopath <- file.path("C:/Program Files/NetLogo 6.2.0")

modello con slider per mask filter

modelpath <- file.path("v5-4-6-6.4_SFERISTERIO.nlogo")
outpath <- file.path("output")

nl <- nl(nlversion = "6.2.0",
nlpath = netlogopath,
modelpath = modelpath,
jvmmem = 4096)

Space use

tickperact = 4
numact = 4
queuetime = 2
pausetime = 2
durata = queuetime *2 + numact * tickperact + pausetime * (numact - 1)

Experiment

nl@experiment <- experiment(expname="Sferisterio",
outpath=outpath,
repetition=1,
tickmetrics="true",
idsetup="setup",
idgo="go",
runtime= durata, #PRIMA: 320
evalticks= durata, #seq(1, 26, by = 1), #26, # seq(2,16, by=2), # c(2,6,15), #seq(1,15, by=2),#PRIMA: 320, by 49
metrics=c("count-infected", "initial-people"),
variables = list(
'initial-people' = list(min = 600, max = 750, qfun = "qunif"),
'init-infectors-percentage' = list(min = 10, max = 15, qfun = "qunif"),
'vaccinated-percentage' = list(min = 30, max = 70, qfun = "qunif"), #dato minimo al 18.4.2021
'vaccine-effectiveness' = list(min = 85, max = 95, qfun = "qunif"), #Pfizer e Moderna 94%, Astra zeneca 79%
'mask-wearing-percentage' = list(min = 90, max = 100, qfun="qunif") #così gestiamo la percentuale di persone con quelle mascherine

                        ),
                        
                        constants = list( "form-coeff" = 0.5,
                                          "mask-filter" = 0.01,
                                          "infection-chance" = 100,
                                          "asymptomatic-ratio" = "\"DP\"",
                                          "average-delay" = 96, #PRIMA: 32
                                          "average-recovery-time" = 256,
                                          "incubation-time" = 512,
                                          "movement-at-breaks" = 56,
                                          "free-moving-agents" = 1,
                                          "tickperact" = tickperact,
                                          "numact" = numact,
                                          "queuetime" = queuetime,
                                          "pausetime" = pausetime,
                                          "moving-at-pause" = 25
                                          
                        )

)

nl@simdesign <- simdesign_sobol2007(nl = nl,
samples = 1000, ##campiona le variables e metrics (per prova messo 10); sobol richiede grandi campionamenti > 1000 valori
#sobolorder = 2, ##fornisce i primi 2 ordini di interazione
sobolnboot = 2, ## lo ripete 100 volte (per prova messo 2)
sobolconf = 0.95,
nseeds = 2,
precision = 3)

processi = 2* nrow(nl@simdesign@siminput)
processi
detectCores()

nl@simdesign@siminput
plan(list(sequential,multisession))
system.time(results %<-% run_nl_all(nl = nl, split = 14 ))
setsim(nl, "simoutput") <- results # si attaccano i risultati all'oggetto nl
sensitivityIndices <- analyze_nl(nl) # si calcolano gli indici

Salvataggio

write.csv(file = "output/RES_SFER_1.csv", results)
write.csv(file = "oputput/sobol_SFER_1.csv", sensitivityIndices)

analyze_nl calculates NA for sensitivity indices without warning if some results are missing

Sometimes, the results tibble of an nl object may not cover all combinations of seeds and siminputrows (due to errors, wrong filtering, etc.). In such cases, the calculation of snesitivity ndices might not work (at least for morris this is defenitely the case).
Task:
Add warning for analyze_nl functions if combinations of seeds and siminputrows of the simoutput tibble do not match the combinations of the simdesign (siminput, simseeds).

help wanted on Error "Temporary simulation output file not found"

Hi,

I was trying to test the example codes in the Wolf-sheep model but found a constant error stopping any further running. The example codes I used are the sensitivity test and ABCmcmc_Marjoram calibration. I have read debugging notes in the ??run_nl_all() and think that there is no problem with file paths and software installation. I hope I could get some help or inspiration here if anybody knows how to fix it.
Thanks in advance.

The error is:

Error in util_gather_results(nl, outfile, seed, siminputrow) : 
  Temporary simulation output file not found: E:/OUT\Rtmp2lBq1P\nlrx_seed_1257402563_row_1_1abc6a2c4941.csv.
Either the simulation did not run or crashed, check the debugging section in ??run_nl_all() for help.
In addition: Warning message:
The `path` argument of `write_lines()` is deprecated as of readr 1.4.0.
Please use the `file` argument instead.
This warning is displayed once every 8 hours.
Call `lifecycle::last_warnings()` to see where this warning was generated. 

The code I use is:

library(nlrx)
Sys.setenv(JAVA_HOME="C:/Program Files/Java/jdk1.8.0_301/bin")
# 1. Windows default NetLogo installation path (adjust to your needs!)
netlogopath <- file.path("C:/Program Files/NetLogo 6.2.0")
modelpath <- file.path(netlogopath, "app/models/Sample Models/Biology/Wolf Sheep Predation.nlogo")
outpath <- file.path("E:/Temp") #this is a new folder created for this experiment

nl <- nl(nlversion = "6.2.0",
         nlpath = netlogopath,
         modelpath = modelpath,
         jvmmem = 1024)
nl@experiment <- experiment(expname = "wolf-sheep-morris",
                            outpath = outpath,
                            repetition = 1,   
                            tickmetrics = "true",
                            idsetup = "setup",  
                            idgo = "go",        
                            runtime = 500,
                            metrics=c("count sheep", "count wolves", "count patches with [pcolor = green]"),
                            variables = list("initial-number-sheep" = list(min=50, max=150, step=10, qfun="qunif"),
                                             "initial-number-wolves" = list(min=50, max=150, step=10, qfun="qunif"),
                                             "grass-regrowth-time" = list(min=0, max=100, step=10, qfun="qunif"),
                                             "sheep-gain-from-food" = list(min=0, max=50, step=10, qfun="qunif"),
                                             "wolf-gain-from-food" = list(min=0, max=100, step=10, qfun="qunif"),
                                             "sheep-reproduce" = list(min=0, max=20, step=5, qfun="qunif"),
                                             "wolf-reproduce" = list(min=0, max=20, step=5, qfun="qunif")),
                            constants = list("model-version" = "\"sheep-wolves-grass\"",
                                             "show-energy?" = "false"))
nl@simdesign <- simdesign_lhs(nl, samples=500, nseeds=1, precision=3)
library(future)
plan(multisession)
results <- run_nl_all(nl, split=10)

run_nl_all() fails and outputs non-intuitive error message if experiment(expname=x) is set incorrectly

Issue

If the following code is run (R 3.5.3 x64, Windows, nlrx version 0.2.0), then the simulation fails with a difficult-to-interpret error message.

Code generating the issue


library(nlrx)

netlogopath <- file.path("C:/Program Files/NetLogo 6.1.0")
modelpath <- file.path(netlogopath, "app/models/Sample Models/Biology/Wolf Sheep Predation.nlogo")

nl <- nl(nlversion = "6.1.0",
         nlpath = netlogopath,
         modelpath = modelpath,
         jvmmem = 1024
         )

nl@experiment <- experiment(
  expname = "wolf sheep",
  outpath = outpath,
  repetition = 1,
  tickmetrics = "false",
  idsetup = "setup",
  idgo = "go",
  runtime=50,
#  evalticks=seq(40,50),
  metrics=c("count sheep", "count wolves", "count patches with [pcolor = green]"),
  metrics.turtles=list("turtles"= c("energy")),
  variables = list(
    "initial-number-sheep" = list(min=50, max=150, qfun="qunif"),
    "initial-number-wolves" = list(min=50, max=150, qfun="qunif")),
  constants = list(
    "model-version" = "\"sheep-wolves-grass\"",
    "grass-regrowth-time" = 30,
    "sheep-gain-from-food" = 4,
    "wolf-gain-from-food" = 20,
    "sheep-reproduce" = 4,
    "wolf-reproduce" = 5,
    "show-energy?" = "false"))

nl@simdesign <- simdesign_lhs(
  nl=nl,
  samples=3,
  nseed=1,
  precision=3
)

results <- run_nl_all(nl)

Error message produced

Error in util_gather_results(nl, outfile, seed, siminputrow) :
Temporary output file C:\Users\LD\AppData\Local\Temp\RtmpOo3xQB\nlrx5572_14230106c1654.csvnot found. On unix systems this can happen if the default system temp folder is used.
Try reassigning the default temp folder for this R session (unixtools package).

Source of the problem

The apparent source of the problem appears to be that expname="wolf sheep". If this is changed to expname="wolf-sheep" then the simulations runs without apparent errors.

Have not tested against nlrx 0.3.0

Missing blank spaces in turtles-own lists

Hi!

I'm currently facing problems with list outputs (I know that there is another issue on lists but I think my question is different). The NLRX output of lists that are defined as globals works fine but when I have a list as turtles-own or patches-own, blank spaces are missing in the NLRX output of that list in metrics.turtles, i.e. one gets an output of "[74148222]" instead of "[74 148 222]".

The behavior can be reproduced by adding, for example, an energy-list to the turtles-own in the Wolf Sheep Predation, i.e. by adding

turtles-own [ energy energy-list ]

and

ask turtles [ set energy-list (list energy (energy * 2) (energy * 3)) ]

in the go procedure and

metrics.turtles = list("turtles" = c("energy-list"))

in the example of simdesign_simple that you provide.

I also tried a workaround and implemented a function that converts the list to a quasi-R vector in NetLogo (a string in the form "c(..., ..., ...)") which can be converted to a real vector in R. However, this also only works for global variables but truncates the string after the first comma (i.e. the output is "c(...")) for turtles-own and patches-own lists (and gives the warning Expected 3 pieces. Additional pieces discarded...).

Thanks for your help (and for this great package which in general works perfect)!

Error: '/tmp/Rtmp*/*.csv' does not exist.

There are two main reasons this error message appears:

  1. The NetLogo model simulation run that was linked to this output file did not complete or did not write the output file properly.

We should add a more informative error message if this happens! Something like "NetLogo model run aborted - no output written".
We then should implement a option for run_nl_all where the user one can define, if run_nl_all should proceed with the next row in the simulation matrix or abort the complete experiment.

  1. nlrx uses the default temp directory of the R session. On some linux systems, the default folder (/tmp) is observed by a system process (tmpwatch) that may from time to time delete folders and files. This may cause file deletions, before the output is gathered by nlrx.

To avoid this issue, set the temp directory manually. The r-package unixtools provides a handy function to accomplish this:

install.packages('unixtools', repos = 'http://www.rforge.net/')
unixtools::set.tempdir("<path-to-temp-dir>")

output csvs not found when using nlrx and clustermq on remote computing cluster

Hello,

I've been trying to get one of the example NetLogo models (Wolf Sheep) to run in parallel on our university's computing cluster before attempting to do the same for my own NetLogo model. I can run models nlrx with no problem, but when I try to follow the clustermq example on the Advanced Configuration page I get errors that look like this when I run the clustermq::Q() function:

Submitting 5 worker jobs (ID: nlrx) ...
Running 5 calculations (2 objs/0.3 Mb common; 1 calls/chunk) ...
Master: [2.1s 1.0% CPU]; Worker: [avg 75.3% CPU, max 286.4 Mb]                
Error in summarize_result(job_result, n_errors, n_warnings, cond_msgs,  : 
  5/5 jobs failed (0 warnings). Stopping.
(Error #1) Temporary output file /bsuscratch/jwiniarski/nlrx/temp/nlrx_seed_-581380792_row_1_1b6fefcb74e.csvnot found. On unix systems this can happen if the default system temp folder is used.
                Try reassigning the default temp folder for this R session (unixtools package).
(Error #2) Temporary output file /bsuscratch/jwiniarski/nlrx/temp/nlrx_seed_-581380792_row_2_1b78131ad8f3.csvnot found. On unix systems this can happen if the default system temp folder is used.
                Try reassigning the default temp folder for this R session (unixtools package).
(Error #3) Temporary output file /bsuscratch/jwiniarski/nlrx/temp/nlrx_seed_-581380792_row_3_1b82112baf79.csvnot found. On unix systems this can happen if the default system temp folder is used.
                Try reassigning the default temp folder for this R session (unixtools package).
(Error #4) Temporary output file /bsuscratch/jwiniarski/nlrx/temp/nlrx_seed_-5

I've tried changing the location of the temp directory, but end up with similar errors. I'm connecting to the university's computing cluster and running the following code using the command line (the terminal on my macbook):

# install unixtools
install.packages('unixtools', repos = 'http://www.rforge.net/')

# set java path
Sys.setenv(JAVA_HOME= "/usr/")

# Load packages
library(nlrx)
library(here)

# create output folder
dir.create(here::here('out'))

# create temp folder
dir.create(here::here('temp'))

# set netlogo, output, and model paths
netlogopath <- file.path("/cm/shared/apps/netlogo/6.1.1/")
outpath <- file.path(here::here("out"))
modelpath <- file.path(here::here("Wolf Sheep Predation_nlrx.nlogo"))


# latin hypercube simulation design ---------------------------------------------------------

# set up nl object
nl <- nl(
  nlversion = "6.1.1",
  nlpath = netlogopath,
  modelpath = modelpath,
  jvmmem = 1024 # memory
)

# create experiment
nl@experiment <- experiment(
  expname = "wolf-sheep",
  outpath = outpath,
  repetition = 1,
  tickmetrics = "true",
  idsetup = "setup", 
  idgo = "go",        
  runtime = 500,
  metrics = c(
    "count sheep", 
    "count wolves", 
    "count patches with [pcolor = green]"
  ),
  variables = list(
    "initial-number-sheep" = list(min = 50, max = 150, step = 10, qfun = "qunif"),
    "initial-number-wolves" = list(min = 50, max = 150, step = 10, qfun = "qunif"),
    "grass-regrowth-time" = list(min = 0, max = 100, step = 10, qfun = "qunif"),
    "sheep-gain-from-food" = list(min = 0, max = 50, step = 10, qfun = "qunif"),
    "wolf-gain-from-food" = list(min = 0, max = 100, step = 10, qfun = "qunif"),
    "sheep-reproduce" = list(min = 0, max = 20, step = 5, qfun = "qunif"),
    "wolf-reproduce" = list(min = 0, max = 20, step = 5, qfun = "qunif")
  ),
  constants = list(
    "model-version" = "\"sheep-wolves-grass\"",
    "show-energy?" = "false"
  )
)

# create latin hypercube parameter set
# just do a handful of simulations for this example
nl@simdesign <- simdesign_lhs(nl, samples = 5, nseeds = 1, precision = 3)

# does the model work before trying clustermq?
results_sequential <- run_nl_all(nl)

setsim(nl, "simoutput") <- results_sequential

# Write output to outpath of experiment within nl
write_simoutput(nl)


# now try in parallel ---------------------------------------------------------

library(clustermq)

# set up number of jobs
njobs <- nrow(nl@simdesign@siminput) * length(nl@simdesign@simseeds)

# Second, we generate vectors for looping trough model runs.
# We generate a vector for simpinputrows by repeating the sequence of parameterisations for each seed.
# Then, we generate a vector of random-seeds by repeating each seed for n times, where n is the number of siminputrows.
siminputrows <- rep(seq(1:nrow(nl@simdesign@siminput)), length(nl@simdesign@simseeds))
rndseeds <- rep(nl@simdesign@simseeds, each=nrow(nl@simdesign@siminput))

# Third, we define our simulation function
# Please adjust the path to the temporary file directory
simfun <- function(nl, siminputrow, rndseed, writeRDS=FALSE) {
  unixtools::set.tempdir(here::here('temp')) # what to set here for temp directory?
  library(nlrx)
  res <- run_nl_one(
    nl = nl, 
    siminputrow = siminputrow, 
    seed = rndseed, 
    writeRDS = TRUE
    )
  return(res)
}

# does the simfun work at all?
simfun(nl = nl, siminputrow = siminputrows[1], rndseed = rndseeds[1], writeRDS = TRUE)

# Fourth, use the Q function from the clustermq package to run the jobs on the HPC:
# The q function loops through our siminputrows and rndseeds vectors.
# The function creates njobs jobs and distributes corresponding chunks of the input vectors to these jobs for executing the simulation function simfun.

# As constants we provide our nl object and the function parameter writeRDS. 
# If write RDS is true, an *.rds file will be written on the HPC after each jobs has finished.
# This can be useful to gain results from completed runs after a crash has occured.
results <- clustermq::Q(
  fun = simfun, 
  siminputrow = siminputrows,
  rndseed = rndseeds,
  const = list(
    nl = nl,
    writeRDS = TRUE
  ),
  export = list(), 
  seed = 42, 
  n_jobs = njobs, 
  template = list(
    job_name = "nlrx",
    log.file = "nlrx.log",
    queue = "bsudfq", # name of borah cluster queue
    service = "normal",
    walltime = "00:30:00",
    mem_cpu = "4000"
  )
)  

# The Q function reports the individual results of each job as a list
# Thus, we convert the list results to tibble format:
results <- dplyr::bind_rows(results)

I can't seem to figure out why those csvs can't be located...is this a clustermq issue?

Thanks, and thanks for creating this great R package,

Jay

Working on Mac

As part of my rOpenSci review I'm having trouble to get this to work on my Mac. Adapting the Windows code I run this:

nl <- nl(nlversion = "6.0.4",
  nlpath = "/Applications/NetLogo 6.0.4/",
  modelpath = "/Applications/NetLogo 6.0.4/models/Sample Models/Biology/Wolf Sheep Predation.nlogo",
  jvmmem = 1024)
nl@experiment <- experiment(expname="wolf-sheep",
  outpath="~/Desktop/",
  repetition=1,
  tickmetrics="true",
  idsetup="setup",
  idgo="go",
  idfinal=NA_character_,
  idrunnum=NA_character_,
  runtime=50,
  evalticks=seq(40,50),
  metrics=c("count sheep", "count wolves", "count patches with [pcolor = green]"),
  variables = list(),
  constants = list("initial-number-sheep" = 20,
    "initial-number-wolves" = 20,
    "model-version" = "\"sheep-wolves-grass\"",
    "grass-regrowth-time" = 30,
    "sheep-gain-from-food" = 4,
    "wolf-gain-from-food" = 20,
    "sheep-reproduce" = 4,
    "wolf-reproduce" = 5,
    "show-energy?" = "false"))
nl@simdesign <- simdesign_simple(nl=nl, nseeds=3)

When I try to run this I get:

future::plan(multisession)
results %<-% run_nl_all(nl = nl, cleanup = "all")
#> sh: : command not found

I can trace this back to util_read_write_batch(nl) returning NULL but beyond that I'm at a loss, but my guess is that it has something to do with differences between Mac and Windows versions?

switching to github actions

moving from travis and appveyor to github actions

we nedd three workflows:

  1. R CMD check
    run R CMD check, running NetLogo models
    Run tests on MacOS, Linux and Windows release versions
    Call codecov
  2. Render README.Rmd
  3. Render pkdown site

Simulation does not end

I have a simulation that does not seem to end. I have stop conditions in both the model and the nlrx code. I am running on a domino data lab server with plenty of resources left. The results object is just never generated. I am sitting here with the stop sign waiting in my RStudio session. I have uploaded my NetLogo model as a text file.

Any ideas?

image

Summer19.txt


# Unix default NetLogo installation path (adjust to your needs!):
netlogopath <- file.path("/opt/NetLogo/NetLogo 6.1.0")
modelpath <- file.path("/mnt/Summer19.nlogo")
outpath <- file.path("/mnt/results/out")

nl <- nl(nlversion = "6.1.0",
         nlpath = netlogopath,
         modelpath = modelpath,
         jvmmem = 1024)

nl@experiment <- experiment(expname="wild-pigs",
                            outpath=outpath,
                            repetition=1,
                            tickmetrics="true",
                            idsetup="setup",
                            idgo="go",
                            runtime=16,
                            evalticks=seq(1,15),
                            metrics=c("count sows", "count boars","count sounders"),
                            metrics.turtles=list("households" = c("who", "HH_income", "wealthi")),
                            variables = list('sex_ratio' = list(min=0, max=1, step=0.25),
                                             'cdecay' = list(min=0, max=1, step=0.25),
                                             'rule' = list(values=c("\"at-random\"",
                                                                    "\"myopic-pmax\"", 
                                                                    "\"at-random-with-uniform-removal\""))),
                            constants = list("y_corn" = 1500,
                                             "y_soy" = 400,
                                             "y_crp" = 10,
                                             "p_corn" = 4,
                                             "p_soy" = 10,
                                             "p_crp" = 75,
                                             "vc_corn" = 100,
                                             "vc_soy" = 100,
                                             "vc_crp" = 1,
                                             "defaultLitter" = 8,
                                             "minSounderN" = 5,
                                             "maxSounderN" = 20,
                                             "pig_range" = 20,
                                             "brange" = 10,
                                             "init_sounder" = 5,
                                             "init_hh" = 45,
                                             "epsilon" = 1.19E-7,
                                             "too-close" = 1.1,
                                             "too-far" = 4.1,
                                             "step-size" = 1,
                                             "uniform_removal" = 0.5,
                                             "max_pigs" = 500)
                            )



nl@simdesign <- simdesign_simple(nl=nl,
                              nseeds=3)

results <- run_nl_one(nl, seed = getsim(nl, "simseeds")[1], siminputrow = 1)

# Attach results to nl object:
setsim(nl, "simoutput") <- results

# Write output to outpath of experiment within nl
write_simoutput(nl)

# Do further analysis:
analyze_nl(nl)

NLRX package archived on CRAN

The NLRX package has been archived on CRAN with the following message:

"_Package ‘nlrx’ was removed from the CRAN repository.

Formerly available versions can be obtained from the archive.

Archived on 2021-06-03 as email to the maintainer was undeliverable.

A summary of the most recent check results can be obtained from the check results archive.

Please use the canonical form https://CRAN.R-project.org/package=nlrx to link to this page._"

As a result it's not possible anymore to install the package in the normal way via the R-console, and instead has to be done from the archive, which has been quite tricky to get sorted and has left me less confident that everything has been installed properly.

fix calculation of attached runs in print_nl() function

print_nl() provides an estimation of expected runs and reports the actual number of runs of the attached simoutput file.
However this calculation is wrong. It does not consider that multiple rows can exist for the same siminputrow/seed combination.
Thus, things like this can happen:
number of siminputrows: 598
number of random seeds: 5
estimated number of runs: 2990
simoutput results attached: ✓
number of runs calculated: 1731645

This can be easily fixed by calculating the number of attached runs by using unique values vectors of the siminputrow and random-seed columns.

nlrx and NetLogo's R extension

Hello,

is it possible to run NetLogo code that uses the R extension? I implented the extension to my model to calculate some spatial indices but now it seems that I can not run a BehaviorSpace experiment using nlrx anymore.

If it is not possible to use both together, is there a way around? Is there a way to run for example an eFast experiment and calculate the indices afterwards (efficiently)?

run_nl_all(): cannot access temporary file

Dear @nldoc ,

I am setting up experiment on linux (slurm cluster) with the wolf-sheep example. I use R version 3.4.4 and Netlogo 6.0.4. It ran into this error:

run_nl_all(nl)
 
sh: /lustre/ssd/ws/kw/tmp/netlogo-headless2c6f268a317f.sh: Permission denied
Error in util_gather_results(nl, outfile, seed, siminputrow) :
   Temporary output file /lustre/ssd/ws/kw/tmp/nlrx4440_12c6f77e97517.csvnot found. On unix systems this can happen if the default system temp folder is used.
       Try reassigning the default temp folder for this R session (unixtools package).

It seems that I got permission denied to the file netlogo-headless2c6f268a317f.sh. However when I tried to run this particular file manually, I didn't get this error (I assume that actually I should have access to the file, then).

sh /lustre/ssd/ws/kw/tmp/netlogo-headless2c6f268a317f.sh
 
you must specify --model

and this is the example code I use to setup the experiment:

library(nlrx)

netlogopath <- file.path("/lustre/ssd/ws/kw/NetLogo-6.0.4/")
modelpath <- file.path("/lustre/ssd/ws/kw/NetLogo-6.0.4/app/models/Sample Models/Biology/Wolf Sheep Predation.nlogo")
outpath <- file.path("/lustre/ssd/ws/kw/out/")

# define Netlogo version used
nl <- nl(nlversion = "6.0.4",
         nlpath = netlogopath,
         modelpath = modelpath,
         jvmmem = 1024)

nl@experiment <- experiment(expname="wolf-sheep",
                            outpath=outpath,
                            repetition=1,
                            tickmetrics="true",
                            idsetup="setup",
                            idgo="go",
                            runtime=50,
                            evalticks=seq(40,50),
                            metrics=c("count sheep", "count wolves", "count patches with [pcolor = green]"),
                            variables = list('initial-number-sheep' = list(min=50, max=150, qfun="qunif"),
                                             'initial-number-wolves' = list(min=50, max=150, qfun="qunif")),
                            constants = list("model-version" = "\"sheep-wolves-grass\"",
                                             "grass-regrowth-time" = 30,
                                             "sheep-gain-from-food" = 4,
                                             "wolf-gain-from-food" = 20,
                                             "sheep-reproduce" = 4,
                                             "wolf-reproduce" = 5,
                                             "show-energy?" = "false"))

nl@simdesign <- simdesign_lhs(nl=nl,
                              samples=100,
                              nseeds=1,
                              precision=3)

The output when checking with print(nl):

supported nlversion: ✓
nlpath exists on local system: ✓
modelpath exists on local system: ✓
valid jvm memory: ✓
valid experiment name: ✓
outpath exists on local system: ✓
setup and go defined: ✓
variables defined: ✓
variables present in model: ✓
constants defined: ✓
constants present in model: ✓
metrics defined: ✓
spatial Metrics defined: ✓
simdesign attached: ✓
siminput parameter matrix: ✓
number of siminputrows: 100
number of random seeds: 1
estimated number of runs: 100
simoutput results attached: ✗
number of runs calculated: ✗

I would really appreciate any help on this issue. Thanks!

experiment class default for runtime

Currently the default for runtime is 1, that means if no runtime is defined, the model will only run for one tick. We should think about if we want to change this to infinite runtim (NA_integer_) or at least print a warning, that runtime is set to 1 if no specific runtime is defined in the experiment!

NetLogo list output

The most common data types of NetLogo globals are numerics, strings and lists.
Currently, nlrx only supports numeric and strings to be entered as metrics or/and agent metrics slot.
While such variables can be entered, they are read as string and are not parsed correctly.

The Problem is that these lists can have multiple dimensions and parsing them to R data structures in a senseful way will be quite complex. A simple list could be just nested within a tibble in the same way we do it for the agent metrics. However, if we have for example a list of lists as an agent variable that would mean to have at least 3 nested layers within the results tibble.

How do we approach this?

  1. Find a workaround to at least measure usual lists without nesting?
  2. Give detailed examples on the homepage, how lists can be written to file from within NetLogo and how these can be related to the nlrx results output?
  3. any other ideas?

random seed generator

The random seed generator needs to be fixed.
There should be no double occurences and the number of digits of each seed need to be dynamic

How to set strings/factors in `experiment()`?

Thanks for this incredibly easy way to call NetLogo from R, what a nice improvement of the hustle of RNetLogo.

However, I struggle to enter strings or factors as variables when setting up an experiment using the experiment() command. The documentation says that "Each list item consist of a min value, a max value, a step value and a qfun (e.g. list("paramA" = list(min=0, max=1, step=0.1, qfun="qunif")))" which is not the case for strings/factors. Every way putting the strings in a list did result in an error asking for 'from' must be of length 1.

Is there a way to set up strings/factors as input variables?

Exceeded step memory limit

I've been trying to run nlrx with my GIS extension model, but seems to fail all the time. I thought it was my local machine that had less memory, however it doesn't seemed to work on the HPC either.

R script

Sys.setenv(JAVA_HOME='/usr/local/software/spack/spack-0.11.2/opt/spack/linux-rhel7-x86_64/gcc-5.4.0/jdk-xxxxxx') #Ubuntu cluster

## Load packages
library(nlrx)
library(tidyverse) 
library(rcartocolor)
library(ggthemes) 


# HPC 
netlogopath <- file.path("/usr/local/Cluster-Apps/netlogo/6.0.4")
outpath <- file.path("/home/hs621/github/nlrx")

## Step1: Create a nl obejct:
nl <- nl(nlversion = "6.0.4",
         nlpath = netlogopath,
         modelpath = file.path("/home/hs621/github/jasss/Gangnam_v6.nlogo"),
         jvmmem = 1024)


## Step2: Add Experiment
nl@experiment <- experiment(expname = "seoul",
                            outpath = outpath,
                            repetition = 1,   
                            tickmetrics = "true",
                            idsetup = "setup",  
                            idgo = "go",        
                            runtime = 8763,
                            evalticks=seq(1,8763),
                            constants = list("PM10-parameters" = 100,
                                             "Scenario" = "\"BAU\"",
                                             "scenario-percent" = "\"inc-sce\""),
                            variables = list('AC' = list(values=c(100,150,200))),
                            metrics.turtles =  list("people" = c("pxcor", "pycor", "homename", "destinationName", "age", "health")),
                            metrics.patches = c("pxcor", "pycor", "pcolor"))

# Evaluate if variables and constants are valid:
eval_variables_constants(nl)

nl@simdesign <- simdesign_distinct(nl = nl, nseeds = 1)

# Step4: Run simulations:
init <- Sys.time()
results <- run_nl_all(nl = nl)
Sys.time() - init

# Attach results to nl object:
setsim(nl, "simoutput") <- results

seesion info:

Changed directory to /home/hs621/github/nlrx.

JobID: 11247244
======
Time: Fri May  3 02:34:12 BST 2019
Running on master node: cpu-e-488
Current directory: /home/hs621/github/nlrx

Nodes allocated:
================
cpu-e-488

numtasks=, numnodes=1, mpi_tasks_per_node=9 (OMP_NUM_THREADS=1)

Executing command:
==================
Rscript /home/hs621/github/nlrx/nlrx_seoul.R

── Attaching packages ─────────────────────────────────────── tidyverse 1.2.1 ──
✔ ggplot2 3.0.0purrr   0.3.1tibble  2.0.1dplyr   0.8.0.1tidyr   0.8.3stringr 1.4.0readr   1.3.1forcats 0.3.0  
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
All defined variables and constants are valid!
Creating distinct simulation design

Time difference of 4.585698 hours
/var/spool/slurm/slurmd/job11247244/slurm_script: line 134: 42363 Killed Rscript /home/hs621/github/nlrx/nlrx_seoul.R
slurmstepd: error: Exceeded step memory limit at some point.

As you can see in the last line, I get errors of slurmstepd: error: Exceeded step memory limit at some point. I presume the error happens when I assign setsim(nl, "simoutput") <- results.

Is there a way to solve this issue, or do I have to split my evalticks? Many thanks

FYI All my netlogo files are stored on my github repository(https://github.com/mrsensible/airpollutionABM).

future updates

List of future nlrx updates:

  • improve analyze_nl for lhs output
  • (Add evalticks.spatial to apply different output filters to spatial and non-spatial data)

Documentation updates:

`simdesign` error with `simdesign_ff()`

Hi,
I have a java issue

Exception in thread "main" Expected a literal value. at position 0 in 
	at org.nlogo.core.Fail$.exception(Fail.scala:27)
	at org.nlogo.core.Fail$.exception(Fail.scala:25)
	at org.nlogo.core.Fail$.exception(Fail.scala:23)
	at org.nlogo.parse.LiteralParser.readLiteralPrefix(LiteralParser.scala:83)
	at org.nlogo.parse.LiteralParser.getLiteralValue(LiteralParser.scala:33)
	at org.nlogo.parse.CompilerUtilities$.$anonfun$readFromString$3(CompilerUtilities.scala:22)
	at org.nlogo.parse.CompilerUtilities$.$anonfun$numberOrElse$1(CompilerUtilities.scala:37)
	at scala.util.Either$RightProjection.getOrElse(Either.scala:665)
	at org.nlogo.parse.CompilerUtilities$.numberOrElse(CompilerUtilities.scala:36)
	at org.nlogo.parse.CompilerUtilities$.readFromString(CompilerUtilities.scala:22)
	at org.nlogo.fileformat.LabLoader.$anonfun$readProtocolElement$4(LabLoader.scala:70)
	at scala.collection.TraversableLike$WithFilter.$anonfun$map$2(TraversableLike.scala:742)
	at scala.collection.Iterator.foreach(Iterator.scala:941)
	at scala.collection.Iterator.foreach$(Iterator.scala:941)
	at scala.collection.AbstractIterator.foreach(Iterator.scala:1429)
	at scala.collection.IterableLike.foreach(IterableLike.scala:74)
	at scala.collection.IterableLike.foreach$(IterableLike.scala:73)
	at scala.collection.AbstractIterable.foreach(Iterable.scala:56)
	at scala.collection.TraversableLike$WithFilter.map(TraversableLike.scala:741)
	at org.nlogo.fileformat.LabLoader.readEnumeratedValueSetElement$1(LabLoader.scala:66)
	at org.nlogo.fileformat.LabLoader.$anonfun$readProtocolElement$5(LabLoader.scala:77)
	at scala.collection.immutable.List.flatMap(List.scala:338)
	at org.nlogo.fileformat.LabLoader.valueSets$1(LabLoader.scala:74)
	at org.nlogo.fileformat.LabLoader.readProtocolElement(LabLoader.scala:94)
	at org.nlogo.fileformat.LabLoader.$anonfun$apply$1(LabLoader.scala:45)
	at scala.collection.immutable.List.map(List.scala:286)
	at org.nlogo.fileformat.LabLoader.apply(LabLoader.scala:45)
	at org.nlogo.fileformat.LabLoader.apply(LabLoader.scala:30)
	at org.nlogo.fileformat.LabFormat.load(NLogoLabFormat.scala:37)
	at org.nlogo.fileformat.LabFormat.load$(NLogoLabFormat.scala:35)
	at org.nlogo.fileformat.NLogoLabFormat.load(NLogoLabFormat.scala:40)
	at org.nlogo.headless.BehaviorSpaceCoordinator$.$anonfun$selectProtocol$1(BehaviorSpaceCoordinator.scala:34)
	at scala.Option.map(Option.scala:163)
	at org.nlogo.headless.BehaviorSpaceCoordinator$.selectProtocol(BehaviorSpaceCoordinator.scala:32)
	at org.nlogo.headless.Main$.runExperiment(Main.scala:23)
	at org.nlogo.headless.Main$.$anonfun$main$1(Main.scala:12)
	at org.nlogo.headless.Main$.$anonfun$main$1$adapted(Main.scala:12)
	at scala.Option.foreach(Option.scala:274)
	at org.nlogo.headless.Main$.main(Main.scala:12)
	at org.nlogo.headless.Main.main(Main.scala)
Error in util_gather_results(nl, outfile, seed, siminputrow) : 
  Output file is empty - simulation aborted due to a runtime error!
         Make sure that parameter value definitions of the experiment are valid and the model code is running properly!
Error in util_gather_results(nl, outfile, seed, siminputrow) : 
  Output file is empty - simulation aborted due to a runtime error!
         Make sure that parameter value definitions of the experiment are valid and the model code is running properly!
De plus : Warning messages:
1: In NCOL(x) : redémarrage de l'évaluation d'une promesse interrompue
2: In stop("Failed to determine columns for object of class '", class(x),  :
  redémarrage de l'évaluation d'une promesse interrompue
3: In stop("Failed to determine columns for object of class '", class(x),  :
  redémarrage de l'évaluation d'une promesse interrompue
Error in util_gather_results(nl, outfile, seed, siminputrow) : 
  Output file is empty - simulation aborted due to a runtime error!
         Make sure that parameter value definitions of the experiment are valid and the model code is running properly!
De plus : Warning message:
In .rs.toDataFrame(obj, objName, TRUE) :
 redémarrage de l'évaluation d'une promesse interrompue

with that

nl@experiment <- experiment(expname="explo-review3",
                            outpath=outpath,
                            repetition=1,
                            tickmetrics="true",
                            idsetup="setup",
                            idgo="go",
                            runtime=1000,
                            evalticks=seq(500,1000),
                            metrics=c("sort [size] of turtles", "sort [color] of turtles", "obsMs", "obsMp", "obsMsr", "obsMsa"),
                            metrics.turtles= list("turtles" = c("who", "size", "color")),
                            variables = list('alpha' = list(values=c(0, 0.2), min=0, max=0.2, step = 0.2, qfun="qunif"),
                                             'file_map_space' = list(values = c("data/maps_space/random-map100.txt",
                                                                                "data/maps_space/sugar-map100.txt")),
                                             'file_map_agents' = list(values = c("data/maps_agents/input_agents_506_1_.txt",
                                                                                 "data/maps_agents/input_agents_506_2_.txt",
                                                                                 "data/maps_agents/input_agents_506_3_.txt",
                                                                                 "data/maps_agents/input_agents_506_4_.txt",
                                                                                 "data/maps_agents/input_agents_506_5_.txt",
                                                                                 "data/maps_agents/input_agents_regular_506_1_.txt",
                                                                                 "data/maps_agents/input_agents_regular_506_2_.txt",
                                                                                 "data/maps_agents/input_agents_regular_506_3_.txt",
                                                                                 "data/maps_agents/input_agents_regular_506_4_.txt",
                                                                                 "data/maps_agents/input_agents_regular_506_5_.txt")),
                                             'timeV' = list(values=c(0,1), min=0, max=1,step = 1, qfun="qunif"),
                                             'simplify?' = list(values=c(TRUE,FALSE))
                                             ),
                            constants = list("coop2stock" = 0.5,
                                             "coop2burn" = 0.5,
                                             "seasonPeriod" = 360,
                                             "maxR" = 0.1,
                                             "scenarii_files?" = TRUE,
                                             "maxStock" = 10000,
                                             "vitalSugar" = 2,
                                             "extractRate" = 1,
                                             "nAgent" = 506,
                                             "diffuse-rate" = 0.2,
                                             "hide-turtles" = FALSE,
                                             "minSize" = 0.5,
                                             "maxSize" = 10,
                                             "heterogResugar?" = TRUE,
                                             "initSize" = 1
                                             )
                            )



nl@simdesign <- simdesign_ff(nl=nl, nseeds = 3)

regarding issue 22 it seems to be an algorithm problem but I'm using simdesign_ff() and nl@simdesign@siminput give me

# A tibble: 160 x 20
   alpha file_map_space file_map_agents timeV `simplify?` coop2stock coop2burn seasonPeriod  maxR `scenarii_files… maxStock vitalSugar
   <dbl> <chr>          <chr>           <dbl> <lgl>            <dbl>     <dbl>        <dbl> <dbl> <lgl>               <dbl>      <dbl>
 1   0   data/maps_spa… data/maps_agen…     0 TRUE               0.5       0.5          360   0.1 TRUE                10000          2
 2   0.2 data/maps_spa… data/maps_agen…     0 TRUE               0.5       0.5          360   0.1 TRUE                10000          2
 3   0   data/maps_spa… data/maps_agen…     0 TRUE               0.5       0.5          360   0.1 TRUE                10000          2
 4   0.2 data/maps_spa… data/maps_agen…     0 TRUE               0.5       0.5          360   0.1 TRUE                10000          2
 5   0   data/maps_spa… data/maps_agen…     0 TRUE               0.5       0.5          360   0.1 TRUE                10000          2
 6   0.2 data/maps_spa… data/maps_agen…     0 TRUE               0.5       0.5          360   0.1 TRUE                10000          2
 7   0   data/maps_spa… data/maps_agen…     0 TRUE               0.5       0.5          360   0.1 TRUE                10000          2
 8   0.2 data/maps_spa… data/maps_agen…     0 TRUE               0.5       0.5          360   0.1 TRUE                10000          2
 9   0   data/maps_spa… data/maps_agen…     0 TRUE               0.5       0.5          360   0.1 TRUE                10000          2
10   0.2 data/maps_spa… data/maps_agen…     0 TRUE               0.5       0.5          360   0.1 TRUE                10000          2
# … with 150 more rows, and 8 more variables: extractRate <dbl>, nAgent <dbl>, `diffuse-rate` <dbl>, `hide-turtles` <lgl>, minSize <dbl>,
#   maxSize <dbl>, `heterogResugar?` <lgl>, initSize <dbl>

I can't see any NA's. Any ideas where m I wrong ?

Thank you for this package!

metrics.turtles only works for turtle-own but not for <breed>-own

-own variables can be either defined for all turtles (turtles-own) or specific breeds (e.g. sheep-own). Currently the metrics.turtles measurement slot only works with turtles-owns variables.

The quickfix would be to copy-paste all breed-own variables to the turtles-own variables.
However, it would be nice if nlrx would work with these breed-own variables as well.

Experiment progression feedback

How would one implement a progress bar for the number of stimulations of total simulation completed in current experiment?
Perhaps a means to resume an experiment? I don't think it's impossible since nlrx would have that data already

Expressions are currently not supported in `rename()`

Hi,

I really appreciate what you're doing here! I'm trying to replicate your articles in a jupyter notebook served up via mybinder.org, for use in one of my classes. I'm hitting a snag in attaching the results to nl object:

setsim(nl, "simoutput") <- results

# Report spatial data:
results_spatial <- get_nl_spatial(nl,
                                  turtles = TRUE,
                                  patches = TRUE,
                                  turtle_coords = "px",
                                  format="spatial")

Result:

Error: Expressions are currently not supported in `rename()`
Traceback:

1. local({
 .     value <- value(future)
 .     rm(list = future_name, envir = assign.env)
 .     value
 . })
2. eval.parent(substitute(eval(quote(expr), envir)))
3. eval(expr, p)
4. eval(expr, p)
5. eval(quote({
 .     value <- value(future)
 .     rm(list = future_name, envir = assign.env)
 .     value
 . }), new.env())
6. eval(quote({
 .     value <- value(future)
 .     rm(list = future_name, envir = assign.env)
 .     value
 . }), new.env())
7. value(future)
8. value.Future(future)
9. resignalCondition(future)

I'm guessing the issue is I'm just missing a particular package in my install?

GenAlg/GenSA runaway process

Using NetLogo 6.2.0 on macOS and following the Wolf Sheep Predation example in the documentation, I've been getting a runaway process after invoking run_nl_dyn, regardless of added limits (e.g., iters = 10 for GenAlg, control=list(max.time = 600) for GenSA). The process has yet to terminate. (Note that I've been able to successfully run latin-hypercube simdesigns without issue.)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.