Over the last few weeks I have had a number of conversations about agent based modelling (ABM). This is a topic that seems interesting, and though I have not used it in my research, is something that I am keen to use in the future.

In order to increase my understanding of ABM, I decided to reproduce an early, and now famous agent based model: Schelling’s Segregation Model. Thomas Schelling was an economist who first devised the model, which was used to show that a small preference for a neighbour to be of the same class/race as yourself can lead to total segregation. Schelling played out the model using coins on graph paper, however there have been numerous computer simulation models run since.

The model is defined:

• A grid of squares represents the world, with each square representing a plot of land in which an agent can live.
• The world is populated (randomly initially) with a number of different kinds of agents (in our case 2), leaving a proportion of the squares vacant.
• The agents have a certain preference for the number of their immediate neighbours (agents living in the plots of land directly adjacent, including diagonally) to be of the same kind as themselves. If the number of similar neighbours is below this preference, then that agent becomes unhappy.
• Each turn, all* unhappy agents move to a randomly selected vacant plot.

I implemented this in Python, using objects as agents. For ease of coding my model assumes the world is a torus, that is the extreme left are neighbours to the extreme right, and the extreme top are neighbours with the extreme bottom. I also bend the rules slightly, and instead of all unhappy agents moving each turn, only some agents move, depending on whether the number of unhappy agents exceeds the number of vacant plots. The code defining the Agent and the World classes in which the agents live are given at the end of this post.

Object-orientated programming is an intuitive way of implementing ABM as objects themselves become the agents: object attributes are the properties of the agents, and object methods are the things the agents do.

Lets see the simulation in action. White squares represent vacant plots, yellow and purple squares represent the plots where the two different kinds of agents live. This animation shows a world grid of 80x80 squares, where agents have a preference for 70% similar neighbours:

The simulation is stopped once 99.995% of agents are happy. The resulting population has a mean neighbour similarity, or mean happiness of 99.51%, much larger than the 70% preference.

Experiments can be run to show how the model behaves with different preferences. These experiments were run on world grid of 40x40 squares:

As the individual agents’ preferences are increased, the world becomes more and more segregated. We can see in the plot below that as agents’ preference is increased, the resulting world’s mean happiness also increases. The world’s mean happiness always remains well above the individual agents’ preference:

Another measure of segregation is the number of ‘colonies’ formed. That is how many clusters of similar agents are formed. For the purpose of illustration, I will very loosely define a colony as follows:

Imagine the grid of squares as a network of vertices, where there is an edge between two vertices if those squares are neighbours and they are occupied by agents of the same kind. Then a colony is a connected component of that network.

Using NetworkX getting the number of connected components is simple. The plot below shows that the number of colonies dramatically decrease as agents’ preference increases, and that the largest decrease actually occurs at a very low preference level:

Loosely, a world populated by agents with 30% preference for similar neighbours results in a world 40 times more segregated than a world populated by agents with 10% preference for similar neighbours.

This ABM shows emergent behaviour. No one told the agents to segregate themselves and form colonies. The agents were given simple rules: look at neighbours and decide if happy, if unhappy move. System wide behaviour, segregation and colony formation, emerged from these simple rules. This is like how societal behaviour emerges from individual actions.

Here is an interesting list of NetLogo agent based models. You will see how these types of models have been used fields as far apart as biology, social sciences, and mathematics. They are being used in operational research too, which is something I hope to read more about.

All the code used to produce the results of this post is available on Github here.

Code defining Agent and World classes: