Hi everyone!
I’m excited to share logolink, an R package that allows you to run NetLogo experiments directly from R. I hope the community finds it useful!
- Documentation website: An Interface for Running NetLogo Simulations • logolink
- Code repository: GitHub - danielvartan/logolink: 🐢 An Interface for Running NetLogo Simulations from R
It’s now available on CRAN. You can install it using the code below:
install.packages("logolink")
Another R Package for NetLogo?
While other R packages connect R and NetLogo, logolink is currently the only one that fully supports the latest NetLogo release (NetLogo 7).
For context, RNetLogo works only with older versions (up to 6.0.0, released in December 2016) and hasn’t been updated since June 2017. nlrx provides a powerful framework for managing experiments and results but supports only up to NetLogo 6.3.0 (released in September 2022) and has several unresolved issues. logolink complements these packages by focusing on simplicity, full compatibility with NetLogo 7, and seamless integration into modern R workflows.
Showcasing the Functionality
library(logolink)
netlogo_path <- file.path("", "opt", "netlogo-7-0-0", "bin", "NetLogo")
model_path <- file.path(
"", "opt", "netlogo-7-0-0", "models", "IABM Textbook", "chapter 4",
"Wolf Sheep Simple 5.nlogox"
)
setup_file <- create_experiment(
name = "Wolf Sheep Simple Model Analysis",
repetitions = 10,
sequential_run_order = TRUE,
run_metrics_every_step = TRUE,
setup = "setup",
go = "go",
time_limit = 1000,
metrics = c(
'count wolves',
'count sheep'
),
run_metrics_condition = NULL,
constants = list(
"number-of-sheep" = 500,
"number-of-wolves" = list(
first = 5,
step = 1,
last = 15
),
"movement-cost" = 0.5,
"grass-regrowth-rate" = 0.3,
"energy-gain-from-grass" = 2,
"energy-gain-from-sheep" = 5
)
)
results <- run_experiment(
netlogo_path = netlogo_path,
model_path = model_path,
setup_file = setup_file
)
library(dplyr)
results |> glimpse()
#> Rows: 110,110
#> Columns: 10
#> $ run_number <dbl> 8, 1, 7, 2, 4, 3, 5, 6, 9, 4, 5, 1, 2, 6, 3,…
#> $ number_of_sheep <dbl> 500, 500, 500, 500, 500, 500, 500, 500, 500,…
#> $ number_of_wolves <dbl> 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,…
#> $ movement_cost <dbl> 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,…
#> $ grass_regrowth_rate <dbl> 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3,…
#> $ energy_gain_from_grass <dbl> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,…
#> $ energy_gain_from_sheep <dbl> 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,…
#> $ step <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,…
#> $ count_wolves <dbl> 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,…
#> $ count_sheep <dbl> 500, 500, 500, 500, 500, 500, 500, 500, 500,…
data <-
results |>
group_by(step, number_of_wolves) |>
summarise(
across(everything(), ~ mean(.x, na.rm = TRUE))
) |>
arrange(number_of_wolves, step)
library(ggplot2)
data |>
mutate(number_of_wolves = as.factor(number_of_wolves)) |>
ggplot(
aes(
x = step,
y = count_sheep,
group = number_of_wolves,
color = number_of_wolves
)
) +
labs(
x = "Time step",
y = "Average number of sheep",
color = "Wolves"
) +
geom_line()

