agent based modelling

An Introduction to Agent-based Modelling

The aim of this blog post is to introduce the concept of agent-based modelling and hopefully excite you with the broad capability of these types of model.

Modelling methodologies aim to approximate how real-world situations evolve over time. A successful model is created with a system of parameters and rules that most likely represent a ‘real’ situation. The ‘real’ situation then incorporates natural variation in multiple repeated scenarios, thereby returning a distribution of results. This allows analysts to identify how the system could respond to changes in factors, without the need to carry out either lengthy experiments or constructions in the real world.

Agent-based modelling is one such methodology that has more recently benefited by both advances in software and processing technology, and the availability of cloud computing (such as Azure, AWS, GCP), making its application more accessible.

My goal in this blog-post is to run through the foundations of agent-based modelling. I will illustrate how it can be used to model real-world situations by creating a not so real-world example, one of a zombie outbreak. We have all probably seen one (if not more) zombie movies, so the situation I describe here will be somewhat familiar and (hopefully) it will be a bit of fun.

What is Agent-based Modelling?

The definition according to Wikipedia for agent-based modelling is:

An agent-based model (ABM) is a class of computational models for simulating the actions and interactions of autonomous agents (both individual or collective entities such as organizations or groups) with a view to assessing their effects on the system as a whole.

– Wikipedia definition

Modelling how a system both is and then changes over time under certain interventions can be helpful when investigating which ones to implement in a real-world situation. In the case of agent-based modelling, the actions of individual agents are simulated over a set period. Analyses and scenarios can be modeled on the individual agents, or at different levels of aggregation, to better understand the overall benefit or consequence to the system.

Agent-based modelling is popular in various domains. It is used extensively in Biology to analyse the spread of epidemics, the impacts of stochastic gene expression, changes to ecology, cognitive function deltas and immune system responses. In Epidemiology, it has been deployed to inform public health outcomes for COVID-19 transmission and hospitalizations. In Business and Economics, it has been for supply chain optimization and logistics, consumer behaviours and economic outcome analysis due to the ability to simulate the complexity of a market.

The nature of simulating each individual agent and repeating the simulation multiple times, is the reason why agent-based modelling can require significant processing power. In recent times, a simulation may require the procurement of both high powered computing and specialized software, or one must have a comprehensive understanding of Object-oriented Programming (OOP) to create a model. Nowadays, there are numerous open source libraries for creating your own agent-based model, such as Mesa for python, and most desktop computers can handle the required processing and storage.

To explain Agent-based modelling, I will break it down into 4 components: agents, actions, environment and simulation.

Agent

Agents are the singular elements of the model which can make decisions, act autonomously, and interact with other agents. An agent can be an individual or group of individuals, just as long as the defined agent is able to behave as a singular entity.

Each agent is set up with distinct attributes which drives how they behave within the system. Agent behaviour encompasses the decision making and how this could lead to specific actions. Rules in the model determine how an agent with a set of attributes will exhibit a particular type of behaviour. These rules can be as simple as: if agent has Attribute A then exhibit Behaviour A. For example, if agent is a zombie then behave like a zombie. These rules can also include how an intervention may alter the agent’s decision making.

Actions

Actions are the materialisation of agent decision-making which lead to a change in the system. The changes could affect only the individual agent performing the action or include other agents and the environment. Impacts of actions can include movement or change in agent attributes.

Actions involving more than one agent or agents and the environment are called interactions. These are the most important type of actions for agent-based modelling because they represent how information can be transferred between individuals or how behaviours can be changed by interventions.

Continuing my example above, zombie behaviour is defined by two actions: hunting humans and eating human brains. Hunting a human is a movement action and eating human brains is an interaction. Interaction is only triggered if the zombie and human are sufficiently close, and the human becomes a zombie because of this interaction.

Environment

The environment is the encompassing system in which the agents exist. Environment can be characterised by the types of interactions that are allowed. Below are two examples of environments distinguished by agent-agent interactions.

  • Spatial environments allow agents to directly interact with each other. This environment can be as simple as a 2D grid, or more complicated to model real world environments. Agents occupy a position and can interact with nearby agents (‘nearby’ as defined in the model) or even with the environment itself.
  • Abstract environments are generally used when agents do not directly interact with other agents, rather they interact with the environment. This is useful when modelling Economic Markets: Corporations (agents) make decisions (actions) based on the market (environment) which in turn affects the market and other corporations.

In my zombie outbreak example, the environment would be spatial. Agent-agent interaction would only be possible for agents in neighbouring positions. The model could allow for agent-environment interactions, for example: obstacles could provide cover for humans, or areas could decrease the speed of agents. I could even add an abstract environment like ‘Global Panic’, which would change human agent behaviours and increase as the zombie population grows.

Simulation

This is the part of the agent-based model which controls how the situation (agent, actions, and environment described above) can be run programmatically.

The simulation controls the start up of the model, the execution of each iteration period, and the number of iterations over which the model is run. The iteration period is a set period of time relevant to the model, it can be a specifically chosen time step or arbitrary.

It is during each iteration that rules are applied to the agents to determine their behaviour and the actions they may take. In most agent-based models, each agent is considered one by one. The simulation controls whether the effect of actions is applied while going through the agents or at the completion of the iteration.

The simulation runs multiple iterations to see how the system evolves. Then the simulation is repeated to determine the likelihood of given outcomes.

It is very difficult to describe how the simulation could work for a specific example without going into deep details of the situation. With the zombie outbreak, the simulation would need to dictate how agents are placed at the start, the order agents are allowed to act (one by one or all at once), when to consider interactions (agent-agent or agent-environment), and more.

I hope that this description of agent-based modelling as four components has been informative and whets your appetite. If you are interested in reading a more in-depth description of the components and seeing how created my own Zombie Outbreak agent-based model, then please continue reading.


Zombie Model

So, in my ‘real-world’ situation I have a group of humans and one zombie. To make it interesting (and give the zombie a chance) the agents will all be restricted to an enclosed area. Zombies want to bite humans and will try to catch them. Humans will try to get away from zombies. I assume that any human caught by a zombie is bitten and becomes a zombie. Humans do not get hungry or tired, and they cannot fight the zombies. The aim of this model is to determine how long a human agent can survive and see if there are any factors which improve their chances.

I will create my model by breaking it down into the four components.

Agents

My model consists of individuals with one defining characteristic: Zombie or Human. To accomplish my aim (see how long humans survive), I will also include an age attribute for human agents. This will count the number of iterations a human has survived for.

Humans behave normally and try to stay human by avoiding zombies. The nature of zombies makes it difficult to describe ‘behaviour’, simply put zombies want to eat human brains.

Actions

Human agents will move to increase the distance between them and the closest zombie, while zombie agents will move closer to decrease this distance. If there are equally close zombies/humans, then one will be picked at random and treated as the closest (improvements can be made so that humans make ‘smarter’ decisions when avoiding multiple zombies).  If there is a zombie-human interaction (a zombie catches a human) then the human becomes a zombie. In my model the humans have no behaviours allowing for resistance, so all interactions have this result.

Environment

The agents will exist in a spatial environment. I’ll create it as a 10×10 grid where each cell can only have one occupant. Each agent has full visibility of all other agents and can only move to adjacent tiles (including diagonals) as part of their movement action. Interaction occurs between zombies and agents on adjacent tiles. There are no interactions between the agents and the environment.

Simulation

The model will begin by randomly placing ten agents in the environment. Nine agents will be human and one will be a zombie. Each agent is assigned an age of 0 at the commencement.

Each iteration is an arbitrary time period over which every agent is given the opportunity to act. Agents will be considered in a random order and their actions will take effect before continuing to the next agent. At the end of the iteration each human agent is aged by 1.

The steps for a human agent will be:

  1. Determine the closest zombie agent.
  2. Move to the adjacent cell that maximises the distance from the closest zombie (including staying in the same cell).

The steps for zombies are:

  1. Determine the closest human agent.
  2. If this human is in an adjacent cell then eat brains (interact) instead of moving.
  3. Move to an adjacent cell that minimises the distance to the closest human (including staying in the same cell).

The simulation will run until there are no remaining humans or a maximum of 50 iterations then record the average number of iterations humans survived for (human age).

Simulation of Zombie Outbreak:
Starting with one zombie (red) and 9 humans (blue)

Here are my results for human age after running the simulation 1000 times. The results are not meaningful on their own but are nice examples of how visual outputs can be created for analysis.

I thought it might be interesting to attach human age to the starting location to see if this has an impact on human survival. Each simulation was generated with the zombie in the bottom-left cell and humans being randomly placed. This was also repeated over 1000 simulations.

Once the basic components of an agent-based model are set up, it is easier to include other factors. Here are some interesting additions that could be made to the model:

  • Obstacles – stationary objects in the environment that humans might be able to use for cover.
  • Limit visibility – give zombies a higher range of visibility because of their smell (while humans rely on sight which is shorter).
  • Change movement range – allow humans to move quicker (ie. two cells at a time).
  • Fight behaviour – when interaction occurs between human and zombie the human could avoid getting bitten.

The codebase used to generate the above simulation using Mesa is available in this GitHub repository.

More from the blog