diff --git a/DESCRIPTION b/DESCRIPTION index 39d7161f..d4c7ee27 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: nlrx Type: Package Title: Setup, Run and Analyze 'NetLogo' Model Simulations from 'R' via 'XML' -Version: 0.1.0 +Version: 0.2.0 Authors@R: c(person("Jan", "Salecker", email = "jsaleck@gwdg.de", role = c("aut", "cre"), comment = c(ORCID = '0000-0002-9000-4229')), person("Marco", "Sciaini", email = "sciaini.marco@gmail.com", role = c("aut"), comment = c(ORCID = '0000-0002-3042-5435')), diff --git a/NEWS.md b/NEWS.md index d062254f..5edb0ba9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ -# nlrx 0.1.0 +## nlrx 0.2.0 + +* nl_to_raster() hotfix + +## nlrx 0.1.0 * First release to CRAN. diff --git a/R/nl_to_raster.R b/R/nl_to_raster.R index 835c2b20..c6e37706 100644 --- a/R/nl_to_raster.R +++ b/R/nl_to_raster.R @@ -51,7 +51,7 @@ nl_to_raster <- function(nl){ patches_dat <- patches %>% dplyr::select(patch.metrics) %>% - split(., patches[,c("siminputrow", "[step]", "random-seed")]) %>% + split(., patches[,c("siminputrow", "[step]", "random-seed")], lex.order=TRUE) %>% purrr::map(., function(x){ raster::rasterFromXYZ(x[, c("pxcor", "pycor", diff --git a/README.Rmd b/README.Rmd index 7ca43358..2db38f34 100644 --- a/README.Rmd +++ b/README.Rmd @@ -49,9 +49,11 @@ While the nlrx package might work without setting the Java system path explicitl ## Installation -~~You can install the released version of nlrx from~~ -~~[CRAN](https://CRAN.R-project.org) with:~~ -~~install.packages("nlrx")~~ +You can install the released version of nlrx from +[CRAN](https://CRAN.R-project.org) with: +``` r +install.packages("nlrx") +``` And the development version from [GitHub](https://github.com/) with: diff --git a/README.md b/README.md index 5fbe8022..44fb7750 100644 --- a/README.md +++ b/README.md @@ -1,86 +1,37 @@ +nlrx +================================================================= -# nlrx - -[![Build -Status](https://travis-ci.org/ropensci/nlrx.svg?branch=master)](https://travis-ci.org/ropensci/nlrx)[![Build -status](https://ci.appveyor.com/api/projects/status/swsstjxxjnkyuoh9/branch/master?svg=true)](https://ci.appveyor.com/project/marcosci/nlrx/branch/master) -[![Codecov test -coverage](https://codecov.io/gh/nldoc/nlrx/branch/master/graph/badge.svg)](https://codecov.io/gh/ropensci/nlrx?branch=master) -[![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://www.tidyverse.org/lifecycle/#maturing) -[![ropensci](https://badges.ropensci.org/262_status.svg)](https://github.com/ropensci/onboarding/issues/262) - -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 -focusses 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](https://ccl.northwestern.edu/netlogo/docs/) - -NetLogo comes with the built-in experiment tool [Behavior -Space](https://ccl.northwestern.edu/netlogo/docs/behaviorspace.html) -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. - -## Prerequirements +[![Build Status](https://travis-ci.org/ropensci/nlrx.svg?branch=master)](https://travis-ci.org/ropensci/nlrx)[![Build status](https://ci.appveyor.com/api/projects/status/swsstjxxjnkyuoh9/branch/master?svg=true)](https://ci.appveyor.com/project/marcosci/nlrx/branch/master) [![Codecov test coverage](https://codecov.io/gh/nldoc/nlrx/branch/master/graph/badge.svg)](https://codecov.io/gh/ropensci/nlrx?branch=master) [![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://www.tidyverse.org/lifecycle/#maturing) [![ropensci](https://badges.ropensci.org/262_status.svg)](https://github.com/ropensci/onboarding/issues/262) + +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 focusses 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](https://ccl.northwestern.edu/netlogo/docs/) + +NetLogo comes with the built-in experiment tool [Behavior Space](https://ccl.northwestern.edu/netlogo/docs/behaviorspace.html) 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. + +Prerequirements +--------------- ### NetLogo -In order to use the nlrx package, [NetLogo](http://netlogoweb.org/) -(\>=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. +In order to use the nlrx package, [NetLogo](http://netlogoweb.org/) (>=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. ### 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](https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html). -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. +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](https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html). 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 +Installation +------------ -~~You can install the released version of nlrx from~~ -~~[CRAN](https://CRAN.R-project.org) with:~~ -~~install.packages(“nlrx”)~~ +You can install the released version of nlrx from [CRAN](https://CRAN.R-project.org) with: + +``` r +install.packages("nlrx") +``` And the development version from [GitHub](https://github.com/) with: @@ -89,35 +40,18 @@ And the development version from [GitHub](https://github.com/) with: devtools::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). +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. +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. +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. ``` r library(nlrx) @@ -138,12 +72,7 @@ nl <- nl(nlversion = "6.0.3", #### 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. +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. ``` r nl@experiment <- experiment(expname="wolf-sheep", @@ -168,19 +97,7 @@ nl@experiment <- experiment(expname="wolf-sheep", #### 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: +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: ``` r nl@simdesign <- simdesign_lhs(nl=nl, @@ -191,14 +108,7 @@ nl@simdesign <- simdesign_lhs(nl=nl, #### 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). +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). ``` r results <- run_nl_all(nl) @@ -206,15 +116,7 @@ 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. +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. ``` r # Attach results to nl object: @@ -227,18 +129,13 @@ write_simoutput(nl) analyze_nl(nl) ``` -## Meta - - - Please [report any issues or - bugs](https://github.com/nldoc/nlrx/issues/new/). - - 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](CONTRIBUTING.md). - - Please note that this project is released with a [Contributor - Code of Conduct](CODE_OF_CONDUCT.md). By participating in this - project you agree to abide by its -terms. +Meta +---- + +- Please [report any issues or bugs](https://github.com/nldoc/nlrx/issues/new/). +- 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](CONTRIBUTING.md). + - Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms. [![ropensci\_footer](https://ropensci.org/public_images/ropensci_footer.png)](https://ropensci.org) diff --git a/cran-comments.md b/cran-comments.md index be702050..87550144 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1,6 +1,7 @@ # Resubmission -We replaced \dontrun with \donttest for examples that are executable but run longer then 5 sec per Rd-file. +Hotfix for nl_to_raster() function. +In the previous package version, under some circumstances the raster column was reported in wrong order. ## Test environments * Windows 10, R 3.5.2 diff --git a/docs/CODE_OF_CONDUCT.html b/docs/CODE_OF_CONDUCT.html index 9ae7912e..824b0de0 100644 --- a/docs/CODE_OF_CONDUCT.html +++ b/docs/CODE_OF_CONDUCT.html @@ -1,6 +1,6 @@ - + @@ -9,17 +9,17 @@ Contributor Code of Conduct • nlrx | NetLogo from R - + - + - + - + - + @@ -38,8 +38,7 @@ - - + - + @@ -9,17 +9,17 @@ CONTRIBUTING • nlrx | NetLogo from R - + - + - + - + - + @@ -38,8 +38,7 @@ - - + - + @@ -9,17 +9,17 @@ GNU General Public License • nlrx | NetLogo from R - + - + - + - + - + @@ -38,8 +38,7 @@ - - + + Interfacing a variety of different NetLogo models • nlrx | NetLogo from R - - - + + + - @@ -23,15 +23,14 @@

… and now are ready to interface some of the models of the NetLogo model library.

@@ -165,7 +164,7 @@

## Step1: Create a nl obejct:
 nl <- nl(nlversion = "6.0.4",
          nlpath = netlogopath,
-         modelpath = file.path(netlogopath, "app/models/Sample Models/Art/Diffusion Graphics.nlogo"),
+         modelpath = file.path(netlogopath, "app/models/Sample Models/Art/Diffusion Graphics.nlogo"),
          jvmmem = 1024)
 
 ## Step2: Add Experiment
@@ -176,14 +175,14 @@ 

idsetup = "setup", idgo = "go", runtime = 500, - evalticks = seq(1,500), - constants = list("num-turtles" = 20, + evalticks = seq(1,500), + constants = list("num-turtles" = 20, "diffusion-rate" = 1, "turtle-heat" = 139, "turtle-speed" = 1.0, "wander?" = "TRUE"), - metrics.turtles = list("turtles" = c("who", "xcor", "ycor")), - metrics.patches = c("pxcor", "pycor", "pcolor", "heat")) + metrics.turtles = list("turtles" = c("who", "xcor", "ycor")), + metrics.patches = c("pxcor", "pycor", "pcolor", "heat")) # Evaluate if variables and constants are valid: eval_variables_constants(nl) @@ -218,7 +217,7 @@

coord_equal() + theme_void() -gganimate::animate(p1, nframes = n, width=800, height=800, duration=6)

+gganimate::animate(p1, nframes = n, width=800, height=800, duration=6)

@@ -231,7 +230,7 @@

## Step1: Create a nl obejct:
 nl <- nl(nlversion = "6.0.4",
          nlpath = netlogopath,
-         modelpath = file.path(netlogopath, "app/models/Sample Models/Earth Science/Fire.nlogo"),
+         modelpath = file.path(netlogopath, "app/models/Sample Models/Earth Science/Fire.nlogo"),
          jvmmem = 1024)
 
 ## Step2: Add Experiment
@@ -242,11 +241,11 @@ 

idsetup = "setup", idgo = "go", runtime = 500, - evalticks = seq(1,500), - metrics = c("count patches"), - metrics.turtles = list("turtles" = c("who", "pxcor", "pycor", "breed", "color")), - metrics.patches = c("pxcor", "pycor", "pcolor"), - constants = list('density' = 62) + evalticks = seq(1,500), + metrics = c("count patches"), + metrics.turtles = list("turtles" = c("who", "pxcor", "pycor", "breed", "color")), + metrics.patches = c("pxcor", "pycor", "pcolor"), + constants = list('density' = 62) ) # Evaluate if variables and constants are valid: @@ -265,32 +264,32 @@

setsim(nl, "simoutput") <- results nl_spatial <- unnest_simoutput(nl) -n <- max(nl_spatial$`[step]`) +n <- max(nl_spatial$`[step]`) embers <- nl_spatial %>% dplyr::filter(breed == "embers" & agent == "turtles" & `[step]` < n) %>% dplyr::select(pxcor, pycor, `[step]`, color, who) fires <- nl_spatial %>% dplyr::filter(breed == "fires" & agent == "turtles" & `[step]` < n) %>% dplyr::select(pxcor, pycor, `[step]`, color, who) patches <- nl_spatial %>% dplyr::filter(agent == "patches" & `[step]` < n) %>% dplyr::select(pxcor, pycor, pcolor, `[step]`) # make same space in the memory -rm(nl) -rm(results) -rm(nl_spatial) -gc() +rm(nl) +rm(results) +rm(nl_spatial) +gc() #---------------------------------- ## Plot animation: p1 <- ggplot(embers) + - geom_tile(data = patches, aes(x=pxcor, y=pycor, fill=factor(pcolor))) + + geom_tile(data = patches, aes(x=pxcor, y=pycor, fill=factor(pcolor))) + geom_point(data = embers, aes(x = pxcor, y = pycor, color = color, group = who), size=2) + - scale_color_gradientn(colors = rev(cartography::carto.pal("orange.pal", n1 = 8))) + + scale_color_gradientn(colors = rev(cartography::carto.pal("orange.pal", n1 = 8))) + geom_point(data = fires, aes(x = pxcor, y = pycor, color = color, group = who), size=3) + - scale_fill_manual(values = c("0" = "gray24", "55" = "#83B37D", "11.4" = "#B59A89")) + + scale_fill_manual(values = c("0" = "gray24", "55" = "#83B37D", "11.4" = "#B59A89")) + transition_time(`[step]`) + guides(fill="none", color="none") + coord_equal() + theme_void() -g1 <- gganimate::animate(p1, nframes = n, width=800, height=800, duration = 6)

+g1 <- gganimate::animate(p1, nframes = n, width=800, height=800, duration = 6)
@@ -311,7 +310,7 @@

## Step1: Create a nl obejct:
 nl <- nl(nlversion = "6.0.4",
          nlpath = netlogopath,
-         modelpath = file.path(netlogopath, "app/models/Sample Models/Biology/Ants.nlogo"),
+         modelpath = file.path(netlogopath, "app/models/Sample Models/Biology/Ants.nlogo"),
          jvmmem = 1024)
 
 ## Step2: Add Experiment
@@ -322,10 +321,10 @@ 

idsetup = "setup", idgo = "go", runtime = 1000, - evalticks = seq(1,1000), - metrics.turtles = list("turtles" = c("who", "pxcor", "pycor", "breed")), - metrics.patches = c("pxcor", "pycor", "pcolor", "chemical", "food", "food-source-number"), - constants = list("population" = 125, + evalticks = seq(1,1000), + metrics.turtles = list("turtles" = c("who", "pxcor", "pycor", "breed")), + metrics.patches = c("pxcor", "pycor", "pcolor", "chemical", "food", "food-source-number"), + constants = list("population" = 125, 'diffusion-rate' = 50, 'evaporation-rate' = 10)) @@ -344,13 +343,13 @@

## Step6: Prepare data for plotting # Extract infromation on food sources and select maximum step as simulation length: food_dat <- nl_spatial %>% dplyr::filter(food > 0) %>% dplyr::select(pxcor, pycor, `[step]`) -nmax <- max(food_dat$`[step]`) +nmax <- max(food_dat$`[step]`) food_dat <- food_dat %>% dplyr::filter(`[step]` %in% 1:nmax) # Extract information on chemicals and apply minimum treshhold for coloring: chem_dat <- nl_spatial %>% dplyr::filter(`[step]` %in% 1:nmax) %>% dplyr::select(pxcor, pycor, chemical, `[step]`) -chem_dat$chemical <- ifelse(chem_dat$chemical <= 0.2, NA, chem_dat$chemical) +chem_dat$chemical <- ifelse(chem_dat$chemical <= 0.2, NA, chem_dat$chemical) # Extract information on turtle positions: -turt_dat <- nl_spatial %>% dplyr::filter(!is.na(who)) %>% dplyr::filter(`[step]` %in% 1:nmax) %>% dplyr::select(pxcor, pycor, who, `[step]`) +turt_dat <- nl_spatial %>% dplyr::filter(!is.na(who)) %>% dplyr::filter(`[step]` %in% 1:nmax) %>% dplyr::select(pxcor, pycor, who, `[step]`) # Create a new data frame to overlay the nest position (in this case the center of the world 0,0) nest_dat <- food_dat nest_dat$pxcor <- 0 @@ -358,9 +357,9 @@

## Step7: Plotting p1 <- ggplot(food_dat) + - geom_tile(data=chem_dat, aes(x=pxcor, y=pycor, fill=sqrt(chemical))) + + geom_tile(data=chem_dat, aes(x=pxcor, y=pycor, fill=sqrt(chemical))) + geom_point(data=food_dat, aes(x=pxcor, y=pycor), color="black", shape=15, size=4.5, alpha=1) + - geom_text(data=turt_dat, aes(x=pxcor, y=pycor, group=who, color=as.numeric(who)), size=5, alpha=1, label="¥") + + geom_text(data=turt_dat, aes(x=pxcor, y=pycor, group=who, color=as.numeric(who)), size=5, alpha=1, label="¥") + geom_point(data=nest_dat, aes(x=pxcor, y=pycor), color="brown", fill="white", size=30, stroke=2, shape=21) + scale_fill_viridis_c(direction=-1, option="magma", na.value = "white") + scale_color_gradient_tableau(palette="Orange") + @@ -371,7 +370,7 @@

theme_void() ## Step8: Animate the plot and use 1 frame for each step of the model simulations -gganimate::animate(p1, nframes = nmax, width=800, height=800, fps=10)

+gganimate::animate(p1, nframes = nmax, width=800, height=800, fps=10)
@@ -392,7 +391,7 @@

## Step1: Create a nl obejct: nl <- nl(nlversion = "6.0.4", nlpath = netlogopath, - modelpath = file.path(netlogopath, modelpath), + modelpath = file.path(netlogopath, modelpath), jvmmem = 1024) ## Step2: Add Experiment @@ -403,10 +402,10 @@

idsetup = "setup", idgo = "go", runtime = 100, - evalticks = seq(1,100), - variables = list('friction' = list(values = c(5,25,50,90))), - constants = list("stiffness" = 20), - metrics.turtles = list("turtles" = c("who", "xcor", "ycor", "driver?", "edge?", "color"))) + evalticks = seq(1,100), + variables = list('friction' = list(values = c(5,25,50,90))), + constants = list("stiffness" = 20), + metrics.turtles = list("turtles" = c("who", "xcor", "ycor", "driver?", "edge?", "color"))) ## Step3: Add a Simulation Design nl@simdesign <- simdesign_distinct(nl = nl, @@ -424,8 +423,8 @@

# Prepare data for plotting nl_spatial <- unnest_simoutput(nl) -nl_spatial$friction <- factor(nl_spatial$friction) -levels(nl_spatial$friction) <- c("Friction = 5", +nl_spatial$friction <- factor(nl_spatial$friction) +levels(nl_spatial$friction) <- c("Friction = 5", "Friction = 25", "Friction = 50", "Friction = 90") @@ -446,7 +445,7 @@

coord_equal() + theme_void() -gganimate::animate(p1, width=800, height=800, duration = 6) +gganimate::animate(p1, width=800, height=800, duration = 6)
@@ -462,7 +461,7 @@

## Step1: Create a nl obejct:
 nl <- nl(nlversion = "6.0.4",
          nlpath = netlogopath,
-         modelpath = file.path(netlogopath, "app/models/Sample Models/Biology/Flocking.nlogo"),
+         modelpath = file.path(netlogopath, "app/models/Sample Models/Biology/Flocking.nlogo"),
          jvmmem = 1024)
 
 ## Step2: Add Experiment
@@ -473,14 +472,14 @@ 

idsetup = "setup", idgo = "go", runtime = 300, - evalticks = seq(1,300), - constants = list("population" = 100, + evalticks = seq(1,300), + constants = list("population" = 100, "vision" = 5, "minimum-separation" = 1, "max-align-turn" = 4, "max-cohere-turn" = 4, "max-separate-turn" = 4), - metrics.turtles = list("turtles" = c("who", "xcor", "ycor", "heading", "color"))) + metrics.turtles = list("turtles" = c("who", "xcor", "ycor", "heading", "color"))) # Evaluate if variables and constants are valid: eval_variables_constants(nl) @@ -515,7 +514,7 @@

coord_equal() + theme_void() -gganimate::animate(p1, nframes=max(nl_spatial$`[step]`), width=600, height=600, fps=20)

+gganimate::animate(p1, nframes=max(nl_spatial$`[step]`), width=600, height=600, fps=20)
@@ -528,7 +527,7 @@

coord_equal() + theme_void() -gganimate::animate(p2, nframes=max(nl_spatial$`[step]`), width=600, height=600, fps=20) +gganimate::animate(p2, nframes=max(nl_spatial$`[step]`), width=600, height=600, fps=20)
@@ -545,7 +544,7 @@

## Step1: Create a nl obejct:
 nl <- nl(nlversion = "6.0.4",
          nlpath = netlogopath,
-         modelpath = file.path(netlogopath, "app/models/Sample Models/Networks/Preferential Attachment.nlogo"),
+         modelpath = file.path(netlogopath, "app/models/Sample Models/Networks/Preferential Attachment.nlogo"),
          jvmmem = 1024)
 
 ## Step2: Add Experiment
@@ -556,13 +555,13 @@ 

idsetup = "setup", idgo = "go", runtime = 200, - evalticks = seq(1,200), - constants = list("layout?" = TRUE), - metrics.turtles = list("turtles" = c("who", + evalticks = seq(1,200), + constants = list("layout?" = TRUE), + metrics.turtles = list("turtles" = c("who", "xcor", "ycor", "color")), - metrics.links = list("links" = c("[who] of end1","[who] of end2"))) + metrics.links = list("links" = c("[who] of end1","[who] of end2"))) ## Step3: Add simdesign nl@simdesign <- simdesign_simple(nl=nl, nseeds = 1) @@ -589,8 +588,8 @@

## Reference who numbers of link start (end1) and end (end2) points to actual coordinates: nl_links <- nl_links %>% - dplyr::left_join(nl_turtles, by=c("[step]"="[step]","end1" = "who")) %>% - dplyr::left_join(nl_turtles, by=c("[step]"="[step]","end2" = "who")) + dplyr::left_join(nl_turtles, by=c("[step]"="[step]","end1" = "who")) %>% + dplyr::left_join(nl_turtles, by=c("[step]"="[step]","end2" = "who")) ## Plot: p1 <- ggplot() + @@ -600,7 +599,7 @@

coord_equal() + theme_void() -gganimate::animate(p1, nframes = max(nl_turtles$`[step]`), width=400, height=400, fps=8)

+gganimate::animate(p1, nframes = max(nl_turtles$`[step]`), width=400, height=400, fps=8)
@@ -611,21 +610,21 @@

It is also possible to use the igraph package to visualize a NetLogo network. We use the nl_to_graph() function from the nlrx package to convert our spatial data into an igraph object. Here we want to display the network for only one simulation step, thus we filter our spatial data and select only the last simulation step. Again, we split the data into a turtles and a links tibble and rename the columns.

After conversion to an igraph object we can use the power of network analysis functions from the igraph package to do further analyses. In this example we perform a community cluster algorithm to color the nodes based on their community membership within the network.

## Load igraph package and convert spatial tibbles to igraph:
-library(igraph)
+library(igraph)
 nw.all <- nl_to_graph(nl)
 
 ## Select the last step and extract list element to get igraph object
-nw <- nw.all %>% dplyr::filter(`[step]` == max(`[step]`))
+nw <- nw.all %>% dplyr::filter(`[step]` == max(`[step]`))
 nw <- nw$spatial.links[[1]]
 
 ## Perform community cluster algorithm and set community dependend colors:
 com <- walktrap.community(nw)
 V(nw)$community <- com$membership
-rain <- rainbow(14, alpha=.5)
+rain <- rainbow(14, alpha=.5)
 V(nw)$color <- rain[V(nw)$community]
 
 ## Plot:
-plot(nw,
+plot(nw,
      vertex.label=NA,
      vertex.size=4,
      edge.curved=0,
@@ -660,8 +659,9 @@ 

-

Site built with pkgdown 1.3.0.

+

Site built with pkgdown.

+ diff --git a/docs/articles/articles/nlrx_nldoc.html b/docs/articles/articles/nlrx_nldoc.html index 99ce4414..13485395 100644 --- a/docs/articles/articles/nlrx_nldoc.html +++ b/docs/articles/articles/nlrx_nldoc.html @@ -1,19 +1,19 @@ - + NetLogo documentation with nlrx • nlrx | NetLogo from R - - - + + + - @@ -23,15 +23,14 @@
@@ -236,10 +235,10 @@

Model procedure network

Another useful function of the nlrx package is the creation of model procedure graphs (nldoc_network()). This function can be used for any NetLogo model, even if the code does not contain noxygen tags.

# Load nlrx:
-library(nlrx)
+library(nlrx)
 
 # List model files (.nls subfiles are also supported)
-modelfiles <- c("https://raw.githubusercontent.com/nldoc/nldoc_pg/master/WSP.nlogo",
+modelfiles <- c("https://raw.githubusercontent.com/nldoc/nldoc_pg/master/WSP.nlogo",
                 "https://raw.githubusercontent.com/nldoc/nldoc_pg/master/WSP.nls")
 
 
@@ -247,13 +246,13 @@ 

nw <- nldoc_network(modelfiles) ## Determine communities within the network and plot using Igraph package: -library(igraph) +library(igraph) com <- walktrap.community(nw) V(nw)$community <- com$membership -rain <- rainbow(14, alpha=.5) +rain <- rainbow(14, alpha=.5) V(nw)$color <- rain[V(nw)$community] -plot(nw, +plot(nw, edge.arrow.size=1, vertex.label.color="black", vertex.label.dist=2.5, @@ -294,8 +293,9 @@

-

Site built with pkgdown 1.3.0.

+

Site built with pkgdown.

+
diff --git a/docs/articles/articles/spatial-output.html b/docs/articles/articles/spatial-output.html index 43e0c437..cadf2ed3 100644 --- a/docs/articles/articles/spatial-output.html +++ b/docs/articles/articles/spatial-output.html @@ -1,19 +1,19 @@ - + Spatial Output • nlrx | NetLogo from R - - - + + + - @@ -23,15 +23,14 @@
@@ -300,17 +299,17 @@

In order to generate igraph objects some metrics are mandatory: The metrics.turtles slot of the experiment must contain “who” numbers (see example experiment). Additional turtle metrics will be stored as properties of the igraph vertices. The metrics.links slot of the experiment must contain “who” numbers of link end1 and end2 (see example experiment). Additional link metrics will be stored as properties of the igraph edges.

For our application example, we are mainly interested in the final network structure. We want to measure who numbers and color of turtles, and who numbers of end1 and end2 of links on the final tick. We define our experiment accordingly and run the simulations:


-library(nlrx)
-library(igraph)
+library(nlrx)
+library(igraph)
 
 # Windows default NetLogo installation path (adjust to your needs!):
-netlogopath <- file.path("C:/Program Files/NetLogo 6.0.4")
-modelpath <- file.path(netlogopath, "app/models/Sample Models/Networks/Giant Component.nlogo")
-outpath <- file.path("C:/out")
+netlogopath <- file.path("C:/Program Files/NetLogo 6.0.4")
+modelpath <- file.path(netlogopath, "app/models/Sample Models/Networks/Giant Component.nlogo")
+outpath <- file.path("C:/out")
 # Unix default NetLogo installation path (adjust to your needs!):
-netlogopath <- file.path("/home/NetLogo 6.0.4")
-modelpath <- file.path(netlogopath, "app/models/Sample Models/Networks/Giant Component.nlogo")
-outpath <- file.path("/home/out")
+netlogopath <- file.path("/home/NetLogo 6.0.4")
+modelpath <- file.path(netlogopath, "app/models/Sample Models/Networks/Giant Component.nlogo")
+outpath <- file.path("/home/out")
 
 nl <- nl(nlversion = "6.0.4",
          nlpath = netlogopath,
@@ -324,9 +323,9 @@ 

idsetup="setup", idgo="go", runtime=50, - metrics.turtles = list("turtles" = c("who", "color")), - metrics.links = list("links" = c("[who] of end1", "[who] of end2")), - constants = list("num-nodes" = 80, + metrics.turtles = list("turtles" = c("who", "color")), + metrics.links = list("links" = c("[who] of end1", "[who] of end2")), + constants = list("num-nodes" = 80, "layout?" = "true")) nl@simdesign <- simdesign_simple(nl, 1) @@ -340,10 +339,10 @@

## Extract graph of tick 1:
 nl.graph.i <- nl.graph$spatial.links[[1]]
 ## Set vertex colors by measured color variable:
-vcols <- c("7" = "grey", "15" = "red")
-V(nl.graph.i)$color <- vcols[as.character(V(nl.graph.i)$color)]
+vcols <- c("7" = "grey", "15" = "red")
+V(nl.graph.i)$color <- vcols[as.character(V(nl.graph.i)$color)]
 ## Set edge colors by measured link breed:
-ecols <- c("links" = "black")
+ecols <- c("links" = "black")
 E(nl.graph.i)$color <- ecols[E(nl.graph.i)$breed]
 
 ## Plot:
@@ -380,8 +379,9 @@ 

-

Site built with pkgdown 1.3.0.

+

Site built with pkgdown.

+

diff --git a/docs/articles/furthernotes.html b/docs/articles/furthernotes.html index b1c567d2..75824f37 100644 --- a/docs/articles/furthernotes.html +++ b/docs/articles/furthernotes.html @@ -1,19 +1,19 @@ - + Advanced configuration • nlrx | NetLogo from R - - - + + + - @@ -23,15 +23,14 @@