Project 2 - Racial Segregation using Thomas C. Schelling Model
- 89905, João Gomes
- 98649, Pedro Guerra
- 98668, Ricardo Gonçalves
schelling.py- Schelling Segregation Model Simulation programgraph.py- program that generates graph of the average ratio of neighbours of the same color depending on the similarity threshold
Function that applies a round of the Schelling Model on the current board
def run_round(self):
number_unhappy = 0
for (row, col), value in np.ndenumerate(self.population):
race = self.population[row, col]
if race != 0: # not empty
neighbourhood = self.get_neighbourhood(row, col)
neighbourhood_size = np.size(neighbourhood)
number_empty_entities = len(np.where(neighbourhood == 0)[0]) # number of empty entities on the neighbourhood
if neighbourhood_size != number_empty_entities + 1: # if its empty
number_similar = len(np.where(neighbourhood == race)[0]) - 1
similarity_ratio = number_similar / (neighbourhood_size - number_empty_entities - 1)
if similarity_ratio < self.similarity_threshold: # unhappy
number_unhappy = number_unhappy + 1
try:
empty_entities = list(zip(np.where(self.population == 0)[0], np.where(self.population == 0)[1]))
random_empty_entity = random.choice(empty_entities)
self.population[random_empty_entity] = race
self.population[row, col] = 0
except IndexError:
pass
if number_unhappy == 0:
return True
return FalseFunction that computes the values to represent a graph that represent the average of the ratio of neighbours of the same race in function on the similarity threshold
def compute(self):
while self.threshold <= 100:
number_of_simulation_in_threshold = 0
while number_of_simulation_in_threshold < self.simulations:
number_iterations = 0
eff_threshold = self.threshold / 100
self.schelling.model_configure(self.width, self.height, self.emptyratio, eff_threshold, self.ndepth, self.races)
while not self.schelling.run_round() and number_iterations < self.maxiterations:
number_iterations += 1
self.values[self.threshold - 1][number_of_simulation_in_threshold] = self.compute_neighbourhood_numbers()
number_of_simulation_in_threshold += 1
print(self.threshold)
self.threshold += 1
return self.values- tkinter (in Python 3.1 or superior)
- matplotlib
- numpy
- configparser
- Clone this repository
git clone https://github.com/siimplex/crc-project-2.git- Open a terminal and install the dependecies
pip install -r requirements.txt- Run the Schelling Segregation Model Simulation
cd src
python schelling.py- (Optional) Edit graph.ini file and run the graph program
vi graph.ini
python graph.py ../graph.iniMain Window
Stats Window
Parameters:
- Number of Iterations: integer less or equal than 0 will run indefinitely else the number in the text box is used;
- Population Width: number of columns in the board;
- Population Height: number of lines in the board;
- Neighbourhood Depth: the radius of each individual's neighbourhood;
- Similarity Threshold: float number between 0.0 and 1.0;
- Toggle Random: button that toggles between random ratios and user inputted ratios;
- Empty Ratio: the ratio of empty spaces, float number between 0.0 and 1.0;
- Races Ratios (A-E): the ratios of the races, float number between 0.0 and 1.0;
Buttons:
- Load: load a new board based on the paramaters;
- Start: starts the simulation, and runs for the number of iterations or indefinitely;
- Step: runs a single iteration of the simulation;
- Stats: shows a popup with the number of individuals and number of neighbours based on it's race;
[DEFAULT]
nsimulations=20
maxniterations=1000
popwidth=25
popheight=25
ndepth=1
random=False
numberraces=2
emptyratio=0.15
raratio=0.4
rbratio=0.15
rcratio=0.15
rdratio=0.15
reratio=0Parameters:
- nsimulation: Number of simulations per 0.01 of threshold;
- maxniterations: Max number of iterations before stopping the board;
- popwidth: number of columns of the boards;
- popheight: number of lines of the boards;
- ndepth: the radius of each individual's neighbourhood;
- random: if True doesn't read any ratios else reads the ratios normally;
- numberraces: number of races in each board;
- emptyratio: the ratio of empty spaces, float number between 0.0 and 1.0;
- r(a-e)ratio: the ratios of the races, float number between 0.0 and 1.0;
- Schelling, T.C., Dynamic models of segregation. Journal of Mathematical Sociology, 1971. 1(2): p. 143-186
- McCown, F. "Schelling's Model of Segregation" (2014).
- Hart, V. and N. Case. Parable of the polygons: A playable post on the shape of society.
- Sheong, S. Simulating racial segregation with Go (2019)
- Moujahid, A. An Implementation of Schelling Segregation Model using Python and Streamlit (2020)
- Moujahid, A. An Introduction to Agent-Based Models: Simulating Segregation with Python (2020)
- Liu, Y.T. The Schelling Model of Segregation: Static and Dynamic Equilibrium (2017)
- Schelling’s Segregation Model
- tkinter documentation
- matplotlib documentation
- configparser documentation


