📚 The CoCalc Library - books, templates and other resources
License: OTHER
Agent Based Models
Code examples from Think Complexity, 2nd edition.
Copyright 2016 Allen Downey, MIT License
Schelling's model
locs_where
is a wrapper on np.nonzero
that returns results as a list of tuples.
Here's my implementation of Schelling's model:
Here's a small example.
And here's an animation for a bigger example:
The degree of segregation increases quickly.
The following figure shows the process after 2 and 10 steps.
And here's how segregation in steady state relates to p
, the threshold on the fraction of similar neighbors.
At p=0.3
, there is a striking difference between the level that would make people happy, at only 30%, and the level they actually get, around 75%.
Exercise: Experiment with different starting conditions: for example, more or fewer empty cells, or unequal numbers of red and blue agents.
Sugarscape
make_locs
takes the dimensions of the grid and returns an array where each row is a coordinate in the grid.
make_visible_locs
takes the range of an agents vision and returns an array where each row is the coordinate of a visible cell.
The cells are at increasing distances. The cells at each distance are shuffled.
distances_from
returns an array that contains the distance of each cell from the given coordinates.
I use np.digitize
to set the capacity in each cell according to the distance from the peak. Here's an example that shows how it works.
Here's my implementation of Sugarscape:
And here's a viewer that displays the state of Sugarscape.
Here's my implementation of the agents.
Here's an example with n=50
, starting with 400 agents.
The distribution of vision is uniform from 1 to 6.
The distribution of metabolism is uniform from 1 to 4.
The distribution of initial endowment of sugar is uniform from 5 to 25.
Here's what the animation looks like.
The number of agents levels off at the "carrying capacity":
This figure shows the state of the system after 2 and 10 steps.
Exercise: Experiment with different numbers of agents. Try increasing or decreasing their vision or metabolism, and see what effect is has on carrying capacity.
Sugarscape with finite lifespans
Now we start with 250 agents, with lifetimes from 60 to 100, and replacement.
After 100 time steps, the distribution of wealth is skewed to the right. Most agents have very little sugar, but a few have a lot.
Starting with the same parameters, I'll run the model 500 steps, recording the distribution of wealth after each 100 steps:
After about 200 steps, the distribution is stationary (doesn't change over time).
On a log scale, it is approximately normal, possibly with a truncated right tail.
Exercise: Experiment with different starting conditions and agents with different vision, metabolism, and lifespan. What effect do these changes have on the distribution of wealth?
Migration in waves
If we start with all agents in the lower left, they propagate up and to the right in waves.
Here's what it looks like after 6 and 12 steps.
This example is interesting because the waves move diagonally, unlike the agents, who can only move up or to the right. They are similar in some ways to gliders and other Game of Life spaceships.
Exercise: Again, experiment with different starting conditions and see what effect they have on the wave behavior.
Exercises
Exercise: Bill Bishop, author of The Big Sort, argues that American society is increasingly segregated by political opinion, as people choose to live among like-minded neighbors.
The mechanism Bishop hypothesizes is not that people, like the agents in Schelling's model, are more likely to move if they are isolated, but that when they move for any reason, they are likely to choose a neighborhood with people like themselves.
Write a version of Schelling's model to simulate this kind of behavior and see if it yields similar degrees of segregation.
There are several ways you can model Bishop's hypothesis. In my implementation, a random selection of agents moves during each step. Each agent considers k
randomly-chosen empty locations and chooses the one with the highest fraction of similar neighbors. How does the degree of segregation depend on k
?
You should be able to implement this model by inheriting from Schelling
and overriding __init__
and step
.
And a test of the step
method
Exercise: In the first version of Sugarscape, we never add agents, so once the population falls, it never recovers. In the second version, we only replace agents when they die, so the population is constant. Now let's see what happens if we add some "population pressure".
Write a version of Sugarscape that adds a new agent at the end of every step. Add code to compute the average vision and the average metabolism of the agents at the end of each step. Run the model for a few hundred steps and plot the population over time, as well as the average vision and average metabolism.
You should be able to implement this model by inheriting from Sugarscape
and overriding __init__
and step
.