Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Agent Based Models

Open in Colab

An agent-based model is a way of modeling some sort of phenomenon using discrete “agents” which interact with other agents, sometimes in very complex ways. In case this is too abstract, you can think of agents as simulated people, and you’re trying to model how they interact with each other.

Example: Spreading an Idea

Let’s say someone has a new idea, and that it is a really good idea. So good in fact that once someone hears this new idea they can’t stop talking about it. We can model a person, or agent as a Python class with a single attribute:

False
True

Now, what we want to do is simulate how this idea spreads through a population. Say that there are N people in the population, and each day each person talks to k random other people. If someone talks to someone else who is enlightened, then the new person becomes enlightened with probability p. We’ll start with one enlightened person and keep track of how the idea spreads

now let’s write a function that will simulate all interactions in a day

we want to count how many people are enlightened at the end of the day

2

now let’s see how the enlightened population changes over time

<Figure size 432x288 with 1 Axes>

Exercises

  1. How does changing N, p and k affect the simulation?

  2. How would you change the simulation model to say that there is a probablity q that an enlightened person will become un-enlightened every day?

Phase Diagrams

In our example, there are a couple of parameters we can tweak. We’ll focus on p and k. One question we might ask is how the outcome changes as we change these parameters. We’ll count the total number of people who are enlightened on day 20.

974

Now, let’s run simulations on a range of values for k and p

10it [00:15,  1.60s/it]
<Figure size 720x360 with 2 Axes>

from the above, we see that if k and p are large enough that the whole population is enlightened on day 20, and if they are too small then very few are enlightened. There is a phase transition between the blue and yellow regions above, meaning the choice of parameters can cause abrupt changes in the measured behavior of the system

The Game of Life

You don’t necessarily need to use Python objects to encode everything about an agent-based model. For instance, you might just use an array to store the state of objects in simple simulations, even when behavior is nonlinear.

We’ll take a look at Conway’s Game of Life implemented using a numpy array. The game of life takes place on a m by n grid, and each element of the grid either contains life or it doesn’t. The game models how life might spread over the area of a continent. The cells are updated at each time step using the following rules:

  1. Any live cell with 0 or 1 live neighbors dies (under-population)

  2. Any live cell with 2 or 3 neighbors survives until the next time step (sustainable population)

  3. Any live cell with more than 3 live neighbors dies (overpopulation)

  4. Any dead cell with exactly 3 neighbors becomes alive (reproduction)

Neighbors are either horizontally, vertically, or diagonally adjacent. We’ll encode whether a cell is alive or dead using 1 and 0 respectively (or True/False)

array([[0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], ..., [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0]])

What makes the game of life interesting is that you can get patterns that propagate over your grid

<Figure size 720x720 with 1 Axes>

The below cell produces an animated GIF of the game of life. See here for an example from matplotlib.

''
<Figure size 360x360 with 1 Axes>

Here’s what the GIF looks like: