Agent-Based Modeling

What Is It? What's It For?

This is an introduction to Agent-Based Modeling (ABM). ABM is a type of computer programming with applications to physics, chemistry, biology, epidemiology, weather forecasting, forestry, sociology, economics, business, and even film making. But to understand what makes it different from other modeling, we need a little background.

What Is a Model?

A model is a construction that "resembles" something we want to investigate, and which, by manipulation of the construct, we hope to learn about the subject we are investigating.

Three Types of Modeling

Simple Orders

The first successes of mathematical modeling of reality were in describing simple orders. A classic expression of a simple order is Newton's second law of motion, F = ma: it relates one quantity to only two others, in a very straightforward manner. Similar examples are models for springs, and pendulums, and heat diffusion. Models of simple orders usually take the form of differential equations. These types of models were developed extensively in the 18th and 19th centuries, and are still very important today.

Hooke's Law is a model of the simple order of an isolated spring.

A simple order: planetary orbits.

Disorder

The second class of phenomena to be successfully modeled were random systems:

"Whereas ordinary mechanics only considers the behaviour of a single state, statistical mechanics introduces the statistical ensemble, which is a large collection of virtual, independent copies of the system in various states. The statistical ensemble is a probability distribution over all possible states of the system." -- Wikipedia

The study of such systems was conducted by the use of statistical models. Examples of phenomena successfully modeled in this way includes thermodynamical systems, gas diffusion, Brownian motion, the evolution of a quantum wave function, height and weight distributions in a population, and games of chance.

*Brownian motion: gas molecules producing random motion of a dust particle.*

In the mid-twentieth century, many scientists believed that these two types of models were all that were needed to capture all of reality: if some phenomenon could not be captured exactly by a simple model, then it must be random, and could be described by statistical analysis. But this view was soon to collapse, as, from multiple fields, researchers discovered...

Complex orders

The discovery of complex orders had been hinted at in work on the three-body problem: Newton had "solved" the problem of the planet's orbits by assuming that only the sun influenced their orbits, which is a good approximation. But once another planet was allowed to sneak into the system, the possible behavior became far more complex:

Well, at least for our solar system, these complexities could (mostly) be ignored. But for other systems this was not the case: as researchers looked into "far-from-equilibrium" systems like our weather, or chaotic fluid dynamics, or the behavior of asset markets, they found that these exhibited patterns that could not be captured by models of simple orders, nor models of pure randomness. They were discovering the existence of complex orders.

The behavior of these orders could not be captured by analysis of a differential equation, nor by statistical procedures. Instead, in general, one creates a model based on some (seemingly) simple rules, and then runs the model, and only by doing so does one discover what the system does.

A milestone along this path was the discovery, by the mathematician Benoit Mandelbrot, of the Mandelbrot set. Mandelbrot took the "simple" equation, f*c*(z) = z2 + c, where z and c are complex numbers, and iterated it (feeding the result of one iteration into the next), with z at the start equal to 0, for c near the origin, and found that this seemingly simple equation produced fantastically complex results. In a simple order, small differences in the initial value of c ought to produce small differences in the final outcome. But Mandelbrot discovered that extremely small differences in the initial value of c could produce wildly divergent outcomes: this is a characteristic of complex systems: small differences in initial values can produce very big differences in the development of the system. If we color each point c by how fast the above equation moves away from the origin, we get a result like:

*The Mandelbrot set*

In [5]:
from IPython.display import HTML

HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/pCpLWbHVNhk" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>')


Out[5]:

Similar phenomena were discovered in many fields... see the list of applications of ABMs above!... and a new field was opened to science: the study of complex orders. In general, we cannot either "solve" a problem in such an order by solving a differential equation, as we can for simple orders, or by purely statistical analysis, as we can for random phenomena. Instead, we must specify the rules the parts of the system must follow, and then we must run the system, most often on a computer, and see what emerges. (Another name for this field of study is emergent phenomena.)

One of the important tools in exploring this (relatively) new field of complex orders has been agent-based models. In such models, instead of trying to find a "top-down" equation that we can solve, and therefore say exactly what the model predicts at time t, we instead create a number of agents, which we program to follow some rules of behavior, and then set those agents "loose" in an "environment" (our model), and allow them to interact. Such models are also sometimes called cellular automata.

An early example of such a model was John Conway's game of life, where each "cell" on a grid evolved according to a simple rule, based on the state of its neighbors. Although the rules were simple, the "game" could produce very complex patterns:

*A "glider" in the game of life*

The pattern produced is clearly not "random," but it also could not be predicted by any "top-down" equation attempting to model the system. The system needs to be run in order to see what patterns emerge.

Applications of Agent-Based Modeling

An early area in which ABMs were successfully applied was that of flocking or schooling. Birds form "flocks," and fish "schools," and these groups exhibit behavior that clearly is not random, but also cannot be described by any simple equation. For instance, check out this school of fish:


In [3]:
HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/15B8qN9dre4" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>')


Out[3]:

Note that there is no fish "choreographer": the school must emerge "bottom-up" from the behavior of inidividual fish.

Using ABMs, researchers discovered that they could create agents that followed simple rules, and yet produced this sort of complex phenomena, like "boids":


In [4]:
HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/QbUPfMXXQIY" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>')


Out[4]:

ABM models have also been useful in many other fields, as noted above. Perhaps of particular interest is their application, over the past two decades, in film making. A common difficulty faced by film makers trying to shoot such scenes was whether to employ large numbers of real humans, all of whom would need to be directed, fed, and so on, or to try to animate these scenes with top-down control.

Massive realized that ABM could cut this Gordian knot: instead of trying to control all of the animation top down, film makers could instead create "agents" who would follow simple rules, and then instantiate a whole host of such agents, and create the movie episode by simple allowing all of those agents to follow their own rules.

This technique was so successful in the Lord of the Rings movies that since then most film makers desiring huge crowd scenes have used ABM.


In [6]:
HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/EmTz7EAYLrs" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>')


Out[6]:

Our ABM Efforts

Let's look at a few agent based models we've made here at Tandon.

The first one is of the spread of a forestfire.


In [ ]:
%matplotlib inline
from models.forestfire import main as ffmain

ffmain()

Another model we've built is of bacteria avoiding toxins and moving towards nutrients:


In [ ]:
%matplotlib inline
from models.bacteria import main as bamain

bamain()

Finally, let's look at a model of trends in fashion.

In this model, there are trendsetters and followers. The trendsetters want to wear something that only other trendsetters wear. Meanwhile, the followers want to be just like the trendsetters.

This leads to waves of fashion.


In [ ]:
%matplotlib inline
from models.fashion import main as famain

famain()


/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scipy/__init__.py:115: UserWarning: Numpy 1.13.3 or above is required for this version of scipy (detected version 1.13.1)
  UserWarning)
What is the grid height? [2-100] (8) 
What is the grid width? [2-100] (8) 
How many fashion trendsetters do you want? [1-100] (4) 
How many fashion followers do you want? [1-100] (4) 
Welcome to Indra, gcallah!
NOTIFICATION: Running model fashion

***************
Menu of Actions
***************
1.  Run for N periods
2.  Display a population graph
3.  Display a scatter plot
4.  Examine model data
6.  View log
0.  Quit
Please choose a number from the menu above:

How many periods? 24
Steps = 24
NOTIFICATION: Running env fashion for 24 periods.
NOTIFICATION: Switching 0 agents between groups
NOTIFICATION: Agent tsetter1 is scheduled to switch from Red Trendsetters to Blue Trendsetters
NOTIFICATION: Agent tsetter2 is scheduled to switch from Red Trendsetters to Blue Trendsetters

Total census for period 1:
==================
Group census:
==================
  Blue Trendsetters: 0
  Red Trendsetters: 4
  Red Followers: 0
  Blue Followers: 4
==================
Agent census:
==================
  Total agents moved: 6
  Total agents who switched groups: 0
NOTIFICATION: Switching 2 agents between groups
NOTIFICATION: Agent tsetter2 is scheduled to switch from Blue Trendsetters to Red Trendsetters
NOTIFICATION: Agent tsetter0 is scheduled to switch from Red Trendsetters to Blue Trendsetters
NOTIFICATION: Agent tsetter3 is scheduled to switch from Red Trendsetters to Blue Trendsetters
NOTIFICATION: Agent follower3 is scheduled to switch from Blue Followers to Red Followers

Total census for period 2:
==================
Group census:
==================
  Blue Trendsetters: 2
  Red Trendsetters: 2
  Red Followers: 0
  Blue Followers: 4
==================
Agent census:
==================
  Total agents moved: 10
  Total agents who switched groups: 2
NOTIFICATION: Switching 4 agents between groups
NOTIFICATION: Agent tsetter1 is scheduled to switch from Blue Trendsetters to Red Trendsetters
NOTIFICATION: Agent follower3 is scheduled to switch from Red Followers to Blue Followers

Total census for period 3:
==================
Group census:
==================
  Blue Trendsetters: 3
  Red Trendsetters: 1
  Red Followers: 1
  Blue Followers: 3
==================
Agent census:
==================
  Total agents moved: 16
  Total agents who switched groups: 4
NOTIFICATION: Switching 2 agents between groups
NOTIFICATION: Agent tsetter0 is scheduled to switch from Blue Trendsetters to Red Trendsetters
NOTIFICATION: Agent tsetter3 is scheduled to switch from Blue Trendsetters to Red Trendsetters

Total census for period 4:
==================
Group census:
==================
  Blue Trendsetters: 2
  Red Trendsetters: 2
  Red Followers: 0
  Blue Followers: 4
==================
Agent census:
==================
  Total agents moved: 22
  Total agents who switched groups: 2
NOTIFICATION: Switching 2 agents between groups
NOTIFICATION: Agent follower1 is scheduled to switch from Blue Followers to Red Followers
NOTIFICATION: Agent follower2 is scheduled to switch from Blue Followers to Red Followers
NOTIFICATION: Agent follower3 is scheduled to switch from Blue Followers to Red Followers

Total census for period 5:
==================
Group census:
==================
  Blue Trendsetters: 0
  Red Trendsetters: 4
  Red Followers: 0
  Blue Followers: 4
==================
Agent census:
==================
  Total agents moved: 27
  Total agents who switched groups: 2
NOTIFICATION: Switching 3 agents between groups
NOTIFICATION: Agent tsetter3 is scheduled to switch from Red Trendsetters to Blue Trendsetters

Total census for period 6:
==================
Group census:
==================
  Blue Trendsetters: 0
  Red Trendsetters: 4
  Red Followers: 3
  Blue Followers: 1
==================
Agent census:
==================
  Total agents moved: 34
  Total agents who switched groups: 3
NOTIFICATION: Switching 1 agents between groups
NOTIFICATION: Agent tsetter2 is scheduled to switch from Red Trendsetters to Blue Trendsetters

Total census for period 7:
==================
Group census:
==================
  Blue Trendsetters: 1
  Red Trendsetters: 3
  Red Followers: 3
  Blue Followers: 1
==================
Agent census:
==================
  Total agents moved: 41
  Total agents who switched groups: 1
NOTIFICATION: Switching 1 agents between groups
NOTIFICATION: Agent tsetter2 is scheduled to switch from Blue Trendsetters to Red Trendsetters

Total census for period 8:
==================
Group census:
==================
  Blue Trendsetters: 2
  Red Trendsetters: 2
  Red Followers: 3
  Blue Followers: 1
==================
Agent census:
==================
  Total agents moved: 48
  Total agents who switched groups: 1
NOTIFICATION: Switching 1 agents between groups
NOTIFICATION: Agent tsetter1 is scheduled to switch from Red Trendsetters to Blue Trendsetters

Total census for period 9:
==================
Group census:
==================
  Blue Trendsetters: 1
  Red Trendsetters: 3
  Red Followers: 3
  Blue Followers: 1
==================
Agent census:
==================
  Total agents moved: 55
  Total agents who switched groups: 1
NOTIFICATION: Switching 1 agents between groups
NOTIFICATION: Agent follower2 is scheduled to switch from Red Followers to Blue Followers

Total census for period 10:
==================
Group census:
==================
  Blue Trendsetters: 2
  Red Trendsetters: 2
  Red Followers: 3
  Blue Followers: 1
==================
Agent census:
==================
  Total agents moved: 62
  Total agents who switched groups: 1
NOTIFICATION: Switching 1 agents between groups
NOTIFICATION: Agent tsetter3 is scheduled to switch from Blue Trendsetters to Red Trendsetters

Total census for period 11:
==================
Group census:
==================
  Blue Trendsetters: 2
  Red Trendsetters: 2
  Red Followers: 2
  Blue Followers: 2
==================
Agent census:
==================
  Total agents moved: 69
  Total agents who switched groups: 1
NOTIFICATION: Switching 1 agents between groups
NOTIFICATION: Agent tsetter1 is scheduled to switch from Blue Trendsetters to Red Trendsetters
NOTIFICATION: Agent tsetter0 is scheduled to switch from Red Trendsetters to Blue Trendsetters
NOTIFICATION: Agent tsetter2 is scheduled to switch from Red Trendsetters to Blue Trendsetters
NOTIFICATION: Agent follower0 is scheduled to switch from Blue Followers to Red Followers

Total census for period 12:
==================
Group census:
==================
  Blue Trendsetters: 1
  Red Trendsetters: 3
  Red Followers: 2
  Blue Followers: 2
==================
Agent census:
==================
  Total agents moved: 73
  Total agents who switched groups: 1
NOTIFICATION: Switching 4 agents between groups
NOTIFICATION: Agent tsetter3 is scheduled to switch from Red Trendsetters to Blue Trendsetters
NOTIFICATION: Agent follower2 is scheduled to switch from Blue Followers to Red Followers

Total census for period 13:
==================
Group census:
==================
  Blue Trendsetters: 2
  Red Trendsetters: 2
  Red Followers: 3
  Blue Followers: 1
==================
Agent census:
==================
  Total agents moved: 79
  Total agents who switched groups: 4
NOTIFICATION: Switching 2 agents between groups
NOTIFICATION: Agent tsetter1 is scheduled to switch from Red Trendsetters to Blue Trendsetters

Total census for period 14:
==================
Group census:
==================
  Blue Trendsetters: 3
  Red Trendsetters: 1
  Red Followers: 4
  Blue Followers: 0
==================
Agent census:
==================
  Total agents moved: 86
  Total agents who switched groups: 2
NOTIFICATION: Switching 1 agents between groups
NOTIFICATION: Agent follower2 is scheduled to switch from Red Followers to Blue Followers

Total census for period 15:
==================
Group census:
==================
  Blue Trendsetters: 4
  Red Trendsetters: 0
  Red Followers: 4
  Blue Followers: 0
==================
Agent census:
==================
  Total agents moved: 93
  Total agents who switched groups: 1
NOTIFICATION: Switching 1 agents between groups
NOTIFICATION: Agent follower3 is scheduled to switch from Red Followers to Blue Followers
NOTIFICATION: Agent follower0 is scheduled to switch from Red Followers to Blue Followers

Total census for period 16:
==================
Group census:
==================
  Blue Trendsetters: 4
  Red Trendsetters: 0
  Red Followers: 3
  Blue Followers: 1
==================
Agent census:
==================
  Total agents moved: 99
  Total agents who switched groups: 1
NOTIFICATION: Switching 2 agents between groups
NOTIFICATION: Agent tsetter3 is scheduled to switch from Blue Trendsetters to Red Trendsetters
NOTIFICATION: Agent tsetter1 is scheduled to switch from Blue Trendsetters to Red Trendsetters

Total census for period 17:
==================
Group census:
==================
  Blue Trendsetters: 4
  Red Trendsetters: 0
  Red Followers: 1
  Blue Followers: 3
==================
Agent census:
==================
  Total agents moved: 105
  Total agents who switched groups: 2
NOTIFICATION: Switching 2 agents between groups
NOTIFICATION: Agent tsetter2 is scheduled to switch from Blue Trendsetters to Red Trendsetters

Total census for period 18:
==================
Group census:
==================
  Blue Trendsetters: 2
  Red Trendsetters: 2
  Red Followers: 1
  Blue Followers: 3
==================
Agent census:
==================
  Total agents moved: 112
  Total agents who switched groups: 2
NOTIFICATION: Switching 1 agents between groups
NOTIFICATION: Agent tsetter0 is scheduled to switch from Blue Trendsetters to Red Trendsetters
NOTIFICATION: Agent follower2 is scheduled to switch from Blue Followers to Red Followers
NOTIFICATION: Agent follower0 is scheduled to switch from Blue Followers to Red Followers

Total census for period 19:
==================
Group census:
==================
  Blue Trendsetters: 1
  Red Trendsetters: 3
  Red Followers: 1
  Blue Followers: 3
==================
Agent census:
==================
  Total agents moved: 117
  Total agents who switched groups: 1
NOTIFICATION: Switching 3 agents between groups
NOTIFICATION: Agent tsetter0 is scheduled to switch from Red Trendsetters to Blue Trendsetters
NOTIFICATION: Agent follower3 is scheduled to switch from Blue Followers to Red Followers

Total census for period 20:
==================
Group census:
==================
  Blue Trendsetters: 0
  Red Trendsetters: 4
  Red Followers: 3
  Blue Followers: 1
==================
Agent census:
==================
  Total agents moved: 123
  Total agents who switched groups: 3
NOTIFICATION: Switching 2 agents between groups
NOTIFICATION: Agent tsetter3 is scheduled to switch from Red Trendsetters to Blue Trendsetters
NOTIFICATION: Agent tsetter1 is scheduled to switch from Red Trendsetters to Blue Trendsetters

Total census for period 21:
==================
Group census:
==================
  Blue Trendsetters: 1
  Red Trendsetters: 3
  Red Followers: 4
  Blue Followers: 0
==================
Agent census:
==================
  Total agents moved: 129
  Total agents who switched groups: 2
NOTIFICATION: Switching 2 agents between groups
NOTIFICATION: Agent tsetter2 is scheduled to switch from Red Trendsetters to Blue Trendsetters
NOTIFICATION: Agent follower0 is scheduled to switch from Red Followers to Blue Followers
NOTIFICATION: Agent follower3 is scheduled to switch from Red Followers to Blue Followers

Total census for period 22:
==================
Group census:
==================
  Blue Trendsetters: 3
  Red Trendsetters: 1
  Red Followers: 4
  Blue Followers: 0
==================
Agent census:
==================
  Total agents moved: 134
  Total agents who switched groups: 2
NOTIFICATION: Switching 3 agents between groups
NOTIFICATION: Agent tsetter1 is scheduled to switch from Blue Trendsetters to Red Trendsetters

Total census for period 23:
==================
Group census:
==================
  Blue Trendsetters: 4
  Red Trendsetters: 0
  Red Followers: 2
  Blue Followers: 2
==================
Agent census:
==================
  Total agents moved: 141
  Total agents who switched groups: 3
NOTIFICATION: Switching 1 agents between groups
NOTIFICATION: Agent tsetter3 is scheduled to switch from Blue Trendsetters to Red Trendsetters
NOTIFICATION: Agent tsetter2 is scheduled to switch from Blue Trendsetters to Red Trendsetters
NOTIFICATION: Agent tsetter1 is scheduled to switch from Red Trendsetters to Blue Trendsetters

Total census for period 24:
==================
Group census:
==================
  Blue Trendsetters: 3
  Red Trendsetters: 1
  Red Followers: 2
  Blue Followers: 2
==================
Agent census:
==================
  Total agents moved: 146
  Total agents who switched groups: 1

***************
Menu of Actions
***************
1.  Run for N periods
2.  Display a population graph
3.  Display a scatter plot
4.  Examine model data
6.  View log
0.  Quit
Please choose a number from the menu above:
2
Drawing a line graph.
***************
Menu of Actions
***************
1.  Run for N periods
2.  Display a population graph
3.  Display a scatter plot
4.  Examine model data
6.  View log
0.  Quit
Updating the line graph.
Please choose a number from the menu above:

In [ ]:
0