Lesson 15 of 15
Agent-Based Modeling
Agent-Based Modeling
Agent-based models (ABMs) simulate collections of autonomous agents following simple local rules. Global patterns — segregation, flocking, traffic jams — emerge without central control.
Schelling's Segregation Model
Thomas Schelling (1971) showed that mild individual preferences for same-type neighbors produce strong global segregation. Even agents who are happy with 50% same-type neighbors will end up in nearly homogeneous clusters.
1D Model
We use a 1D ring (periodic boundary conditions):
- Grid values:
0= empty,1= type A,2= type B - An agent at position looks at neighbors within distance
neighborhood - The agent is happy if at least fraction of its non-empty neighbors are the same type
- Satisfaction: fraction of agents who are happy
Segregation Index
The segregation index measures how same-type pairs dominate adjacent slots:
For a periodic ring of sites, there are adjacent pairs . Only non-empty pairs count.
- Perfectly alternating
[1,2,1,2,...]: - Perfectly segregated
[1,1,...,2,2,...]: (minus boundary pairs)
Implementation
def segregation_index(grid):
# Count adjacent same-type pairs / total adjacent non-empty pairs
# Periodic boundary conditions
...
def count_happy(grid, f=0.5, neighborhood=1):
# Count agents where same-type fraction of neighbors >= f
# Skip empty sites (value 0)
# Periodic boundary conditions
...
def schelling_satisfaction(grid, f=0.5, neighborhood=1):
# Return fraction of non-empty agents that are happy
...
def interface_density(grid):
# Fraction of adjacent non-empty pairs that are DIFFERENT type
# = 1 - segregation_index(grid)
...Python runtime loading...
Loading...
Click "Run" to execute your code.