1 / 12

Agent-Based Models with R?

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.

kjacobson
Download Presentation

Agent-Based Models with R?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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?

  2. 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

  3. 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)

  4. 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

  5. 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.

  6. 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

  7. 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

  8. Move Prey • Add normal random value to x and y • Make sure values are within range 0 to 1

  9. 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

  10. 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

  11. 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

  12. Grass Grows • If the grass is less than 1: • Add the amount of increase per cycle

More Related