# Agent-based Modeling & How It’s Used
Published: 2020-06-19
Category: HS/MS Curriculum
Category URL: https://codehs.com/blog/category/hsms-curriculum
URL: https://codehs.com/blog/agent-based-modeling-how-its-used-e5e1d727a80a

* * *

### Agent-based Modeling & How It’s Used

#### By Andy Bayer, CodeHS Engineer

![undefined](https://prod.superblogcdn.com/site_cuid_cm264m3an0084qegvyd7aetst/images/1lf61kwxmv1o5cbx8h82pza-1728980812463-compressed.png)

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](https://prod.superblogcdn.com/site_cuid_cm264m3an0084qegvyd7aetst/images/0djzyk-jizclc0wzt-1728980816055-compressed.0*DJZyk-jiZCLc0WZt)_Agent-based model simulation._

Agent-based modeling has been recently useful for modeling the spread of coronavirus, such as in this insightful [simulation](https://www.washingtonpost.com/graphics/2020/world/corona-simulator/) 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](https://codehs.com/share/andy/Langton%27s_Karel).

![undefined](https://prod.superblogcdn.com/site_cuid_cm264m3an0084qegvyd7aetst/images/04qlucyftwmb8xqx-1728980818500-compressed.0*4_QluCYFTwMb8xqx)_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](https://codehs.com/share/andy/Game_of_Life). 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](https://prod.superblogcdn.com/site_cuid_cm264m3an0084qegvyd7aetst/images/0fyhvjbs0mgffakhu-1728980820407-compressed.0*FYHvjBS0mGFFaKhu)_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](https://prod.superblogcdn.com/site_cuid_cm264m3an0084qegvyd7aetst/images/0e9cfgirax04q2cbm-1728980822202-compressed.0*e9CFgIRaX04q2cbM)

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](mailto:hello@codehs.com)!

* * *

_\*_ [_Langtons Ant_](https://en.wikipedia.org/wiki/Langton%27s_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:_](https://en.wikipedia.org/wiki/Logic_gate) _“…a logical operation performed on one or more binary inputs that produces a single binary output.”_


---
This blog is powered by Superblog. Visit https://superblog.ai to know more.
---

