DEV Community

Cover image for Introduction to Agent-Based Models and Cellular Automata
Geoff Stevens
Geoff Stevens

Posted on

Introduction to Agent-Based Models and Cellular Automata

An agent-based model (ABM) is a computational tool for simulating a group of autonomous agents with the purpose of analyzing how the system as a whole is affected.

In an ABM each individual agent follows a specific set of rules. As an observer, we can see how these rules impact the overall system.

Cities are an example of simple agent-based models. The rules of the simulation are encoded on a micro level (i.e. individual people), but the most interesting revelations occur at a macro level.

During rush hour, for instance, each individual makes his or her decision to take the shortest commute home. Some workers will take the freeway, while others may opt to navigate backroads.

As a result, certain roads may become more congested than others. The impact on the city, as a whole, is a result of the individual actions taken by many different people.

Cellular automata and Conway’s Game of Life

There are many different ways to build ABMs, but one of the most basic implementations is known as a cellular automaton.

In its simplest form, a cellular automaton is an agent-based model implemented using a grid. Each cell in the grid is an individual agent and can exist in a finite number of states. The state of a cell depends on the states of its neighbors. As time progresses, each cell will reevaluate its current state and make any required changes.

Let’s look at an example. The most well-known cellular automaton is Conway’s Game of Life.

The Game of Life is a two-dimensional grid, where each cell can be either alive or dead. At the beginning of the Game of Life, in a process known as seeding, a set of cells are randomly selected to be alive. During each iteration of the simulation, every cell will update its state depending on its neighbors. The basic rules are:

  1. Any live cell with fewer than two live neighbors dies.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies.
  4. Any dead cell with exactly three live neighbors becomes a live cell.

If you visualize this process on a grid and keep iterating, you’ll get something like this:

Conway's Game of Life

If you’re interested in trying out different patterns, you can play around with an online version of the Game of Life.

Why developers should care about ABMs and the Game of Life

Here’s why I think developers of any skill level should learn about ABMs:

  • Highly accessible: Developers without backgrounds in complex statistics or mathematics can build meaningful models of real-life scenarios with interesting results.
  • Great for experimentation: Because even complex ABMs can be built from simple rules, there are plenty of ways to customize, tweak, and scale your ABM’s complexity. ABMs also offer plenty of opportunities to learn more about related types of models as well, like multi-agent systems, which use intelligent agents to solve problems that are difficult for an individual agent or monolithic system to solve.
  • Fun to learn: If you’re thinking about picking up a new language, the Game of Life is written in multiple languages, which is great for cross-referencing. With simple rules for the simulation, you can focus more on understanding different parts of the code.
  • Powerfully applicable: The world around us is full of opportunities to replicate using ABMs. Stock trading, gentrification, forest fires, epidemics, social networks—all can be modeled with ABMs. There’s an endless set of things for you to try out.

Helpful code examples for the Game of Life

If you’re looking for a few examples or tutorials that you can explore, I’ve compiled a few helpful repos that I’ve come across.

HTML/CSS/JavaScript

Chris Coyier shows a variety of ways to implement the Game of Life here using different libraries and tools, including Backbone.js, D3.js, and CoffeeScript. Each has its own CodePen, so they’re easy to replicate.

Robert Spatz also have a very intuitive CodePen here that’s worth checking out.

Python

Robert Andrew Martin has a short Medium post here and GitHub repo here to get started. The only dependencies are numpy and matplotlib, two very common and well-documented Python libraries, so the code is very accessible.

Jake VanderPlas has a well-documented and carefully explained implementation of the Game of Life in Python here using a Jupyter notebook, which makes for readable code and nice visualizations. Jake also includes a few different starting configurations to try out.

Java

Tom Blackmore has a short and simple implementation using Java here. The simulation uses the console as its display, so it’s easy to get up and running without needing a complex GUI or many dependencies.

Go

The main site for Go, golang.org, has a very basic and concise implementation of the Game of Life here.

If you’re interested in learning more, I’d highly recommend checking out some of the projects I mentioned above. And if you’ve come across any other helpful repos or tools, please share them in the comments!

Top comments (1)

Collapse
 
linnartsf profile image
Linnart Felkl

Hi Geoff. Applied ABM in any commercial setting so far? Working on a repo in Pyton: abm_framework github repo