Introduction to Agent-Based Models and Cellular Automata
Tags: learning, beginners, programming
Published: 2019-01-17 21:35:27

---
title: Introduction to Agent-Based Models and Cellular Automata
published: true
description: A quick overview of agent-based models so you can start building your own models
tags: learning, beginners, programming 
cover_image: https://thepracticaldev.s3.amazonaws.com/i/9rm19h7dc9fgflr5usga.jpg
---

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](https://en.wikipedia.org/wiki/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](https://en.wikipedia.org/wiki/Conway%27s_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](https://upload.wikimedia.org/wikipedia/commons/e/e5/Gospers_glider_gun.gif)

If you’re interested in trying out different patterns, you can play around with an [online version](https://playgameoflife.com/) 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](https://en.wikipedia.org/wiki/Multi-agent_system), 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](https://css-tricks.com/game-life/) 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](https://codepen.io/RBSpatz/pen/rLyNLb) that’s worth checking out. 

####Python 
Robert Andrew Martin has a short Medium post [here](https://medium.com/@martin.robertandrew/conways-game-of-life-in-python-2900a6dcdc97) and GitHub repo [here](https://github.com/robertmartin8/PyGameofLife) 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](https://jakevdp.github.io/blog/2013/08/07/conways-game-of-life/) 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](https://github.com/tablackmore/Game-Of-Life-Java). 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](https://golang.org/), has a very basic and concise implementation of the Game of Life [here](https://golang.org/doc/play/life.go). 


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!