Segregation
Segregation: residential sorting of agents and its dissimilarity index.
BlockMovesInteraction(label, *, target, width=20, height=20, side=5, probability=1.0, phase=None)
Bases: MovementInteraction
Moves of the stochastic Schelling system.
A visited agent proposes to move to a random empty cell of the grid, and stays when the cell lies in its own block.
With block densities rho_i before and rho_f' after the move, the proposal is accepted with probability
1 / (1 + exp(beta * dU)), where dU = (1 - altruism) * (u(rho_f') - u(rho_i)) + altruism * dU_global and
dU_global is the change of M * sum_q rho_q u(rho_q) over the blocks of M cells. The densities are the counts
of the block states of the group. In StochasticSchellingSystem, a phase of visits draws as many agents with
replacement as there are agents every step, and the rule set_block sets the block of an agent after its move.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
target
|
Role
|
The path of the group of dwellers. |
required |
width
|
int
|
The number of columns of the grid. |
20
|
height
|
int
|
The number of rows of the grid. |
20
|
side
|
int
|
The side of a square block in cells, which divides the width and the height. |
5
|
probability
|
float
|
The probability that a visited agent proposes a move. |
1.0
|
phase
|
str | None
|
The phase of the step the moves run in, or |
None
|
Examples:
>>> from skcomplex.systems.social.segregation import BlockMovesInteraction
>>> BlockMovesInteraction('moves', target='dwellers', side=4).schedule
'member'
Source code in src/skcomplex/systems/social/segregation/_blocks.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
get_block(agent, system)
Return the index of the block that holds the cell of an agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
AgentViewProtocol
|
The dweller. |
required |
system
|
SystemViewProtocol
|
The system view. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
block |
float
|
The index of the block. |
Source code in src/skcomplex/systems/social/segregation/_blocks.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
set_block(agent, system)
Set the block of an agent to the block that holds its cell.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
AgentViewProtocol
|
The dweller. |
required |
system
|
SystemViewProtocol
|
The system view. |
required |
Source code in src/skcomplex/systems/social/segregation/_blocks.py
216 217 218 219 220 221 222 223 224 225 226 227 | |
DwellerElement(label, *, block=0.0, beta=3.0, altruism=0.5, slope=-4.0 / 3.0, intercept=1.0, values=None, size=None, space=None)
Bases: Element
Agent of the stochastic Schelling system, with the utility a * rho + b.
The utility depends on the density of its block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the agent or group. |
required |
block
|
Annotated[object, NN]
|
The index of the agent's block, which |
0.0
|
beta
|
Annotated[object, closed_open(0, oo)]
|
The inverse temperature of the moves. |
3.0
|
altruism
|
Annotated[object, closed_open(0, oo)]
|
The weight of the change of the global utility against the agent's own utility. |
0.5
|
slope
|
object
|
The slope |
-4.0 / 3.0
|
intercept
|
object
|
The intercept |
1.0
|
values
|
Mapping[str, object] | None
|
Further values by name, such as the cell |
None
|
size
|
int | None
|
The number of members, or |
None
|
space
|
BaseSpace | None
|
The grid whose axes |
None
|
Examples:
>>> from skcomplex.systems.social.segregation import DwellerElement
>>> group = DwellerElement('dwellers', beta=3.0, size=10)
>>> group.label, group.size
('dwellers', 10)
Source code in src/skcomplex/systems/social/segregation/_blocks.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
DwellerView
Bases: AgentViewProtocol, Protocol
View of a dweller of the stochastic Schelling system.
Its states and parameters are attributes.
RelocationInteraction(label, *, target, probability=1.0, choices=None, phase=None)
Bases: MovementInteraction
Relocation of the Schelling system.
Every step every resident, in random order, moves to a random empty cell when the share of its neighbours of its own
kind is below its threshold. With choices, the Big Sort of Bishop's hypothesis, every resident moves with the
probability probability whatever its neighbours, to the cell with the highest share of neighbours of its own kind
among choices random empty cells, the first of them on ties.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
target
|
Role
|
The path of the group of residents. |
required |
probability
|
Annotated[float, closed(0, 1)]
|
The probability that an unhappy resident moves in a step, or that any resident moves under the Big Sort. |
1.0
|
choices
|
int | None
|
The number of random empty cells a moving resident compares under the Big Sort, or |
None
|
phase
|
str | None
|
The phase of the step the relocation runs in, or |
None
|
Examples:
>>> from skcomplex.systems.social.segregation import RelocationInteraction
>>> RelocationInteraction('relocation', target='residents').target
'residents'
Source code in src/skcomplex/systems/social/segregation/_schelling.py
106 107 108 109 110 111 112 113 114 115 116 | |
ResidentElement(label, *, kind=0.0, threshold=0.5, size=None)
Bases: Element
Resident of the Schelling system.
It moves to an empty cell while too few neighbours share its kind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the agent or group. |
required |
kind
|
Annotated[object, NN]
|
The kind of the resident, 0 or 1. |
0.0
|
threshold
|
Annotated[object, closed_open(0, oo)]
|
The smallest share of neighbours of the same kind that keeps a resident in place. |
0.5
|
size
|
int | None
|
The number of members, or |
None
|
Examples:
>>> from skcomplex.systems.social.segregation import ResidentElement
>>> group = ResidentElement('residents', kind=1.0, size=10)
>>> group.label, group.size
('residents', 10)
Source code in src/skcomplex/systems/social/segregation/_schelling.py
62 63 64 65 66 67 68 69 70 71 72 | |
ResidentView
Bases: AgentViewProtocol, Protocol
View of a resident of the Schelling system.
Its states and parameters are attributes.
SchellingSystem(width=20, height=20, *, density=0.8, minority=0.3, threshold=0.5, choices=None, moving=0.1, random_state=0, label='system', solver='auto', backend='auto')
Bases: BaseSystem
A Schelling segregation system on a grid with at most one resident per cell.
Every step every resident whose share of neighbours of its own kind is below the threshold moves to a random empty
cell. With choices, the Big Sort of Downey's Exercise 9-1 after Bishop's hypothesis, residents move for any
reason, a share moving of them every step, and choose among choices random empty cells the one with the highest
share of neighbours of their own kind, so the threshold plays no part. The system has the group residents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
|
int
|
The number of columns. |
20
|
height
|
int
|
The number of rows. |
20
|
density
|
float
|
The share of cells with a resident. |
0.8
|
minority
|
float
|
The share of residents of kind 1. |
0.3
|
threshold
|
float
|
The smallest share of similar neighbours that keeps a resident in place. |
0.5
|
choices
|
int | None
|
The number of random empty cells a moving resident compares under the Big Sort, or |
None
|
moving
|
float
|
The probability that a resident moves in a step under the Big Sort. |
0.1
|
random_state
|
int | Generator | None
|
The seed or generator of the kinds. |
0
|
label
|
str
|
The label of the system, which names it when it is nested in another one. |
'system'
|
solver
|
BaseSolver | str
|
The solver of the problem, or |
'auto'
|
backend
|
BaseBackend | str
|
The backend that evaluates the compiled functions, or |
'auto'
|
Examples:
>>> from skcomplex.systems.social.segregation import SchellingSystem
>>> SchellingSystem(10, 10, density=0.5).prepare().system_.elements[0].size
50
Source code in src/skcomplex/systems/social/segregation/_schelling.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
StochasticSchellingSystem(width=20, height=20, *, side=5, density=0.5, beta=3.0, altruism=0.5, slope=-4.0 / 3.0, intercept=1.0, random_state=0, label='system', solver='auto', backend='auto')
Bases: BaseSystem
The stochastic Schelling system of agents moving between blocks.
It segregates above beta = 2. The system has the group dwellers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
|
int
|
The number of columns, a multiple of the block side. |
20
|
height
|
int
|
The number of rows, a multiple of the block side. |
20
|
side
|
int
|
The side of a square block in cells. |
5
|
density
|
float
|
The share of cells with an agent. |
0.5
|
beta
|
float
|
The inverse temperature of the moves. |
3.0
|
altruism
|
float
|
The weight of the change of the global utility. |
0.5
|
slope
|
float
|
The slope of the utility in the block density. |
-4.0 / 3.0
|
intercept
|
float
|
The intercept of the utility. |
1.0
|
random_state
|
int | Generator | None
|
The seed or generator of the initial cells. |
0
|
label
|
str
|
The label of the system, which names it when it is nested in another one. |
'system'
|
solver
|
BaseSolver | str
|
The solver of the problem, or |
'auto'
|
backend
|
BaseBackend | str
|
The backend that evaluates the compiled functions, or |
'auto'
|
Examples:
>>> from skcomplex.systems.social.segregation import StochasticSchellingSystem
>>> StochasticSchellingSystem(8, 8, side=4, random_state=0).prepare().system_.elements[0].size
32
Source code in src/skcomplex/systems/social/segregation/_blocks.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |