This simulation models Schelling's segregation model on a 2D grid with toroidal (wrap-around) boundaries.
Agents and Preferences
There are two types of agents: Red and Blue.
Each agent has a tolerance level between 0.0 and 1.0.
An agent is considered "happy" if the proportion of similar neighbors within its Moore neighborhood (including the 8 adjacent cells) is greater than or equal to its tolerance. The agent itself is not counted as a neighbor.
Dynamics
The simulation iterates through the grid. For each agent:
It counts the number of similar and dissimilar neighbors within its Moore neighborhood.
If the proportion of similar neighbors is less than the agent's tolerance, the agent is considered "unhappy".
Unhappy agents are moved to a randomly chosen empty location on the grid.
This process continues until a stable state where no agents are unhappy, or a maximum number of iterations is reached.
Parameters
Size: The dimension of the square grid.
Radius: Defines the extent of the neighborhood considered (Moore neighborhood, which includes all 8 adjacent cells, is used here, so this parameter doesn't directly control the radius but influences the neighbor count).
Tolerance: The threshold for happiness (proportion of similar neighbors).