120 likes | 129 Views
Learn how to build explicit agent-based models in R, using a prey feeding model as an example. Explore concepts like initialization, updating plots, statistics, and population dynamics.
E N D
Agent-Based Models with R? • R is a scripting language and not the fastest. • R does handle large datasets. • Question: Can we build explicit agent-based models in R?
Simple Prey Feeding Model Initialize Grass Matrix Update Plots Sleep Initialize Prey Data Frame Prey Feed Initialize Population Vector Prey Move Update Statistics Prey Die Grass Grows Prey Reproduce
Approach • Use a matrix to represent grass that can be fed upon • Use a data frame for the attributes of each agent: • X, Y – location (0 to 1) • Health – when it’s zero, the prey dies (0 to 1) • TimeToReproduce – when a limit it reached, the prey produces one offspring (0 to specified number of cycles)
Problems • Agent-based models can involve a lot of code • Typically we would do this with classes in a compiled language • R has a class structure but it is beyond this class (and my knowledge) • Functions can be used to organize the code • Data frames are duplicated when passed to functions greatly slowing execution
Solution • Use data frames and matrixes that are globally available to all functions. • If variables are not passed into functions, they can still be accessed from within the function. • If a variable is assigned a value inside a function, a new variable is created locally. • To set the global variables values, the operator “<<-” is used.
Initialize Grass • Grass values: • 0: None • 1: Fully available • Create a grid of Columns and Rows • Use uniform random data between 0 and 1 to initialize
Initialize Prey • Setup data frame with maximum number of rows required • Randomly set initial attributes: • X,Y: uniform random from 0 to 1 • Health: uniform random from 0 to 1 • TimeSinceReproduction: uniform random from 0 to TimeToRepo
Move Prey • Add normal random value to x and y • Make sure values are within range 0 to 1
Prey Feed • Find the prey location in the grass grid • Find the available food for the prey • If the available food > 0 • Find the amount of food eaten (mouthful or available, which ever is less) • Reduce the available food by the amount eaten • Compute the new value for the available food and make sure it is not negative • Store this in the grass grid • Add the amount eaten to prey’s health
Prey Die • Deduct a set amount from every Prey’s health • If the health of a prey is less than or equal to 0: • Set all values for the Prey to zero (0 health indicates the Prey is not alive) • Deduce one from the number of prey
Prey Reproduce • For each prey, if their health is greater than zero: • If the time from last reproduction is greater than required: • Set the time from last reproduction to zero • Find an entry in the Prey data frame with health = 0 • Initialize the new prey entry • Increment the number of prey variable • Otherwise • Increment the time from last reproduction
Grass Grows • If the grass is less than 1: • Add the amount of increase per cycle