Skip to content

An implementation of the Schelling segregation model.

Notifications You must be signed in to change notification settings

nickteff/schelling

Repository files navigation

Schelling Segregation Model

A Python implementation of Thomas Schelling's segregation model, demonstrating how individual preferences can lead to emergent segregation patterns.

Overview

The Schelling segregation model shows how a small preference for similar neighbors can lead to large-scale segregation patterns. In this implementation:

  • Agents of different types (races) are placed randomly on a grid
  • Some cells are left empty to allow movement
  • Agents are "satisfied" if a certain percentage of their neighbors are similar
  • Unsatisfied agents randomly move to empty cells
  • The process continues until all agents are satisfied or no more moves are possible

Installation

Clone the repository

git clone https://github.com/yourusername/schelling.git
cd schelling

Create and activate a virtual environment (optional but recommended)

python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate

Install dependencies

pip install -r requirements.txt

Usage

Run the simulation with default parameters:

python examples/run_simulation.py

This will create a GIF animation showing the evolution of the segregation patterns.

Configuration

The model can be customized with different parameters:

from schelling.model import Schelling, SchellingConfig
from schelling.visualizer import SchellingVisualizer

config = SchellingConfig(
    grid_size=50,        # Size of the grid (50x50)
    n_races=2,           # Number of different agent types
    empty_ratio=0.1,     # Proportion of cells left empty
    similarity_threshold=0.6  # Minimum ratio of similar neighbors for satisfaction
)

model = Schelling(config)
viz = SchellingVisualizer(model)

Testing

Run the test suite:

pytest

About

An implementation of the Schelling segregation model.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published