While using step_measures and run_measures (see Fire experiments) is good enough for aggregate measures, there are cases when we need to “measure” each agent individually.

Example below is using Netlogo Segregation model (Wilensky, 1997) from NetLogo model library to demonstrate “agents” measures.

To get data about agents use agents_after argument with a list of agents variables and type of agent (turtles in this example).

experiment <- nl_experiment(
  model_file = "models/Sample Models/Social Science/Segregation.nlogo", 
  while_condition = "count turtles with [not happy?] > 0",
  param_values = list(
    percent_similar_wanted = c(30, 50, 70)
  ),
  mapping = c(
    percent_similar_wanted = "%-similar-wanted"
  ),
  agents_after = list(
    individuals = agent_set(
      vars = c("who", "xcor", "ycor", group = "color", happy = "happy?"), 
      agents = "turtles")  
  ),
  random_seed = 1
)

result <- nl_run(experiment, print_progress = TRUE)
#> Params: 1, Run: 1
#> Params: 2, Run: 1
#> Params: 3, Run: 1

The data about individual agents are now stored in result$agents_after$individuals. To join the data with the values from parameter sets use nl_get_result function.

dat <- nl_get_result(result, type = "agents_after", sub_type = "individuals")

library(ggplot2)
dat$group <- factor(dat$group, labels = c("A", "B"))
ggplot(dat, aes(x = xcor, y = ycor, fill = group)) +
  geom_raster() +
  coord_fixed() +
  facet_grid( ~ percent_similar_wanted, labeller = label_both) +
  scale_fill_manual(values = c("cadetblue4", "gray" )) +
  theme_minimal() 

See also

Network example demonstrates how to read a network structure from NetLogo turtles and links.

Traffic example demonstrates how to collect values from turtles per each time step.

DLA example demonstrates how to read patches and their variables.

Ants example demonstrates simple parameter sets definition and parameter mapping.

Get rmarkdown source of this page.