Home▸Society & Economics▸2D Schelling Segregation Model

🏙️ 2D Schelling Segregation — Real Neighbor Counting, Real Relocation

A 2D grid implementation of Schelling's segregation model: agents check real neighbor fractions against a satisfaction threshold and relocate, with a live segregation index tracked over time.

Society & Economics2DEasy60 FPS📱 Mobile-adapted⇄ 3D version
2d-schelling ↗ Open standalone

A flat 2D grid rendition of Thomas Schelling's 1971 model: every agent inspects the real occupancy of its eight-cell Moore neighborhood, compares the true fraction of same-type neighbors to an adjustable satisfaction threshold, and — if unsatisfied — relocates to a genuinely random empty cell. Watch clustering emerge from a uniformly random start, and track the segregation index live as it climbs.

🔬 What It Demonstrates

Each occupied cell counts how many of its occupied neighbors share its own type. If that real fraction falls below the Threshold slider, the agent is unhappy and moves to a randomly chosen empty cell next step. No shortcuts: the neighbor count, the threshold comparison, and the relocation are all computed directly from the live grid state every step.

🎮 How to Use

Adjust Grid size, Density of filled cells, and the Threshold each agent requires to feel settled. Press Run to watch relocation happen continuously, or Step through one iteration at a time. The Segregation Index stat and its trend chart track the mean same-type neighbor fraction across all occupied cells over time.

💡 Did You Know?

This 2D version pairs with the site's 3D GPU-textured rendition of the same model — same underlying algorithm (real Moore-neighborhood counting, real threshold-driven relocation), rendered differently. Comparing the two is a good way to confirm the mechanics, not just the visuals, are equivalent.

About the 2D Schelling Segregation Model

This is a plain 2D canvas rendition of Thomas Schelling's 1971 agent-based segregation model, computed directly rather than through a GPU texture. Two groups of agents occupy a square lattice of cells alongside empty spaces. Every step, each occupied cell inspects its eight surrounding cells — the Moore neighborhood, clipped at the grid edges — counts how many of those neighbors are occupied, and measures the real fraction that share its own type. An agent is unhappy when that fraction is below the Threshold slider, or when it has no occupied neighbors at all.

Unhappy agents are collected into a list, shuffled, and roughly 30 percent of them (at least ten, when available) are relocated to genuinely randomly chosen empty cells each step. This partial, randomized relocation — not a deterministic sweep — keeps the dynamics smooth. The Segregation Index reported alongside the grid is the mean same-type neighbor fraction across every occupied cell with at least one occupied neighbor, sampled every step and plotted as a trend line, so the emergence of clustering from an initially uniform random layout can be watched numerically as well as visually.

Frequently Asked Questions

How is this different from the 3D version?

Both implement the identical Schelling algorithm — real Moore-neighborhood counting, real threshold comparison, real random relocation to empty cells. The 3D version renders the grid as a GPU data-texture on a WebGL quad; this version draws each cell directly with the 2D canvas API. The mechanics, not just the colors, are the same.

What exactly does the Segregation Index measure?

For every occupied cell that has at least one occupied neighbor, the model computes the real fraction of those neighbors sharing its type, then averages that fraction across all such cells. A value near 50% means neighborhoods are well mixed; a value approaching 100% means agents are almost entirely surrounded by their own type.

Why does an isolated agent with no neighbors count as unhappy?

With zero occupied neighbors there is no evidence the agent's preference is satisfied, so this implementation conservatively treats it as unhappy and eligible to move. This matches the convention used in the site's 3D version of the same model.

How does the Threshold slider affect the outcome?

Threshold sets the minimum fraction of same-type occupied neighbors an agent needs to stay put. Low values (around 0.1–0.2) allow well-mixed neighborhoods to persist; values above roughly 0.4 reliably drive the grid toward strongly clustered, high-Segregation-Index configurations within a few hundred steps.

What do Grid and Density control?

Grid sets the side length of the square lattice from 20 to 100 cells (400 to 10,000 total cells). Density sets what fraction of cells start occupied, from 30% to 95%; the remainder are empty cells that unhappy agents can move into. Very high density leaves few vacancies and can slow convergence.

When does the simulation stop on its own?

Each step recomputes the unhappy count directly from the current grid. When zero agents are unhappy, the run halts automatically and the Run button switches to a "Stable" state — the configuration has reached a genuine local equilibrium under the current threshold.

⚙ Under the hood

A 2D grid implementation of Thomas Schelling's segregation model: agents check the real fraction of same-type neighbors in their Moore neighborhood against an adjustable satisfaction threshold, unsatisfied agents relocate to random empty cells, and a live segregation-index chart tracks the emergent clustering over time.

schelling segregationagent-based modelsocial dynamicsmoore neighborhoodemergent behavior2dsegregation index

2D · HTML5 Canvas 2D · 60 FPS target · runs fully client-side, no install

What did you find?

Add reproduction steps (optional)