Add agents to the environment
Source:R/environment.R, R/environment_grid.r, R/environment_network.R
add_agents.RdCreate an agent first (through create_agent). This agent can be seen as a blueprint. Then, use this function here to add as many replicates of this blueprint agent as necessary. They will be added independently so that they do not interfere with each other throughout runtime.
Usage
add_agents(.tidyabm, agent, n, ...)
# S3 method for tidyabm_env
add_agents(.tidyabm, agent, n, ...)
# S3 method for tidyabm_env_grid
add_agents(.tidyabm, agent, n, initial_position = NULL, ...)
# S3 method for tidyabm_env_network
add_agents(.tidyabm, agent, n, ...)Arguments
- .tidyabm
the
tidyabm_envobject to which agents should be added- agent
an
tidyabm_agentobject (the blueprint) to replicate from- n
number of agents in the style of
agentto add- ...
other arguments to pass to sub functions
- initial_position
either
NULL(the default) where agents are placed randomly and independent from each other or a function with two parametersme(the agent to be placed) andabm(the current environment with previous agents already placed) that gets called once per agent to place and which has to return a vector of length 2 that will be interpreted asxandycoordinates (i.e.,c(x, y)). Here is an example to position agents in subsequent order via a function: coordinate_counter <- c(0, 1) create_grid_environment(seed = 892, size = 3) %>% add_agents(create_agent(), n = 3, initial_position = \(me, abm) coordinate_counter[1] <- coordinate_counter[1] + 1 if (coordinate_counter[1] > 3) coordinate_counter[1] <- 1 coordinate_counter[2] <- coordiante_counter[2] + 1return(coordinate_counter) )
Examples
a1 <- create_agent() %>%
set_characteristic(age = 'young')
a2 <- create_agent() %>%
set_characteristic(age = 'old')
m <- create_grid_environment(seed = 1024, size = 50) %>%
add_agents(a1, 100) %>%
add_agents(a2, 100)