Skip to content

Crowds

Crowds: pedestrians driven by social forces, evacuations, and criminality.

The criminality acts against security forces.

CriminalContagionInteraction(label, *, candidate, field, classes=5, eta=1.0, attraction=0.3, redemption=0.1)

Bases: EncounterInteraction

Conservative encounters between citizens.

In them crime attracts and honesty redeems.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
candidate Role

The path of the distribution of citizens whose members move.

required
field Role

The path of the distribution of citizens they meet, usually the same distribution.

required
classes Annotated[int, open(0, oo)]

The number of criminality classes.

5
eta Annotated[float, closed_open(0, oo)]

The encounter rate.

1.0
attraction Annotated[float, closed_open(0, oo)]

The probability that a citizen who meets a more criminal one moves one class up.

0.3
redemption Annotated[float, closed_open(0, oo)]

The probability that a citizen who meets a more honest one moves one class down.

0.1

Examples:

>>> from skcomplex.systems.social.crowds import CriminalContagionInteraction
>>> CriminalContagionInteraction('contagion', candidate='citizens', field='citizens').attraction
0.3
Source code in src/skcomplex/systems/social/crowds/_criminality.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
def __init__(
    self,
    label: str,
    *,
    candidate: Role,
    field: Role,
    classes: Annotated[int, RealSet.open(0, oo)] = 5,
    eta: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
    attraction: Annotated[float, RealSet.closed_open(0, oo)] = 0.3,
    redemption: Annotated[float, RealSet.closed_open(0, oo)] = 0.1,
) -> None:
    super().__init__(label, candidate=candidate, field=field, rate=self._get_rate, kernel=self._get_kernel)
    self.classes = classes
    self.eta = eta
    self.attraction = attraction
    self.redemption = redemption

CriminalitySystem(classes=5, forces=3, *, criminals=0.2, security=0.05, attraction=0.3, redemption=0.1, deterrence=2.0, recruitment=0.5, retirement=0.1, label='system', solver='auto', backend='auto')

Bases: BaseSystem

The kinetic system of criminality in a society.

It includes the security forces that act against it. Citizens are a distribution over criminality classes and security agents a distribution over efficacy classes, held by site groups on lattices of the classes. Encounters between citizens spread crime when it attracts more than honesty redeems, encounters with security agents move criminal citizens toward honesty, a micro-scale action, and the security forces grow in proportion to the mean criminality, a macro-scale action that reads a moment of the citizens. The security forces grow while crime is high, crime recedes, and the forces retire, so crime and security oscillate toward a balance where some crime persists.

The system has the distributions citizens and security and the interactions contagion, deterrence, and recruitment. Its standard observables are the mean criminality, the criminal fraction, and the density of the security forces.

Parameters:

Name Type Description Default
classes int

The number of criminality classes.

5
forces int

The number of efficacy classes of the security forces.

3
criminals float

The initial fraction of citizens in the most criminal class, the others being honest.

0.2
security float

The initial density of the security forces, spread evenly over their efficacy classes.

0.05
attraction float

The probability that a citizen who meets a more criminal one moves one class up.

0.3
redemption float

The probability that a citizen who meets a more honest one moves one class down.

0.1
deterrence float

The rate at which the most effective agents deter a criminal citizen.

2.0
recruitment float

The recruitment rate of security agents per unit of mean criminality.

0.5
retirement float

The retirement rate of security agents.

0.1
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' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.social.crowds import CriminalitySystem
>>> [item.label for item in CriminalitySystem().prepare().system_.interactions]
['contagion', 'deterrence', 'recruitment']
Source code in src/skcomplex/systems/social/crowds/_criminality.py
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
def __init__(
    self,
    classes: int = 5,
    forces: int = 3,
    *,
    criminals: float = 0.2,
    security: float = 0.05,
    attraction: float = 0.3,
    redemption: float = 0.1,
    deterrence: float = 2.0,
    recruitment: float = 0.5,
    retirement: float = 0.1,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.classes = classes
    self.forces = forces
    self.criminals = criminals
    self.security = security
    self.attraction = attraction
    self.redemption = redemption
    self.deterrence = deterrence
    self.recruitment = recruitment
    self.retirement = retirement

CrowdingInteraction(label, *, candidate, field, rooms=7, speeds=3, crowding=2.0)

Bases: EncounterInteraction

Encounters that slow pedestrians down in crowded rooms.

It is an EncounterInteraction of the crowd with itself. Two pedestrians in the same room meet at the rate crowding, and the candidate slows down one speed class. Pedestrians outside keep their speed.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
candidate Role

The path of the crowd, a distribution over rooms and speed classes.

required
field Role

The path of the crowd that the candidates meet, the crowd again.

required
rooms Annotated[int, open(0, oo)]

The number of rooms.

7
speeds Annotated[int, open(0, oo)]

The number of speed classes.

3
crowding Annotated[float, closed_open(0, oo)]

The rate of the encounters within a room that slow pedestrians down.

2.0

Examples:

>>> from skcomplex.systems.social.crowds import CrowdingInteraction
>>> CrowdingInteraction('crowding', candidate='crowd', field='crowd').crowding
2.0
Source code in src/skcomplex/systems/social/crowds/_evacuation.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
def __init__(
    self,
    label: str,
    *,
    candidate: Role,
    field: Role,
    rooms: Annotated[int, RealSet.open(0, oo)] = 7,
    speeds: Annotated[int, RealSet.open(0, oo)] = 3,
    crowding: Annotated[float, RealSet.closed_open(0, oo)] = 2.0,
) -> None:
    super().__init__(label, candidate=candidate, field=field, rate=self._get_rate, kernel=self._get_kernel)
    self.rooms = rooms
    self.speeds = speeds
    self.crowding = crowding

DeterrenceInteraction(label, *, candidate, field, classes=5, forces=3, deterrence=2.0)

Bases: EncounterInteraction

Encounters of citizens with security forces.

They move criminal citizens toward honesty.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
candidate Role

The path of the distribution of citizens.

required
field Role

The path of the distribution of the security forces over efficacy.

required
classes Annotated[int, open(0, oo)]

The number of criminality classes.

5
forces Annotated[int, open(0, oo)]

The number of efficacy classes of the security forces.

3
deterrence Annotated[float, closed_open(0, oo)]

The rate at which the most effective agents deter a criminal citizen.

2.0

Examples:

>>> from skcomplex.systems.social.crowds import DeterrenceInteraction
>>> DeterrenceInteraction('deterrence', candidate='citizens', field='security').deterrence
2.0
Source code in src/skcomplex/systems/social/crowds/_criminality.py
234
235
236
237
238
239
240
241
242
243
244
245
246
247
def __init__(
    self,
    label: str,
    *,
    candidate: Role,
    field: Role,
    classes: Annotated[int, RealSet.open(0, oo)] = 5,
    forces: Annotated[int, RealSet.open(0, oo)] = 3,
    deterrence: Annotated[float, RealSet.closed_open(0, oo)] = 2.0,
) -> None:
    super().__init__(label, candidate=candidate, field=field, rate=self._get_rate, kernel=self._get_kernel)
    self.classes = classes
    self.forces = forces
    self.deterrence = deterrence

EvacuationSystem(branching=2, depth=2, speeds=3, *, mobility=0.5, crowding=2.0, recovery=0.5, label='system', solver='auto', backend='auto')

Bases: BaseSystem

The kinetic system of the evacuation of a building whose rooms form a network.

The crowd is a distribution over pairs of a room and a speed class, held by a site group on a two-dimensional lattice indexed by room and speed, with one last row of sites for the outside. The rooms form a balanced tree with the exit in the root room, and the walking encounters read its doors, so pedestrians walk to the next room on the way out at a rate proportional to their speed. Encounters within a room slow pedestrians down, and the exit signs speed them up again, so the crowd queues in the rooms near the exit and the evacuated fraction grows along an S-shaped curve.

The rooms are the nodes and the speed classes the discrete activity of the kinetic structure for a network of Bellomo et al. (2017, sections 3.4 and 3.4.1), and the slowing and guiding encounters follow the behavioural crowd of Bellomo and Gibelli (2015). The crowd of chapter 6 of Bellomo et al. (2017), which moves in continuous space around walls and obstacles, is a different model. The system has the crowd crowd, the exit signs signs, and the interactions walking, crowding, and guidance. Its standard observables are the evacuated fraction and the mean walking speed.

Parameters:

Name Type Description Default
branching int

The number of rooms behind every door of the building.

2
depth int

The number of doors between the exit room and the rooms furthest from it.

2
speeds int

The number of speed classes.

3
mobility float

The rate at which pedestrians at the free speed leave a room.

0.5
crowding float

The rate of the encounters within a room that slow pedestrians down.

2.0
recovery float

The rate at which pedestrians speed up again.

0.5
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' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.social.crowds import EvacuationSystem
>>> EvacuationSystem().prepare().system_.elements[0].x.size
24
Source code in src/skcomplex/systems/social/crowds/_evacuation.py
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
def __init__(
    self,
    branching: int = 2,
    depth: int = 2,
    speeds: int = 3,
    *,
    mobility: float = 0.5,
    crowding: float = 2.0,
    recovery: float = 0.5,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.branching = branching
    self.depth = depth
    self.speeds = speeds
    self.mobility = mobility
    self.crowding = crowding
    self.recovery = recovery

GuidanceInteraction(label, *, candidate, field, rooms=7, speeds=3, recovery=0.5)

Bases: EncounterInteraction

Encounters with the exit signs that speed pedestrians up again.

It is an EncounterInteraction of the crowd with the exit signs. A pedestrian meets the exit signs at the rate recovery and speeds up one class. Pedestrians outside keep their speed.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
candidate Role

The path of the crowd, a distribution over rooms and speed classes.

required
field Role

The path of the group of one member whose fixed density is the guidance of the exit signs.

required
rooms Annotated[int, open(0, oo)]

The number of rooms.

7
speeds Annotated[int, open(0, oo)]

The number of speed classes.

3
recovery Annotated[float, closed_open(0, oo)]

The rate at which pedestrians speed up again.

0.5

Examples:

>>> from skcomplex.systems.social.crowds import GuidanceInteraction
>>> GuidanceInteraction('guidance', candidate='crowd', field='signs').recovery
0.5
Source code in src/skcomplex/systems/social/crowds/_evacuation.py
168
169
170
171
172
173
174
175
176
177
178
179
180
181
def __init__(
    self,
    label: str,
    *,
    candidate: Role,
    field: Role,
    rooms: Annotated[int, RealSet.open(0, oo)] = 7,
    speeds: Annotated[int, RealSet.open(0, oo)] = 3,
    recovery: Annotated[float, RealSet.closed_open(0, oo)] = 0.5,
) -> None:
    super().__init__(label, candidate=candidate, field=field, rate=self._get_rate, kernel=self._get_kernel)
    self.rooms = rooms
    self.speeds = speeds
    self.recovery = recovery

KineticCrowdInteraction(label, *, target, length=20.0, width=5.0, cell=2.5, packing=4.0, quality=1.0, stress=0.5, deceleration=0.95, rate=1.0, wall_distance=0.5, dt=0.1, panic_time=None, panic_stress=0.9, band_width=1.0)

Bases: Interaction

Decision process of Bellomo's kinetic crowd model.

A direct simulation Monte Carlo of the walkers runs it. The density rho and the mean velocity xi are counted on square cells of side cell of a street of length length, periodic along it, and width width, between walls, with rho = 1 at packing walkers per unit area. Every walker interacts with the crowd with the probability eta_A dt in a step of length dt. It first turns to the preferred direction nu_p ~ rho nu_v + (1 - rho) (beta nu_s + (1 - beta) nu_t) / |.|, where nu_v points down the gradient of the density, nu_s along the mean velocity, and nu_t to its target, and then adjusts its speed to the perceived density rho_theta = rho + d / sqrt(1 + d^2) ((1 - rho) H(d) + rho H(-d)) of the derivative d of the density along its new direction: a walker no faster than the mean speed accelerates to xi + alpha beta (1 - rho_theta) (alpha beta - xi) with the probability alpha beta (1 - rho_theta), and a faster one decelerates to xi - rho_theta xi with the probability (1 - c alpha beta) rho_theta. A walker at a distance d < d_w from a wall and heading to it then reduces the normal component of its velocity to d / d_w of it at constant speed, and every walker moves along its velocity. From the time panic_time on, the stress parameter is panic_stress in place of stress. The interaction reports the band index, the mean of |rho_1 - rho_2| / (rho_1 + rho_2) over the cells of the street where walkers are, of the densities smoothed by a Gaussian of width band_width, and the mean speed.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the group of walkers.

required
length Annotated[float, open(0, oo)]

The length of the street, periodic along it.

20.0
width Annotated[float, open(0, oo)]

The width of the street, between the walls.

5.0
cell Annotated[float, open(0, oo)]

The side of the square cells of the density and the mean velocity.

2.5
packing Annotated[float, open(0, oo)]

The number of walkers per unit area at the largest density 1.

4.0
quality Annotated[float, closed(0, 1)]

The quality alpha of the venue.

1.0
stress Annotated[float, closed(0, 1)]

The stress beta, the weight of the stream against the target.

0.5
deceleration Annotated[float, closed_open(0, 1)]

The constant c < 1 of the probability of deceleration.

0.95
rate Annotated[float, closed_open(0, oo)]

The interaction rate eta_A between walkers.

1.0
wall_distance Annotated[float, open(0, oo)]

The distance d_w from a wall below which walkers turn away from it.

0.5
dt Annotated[float, open(0, oo)]

The length of a step.

0.1
panic_time float | None

The time t_c from which the stress is panic_stress, or None for no panic.

None
panic_stress Annotated[float, closed(0, 1)]

The stress beta_2 after the panic time.

0.9
band_width Annotated[float, open(0, oo)]

The width of the Gaussian smoothing of the densities of the band index.

1.0

Examples:

>>> from skcomplex.systems.social.crowds import KineticCrowdInteraction
>>> KineticCrowdInteraction('decisions', target='walkers').stress
0.5
Source code in src/skcomplex/systems/social/crowds/_kinetic_crowd.py
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
def __init__(
    self,
    label: str,
    *,
    target: Role,
    length: Annotated[float, RealSet.open(0, oo)] = 20.0,
    width: Annotated[float, RealSet.open(0, oo)] = 5.0,
    cell: Annotated[float, RealSet.open(0, oo)] = 2.5,
    packing: Annotated[float, RealSet.open(0, oo)] = 4.0,
    quality: Annotated[float, RealSet.closed(0, 1)] = 1.0,
    stress: Annotated[float, RealSet.closed(0, 1)] = 0.5,
    deceleration: Annotated[float, RealSet.closed_open(0, 1)] = 0.95,
    rate: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
    wall_distance: Annotated[float, RealSet.open(0, oo)] = 0.5,
    dt: Annotated[float, RealSet.open(0, oo)] = 0.1,
    panic_time: float | None = None,
    panic_stress: Annotated[float, RealSet.closed(0, 1)] = 0.9,
    band_width: Annotated[float, RealSet.open(0, oo)] = 1.0,
) -> None:
    super().__init__(label)
    self.target = target
    self.length = length
    self.width = width
    self.cell = cell
    self.packing = packing
    self.quality = quality
    self.stress = stress
    self.deceleration = deceleration
    self.rate = rate
    self.wall_distance = wall_distance
    self.dt = dt
    self.panic_time = panic_time
    self.panic_stress = panic_stress
    self.band_width = band_width

walk(system)

Run the decisions, the turns at the walls, and the motion of one step.

Parameters:

Name Type Description Default
system SystemView

The system.

required
Source code in src/skcomplex/systems/social/crowds/_kinetic_crowd.py
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
def walk(self, system: SystemView) -> None:
    """Run the decisions, the turns at the walls, and the motion of one step.

    Args:
        system:
            The system.
    """
    views = cast(list[KineticWalkerView], system.get_agents(self.target))
    if not views:
        return
    walkers = {
        name: np.array([getattr(view, name) for view in views])
        for name in ('x', 'y', 'speed', 'direction', 'target')
    }
    stress = self.stress
    if self.panic_time is not None and system.time >= self.panic_time:
        stress = self.panic_stress
    velocity = np.stack([np.cos(walkers['direction']), np.sin(walkers['direction'])], axis=1)
    fields = self._get_fields(walkers['x'], walkers['y'], walkers['speed'][:, None] * velocity, walkers['target'])
    self._interact(walkers, fields, stress, system.rng)
    self._avoid_walls(walkers)
    walkers['x'] = np.mod(walkers['x'] + self.dt * walkers['speed'] * np.cos(walkers['direction']), self.length)
    walkers['y'] = np.clip(
        walkers['y'] + self.dt * walkers['speed'] * np.sin(walkers['direction']),
        0.0,
        self.width,
    )
    for position, view in enumerate(views):
        for name in ('x', 'y', 'speed', 'direction'):
            setattr(view, name, float(walkers[name][position]))
    system.report('band_index', self._get_band_index(fields['first'], fields['second']))
    system.report('mean_speed', float(walkers['speed'].mean()))

KineticCrowdSystem(n_walkers=50, *, length=20.0, width=5.0, cell=2.5, packing=4.0, quality=1.0, stress=0.5, deceleration=0.95, rate=1.0, wall_distance=0.5, panic_time=None, panic_stress=0.9, random_state=0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

Bellomo's kinetic crowd of two groups walking in opposite directions.

They walk along a street with walls. Walkers in a continuous venue choose first a direction and then a speed (Bellomo et al. 2017 sections 6.4 and 6.5). The preferred direction weighs the search of less crowded areas, the attraction of the mean stream, and the target, by the density rho and the stress beta (Eq. 6.19). The speed then adjusts to the density perceived along that direction, with the venue quality alpha and the stress (Eqs. 6.22 to 6.24), and walkers near a wall turn parallel to it at constant speed (Eq. 6.25). Panic raises the stress from beta_1 to beta_2 at a critical time (Eqs. 6.27 and 6.28). The kinetic equation is solved, as in the book, by a particle method close to direct simulation Monte Carlo, here with one particle per walker. In space-homogeneous conditions the mean speed follows the fundamental diagram alpha^3 beta^3 (1 - rho)^2 / (alpha^2 beta^2 (1 - rho)^2 + (1 - c alpha beta) rho^2) (Eq. 6.30), which a single cell spanning the street reproduces. The default is the counterflow of section 6.5.2, 50 walkers uniformly distributed in a street periodic along its length, for which the book reports segregation into lanes, measured by the band index of Eq. 6.31, most pronounced at beta = 0.5. The densities are counted on cells of side cell, an assumption of this particle method. The venue is a distinct system from EvacuationSystem, a network of rooms. One step is a tenth of a time unit.

The system has the group walkers and the reporters decisions.band_index and decisions.mean_speed.

Parameters:

Name Type Description Default
n_walkers int

The number of walkers, half walking to each end.

50
length float

The length of the street, periodic along it.

20.0
width float

The width of the street, between the walls.

5.0
cell float

The side of the square cells of the density and the mean velocity.

2.5
packing float

The number of walkers per unit area at the largest density 1.

4.0
quality float

The quality alpha of the venue.

1.0
stress float

The stress beta, the weight of the stream against the target.

0.5
deceleration float

The constant c < 1 of the probability of deceleration.

0.95
rate float

The interaction rate eta_A between walkers.

1.0
wall_distance float

The distance d_w from a wall below which walkers turn away from it.

0.5
panic_time float | None

The time t_c, in steps, from which the stress is panic_stress, or None for no panic.

None
panic_stress float

The stress beta_2 after the panic time.

0.9
random_state int | Generator | None

The seed or generator of the initial positions and speeds.

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' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.social.crowds import KineticCrowdSystem
>>> KineticCrowdSystem(10).prepare().system_.elements[0].size
10
Source code in src/skcomplex/systems/social/crowds/_kinetic_crowd.py
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
def __init__(
    self,
    n_walkers: int = 50,
    *,
    length: float = 20.0,
    width: float = 5.0,
    cell: float = 2.5,
    packing: float = 4.0,
    quality: float = 1.0,
    stress: float = 0.5,
    deceleration: float = 0.95,
    rate: float = 1.0,
    wall_distance: float = 0.5,
    panic_time: float | None = None,
    panic_stress: float = 0.9,
    random_state: int | np.random.Generator | None = 0,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.n_walkers = n_walkers
    self.length = length
    self.width = width
    self.cell = cell
    self.packing = packing
    self.quality = quality
    self.stress = stress
    self.deceleration = deceleration
    self.rate = rate
    self.wall_distance = wall_distance
    self.panic_time = panic_time
    self.panic_stress = panic_stress
    self.random_state = random_state

KineticWalkerElement(label, *, x=0.0, y=0.0, speed=0.5, direction=0.0, target=0.0, size=None)

Bases: Element

Walker of the kinetic crowd system.

It holds its position, its speed, its direction, and the side it walks to.

Parameters:

Name Type Description Default
label str

The label of the walker or the group.

required
x object

The position along the street.

0.0
y object

The position across the street, between the walls at 0 and the width.

0.0
speed Annotated[object, closed(0, 1)]

The dimensionless speed v, between 0 and the largest speed 1.

0.5
direction object

The direction theta of the velocity.

0.0
target Annotated[object, FiniteEnumeratedSet([0, 1])]

The direction of the target, 0 for the exit at the right end and 1 for the exit at the left end.

0.0
size int | None

The number of members, or None for one.

None

Examples:

>>> from skcomplex.systems.social.crowds import KineticWalkerElement
>>> KineticWalkerElement('walker', target=1.0).target
1.0
Source code in src/skcomplex/systems/social/crowds/_kinetic_crowd.py
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def __init__(
    self,
    label: str,
    *,
    x: object = 0.0,
    y: object = 0.0,
    speed: Annotated[object, RealSet.closed(0, 1)] = 0.5,
    direction: object = 0.0,
    target: Annotated[object, FiniteEnumeratedSet([0, 1])] = 0.0,
    size: int | None = None,
) -> None:
    super().__init__(label, size=size)
    self.x = x
    self.y = y
    self.speed = speed
    self.direction = direction
    self.target = target

KineticWalkerView

Bases: AgentViewProtocol, Protocol

View of a walker of the kinetic crowd system, with its states as attributes.

MotivationInteraction(label, *, target, speed=1.34, direction=0.0, tau=0.5)

Bases: Interaction

Driving force of the social force system, which relaxes a walker's velocity to its desired velocity.

The force per unit mass is (speed * direction - x') / tau, where the velocity x' is the derivative of the position x.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the walker or group of walkers.

required
speed Annotated[float, closed_open(0, oo)]

The desired speed.

1.34
direction ArrayLike

The unit vector of the desired direction.

0.0
tau Annotated[float, open(0, oo)]

The relaxation time.

0.5

Examples:

>>> from skcomplex.systems.social.crowds import MotivationInteraction
>>> MotivationInteraction('drive', target='walkers', direction=[1.0, 0.0]).speed
1.34
Source code in src/skcomplex/systems/social/crowds/_pedestrians.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def __init__(
    self,
    label: str,
    *,
    target: Role,
    speed: Annotated[float, RealSet.closed_open(0, oo)] = 1.34,
    direction: npt.ArrayLike = 0.0,
    tau: Annotated[float, RealSet.open(0, oo)] = 0.5,
) -> None:
    super().__init__(label)
    self.target = target
    self.speed = speed
    self.direction = direction
    self.tau = tau

RecruitmentInteraction(label, *, target, citizens, classes=5, recruitment=0.5, retirement=0.1)

Bases: Interaction

Recruitment of security agents in proportion to the mean criminality.

It is a macro-scale external action, a rate of the density x of the security forces that reads the citizens. The security forces grow at the rate recruitment m share_k in efficacy class k, where m is the mean criminality of the citizens, a moment of their distribution, and retire at the rate retirement.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the distribution of the security forces over efficacy.

required
citizens Role

The path of the distribution of citizens.

required
classes Annotated[int, open(0, oo)]

The number of criminality classes.

5
recruitment Annotated[float, closed_open(0, oo)]

The recruitment rate per unit of mean criminality.

0.5
retirement Annotated[float, closed_open(0, oo)]

The retirement rate.

0.1

Examples:

>>> from skcomplex.systems.social.crowds import RecruitmentInteraction
>>> RecruitmentInteraction('recruitment', target='security', citizens='citizens').retirement
0.1
Source code in src/skcomplex/systems/social/crowds/_criminality.py
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
def __init__(
    self,
    label: str,
    *,
    target: Role,
    citizens: Role,
    classes: Annotated[int, RealSet.open(0, oo)] = 5,
    recruitment: Annotated[float, RealSet.closed_open(0, oo)] = 0.5,
    retirement: Annotated[float, RealSet.closed_open(0, oo)] = 0.1,
) -> None:
    super().__init__(label)
    self.target = target
    self.citizens = citizens
    self.classes = classes
    self.recruitment = recruitment
    self.retirement = retirement

SecurityAgentsElement(label, *, x=0.0, share=1.0, size=None, space=None)

Bases: ActivityDensityElement

Density of security agents at one efficacy class.

It holds the share of new recruits that join the class.

Parameters:

Name Type Description Default
label str

The label of the distribution.

required
x Annotated[object, closed_open(0, oo)]

The initial density.

0.0
share Annotated[object, closed_open(0, oo)]

The share of new recruits that join the efficacy class.

1.0
size int | None

The number of classes, or None for one or one per site of the space.

None
space BaseSpace | None

The lattice whose sites are the efficacy classes, or None for none.

None

Examples:

>>> from skcomplex.systems.social.crowds import SecurityAgentsElement
>>> SecurityAgentsElement('security', share=[0.5, 0.3, 0.2], size=3).size
3
Source code in src/skcomplex/systems/social/crowds/_criminality.py
62
63
64
65
66
67
68
69
70
71
72
def __init__(
    self,
    label: str,
    *,
    x: Annotated[object, RealSet.closed_open(0, oo)] = 0.0,
    share: Annotated[object, RealSet.closed_open(0, oo)] = 1.0,
    size: int | None = None,
    space: BaseSpace | None = None,
) -> None:
    super().__init__(label, x=x, size=size, space=space)
    self.share = share

SocialForceSystem(n_walkers=20, *, length=10.0, speed=1.34, random_state=0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

Two crowds walking in opposite directions along a periodic corridor.

The crowds form lanes. The system has the groups rightward and leftward.

Parameters:

Name Type Description Default
n_walkers int

The number of walkers in each direction.

20
length float

The length and the width of the corridor, periodic along its length.

10.0
speed float

The desired speed.

1.34
random_state int | Generator | None

The seed or generator of the initial positions.

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' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.social.crowds import SocialForceSystem
>>> [group.size for group in SocialForceSystem(5).prepare().system_.elements]
[5, 5]
Source code in src/skcomplex/systems/social/crowds/_pedestrians.py
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
def __init__(
    self,
    n_walkers: int = 20,
    *,
    length: float = 10.0,
    speed: float = 1.34,
    random_state: int | np.random.Generator | None = 0,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.n_walkers = n_walkers
    self.length = length
    self.speed = speed
    self.random_state = random_state

SocialRepulsionInteraction(label, *, first, second, cutoff=1.0, strength=2.0, falloff=0.3, radius=0.3)

Bases: Interaction

Repulsion between two walkers of the social force system.

Its force is strength * exp((2 * radius - r) / falloff) along the line between the walkers. It derives from the pair energy strength * falloff * exp((2 * radius - r) / falloff), and it runs once for every pair of walkers of the groups first and second closer than the cutoff, pushing both apart.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
first Role

The path of the group of the first walkers.

required
second Role

The path of the group of the second walkers, which may be the first group.

required
cutoff float

The distance beyond which walkers do not interact.

1.0
strength Annotated[float, closed_open(0, oo)]

The strength of the repulsion.

2.0
falloff Annotated[float, open(0, oo)]

The distance over which the repulsion falls off.

0.3
radius Annotated[float, closed_open(0, oo)]

The radius of a walker.

0.3

Examples:

>>> from skcomplex.systems.social.crowds import SocialRepulsionInteraction
>>> SocialRepulsionInteraction('repulsion', first='walkers', second='walkers').strength
2.0
Source code in src/skcomplex/systems/social/crowds/_pedestrians.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
def __init__(
    self,
    label: str,
    *,
    first: Role,
    second: Role,
    cutoff: float = 1.0,
    strength: Annotated[float, RealSet.closed_open(0, oo)] = 2.0,
    falloff: Annotated[float, RealSet.open(0, oo)] = 0.3,
    radius: Annotated[float, RealSet.closed_open(0, oo)] = 0.3,
) -> None:
    super().__init__(label)
    self.first = first
    self.second = second
    self.cutoff = cutoff
    self.strength = strength
    self.falloff = falloff
    self.radius = radius

WalkingInteraction(label, *, candidate, field, branching=2, depth=2, speeds=3, mobility=0.5)

Bases: EncounterInteraction

Walking of pedestrians to the next room on the way out.

It is an encounter with the exit signs.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
candidate Role

The path of the crowd, a distribution over rooms and speed classes.

required
field Role

The path of the group of one member whose fixed density is the guidance of the exit signs.

required
branching Annotated[int, open(0, oo)]

The number of rooms behind every door of the building, a balanced tree of rooms with the exit in room 0.

2
depth Annotated[int, open(0, oo)]

The number of doors between the exit room and the rooms furthest from it.

2
speeds Annotated[int, open(0, oo)]

The number of speed classes.

3
mobility Annotated[float, closed_open(0, oo)]

The rate at which pedestrians at the free speed leave a room.

0.5

Examples:

>>> from skcomplex.systems.social.crowds import WalkingInteraction
>>> WalkingInteraction('walking', candidate='crowd', field='signs').mobility
0.5
Source code in src/skcomplex/systems/social/crowds/_evacuation.py
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
def __init__(
    self,
    label: str,
    *,
    candidate: Role,
    field: Role,
    branching: Annotated[int, RealSet.open(0, oo)] = 2,
    depth: Annotated[int, RealSet.open(0, oo)] = 2,
    speeds: Annotated[int, RealSet.open(0, oo)] = 3,
    mobility: Annotated[float, RealSet.closed_open(0, oo)] = 0.5,
) -> None:
    super().__init__(label, candidate=candidate, field=field, rate=self._get_rate, kernel=self._get_kernel)
    self.branching = branching
    self.depth = depth
    self.speeds = speeds
    self.mobility = mobility

WallsInteraction(label, *, target, width=10.0, strength=10.0, falloff=0.2, radius=0.3)

Bases: GradientInteraction

Repulsion of walkers by two walls of a corridor at y = 0 and y = width.

It is a GradientInteraction of the position x down the potential strength falloff (exp((radius - y) / falloff) + exp((radius - width + y) / falloff)), so the force per unit mass from a wall at distance d is strength * exp((radius - d) / falloff), pointing away from the wall. It moves the acceleration x'' of a walker, with inertia.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the walker or group of walkers.

required
width Annotated[float, open(0, oo)]

The distance between the walls.

10.0
strength Annotated[float, closed_open(0, oo)]

The strength of the repulsion.

10.0
falloff Annotated[float, open(0, oo)]

The distance over which the repulsion falls off.

0.2
radius Annotated[float, closed_open(0, oo)]

The radius of a walker.

0.3

Examples:

>>> from skcomplex.systems.social.crowds import WallsInteraction
>>> WallsInteraction('walls', target='walkers', width=5.0).width
5.0
Source code in src/skcomplex/systems/social/crowds/_pedestrians.py
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
def __init__(
    self,
    label: str,
    *,
    target: Role,
    width: Annotated[float, RealSet.open(0, oo)] = 10.0,
    strength: Annotated[float, RealSet.closed_open(0, oo)] = 10.0,
    falloff: Annotated[float, RealSet.open(0, oo)] = 0.2,
    radius: Annotated[float, RealSet.closed_open(0, oo)] = 0.3,
) -> None:
    super().__init__(label, target=target, potential=self._get_potential, inertia=True)
    self.width = width
    self.strength = strength
    self.falloff = falloff
    self.radius = radius

building_of(branching, depth)

Return a building whose rooms form a balanced tree.

The exit door is in room 0, the root.

Parameters:

Name Type Description Default
branching int

The number of rooms behind every door of a room further from the exit.

required
depth int

The number of doors between the exit room and the rooms furthest from it.

required

Returns:

Name Type Description
building Graph

The network of rooms, labelled 0 to rooms - 1, whose edges are the doors.

Examples:

>>> from skcomplex.systems.social.crowds import building_of
>>> building_of(2, 2).number_of_nodes()
7
Source code in src/skcomplex/systems/social/crowds/_evacuation.py
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
def building_of(branching: int, depth: int) -> nx.Graph:
    """Return a building whose rooms form a balanced tree.

    The exit door is in room 0, the root.

    Args:
        branching:
            The number of rooms behind every door of a room further from the exit.

        depth:
            The number of doors between the exit room and the rooms furthest from it.

    Returns:
        building:
            The network of rooms, labelled `0` to `rooms - 1`, whose edges are the doors.

    Examples:
        >>> from skcomplex.systems.social.crowds import building_of
        >>> building_of(2, 2).number_of_nodes()
        7
    """
    return nx.balanced_tree(branching, depth)

contagion_table(classes, attraction, redemption)

Return the transition table of encounters between citizens, A[h, k, i].

A citizen of class h who meets a more criminal citizen moves one class up with probability attraction, and one who meets a more honest citizen moves one class down with probability redemption.

Parameters:

Name Type Description Default
classes int

The number of criminality classes.

required
attraction object

The probability that crime attracts, a number or a parameter.

required
redemption object

The probability that honesty redeems, a number or a parameter.

required

Returns:

Name Type Description
table NDArray[object_]

The transition probabilities, which sum to one over the outcomes.

Examples:

>>> from skcomplex.systems.social.crowds import contagion_table
>>> contagion_table(3, 0.3, 0.1)[0, 2].astype(float).tolist()
[0.7, 0.3, 0.0]
Source code in src/skcomplex/systems/social/crowds/_criminality.py
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def contagion_table(classes: int, attraction: object, redemption: object) -> npt.NDArray[np.object_]:
    """Return the transition table of encounters between citizens, `A[h, k, i]`.

    A citizen of class `h` who meets a more criminal citizen moves one class up with probability `attraction`, and one
    who meets a more honest citizen moves one class down with probability `redemption`.

    Args:
        classes:
            The number of criminality classes.

        attraction:
            The probability that crime attracts, a number or a parameter.

        redemption:
            The probability that honesty redeems, a number or a parameter.

    Returns:
        table:
            The transition probabilities, which sum to one over the outcomes.

    Examples:
        >>> from skcomplex.systems.social.crowds import contagion_table
        >>> contagion_table(3, 0.3, 0.1)[0, 2].astype(float).tolist()
        [0.7, 0.3, 0.0]
    """
    table = np.zeros((classes, classes, classes), dtype=object)
    for candidate in range(classes):
        for field in range(classes):
            if field > candidate:
                table[candidate, field, candidate + 1] += attraction
            elif field < candidate:
                table[candidate, field, candidate - 1] += redemption
            table[candidate, field, candidate] += 1 - table[candidate, field].sum()
    return table

criminal_fraction(s, criminality)

Return the fraction of citizens whose criminality is at least one half.

Parameters:

Name Type Description Default
s SimpleNamespace

The namespace of one member of the distribution.

required
criminality NDArray[float64]

The criminality of every class.

required

Returns:

Name Type Description
fraction Expression

The moment of the indicator of the criminal classes, divided by the density.

Source code in src/skcomplex/systems/social/crowds/_criminality.py
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
def criminal_fraction(s: SimpleNamespace, criminality: npt.NDArray[np.float64]) -> Expression:
    """Return the fraction of citizens whose criminality is at least one half.

    Args:
        s:
            The namespace of one member of the distribution.

        criminality:
            The criminality of every class.

    Returns:
        fraction:
            The moment of the indicator of the criminal classes, divided by the density.
    """
    return moment(s.x, 1, (criminality >= CRIMINAL_THRESHOLD).astype(float), normalized=True)

criminality_grid(classes)

Return the criminality of every class of citizens.

It runs from 0 for honest to 1 for criminal.

Parameters:

Name Type Description Default
classes int

The number of criminality classes.

required

Returns:

Name Type Description
criminality NDArray[float64]

The criminality of every class.

Examples:

>>> from skcomplex.systems.social.crowds import criminality_grid
>>> criminality_grid(3).tolist()
[0.0, 0.5, 1.0]
Source code in src/skcomplex/systems/social/crowds/_criminality.py
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
def criminality_grid(classes: int) -> npt.NDArray[np.float64]:
    """Return the criminality of every class of citizens.

    It runs from 0 for honest to 1 for criminal.

    Args:
        classes:
            The number of criminality classes.

    Returns:
        criminality:
            The criminality of every class.

    Examples:
        >>> from skcomplex.systems.social.crowds import criminality_grid
        >>> criminality_grid(3).tolist()
        [0.0, 0.5, 1.0]
    """
    return np.linspace(0.0, 1.0, classes)

criminality_observables(classes, forces)

Return the mean criminality, the criminal fraction, and the security density.

The density is that of the security forces of the system.

Parameters:

Name Type Description Default
classes int

The number of criminality classes.

required
forces int

The number of efficacy classes of the security forces.

required

Returns:

Name Type Description
observables list[Observable]

The observables, each reading the moments of a distribution.

Examples:

>>> from skcomplex.systems.social.crowds import criminality_observables
>>> [item.label for item in criminality_observables(5, 3)]
['mean_criminality', 'criminal_fraction', 'security_forces']
Source code in src/skcomplex/systems/social/crowds/_criminality.py
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
def criminality_observables(classes: int, forces: int) -> list[Observable]:
    """Return the mean criminality, the criminal fraction, and the security density.

    The density is that of the security forces of the system.

    Args:
        classes:
            The number of criminality classes.

        forces:
            The number of efficacy classes of the security forces.

    Returns:
        observables:
            The observables, each reading the moments of a distribution.

    Examples:
        >>> from skcomplex.systems.social.crowds import criminality_observables
        >>> [item.label for item in criminality_observables(5, 3)]
        ['mean_criminality', 'criminal_fraction', 'security_forces']
    """
    criminality = criminality_grid(classes)
    return [
        Observable('mean_criminality', partial(mean_criminality, criminality=criminality), group='citizens'),
        Observable('criminal_fraction', partial(criminal_fraction, criminality=criminality), group='citizens'),
        Observable('security_forces', partial(security_density, forces=forces), group='security'),
    ]

crowding_table(rooms, speeds, step)

Return the rate pattern and the transition table of crowding encounters.

The encounters change the speed class by one step. With step = -1, two pedestrians in the same room meet and the candidate slows down one class, so crowded rooms walk slower. With step = 1, a pedestrian meets the exit signs and speeds up one class.

Parameters:

Name Type Description Default
rooms int

The number of rooms.

required
speeds int

The number of speed classes.

required
step int

-1 to slow down in encounters within a room, 1 to speed up in encounters with the exit signs.

required

Returns:

Name Type Description
pattern NDArray[float64]

The rate pattern, 1 for the encounters that act.

table ...

The transition table.

Examples:

>>> from skcomplex.systems.social.crowds import crowding_table
>>> crowding_table(1, 2, 1)[0].ravel().tolist()
[1.0, 0.0, 0.0, 0.0]
Source code in src/skcomplex/systems/social/crowds/_evacuation.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def crowding_table(rooms: int, speeds: int, step: int) -> tuple[npt.NDArray[np.float64], ...]:
    """Return the rate pattern and the transition table of crowding encounters.

    The encounters change the speed class by one step. With `step = -1`, two pedestrians in the same room meet and the
    candidate slows down one class, so crowded rooms walk slower. With `step = 1`, a pedestrian meets the exit signs and
    speeds up one class.

    Args:
        rooms:
            The number of rooms.

        speeds:
            The number of speed classes.

        step:
            `-1` to slow down in encounters within a room, `1` to speed up in encounters with the exit signs.

    Returns:
        pattern:
            The rate pattern, 1 for the encounters that act.

        table:
            The transition table.

    Examples:
        >>> from skcomplex.systems.social.crowds import crowding_table
        >>> crowding_table(1, 2, 1)[0].ravel().tolist()
        [1.0, 0.0, 0.0, 0.0]
    """
    sites = (rooms + 1) * speeds
    fields = sites if step < 0 else 1
    pattern = np.zeros((sites, fields))
    table = np.zeros((sites, fields, sites))
    for site in range(sites):
        room, speed = divmod(site, speeds)
        outcome = min(max(speed + step, 0), speeds - 1)
        table[site, :, room * speeds + outcome] = 1.0
        if room == rooms or outcome == speed:
            continue
        if step > 0:
            pattern[site, 0] = 1.0
        else:
            pattern[site, room * speeds : (room + 1) * speeds] = 1.0
    return pattern, table

deterrence_table(classes, forces)

Return the rate pattern and the transition table of deterrence encounters.

They are the encounters of citizens with security forces. A citizen of a criminal class h > 0 who meets a security agent of efficacy v_k moves one class down at a rate proportional to v_k. Honest citizens are left alone.

Parameters:

Name Type Description Default
classes int

The number of criminality classes.

required
forces int

The number of efficacy classes of the security forces.

required

Returns:

Name Type Description
pattern NDArray[float64]

The rate pattern eta[h, k], the efficacy v_k for criminal classes and 0 for honest ones.

table NDArray[float64]

The transition table A[h, k, i].

Examples:

>>> from skcomplex.systems.social.crowds import deterrence_table
>>> deterrence_table(2, 2)[0].tolist()
[[0.0, 0.0], [0.5, 1.0]]
Source code in src/skcomplex/systems/social/crowds/_criminality.py
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
def deterrence_table(classes: int, forces: int) -> tuple[npt.NDArray[np.float64], npt.NDArray[np.float64]]:
    """Return the rate pattern and the transition table of deterrence encounters.

    They are the encounters of citizens with security forces. A citizen of a criminal class `h > 0` who meets a security
    agent of efficacy `v_k` moves one class down at a rate proportional to `v_k`. Honest citizens are left alone.

    Args:
        classes:
            The number of criminality classes.

        forces:
            The number of efficacy classes of the security forces.

    Returns:
        pattern:
            The rate pattern `eta[h, k]`, the efficacy `v_k` for criminal classes and 0 for honest ones.

        table:
            The transition table `A[h, k, i]`.

    Examples:
        >>> from skcomplex.systems.social.crowds import deterrence_table
        >>> deterrence_table(2, 2)[0].tolist()
        [[0.0, 0.0], [0.5, 1.0]]
    """
    efficacy = np.linspace(1.0 / forces, 1.0, forces)
    pattern = np.zeros((classes, forces))
    pattern[1:] = efficacy
    table = np.zeros((classes, forces, classes))
    for candidate in range(classes):
        table[candidate, :, max(candidate - 1, 0)] = 1.0
    return pattern, table

evacuated_fraction(s, rooms, speeds)

Return the fraction of the crowd that has left the building.

Parameters:

Name Type Description Default
s SimpleNamespace

The namespace of one site of the crowd.

required
rooms int

The number of rooms.

required
speeds int

The number of speed classes.

required

Returns:

Name Type Description
fraction Expression

The moment of the indicator of the outside sites, divided by the density.

Source code in src/skcomplex/systems/social/crowds/_evacuation.py
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
def evacuated_fraction(s: SimpleNamespace, rooms: int, speeds: int) -> Expression:
    """Return the fraction of the crowd that has left the building.

    Args:
        s:
            The namespace of one site of the crowd.

        rooms:
            The number of rooms.

        speeds:
            The number of speed classes.

    Returns:
        fraction:
            The moment of the indicator of the outside sites, divided by the density.
    """
    outside = (np.arange((rooms + 1) * speeds) >= rooms * speeds).astype(float)
    return moment(s.x, 1, outside, normalized=True)

evacuation_observables(rooms, speeds)

Return the evacuated fraction and the mean walking speed of a crowd crowd.

Parameters:

Name Type Description Default
rooms int

The number of rooms.

required
speeds int

The number of speed classes.

required

Returns:

Name Type Description
observables list[Observable]

The observables, each reading the moments of the crowd.

Examples:

>>> from skcomplex.systems.social.crowds import evacuation_observables
>>> [item.label for item in evacuation_observables(7, 3)]
['evacuated', 'mean_speed']
Source code in src/skcomplex/systems/social/crowds/_evacuation.py
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
def evacuation_observables(rooms: int, speeds: int) -> list[Observable]:
    """Return the evacuated fraction and the mean walking speed of a crowd `crowd`.

    Args:
        rooms:
            The number of rooms.

        speeds:
            The number of speed classes.

    Returns:
        observables:
            The observables, each reading the moments of the crowd.

    Examples:
        >>> from skcomplex.systems.social.crowds import evacuation_observables
        >>> [item.label for item in evacuation_observables(7, 3)]
        ['evacuated', 'mean_speed']
    """
    return [
        Observable('evacuated', partial(evacuated_fraction, rooms=rooms, speeds=speeds), group='crowd'),
        Observable('mean_speed', partial(mean_speed, rooms=rooms, speeds=speeds), group='crowd'),
    ]

mean_criminality(s, criminality)

Return the mean criminality of a distribution of citizens.

Parameters:

Name Type Description Default
s SimpleNamespace

The namespace of one member of the distribution.

required
criminality NDArray[float64]

The criminality of every class.

required

Returns:

Name Type Description
mean Expression

The normalized first moment.

Source code in src/skcomplex/systems/social/crowds/_criminality.py
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
def mean_criminality(s: SimpleNamespace, criminality: npt.NDArray[np.float64]) -> Expression:
    """Return the mean criminality of a distribution of citizens.

    Args:
        s:
            The namespace of one member of the distribution.

        criminality:
            The criminality of every class.

    Returns:
        mean:
            The normalized first moment.
    """
    return moment(s.x, 1, criminality, normalized=True)

mean_speed(s, rooms, speeds)

Return the mean walking speed of the crowd, as a fraction of the free speed.

Parameters:

Name Type Description Default
s SimpleNamespace

The namespace of one site of the crowd.

required
rooms int

The number of rooms.

required
speeds int

The number of speed classes.

required

Returns:

Name Type Description
speed Expression

The normalized first moment of the speeds of the sites.

Source code in src/skcomplex/systems/social/crowds/_evacuation.py
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
def mean_speed(s: SimpleNamespace, rooms: int, speeds: int) -> Expression:
    """Return the mean walking speed of the crowd, as a fraction of the free speed.

    Args:
        s:
            The namespace of one site of the crowd.

        rooms:
            The number of rooms.

        speeds:
            The number of speed classes.

    Returns:
        speed:
            The normalized first moment of the speeds of the sites.
    """
    return moment(s.x, 1, np.tile(walking_speeds(speeds), rooms + 1), normalized=True)

next_rooms(building, exit_room)

Return the next room on the shortest path to the exit room from every room.

For the exit room it is the outside.

Parameters:

Name Type Description Default
building Graph

The network of rooms, labelled 0 to rooms - 1, whose edges are the doors.

required
exit_room int

The room whose door leads outside.

required

Returns:

Name Type Description
next_rooms list[int]

The next room of every room, with rooms standing for the outside.

Examples:

>>> import networkx as nx
>>> from skcomplex.systems.social.crowds import next_rooms
>>> next_rooms(nx.path_graph(3), 0)
[3, 0, 1]
Source code in src/skcomplex/systems/social/crowds/_evacuation.py
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
def next_rooms(building: nx.Graph, exit_room: int) -> list[int]:
    """Return the next room on the shortest path to the exit room from every room.

    For the exit room it is the outside.

    Args:
        building:
            The network of rooms, labelled `0` to `rooms - 1`, whose edges are the doors.

        exit_room:
            The room whose door leads outside.

    Returns:
        next_rooms:
            The next room of every room, with `rooms` standing for the outside.

    Examples:
        >>> import networkx as nx
        >>> from skcomplex.systems.social.crowds import next_rooms
        >>> next_rooms(nx.path_graph(3), 0)
        [3, 0, 1]
    """
    paths = nx.shortest_path(building, target=exit_room)
    rooms = building.number_of_nodes()
    return [rooms if room == exit_room else int(paths[room][1]) for room in range(rooms)]

security_density(s, forces)

Return the density of the security forces.

Parameters:

Name Type Description Default
s SimpleNamespace

The namespace of one member of the distribution of the security forces.

required
forces int

The number of efficacy classes.

required

Returns:

Name Type Description
density Expression

The order-0 moment.

Source code in src/skcomplex/systems/social/crowds/_criminality.py
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
def security_density(s: SimpleNamespace, forces: int) -> Expression:
    """Return the density of the security forces.

    Args:
        s:
            The namespace of one member of the distribution of the security forces.

        forces:
            The number of efficacy classes.

    Returns:
        density:
            The order-0 moment.
    """
    return moment(s.x, 0, np.arange(forces, dtype=float))

walking_speeds(speeds)

Return the walking speed of every speed class.

The speeds are evenly spaced from a quarter of the free speed to the free speed.

Parameters:

Name Type Description Default
speeds int

The number of speed classes.

required

Returns:

Name Type Description
speeds NDArray[float64]

The walking speed of every class, as a fraction of the free speed.

Examples:

>>> from skcomplex.systems.social.crowds import walking_speeds
>>> walking_speeds(4).tolist()
[0.25, 0.5, 0.75, 1.0]
Source code in src/skcomplex/systems/social/crowds/_evacuation.py
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
def walking_speeds(speeds: int) -> npt.NDArray[np.float64]:
    """Return the walking speed of every speed class.

    The speeds are evenly spaced from a quarter of the free speed to the free speed.

    Args:
        speeds:
            The number of speed classes.

    Returns:
        speeds:
            The walking speed of every class, as a fraction of the free speed.

    Examples:
        >>> from skcomplex.systems.social.crowds import walking_speeds
        >>> walking_speeds(4).tolist()
        [0.25, 0.5, 0.75, 1.0]
    """
    return np.linspace(0.25, 1.0, speeds)

walking_table(building, exit_room, speeds)

Return the rate pattern and the transition table of walking to the next room.

The walking is an encounter with exit signs. The sites of the crowd are indexed by room and speed class in row-major order, with one last row of sites for the outside. A pedestrian of speed v in a room walks to the next room on the way to the exit at the rate v.

Parameters:

Name Type Description Default
building Graph

The network of rooms, labelled 0 to rooms - 1.

required
exit_room int

The room whose door leads outside.

required
speeds int

The number of speed classes.

required

Returns:

Name Type Description
pattern NDArray[float64]

The rate pattern eta[h, 0], the walking speed of the site, 0 outside.

table ...

The transition table A[h, 0, i].

Examples:

>>> import networkx as nx
>>> from skcomplex.systems.social.crowds import walking_table
>>> pattern, table = walking_table(nx.path_graph(2), 0, 2)
>>> pattern.ravel().tolist()
[0.25, 1.0, 0.25, 1.0, 0.0, 0.0]
Source code in src/skcomplex/systems/social/crowds/_evacuation.py
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
def walking_table(building: nx.Graph, exit_room: int, speeds: int) -> tuple[npt.NDArray[np.float64], ...]:
    """Return the rate pattern and the transition table of walking to the next room.

    The walking is an encounter with exit signs. The sites of the crowd are indexed by room and speed class in row-major
    order, with one last row of sites for the outside. A pedestrian of speed `v` in a room walks to the next room on the
    way to the exit at the rate `v`.

    Args:
        building:
            The network of rooms, labelled `0` to `rooms - 1`.

        exit_room:
            The room whose door leads outside.

        speeds:
            The number of speed classes.

    Returns:
        pattern:
            The rate pattern `eta[h, 0]`, the walking speed of the site, 0 outside.

        table:
            The transition table `A[h, 0, i]`.

    Examples:
        >>> import networkx as nx
        >>> from skcomplex.systems.social.crowds import walking_table
        >>> pattern, table = walking_table(nx.path_graph(2), 0, 2)
        >>> pattern.ravel().tolist()
        [0.25, 1.0, 0.25, 1.0, 0.0, 0.0]
    """
    following = next_rooms(building, exit_room)
    velocity = walking_speeds(speeds)
    sites = (len(following) + 1) * speeds
    pattern = np.zeros((sites, 1))
    table = np.zeros((sites, 1, sites))
    for site in range(sites):
        room, speed = divmod(site, speeds)
        if room == len(following):
            table[site, 0, site] = 1.0
            continue
        pattern[site, 0] = velocity[speed]
        table[site, 0, following[room] * speeds + speed] = 1.0
    return pattern, table