# Software setup

This package and analysis can be reproduced using a conda environment with the required dependencies installed. Dependencies are listed in env_spec.txt, placed on the code folder. Assuming you have the Anaconda Python distribution installed (see page), an environment can be recreated automatically by running the following command:

conda create -n schelling --file env_spec.txt


Once that is completed, activate the environment by running:

source activate schelling


When you are done with the session, deactivate the environment with:

source deactivate


# Simulation code

There are three main scripts used to for the simulations in the paper, and all are included in the code folder:

• schelling.py contains the core methods to set up and run a Schelling-type agent-based model. See some instructions on how to use it below.
• sim_enginge_scoop.py is the script used to run the simulations on a super-computer. It relies on the library scoop for the parallel computations. The results can be found in the code folder and are named sim_res.csv.
• results.py contains the methods required to generate the figures in the paper and those included additionally in this page. An example on how to use them can be found in the visualizations section of this website.

# A tutorial for the schelling module¶

In [1]:
import time
print "Ran last ", time.strftime("%d/%m/%Y")

Ran last  23/09/2015



This document offers a short tutorial of the basic usage of the schelling module, employed in the simulations of the paper "How diverse can spatial measures of cultural diversity be? -- Results from Monte Carlo simulations on an agent-based model", by Dani Arribas-Bel, Peter Nijkamp and Jacques Poot.

The library flexibly allows to generate different topologies on which to run the "bounded neighborhood" of the Schelling model. This includes generating standard grids or using arbitrary topologies specified in a shapefile.

In [2]:
%matplotlib inline

In [3]:
import schelling


For this tutorial, we will be using a world of 70 by 70 cells, which can be vacant or occupied by agents.

In [4]:
world_dims = 70, 70


## Grid world¶

This example uses a grid of 10 by 10 neighborhoods (hence 7x7 locations per neighborhood. With this information, we can build the world topology:

In [5]:
nr, nc = neighs = (7, 7)
w, ns, xys = schelling.bounded_world(world_dims[0], world_dims[1], nr, nc)


Similarly, we allow for 25% of the spots to be vacant at any time, thus setting population size to 75% of all the 4,900 (70x70) potential spots.

In [6]:
pop_size = int((world_dims[0] * world_dims[1]) * 0.75)


At this point, we are ready to specify the groups in the population and their preferences for diversity.

We will use a case with two groups only and equal distribution (50%-50%). Note that you only specify population shares for n-1 groups, the remaining being assumed:

In [7]:
prop_groups = [0.5]


Preferences are controlled globally by the parameter $\tau$, which represents the number of agents alike every individual needs in the inmediate neighborhood to be happy. We will start with flat preferences:

In [8]:
pct_similar_wanted = 0.


At this point, we are ready to initialize the world:

In [9]:
world = schelling.World(pop_size, pct_similar_wanted, prop_groups, w, ns)


And ready to run the simulation!

In [10]:
%%time
world.go()

CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 217 µs



Once the world converges to a situation in which every individual is content, it stops. We can then examine the output by querying the object world.

The most obvious way to explore the output is by visualizing it. Note we need to pass coordinates for the locations and, in this case, a specification of the grid-like neighborhoods (both specififed above):

In [11]:
world.plot(xys, neighborhoods=neighs)


For the sake of the example, we can try now with a situation in which everyone requires at least three quarters of its neighbors to be similar. Note the clearly longer time of computation until convergence but also the radically different pattern that emerges).

In [12]:
pct_similar_wanted = 0.5
world = schelling.World(pop_size, pct_similar_wanted, prop_groups, w, ns)

In [13]:
%%time
world.go()

CPU times: user 5.52 s, sys: 40 ms, total: 5.56 s
Wall time: 5.5 s


In [14]:
world.plot(xys, neighborhoods=neighs)