Agent-based Modeling & How It’s Used

By Andy Bayer, CodeHS Engineer

undefined

Agent-based modeling is a way scientists can model large, complicated systems by defining the rules for the individuals in those systems. Something complex like the movements of a flock of birds (this type of flight pattern is called “murmuration”) can be programmed by writing a few simple rules for each bird.

undefined
Agent-based model simulation.

Agent-based modeling has been recently useful for modeling the spread of coronavirus, such as in this insightful simulation published in The Washington Post. You can create similar, simple agent-based models called “cellular automata” in CodeHS using just a few simple computer science techniques! Cellular Automata may sound like a complicated term from bio, but it’s really a simple way to model real-world behavior, where individual cells follow a set of rules and only change according to their neighbor cells that directly surround it.

So, how can we create agent-based models in CodeHS? Que Karel, below Karel demonstrates Langton’s Ant*, a seemingly simple cellular automaton with only two rules that creates intricate patterns. View the full Karel version of Langton’s Ant here.

undefined
Karel demonstrates Langton’s Ant.

This automaton only has two rules:

  1. If Karel is on a square with a tennis ball, take the ball, turn left, then move.
  2. Otherwise, put a ball down, turn right, then move.

These two rules can be programmed using a single if/else statement. From these two simple rules, complex patterns emerge, and by constructing boolean logic gates*, it’s actually possible to program a computer (such as a calculator) using only Karel and tennis balls.

Conway’s Game of Life is another classic cellular automaton, created here in CodeHS. The Game of Life is pretty simple — it’s a grid of cells, each of which can either be alive (black) or dead (white). Over time, each cell can come to life or die off based on the 8 cells surrounding it.

A large for loop iterates over each of the cells in the grid, then iterates over each of the cell’s 8 neighbors. An if/else statement determines whether a cell should die, come to life, or stay the same:

undefined
The Game of Life code example.

These simple rules create interesting patterns. Here’s an animation of one of those patterns, the “Gosper glider gun,” which generates “gliders” resembling spaceships:

undefined

Agent-based models can be used in a number of other applications: procedurally-generated dungeons for video games, predicting biological interactions like the predator-prey model, and understanding the way humans interact with one another, from public transportation to the stock market to pandemics like COVID-19.

What agent-based models can you create on CodeHS? Possibly the traffic pattern in a city or the movement bees make to build a honeycomb?

The possibilities are endless, share your agent-based models by sending it to our team at hello@codehs.com!


*Langtons Ant: “Langton’s ant can also be described as a cellular automaton, where the grid is colored black or white and the ‘ant’ square has one of eight different colors assigned to encode the combination of black/white state and the current direction of motion of the ant.”

*Binary Logic Gate: “…a logical operation performed on one or more binary inputs that produces a single binary output.”