Data Science

Agent Based Modelling

Agent Based Modelling

I was experimenting with Agent based models and came across a really great talk Jackie Kazil | Agent based modelling in Python. This lead me to look at the Python module that was being discussed Mesa | Agent-based modelling in Python 3+.

General Practitioner (GP) Workforce Model

The Mesa project has many examples which I thought were a great place to get started. I decided to try to build a model that was similar to the Wolf Sheep model in this example:

So I cloned the repo and the rest of this blog post is based on my associated github repo.

I decided to use the concepts and framework for a simple workforce model, consisting of three agent types:

  1. GP Fellows (fully qualified and practicing)
  2. GP trainees (working towards qualification)
  3. Patients

I made a few assumptions which were:

  1. The GP Fellows train trainees over 5 years
  2. The Trainees then become Fellows after 5 years.
  3. The GP Fellow may or may not take on another trainee.

The model tests and demonstrates several Mesa concepts and features:

  • MultiGrid
  • Multiple agent types (GP Fellow, Trainee and Patient)
  • Overlay arbitrary text (Age and Sex of the Provider) on agent’s shapes while drawing on CanvasGrid
  • Agents inheriting a behaviour (random movement) from an abstract parent
  • Writing a model composed of multiple files.
  • Dynamically adding and removing agents from the schedule


To install the dependencies use pip and the requirements.txt in this directory. e.g.

pip install -r requirements.txt

How to Run

To run the model interactively, run mesa runserver in this directory. e.g.

mesa runserver

Then open your browser to and press Reset, then Run.


This lists the key files that were modified from the base Wolf and Sheep model that this is inherited from.

folder workforceWorkforce components
python lvl1 random_walker.pyThis defines the RandomWalker agent.
python lvl1 agents.pyDefines the GP Fellow, Trainee, and Patient agent classes.
python lvl1 model.pyDefines the Workforce model itself
python run.pyLaunches a model visualization server.