Skip to content

siimplex/crc-project-2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CRC Project 2

Project 2 - Racial Segregation using Thomas C. Schelling Model


Grupo 15

  • 89905, João Gomes
  • 98649, Pedro Guerra
  • 98668, Ricardo Gonçalves

Overview

  • schelling.py - Schelling Segregation Model Simulation program
  • graph.py - program that generates graph of the average ratio of neighbours of the same color depending on the similarity threshold

Code Overview

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 False

Function 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

Dependencies


Installation and execution

  1. Clone this repository
git clone https://github.com/siimplex/crc-project-2.git
  1. Open a terminal and install the dependecies
pip install -r requirements.txt
  1. Run the Schelling Segregation Model Simulation
cd src
python schelling.py
  1. (Optional) Edit graph.ini file and run the graph program
vi graph.ini
python graph.py ../graph.ini

Demo (Schelling Segregation Model Simulation)

Main Window

execution example

Stats Window

execution example

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;

Demo Graph calculation

graph execution example

graph.ini

[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=0

Parameters:

  • 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;

References

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages