IYO Best packages to simulate netlogo models in R or Python

First question would be would you use R or Python? would it make a difference on speed or is it still limited by the speed of the model run in netlogo?

Second question- in R I found ‘logolink’ and it says its the one compatible with 7.0.0+.
In python, I found ‘netlogopy’, ‘pyNetLogo’, and ‘NL4Py’. Is there a better one for ease of use (new to python and netlogo), speed, replication, and outputs?

From what I understand, these interact with Behaviour space? Or can I just link the model code from R or Python and run everything in there?

Thank you!

Hi Pandora,

If you’re new to Python as well as NetLogo, I think you should first focus on one of the two.
Using two (or more) languages at the same time can be confusing, still for experts.
Learning two languages at the same time makes it difficult to learn either language properly.

So, if your primary focus is on Agent-Based Modelling, start with NetLogo. (If not, this would not be the best place to ask.)
You can analyse results of BehaviorSpace runs with any tool that can process CSV files.
Only for advanced processing, like feedback between analysis and model runs, you would really need the links between R/Python and NetLogo.

After some time, you might return to the question of combining combining Python or R with NetLogo.
Then you can start from NetLogo or from Python/R, again depending on your focus.
From NetLogo, you can use extensioms py (Python) or sr (Simple R), that come with NetLogo.
From Python, my experience is that using the packages you mention require more than basic Python.
My experiences with R are very limited, but my impression is that logolink is a good choice.

(Oh, now I see the first question again. Until runtime becomes a bottleneck, just focus on the model mechanisms. Anyway, the packages you mention will still run NetLogo under the hood.)

I hope this helps and that you enjoy learning the languages.

Thank you for your reply. I am proficient in R and have some experience with python and have friends that are proficient in python that can help me with that side of the code. It’s netlogo i’m new to, as in 6 months new. I have my model completely written and now need to run simulations on a campus cluster computer. My model takes several hours to run in netlogo once so i’m worried about runtime and would like to make the simulations as fast as possible (since i’ll need to run thousands of them). I’ve read up on Behaviour space, and the joint outcome was if you can run them in R/python for reproducibility/output this is a better choice. Which is why i’m exploring this.

Are you saying I need to use the ‘py’ or ‘sr’ extensions in netlogo to be able to run the simulations in Py/R along with using the packages in Py/R?

My original questions then still stand? will it be faster to use them jointly than by using just behaviour space? And if anyone has suggestions as to which program/packages they recommend.
thank you!

Hi Pandora,

Six months of experience with NetLogo is more than “new to NetLogo” for me.
And, if you have built an extensive model, I guess you know what you are doing.

Still, a runtime of hours for one simulation is huge. Either the model is inherently complicated, or there are still some points for improving the NetLogo code. An earlier post on this forum (https://forum.netlogo.org/t/guidance-on-speeding-up-netlogo-models/412) and the journal paper referenced there can be helpful in finding bottlenecks.

Anyway, using Python/R packages to control your NetLogo code do not improve performance. They still use the same NetLogo code in the end.
In my experience, rewriting the model in another language is usually not worth the effort, either.

If the model is too complicated, the only sensible thing to do is finding which mechanisms you can leave out of the model. Remember, a model does not need to include the whole system being modelled.

Using NetLogo extensions like py or sr can only help if you have some model mechanism already coded in Python or R and that code is more efficient than its NetLogo equivalent. Otherwise, they would not solve the problem.

So, the bottom line is “first find the bottlenecks in the model”.

Hi Pandora,

I’ve been using the logolink package in R to run thousands of simulations on a Kubernetes cluster, and overall the experience has been very positive.

To make the most of the cluster, I structured my workflow so that each model run performs only a single repetition. In my R script, I set the number of threads to match the number of sub-experiments and ensure that each run saves its output as an .RDS file. On the cluster side, I request one core per sub-experiment, and allocate RAM based on the model’s requirements (in my case, about 2 GB per sub-experiment).

I then control the total number of repetitions directly through the cluster configuration rather than within the model itself. This approach produces one .RDS file per repetition—usually just a few kilobytes or megabytes—which can later be merged into a single dataset for analysis.

With this setup, I’ve been able to run the model as efficiently as possible, fully release memory after each repetition, and take full advantage of the cluster’s resources.

Finally, it’s worth noting that R is essentially just generating the XML file for NetLogo to execute; the runtime speed ultimately depends on the complexity of the model itself, so running it from R will not make it faster.

1 Like

Thank you both for your input. I fully appreciate it and the resources provided. I’m pretty sure all the bottelnecks are for all the loops that my code does, but i’m not sure how to vectorize everything in netlogo more than how I’ve already done it. But i’ll keep searching. I just had the cluster conversation with a friend last night, so this information is very helpful for multi-threading and i’ll give it a try. One other thing she suggested was writing an LLM model to decrease the number of simulations i’ll have to do. I’m not quite sure how to do that, but the postdoc in my lab seems to have an idea for implimentation.
Again, i appreciate all the help and feedback.

1 Like