ropensci / nlrx

nlrx NetLogo R
https://docs.ropensci.org/nlrx
GNU General Public License v3.0
77 stars 12 forks source link

nlrx and NetLogo's R extension #24

Closed schellheinz closed 5 years ago

schellheinz commented 5 years ago

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)?

marcosci commented 5 years ago

Are the spatial indices landscape metrics? :)

schellheinz commented 5 years ago

The indices (Clarc Evans, Markcorrelation) use the positions of my turtles, one of their state variables and the height and width of the simulated world.

The model runs smoothly if I run it directly in NetLogo.

marcosci commented 5 years ago

I guess you're using spatstat anyways?

If so, couldn't you measure the turtle metrics (coordinates and your marks) and do the analysis after running your eFast?

See here how to capture spatial output with nlrx:

https://ropensci.github.io/nlrx/articles/articles/spatial-output.html

schellheinz commented 5 years ago

Yes, I'm using spatstat anyways.

But what I want to do is using the indices for sensitivity analysis.

I've thought about calculating the indices from the output metrics of the turtles afterwards but then I needed to give analyze_nl() a function that creates a ppp and calc the indices -- basicly, I don't now how this should look like.

If nlrx could handle models which use the R extension, that would reduce the output I need and I could just use the mean(), ord sd() function for analysis.

nldoc commented 5 years ago

I did not use the r extension yet. Thus, I first need to setup a test environment with a working r extension to locate whats causing the problem. However, while researching how to setup the r extension I found this section in the description of the extension: Headless Since R-Extension version 1.1 it is possible use the extension when NetLogo is running in headless mode. This is for example the case, when you run BehaviorSpace experiments from the command line (see here). The difference is, that the interactiveShell is not initialized/instanciated. You can use the extension as you know it from GUI mode, but it is not possible to open the interactiveShell (r:interactiveShell) and to set the plot device (r:setPlotDevice). But one additional things has to be done: You have to call r:stop finally when running NetLogo headless to stop the R engine. Otherwise NetLogo will not be closed and you will not get back to the command line prompt. When setting up a BehaviorSpace experiment, there is the option to set final commands. This is a good place to add the r:stop command (see image).

Did you already add the r:stop command to your code? You could also add it in the "idfinal" field of the experiment.

schellheinz commented 5 years ago

I just implemented the stop command at the last tick but no change. The package starts the simulation experiment but nothing seems to happen. I don't get an error but neither do I see any progress (silent = false).

nldoc commented 5 years ago

I spend some time to setup the r extension and did some tests. I can verify, that the r extension should work, even in headless mode. Howver, I had to put the r:stop command into the idfinal field of the experiment, otherwise the process was stuck. Here is a small reproducable example, using one of the example models that comes with the r extension. Please note that I had to include a strange stop condition (stopcond), because the model does not use ticks and I had to stop it somehow. It also has no globals that would make sense to measure. I just added count turtles to see if the model works in general. On my PC it ran through without any problems.

library(nlrx)

netlogopath <- file.path("C:/Program Files/NetLogo 6.0.3")
modelpath <- file.path(netlogopath, "app/extensions/r/models/l-function.nlogo")
outpath <- file.path("C:/out")

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

nl@experiment <- experiment(expname="rext",
                            outpath=outpath,
                            repetition=1,
                            tickmetrics="true",
                            idsetup=c("cluster_setup"),
                            idgo=c("go_with_L-caluclation"),
                            stopcond="plot-x-max != 1",
                            idfinal = "r:stop",
                            runtime=0,
                            metrics=c("count turtles"),
                            constants = list("let-them-walk" = "false"))

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

results <- run_nl_all(nl)

Could you please verify, that this example model works on your machine as well? If it does not work there may be something wrong with your general setup. If it works, the problem seems to be more specific to your particular model. In that case it would be good if you could post your nl object setup here.

schellheinz commented 5 years ago

Hello,

I tried your example and my own model on my Mac and both work fine. The problem was that I put the "r:stop" in the NetLogo code instead in the "idfinal" slot in R...

So the solution to my problem was very simple but I think it would have taken a lot of time to figure it out myself. Thank you very much for your help!

nldoc commented 5 years ago

Great news! We also appreciate your feedback, as it helps us to improve the package and documentation. And finally, I took the time to set up the NetLogo r extension on my system ;)