Collective behaviour
Collective behaviour of animals and people.
Animals and people coordinate through signals, trails, and contact.
ActiveWalkersSystem(size=30, *, n_walkers=20, attraction=1.0, T=200.0, random_state=0, label='system', solver='auto', backend='auto')
Bases: BaseSystem
Active walkers who wear a system of trails into a green.
They walk between the corners of the green.
Walkers prefer existing trails to the direct way, and trails fade unless they are used, so a few shared trails
emerge that bundle the ways between the corners, a compromise between short and comfortable routes. The system has
the walkers people/walkers, the ground structure land/ground.G, and the trail potential land/ground.V.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
The number of rows and columns of the green. |
30
|
n_walkers
|
int
|
The number of walkers. |
20
|
attraction
|
float
|
How strongly trails draw the walkers. |
1.0
|
T
|
float
|
The durability of trails. |
200.0
|
random_state
|
int | Generator | None
|
The seed or generator of the starting corners and goals. |
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.biology.collective_behaviour import ActiveWalkersSystem
>>> [child.label for child in ActiveWalkersSystem().prepare().system_.systems]
['people', 'land']
Source code in src/skcomplex/systems/biology/collective_behaviour/_trails.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |
AlarmInteraction(label, *, calm, nervous, spontaneous=0.01, contagion=2.0, stochastic=True)
Bases: ReactionInteraction
Calm rats that turn nervous on their own or by meeting nervous rats.
They turn nervous at the alarm propensity.
A calm rat turns nervous at rate spontaneous + contagion * nervous / N, where N is the total number of rats, so
the propensity of the reaction calm to nervous is that rate times the calm rats. It is a ReactionInteraction that
moves one rat from the calm to the nervous rats, a rate equation, or in stochastic mode a random event per rat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
calm
|
Role
|
The path of the calm rats. |
required |
nervous
|
Role
|
The path of the nervous rats. |
required |
spontaneous
|
Annotated[float, closed_open(0, oo)]
|
The rate at which a calm rat turns nervous on its own. |
0.01
|
contagion
|
Annotated[float, closed_open(0, oo)]
|
The rate at which a calm rat turns nervous when all others are nervous. |
2.0
|
stochastic
|
bool
|
Whether changes are random events. |
True
|
Examples:
>>> from skcomplex.core.systems import System
>>> from skcomplex.systems.biology.collective_behaviour import AlarmInteraction, RatsElement
>>> alarm = AlarmInteraction('alarm', calm='calm', nervous='nervous', stochastic=False)
>>> rats = [RatsElement('calm', x=90.0), RatsElement('nervous', x=10.0)]
>>> system = System(elements=rats, interactions=[alarm])
>>> trajectory = system.prepare().simulate(until=5.0, dt=0.1)
>>> round(float(trajectory.get_values('calm.x')[-1] + trajectory.get_values('nervous.x')[-1]), 6)
100.0
Source code in src/skcomplex/systems/biology/collective_behaviour/_decisions.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
AntColonyOptimizationSystem(n_points=40, *, ants=20, steps=80, alpha=0.8, beta=1.0, rho=0.5, Q=1.0, cut=None, seed=0, label='system', solver='auto', backend='auto')
Bases: BaseSystem
Ants that find the shortest path between two points of a Delaunay road network.
The ants search round after round.
Every round the ants walk from the point nearest one corner of the unit square towards the point nearest the
opposite corner, choosing segments by their pheromone and length, and mark the paths they find with pheromone in
inverse proportion to their length, while all pheromone evaporates. Short paths gather pheromone fastest, so the
colony converges on a short path, usually the shortest, as in exercise 15.7 of Argun et al. (2021). Cutting the best
path at the round cut makes the colony explore again and settle on a new route, exercise 15.12. The system has the
pheromone roads.tau and the open segments roads.passable of the agent group roads, and the shortest path of
every round search.shortest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_points
|
int
|
The number of points of the network. |
40
|
ants
|
int
|
The number of ants of a round. |
20
|
steps
|
int
|
The largest number of steps of an ant in a round. |
80
|
alpha
|
float
|
The exponent of the pheromone. |
0.8
|
beta
|
float
|
The exponent of the weight |
1.0
|
rho
|
float
|
The evaporation rate. |
0.5
|
Q
|
float
|
The pheromone an ant lays along a path. |
1.0
|
cut
|
int | None
|
The round at which the best path is cut, or |
None
|
seed
|
int | None
|
The seed of the points. |
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.biology.collective_behaviour import AntColonyOptimizationSystem
>>> AntColonyOptimizationSystem(10).prepare().system_.interactions[0].ants
20
Source code in src/skcomplex/systems/biology/collective_behaviour/_colony_optimization.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | |
AntElement(label, *, carrying=0.0, arrived=0.0, nest_x=5.0, nest_y=10.0, food_x=34.0, food_y=10.0, food_radius=3.0, attraction=5.0, outward=0.5, deposit=1.0, values=None, size=None, space=None)
Bases: Element
Ant that searches for food guided by pheromone and carries food home.
It marks the way home with pheromone.
A searching ant moves to a neighbouring cell with a probability that grows with its pheromone and, by outward,
with its distance from the nest, as a scent of the nest would, and picks up food within a radius of the food. A
carrying ant walks straight home, deposits pheromone on every cell it enters, and drops the food at the nest. The
system adds the walk home, the deposit, the search, the pick-up and drop, and the arrival as member interactions,
and an ant picks up or drops food the step after it arrives.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the agent or group. |
required |
carrying
|
Annotated[float, FiniteEnumeratedSet([0, 1])]
|
Whether the ant carries food. |
0.0
|
arrived
|
Annotated[float, FiniteEnumeratedSet([0, 1])]
|
Whether the ant stood at the nest with food, or near the food without it, at the end of the previous step. |
0.0
|
nest_x
|
float
|
The column of the nest. |
5.0
|
nest_y
|
float
|
The row of the nest. |
10.0
|
food_x
|
float
|
The column of the food. |
34.0
|
food_y
|
float
|
The row of the food. |
10.0
|
food_radius
|
Annotated[float, closed_open(0, oo)]
|
The distance from the food within which ants pick it up. |
3.0
|
attraction
|
Annotated[float, closed_open(0, oo)]
|
How strongly pheromone draws searching ants. |
5.0
|
outward
|
Annotated[float, closed_open(0, oo)]
|
How strongly searching ants move away from the nest. |
0.5
|
deposit
|
Annotated[float, closed_open(0, oo)]
|
The pheromone an ant deposits per step home. |
1.0
|
values
|
Mapping[str, object] | None
|
Further values by name, such as the cells |
None
|
size
|
int | None
|
The number of ants, or |
None
|
space
|
BaseSpace | None
|
The grid the ants walk on, whose axes |
None
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import AntElement
>>> AntElement('ants', size=5).size
5
Source code in src/skcomplex/systems/biology/collective_behaviour/_foraging.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
BinaryAntDecisionSystem(*, flux=1.0, k=20.0, h=2.0, evaporation=0.0, stochastic=True, label='system', solver='auto', backend='auto')
Bases: BaseSystem
Ants that choose between two equal branches to a food source.
The ants follow the marks of earlier ants.
Every ant reinforces the branch it takes, so the first random fluctuations grow and most ants end up on one branch,
the symmetry breaking of the double bridge experiment. The deterministic mode stays symmetric. The system has the
marks left.x and right.x.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flux
|
float
|
The rate at which ants arrive at the fork. |
1.0
|
k
|
float
|
The attractiveness of an unmarked branch. |
20.0
|
h
|
float
|
The nonlinearity of the choice. |
2.0
|
evaporation
|
float
|
The rate at which each mark evaporates. |
0.0
|
stochastic
|
bool
|
Whether ants and evaporation are random events. |
True
|
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.biology.collective_behaviour import BinaryAntDecisionSystem
>>> [branch.label for branch in BinaryAntDecisionSystem().prepare().system_.elements]
['left', 'right']
Source code in src/skcomplex/systems/biology/collective_behaviour/_decisions.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | |
BirdSwarmSystem(n_birds=30, *, alpha=1.0, beta=0.5, C_a=1.0, l_a=2.0, C_r=2.0, l_r=0.5, random_state=0, label='system', solver='auto', backend='auto')
Bases: BaseSystem
A swarm of self-propelled birds held together by a pair potential.
The potential attracts far and repels near.
Every bird is driven to the speed sqrt(alpha / beta) and feels the Morse-type potential of every other bird, so
the swarm stays together and organizes into a flock or a rotating mill. The system has the positions birds.x and
the velocities birds.x'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_birds
|
int
|
The number of birds. |
30
|
alpha
|
float
|
The propulsion. |
1.0
|
beta
|
float
|
The friction at high speed. |
0.5
|
C_a
|
float
|
The strength of the attraction. |
1.0
|
l_a
|
float
|
The range of the attraction. |
2.0
|
C_r
|
float
|
The strength of the repulsion. |
2.0
|
l_r
|
float
|
The range of the repulsion. |
0.5
|
random_state
|
int | Generator | None
|
The seed or generator of the initial positions and velocities. |
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.biology.collective_behaviour import BirdSwarmSystem
>>> BirdSwarmSystem(5).prepare().system_.elements[0].size
5
Source code in src/skcomplex/systems/biology/collective_behaviour/_flocks.py
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 | |
BoidElement(label, *, x=0.0, y=0.0, radius=5.0, separation_radius=1.0, cohesion=0.01, separation=0.1, alignment=0.1, min_speed=0.5, max_speed=1.0, width=30.0, height=30.0, values=None, size=None, space=None)
Bases: Element
Boid of Reynolds, a bird that steers by three rules.
The rules steer it toward, away from, and along its neighbours.
Every step a boid sees the boids within radius. FlockingInteraction steers it by cohesion toward their center,
by separation away from those closer than separation_radius, and by alignment toward their mean velocity, with
its speed between min_speed and max_speed, and FlightInteraction moves it on a torus of side width by
height. Its position is x and y, and its velocity "x'" and "y'", which its rules read and change as
agent.get_value("x'") and agent.set_value("x'", value).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the agent or group. |
required |
x
|
float | Array
|
The initial horizontal position. |
0.0
|
y
|
float | Array
|
The initial vertical position. |
0.0
|
radius
|
Annotated[float, open(0, oo)]
|
The distance within which a boid sees others. |
5.0
|
separation_radius
|
Annotated[float, closed_open(0, oo)]
|
The distance within which a boid avoids others. |
1.0
|
cohesion
|
Annotated[float, closed_open(0, oo)]
|
The weight of cohesion. |
0.01
|
separation
|
Annotated[float, closed_open(0, oo)]
|
The weight of separation. |
0.1
|
alignment
|
Annotated[float, closed_open(0, oo)]
|
The weight of alignment. |
0.1
|
min_speed
|
Annotated[float, closed_open(0, oo)]
|
The smallest speed. |
0.5
|
max_speed
|
Annotated[float, open(0, oo)]
|
The largest speed. |
1.0
|
width
|
Annotated[float, open(0, oo)]
|
The width of the torus. |
30.0
|
height
|
Annotated[float, open(0, oo)]
|
The height of the torus. |
30.0
|
values
|
Mapping[str, object] | None
|
Further values by name, such as the velocity |
None
|
size
|
int | None
|
The number of boids, or |
None
|
space
|
BaseSpace | None
|
The space of the boids, or |
None
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import BoidElement
>>> BoidElement('flock', x=5.0, y=5.0, values={"x'": 1.0, "y'": 0.0}, size=10).size
10
Source code in src/skcomplex/systems/biology/collective_behaviour/_flocks.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
Boids3dSystem(n_boids=50, *, size=20.0, radius=6.0, cohesion=0.01, separation=0.1, alignment=0.1, random_state=0, label='system', solver='auto', backend='auto')
Bases: BaseSystem
Boids in three dimensions, birds that flock by local rules in a periodic cube.
The boids start at random positions with random headings. Every step a boid steers by the boids within radius and
then flies along its velocity, so with alignment alone the flock turns to one heading and keeps its spread in depth.
The system has the group flock and the velocities "x'", "y'", and "z'" of its boids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_boids
|
int
|
The number of boids. |
50
|
size
|
float
|
The side of the periodic cube. |
20.0
|
radius
|
float
|
The distance within which a boid sees others. |
6.0
|
cohesion
|
float
|
The weight of cohesion. |
0.01
|
separation
|
float
|
The weight of separation. |
0.1
|
alignment
|
float
|
The weight of alignment. |
0.1
|
random_state
|
int | Generator | None
|
The seed or generator of the initial positions and headings. |
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.biology.collective_behaviour import Boids3dSystem
>>> Boids3dSystem(10).prepare().system_.space.extent
(20.0, 20.0, 20.0)
Source code in src/skcomplex/systems/biology/collective_behaviour/_flocks.py
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 | |
BoidsSystem(n_boids=40, *, size=30.0, radius=5.0, cohesion=0.01, separation=0.1, alignment=0.1, random_state=0, label='system', solver='auto', backend='auto')
Bases: BaseSystem
Reynolds' boids in two dimensions.
They are birds that flock by local rules without a leader.
The boids start at random positions with random headings on a torus. Alignment makes neighbours fly in the same
direction and cohesion pulls them together, so the flock becomes polarized while separation keeps the birds apart.
The system has the group flock and the velocities "x'" and "y'" of its boids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_boids
|
int
|
The number of boids. |
40
|
size
|
float
|
The side of the square torus. |
30.0
|
radius
|
float
|
The distance within which a boid sees others. |
5.0
|
cohesion
|
float
|
The weight of cohesion. |
0.01
|
separation
|
float
|
The weight of separation. |
0.1
|
alignment
|
float
|
The weight of alignment. |
0.1
|
random_state
|
int | Generator | None
|
The seed or generator of the initial positions and headings. |
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.biology.collective_behaviour import BoidsSystem
>>> BoidsSystem(10).prepare().system_.elements[0].size
10
Source code in src/skcomplex/systems/biology/collective_behaviour/_flocks.py
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 | |
BranchElement(label, *, x=0.0)
Bases: CompartmentElement
Branch of a path that ants can take, holding its number x of pheromone marks.
The marks are the ones left by the ants that took the branch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the branch. |
required |
x
|
Annotated[float, closed_open(0, oo)]
|
The initial number of marks. |
0.0
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import BranchElement
>>> BranchElement('left', x=5.0).x
5.0
Source code in src/skcomplex/systems/biology/collective_behaviour/_decisions.py
44 45 46 47 48 49 50 | |
ClampSpec(max_angle=None, min_norm=None, max_norm=None)
dataclass
Bases: BaseSpec
Clamp of the change of a vector.
It bounds the largest angle the vector turns and its norm. Every bound is a number, the name of a parameter of the
agent, or None for no bound. The vector turns from its current direction toward its new one by at most
max_angle, keeping the new norm, and its norm is then clipped between min_norm and max_norm. A zero vector
stays zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_angle
|
float | str | None
|
The largest angle in radians the vector turns, or |
None
|
min_norm
|
float | str | None
|
The smallest norm, or |
None
|
max_norm
|
float | str | None
|
The largest norm, or |
None
|
Raises:
| Type | Description |
|---|---|
ValidationError
|
A bound is negative, raised when its owner checks it. |
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import ClampSpec
>>> ClampSpec(min_norm=0.5, max_norm='speed').max_norm
'speed'
ColonySearchInteraction(label, *, segments, edges=(), lengths=(), start=0, target=1, ants=20, steps=80, alpha=0.8, beta=1.0, rho=0.5, Q=1.0, cut=None)
Bases: Interaction
Rounds of ants that walk a road network by pheromone and length.
The ants mark the paths they find, and the pheromone fades.
In every round, following Argun et al. (2021, section 15.2), ants ants start at the vertex start and take up to
steps steps. At vertex i an ant moves to the neighbour j with the probability tau_ij^alpha w_ij^beta / sum_k
tau_ik^alpha w_ik^beta, with the weight w_ij = 1 / d_ij of the length d_ij, Eq. (15.2), and stops at the vertex
target. The path of every ant that arrives is cleared of its loops, and every segment of it receives the pheromone
Q / L of its length L, Eq. (15.3), after the pheromone of every segment has evaporated by the factor 1 - rho,
Eq. (15.4). At the round cut, two segments near the middle of the shortest path found so far are cut, as long as
the network stays connected, and the colony must heal the disruption. The interaction reports the length of the
shortest path of every round as shortest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
segments
|
Role
|
The path of the group of segments. |
required |
edges
|
Sequence[tuple[int, int]]
|
The two ends of every segment, in the order of the group. |
()
|
lengths
|
Sequence[float]
|
The length of every segment. |
()
|
start
|
int
|
The vertex the ants start from. |
0
|
target
|
int
|
The vertex the ants look for. |
1
|
ants
|
int
|
The number of ants of a round. |
20
|
steps
|
int
|
The largest number of steps of an ant in a round. |
80
|
alpha
|
float
|
The exponent of the pheromone. |
0.8
|
beta
|
float
|
The exponent of the weight. |
1.0
|
rho
|
float
|
The evaporation rate. |
0.5
|
Q
|
float
|
The pheromone an ant lays along a path. |
1.0
|
cut
|
int | None
|
The round at which the shortest path found is cut, or |
None
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import ColonySearchInteraction
>>> ColonySearchInteraction('search', segments='roads', edges=((0, 1),), lengths=(1.0,), target=1).ants
20
Source code in src/skcomplex/systems/biology/collective_behaviour/_colony_optimization.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
search(system)
Run one round of ants and update the pheromone.
At the round cut, the round also cuts the best path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
SystemView
|
The system. |
required |
Source code in src/skcomplex/systems/biology/collective_behaviour/_colony_optimization.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
CouzinLeadershipSystem(n_fish=50, *, informed=0.1, omega=0.5, preferred=0.0, side=100.0, random_state=0, label='system', solver='auto', backend='auto')
Bases: BaseSystem
The Couzin shoal, in which a small informed minority leads a group.
The group does not know who is informed.
The fish start in a disk of radius 5 at the center of a torus with random headings, and a fraction informed of
them prefers the direction at the angle preferred with the weight omega. Repulsion, attraction, and alignment
keep the shoal together, and the informed fish pull its mean heading toward their direction. The accuracy, the
closeness of the group's heading to the preferred direction, grows with the size of the minority, and a minority of
about 10% already steers a group of 50. The system has the group shoal, the unit headings vx and vy of its
fish, and the weight omega of each, positive for the informed ones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_fish
|
int
|
The number of fish. |
50
|
informed
|
float
|
The fraction of informed fish. |
0.1
|
omega
|
float
|
The weight of the preferred direction of the informed fish. |
0.5
|
preferred
|
float
|
The angle of the preferred direction. |
0.0
|
side
|
float
|
The side of the square torus. |
100.0
|
random_state
|
int | Generator | None
|
The seed or generator of the initial positions and headings. |
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.biology.collective_behaviour import CouzinLeadershipSystem
>>> int((CouzinLeadershipSystem(20, informed=0.2).prepare().system_.elements[0].omega > 0).sum())
4
Source code in src/skcomplex/systems/biology/collective_behaviour/_leadership.py
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |
EncirclementInteraction(label, *, target, R=3.0, kappa_4=4.0, l_3=1.0)
Bases: Interaction
Four-body alignment of an agent with triples of neighbours that surround it.
An agent i gains from every triple j < k < l of its neighbours within R the acceleration
kappa_4 g_ij g_ik g_il (1 - |r_hat_ij + r_hat_ik + r_hat_il|^2 / 9) (u_ij + u_ik + u_il) / 3, where r_hat_ij
is the unit vector from i to j, u_ij the velocity of j relative to i, and
g(rho) = (1 - rho^2 / R^2)^2 exp(-rho / l_3) the weight of a neighbour at the distance rho. Three neighbours
spread evenly around the agent weigh most.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
target
|
Role
|
The path of the group of agents, whose position |
required |
R
|
float
|
The radius of the neighbourhood and of the cutoff. |
3.0
|
kappa_4
|
float
|
The strength of the encirclement alignment. |
4.0
|
l_3
|
Annotated[float, open(0, oo)]
|
The range of the weight of a neighbour. |
1.0
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import EncirclementInteraction
>>> EncirclementInteraction('encirclement', target='agents').kappa_4
4.0
Source code in src/skcomplex/systems/biology/collective_behaviour/_many_body_swarms.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
FlankingInteraction(label, *, target, R=3.0, kappa_v=2.0, l_3=1.0)
Bases: Interaction
Three-body alignment of an agent with pairs of neighbours that flank it.
An agent i gains from every pair j < k of its neighbours within R the acceleration
kappa_v g_ij g_ik (1 - r_hat_ij . r_hat_ik) / 2 (u_ij + u_ik) / 2, where r_hat_ij is the unit vector from i
to j, u_ij the velocity of j relative to i, and g(rho) = (1 - rho^2 / R^2)^2 exp(-rho / l_3) the weight of
a neighbour at the distance rho. Two neighbours on opposite sides weigh most, and two on one side not at all.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
target
|
Role
|
The path of the group of agents, whose position |
required |
R
|
float
|
The radius of the neighbourhood and of the cutoff. |
3.0
|
kappa_v
|
float
|
The strength of the flanking alignment. |
2.0
|
l_3
|
Annotated[float, open(0, oo)]
|
The range of the weight of a neighbour. |
1.0
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import FlankingInteraction
>>> FlankingInteraction('flanking', target='agents').kappa_v
2.0
Source code in src/skcomplex/systems/biology/collective_behaviour/_many_body_swarms.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
FlightInteraction(label, *, target, probability=1.0)
Bases: MovementInteraction
Flight of every boid by its velocity ("x'", "y'") in one step.
The boids fly on the torus of side width by height.
Every boid moves in its own activation, after the rules and member interactions listed before it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction, unique among the components of its system. |
required |
target
|
Role
|
The path of the group of boids. |
required |
probability
|
Annotated[float, closed(0, 1)]
|
The probability that a boid moves in a step. |
1.0
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import FlightInteraction
>>> FlightInteraction('flight', target='flock').schedule
'member'
Source code in src/skcomplex/systems/biology/collective_behaviour/_flocks.py
278 279 280 281 282 283 284 285 | |
FlockingInteraction(label, *, target, phase=None)
Bases: ZoneCouplingInteraction
Reynolds' steering of every boid by the boids it sees, with a bounded speed.
It steers a boid toward, away from, and along those boids.
The velocity of a boid gains the mean offset to the boids within radius weighted by cohesion, loses the summed
offset to the boids within separation_radius weighted by separation, and gains the mean difference of their
velocities from its own weighted by alignment. Its speed is then clipped between min_speed and max_speed.
Offsets are the shortest ones on the torus of side width by height. It is the agent form of coupling over two
radii of one priority, with a clamp of the norm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction, unique among the components of its system. |
required |
target
|
Role
|
The path of the group of boids. |
required |
phase
|
str | None
|
The phase of the step the steering runs in, or |
None
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import FlockingInteraction
>>> [spec.reduce for spec in FlockingInteraction('flocking', target='flock').radii]
['mean', 'sum', 'mean']
Source code in src/skcomplex/systems/biology/collective_behaviour/_flocks.py
327 328 329 330 331 332 333 334 335 336 337 338 339 | |
GroundElement(label, *, G=0.0, V=0.0, T=200.0, D=0.5, tau=1.0, space=None)
Bases: Element
Ground whose structure G is raised by footprints and recovers.
A trail potential V smooths the structure.
The structure relaxes back to bare ground as G' = -G / T, and walkers add footprints. The trail potential
follows V' = D ΔV + (G - V) / tau, so it settles to the structure smoothed over a length sqrt(D tau), the
distance from which walkers notice a trail. The element holds the states and their parameters, and the system adds
the fading, the diffusion, and the relaxation as interactions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the ground or the lattice field. |
required |
G
|
Annotated[float | Array, closed_open(0, oo)]
|
The initial ground structure. |
0.0
|
V
|
Annotated[float | Array, closed_open(0, oo)]
|
The initial trail potential. |
0.0
|
T
|
Annotated[float, open(0, oo)]
|
The durability of trails, the time over which they fade. |
200.0
|
D
|
Annotated[float, closed_open(0, oo)]
|
The diffusion of the trail potential. |
0.5
|
tau
|
Annotated[float, open(0, oo)]
|
The relaxation time of the trail potential. |
1.0
|
space
|
BaseSpace | None
|
The lattice whose cells hold the ground, or |
None
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import GroundElement
>>> GroundElement('ground').T
200.0
Source code in src/skcomplex/systems/biology/collective_behaviour/_trails.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
HeadingInteraction(label, *, target, phase=None)
Bases: Interaction
Steering of the velocity of every boid by cohesion, separation, and alignment.
The speed stays bounded.
The velocity of a boid gains the mean offset to the boids within radius weighted by cohesion, loses the summed
offset to the boids within separation_radius weighted by separation, and gains the mean difference of their
velocities from its own weighted by alignment. Its speed is then clipped between min_speed and max_speed.
Offsets are the shortest ones of the space. Every boid steers in its own activation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction, unique among the components of its system. |
required |
target
|
Role
|
The path of the group of boids. |
required |
phase
|
str | None
|
The phase of the step the steering runs in, or |
None
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import HeadingInteraction
>>> HeadingInteraction('heading', target='flock').target
'flock'
Source code in src/skcomplex/systems/biology/collective_behaviour/_flocks.py
390 391 392 393 | |
steer(agent, system)
Steer a boid by the boids it sees, with its speed clipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
SpatialBoidView
|
The boid. |
required |
system
|
SystemView
|
The system. |
required |
Source code in src/skcomplex/systems/biology/collective_behaviour/_flocks.py
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | |
HerdedSheepElement(label, *, hx=0.0, hy=0.0, values=None, size=None, space=None)
Bases: Element
Sheep of a herded flock, an agent with a heading.
A shepherding interaction moves it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the sheep or of the flock. |
required |
hx
|
float | Array
|
The first component of the heading of the last move. |
0.0
|
hy
|
float | Array
|
The second component of the heading of the last move. |
0.0
|
values
|
Mapping[str, object] | None
|
Further values by name, such as the coordinates |
None
|
size
|
int | None
|
The number of sheep, or |
None
|
space
|
BaseSpace | None
|
The pasture the sheep move on, whose axes |
None
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import HerdedSheepElement
>>> HerdedSheepElement('sheep', hx=1.0).hx
1.0
Source code in src/skcomplex/systems/biology/collective_behaviour/_herding.py
67 68 69 70 71 72 73 74 75 76 77 78 79 | |
ManyBodyAlignmentSwarmSystem(size=40, dimension=2, *, alpha=20.0, beta=20.0, C_r=2.0, l_r=0.5, C_a=1.0, l_a=2.0, gamma=1.0, l_v=1.0, l_3=1.0, kappa_v=2.0, kappa_4=4.0, R=3.0, random_state=0, label='system', solver='auto', backend='auto')
Bases: BaseSystem
A swarm of self-propelled agents that align through pairs, triples, and quadruples of neighbours.
The acceleration of an agent is the sum of terms of one body order each (Gutleb 2026, Eq. 2): the self-propulsion
(alpha - beta |v|^2) v, a Morse force and Cucker-Smale alignment with every neighbour, a 3-body flanking
alignment with every pair of neighbours weighted by kappa_v, and a 4-body encirclement alignment with every
triple of neighbours weighted by kappa_4. The neighbours are the agents within R, found again at every
evaluation, and the smooth cutoff (1 - rho^2 / R^2)^2 weighs every neighbour at the distance rho. With
kappa_v = kappa_4 = 0 the swarm is the classical pairwise collective. The agents move in a periodic box that holds
about ten neighbours around each. The system has the positions agents.x and the velocities agents.x'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
The number of agents. |
40
|
dimension
|
int
|
The dimension of the space, 2 or 3. |
2
|
alpha
|
float
|
The propulsion. |
20.0
|
beta
|
float
|
The friction at high speed. |
20.0
|
C_r
|
float
|
The strength of the repulsion. |
2.0
|
l_r
|
float
|
The range of the repulsion. |
0.5
|
C_a
|
float
|
The strength of the attraction. |
1.0
|
l_a
|
float
|
The range of the attraction. |
2.0
|
gamma
|
float
|
The strength of the pairwise alignment. |
1.0
|
l_v
|
float
|
The range of the pairwise alignment. |
1.0
|
l_3
|
float
|
The range of the weight of a neighbour in the 3-body and 4-body terms. |
1.0
|
kappa_v
|
float
|
The strength of the 3-body flanking alignment. |
2.0
|
kappa_4
|
float
|
The strength of the 4-body encirclement alignment. |
4.0
|
R
|
float
|
The radius of the neighbourhood and of the cutoff. |
3.0
|
random_state
|
int | Generator | None
|
The seed or generator of the initial positions and headings. |
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.biology.collective_behaviour import ManyBodyAlignmentSwarmSystem
>>> system = ManyBodyAlignmentSwarmSystem(12).prepare().system_
>>> system.elements[0].size, [component.label for component in system.interactions]
(12, ['propulsion', 'pairs', 'flanking', 'encirclement'])
Source code in src/skcomplex/systems/biology/collective_behaviour/_many_body_swarms.py
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | |
MorseAlignmentInteraction(label, *, target, R=3.0, C_r=2.0, l_r=0.5, C_a=1.0, l_a=2.0, gamma=1.0, l_v=1.0)
Bases: Interaction
Morse force and Cucker-Smale alignment between neighbours under a smooth cutoff.
An agent i gains from every neighbour j within R the acceleration
f_c(rho) (U'(rho) r_hat + gamma exp(-rho / l_v) u), where rho is their distance, r_hat the unit vector from
i to j, u the velocity of j relative to i, U(rho) = C_r exp(-rho / l_r) - C_a exp(-rho / l_a) the Morse
potential, and f_c(rho) = (1 - rho^2 / R^2)^2 the smooth cutoff.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
target
|
Role
|
The path of the group of agents, whose position |
required |
R
|
float
|
The radius of the neighbourhood and of the cutoff. |
3.0
|
C_r
|
Annotated[float, closed_open(0, oo)]
|
The strength of the repulsion. |
2.0
|
l_r
|
Annotated[float, open(0, oo)]
|
The range of the repulsion. |
0.5
|
C_a
|
Annotated[float, closed_open(0, oo)]
|
The strength of the attraction. |
1.0
|
l_a
|
Annotated[float, open(0, oo)]
|
The range of the attraction. |
2.0
|
gamma
|
Annotated[float, closed_open(0, oo)]
|
The strength of the alignment. |
1.0
|
l_v
|
Annotated[float, open(0, oo)]
|
The range of the alignment. |
1.0
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import MorseAlignmentInteraction
>>> MorseAlignmentInteraction('pairs', target='agents', R=3.0).R
3.0
Source code in src/skcomplex/systems/biology/collective_behaviour/_many_body_swarms.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
MorsePotentialInteraction(label, *, target, cutoff=1.0, C_a=1.0, l_a=2.0, C_r=2.0, l_r=0.5)
Bases: Interaction
Attraction at long range and repulsion at short range.
The potential is U(r) = C_r exp(-r / l_r) - C_a exp(-r / l_a), and every member feels the force -U'(r) along
the line of centres from each member within the cutoff, x_i'' += sum_j -U'(r_ij) (x_i - x_j) / r_ij. It holds a
flock of birds, a school of fish, or a swarm of robots together at a spacing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
target
|
Role
|
The path of the group of members. |
required |
cutoff
|
Annotated[float, open(0, oo)]
|
The distance beyond which pairs do not interact. |
1.0
|
C_a
|
Annotated[float, closed_open(0, oo)]
|
The strength of the attraction. |
1.0
|
l_a
|
Annotated[float, open(0, oo)]
|
The range of the attraction. |
2.0
|
C_r
|
Annotated[float, closed_open(0, oo)]
|
The strength of the repulsion. |
2.0
|
l_r
|
Annotated[float, open(0, oo)]
|
The range of the repulsion. |
0.5
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import MorsePotentialInteraction
>>> MorsePotentialInteraction('flocking', target='birds', cutoff=10.0).l_a
2.0
Source code in src/skcomplex/systems/biology/collective_behaviour/_flocks.py
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | |
PheromoneElement(label, *, level=0.0, D=0.05, evaporation=0.05, space=None)
Bases: Element
Pheromone of one cell of the ground.
It is a level that diffuses to the neighbouring cells and evaporates.
The element holds the level and its parameters. The system adds the diffusion D laplacian(level) and the
evaporation -evaporation level as interactions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the element or the lattice field. |
required |
level
|
Annotated[object, closed_open(0, oo)]
|
The initial level of pheromone. |
0.0
|
D
|
Annotated[object, closed_open(0, oo)]
|
The diffusion coefficient of the pheromone. |
0.05
|
evaporation
|
Annotated[object, closed_open(0, oo)]
|
The rate at which the pheromone evaporates. |
0.05
|
space
|
BaseSpace | None
|
The lattice whose cells hold the pheromone, or |
None
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import PheromoneElement
>>> PheromoneElement('pheromone').evaporation
0.05
Source code in src/skcomplex/systems/biology/collective_behaviour/_foraging.py
61 62 63 64 65 66 67 68 69 70 71 72 73 | |
PheromoneForagingSystem(width=40, height=20, *, n_ants=40, D=0.05, evaporation=0.05, attraction=5.0, label='system', solver='auto', backend='auto')
Bases: BaseSystem
Ants that find food and lay a pheromone trail home, which other ants follow.
It is stigmergy.
The ants live on a grid and the pheromone on a lattice of the same shape, as two partitions of a co-simulation that
exchange the field and the deposits at every step. The nest is near the left edge and the food near the right. The
system has the ants colony/ants and the pheromone ground/pheromone.level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
|
int
|
The number of columns. |
40
|
height
|
int
|
The number of rows. |
20
|
n_ants
|
int
|
The number of ants, which start at the nest. |
40
|
D
|
float
|
The diffusion of the pheromone. |
0.05
|
evaporation
|
float
|
The evaporation rate of the pheromone. |
0.05
|
attraction
|
float
|
How strongly pheromone draws searching ants. |
5.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.biology.collective_behaviour import PheromoneForagingSystem
>>> [child.label for child in PheromoneForagingSystem().prepare().system_.systems]
['colony', 'ground']
Source code in src/skcomplex/systems/biology/collective_behaviour/_foraging.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
RadiusSpec(radius, function, reduce='sum', priority=0)
dataclass
Bases: BaseSpec
Term of the other agents within a radius of an agent.
It is reduced by a sum or a mean, with a priority. Every other agent within radius gives the vector
function(agent, other, system), and the terms reduce by reduce. Among several radii, only the radii of the
highest priority that hold another agent count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
radius
|
float | str
|
The radius, a number or the name of a parameter of the agent. |
required |
function
|
Callable[..., object]
|
The term of one other agent, a function of the agent, the other agent, and the system view that returns a vector. |
required |
reduce
|
str
|
|
'sum'
|
priority
|
int
|
The priority of the radius, higher first. |
0
|
Raises:
| Type | Description |
|---|---|
ValidationError
|
The reduction is unknown, the function is not callable, or the radius is negative, raised when its owner checks it. |
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import RadiusSpec
>>> RadiusSpec(2.0, lambda agent, other, system: (1.0, 0.0), reduce='mean').reduce
'mean'
RatsElement(label, *, x=0.0)
Bases: CompartmentElement
Rats in one behavioural state, calm or nervous, counted by the state x.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the state. |
required |
x
|
Annotated[float, closed_open(0, oo)]
|
The initial number of rats. |
0.0
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import RatsElement
>>> RatsElement('calm', x=90.0).x
90.0
Source code in src/skcomplex/systems/biology/collective_behaviour/_decisions.py
69 70 71 72 73 74 75 | |
RatsSystem(n_rats=100, *, nervous=5, spontaneous=0.01, contagion=2.0, calming=1.0, stochastic=True, label='system', solver='auto', backend='auto')
Bases: BaseSystem
A colony of calm and nervous rats.
Nervousness spreads by contact and fades on its own.
Without spontaneous alarm the colony has a phase transition at contagion = calming: below it every rat calms down,
and above it a share 1 - calming / contagion of the rats stays nervous. The system has the numbers calm.x and
nervous.x.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_rats
|
int
|
The number of rats. |
100
|
nervous
|
int
|
The initial number of nervous rats. |
5
|
spontaneous
|
float
|
The rate at which a calm rat turns nervous on its own. |
0.01
|
contagion
|
float
|
The rate at which a calm rat turns nervous when all others are nervous. |
2.0
|
calming
|
float
|
The rate at which a nervous rat calms down. |
1.0
|
stochastic
|
bool
|
Whether changes are random events. |
True
|
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.biology.collective_behaviour import RatsSystem
>>> [group.x for group in RatsSystem().prepare().system_.elements]
[95.0, 5.0]
Source code in src/skcomplex/systems/biology/collective_behaviour/_decisions.py
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | |
RayleighPropulsionInteraction(label, *, target, alpha=1.0, beta=1.0)
Bases: Interaction
Rayleigh self-propulsion x'' = (alpha - beta |x'|^2) x' of the agents of a swarm.
It drives every agent to the speed sqrt(alpha / beta).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
target
|
Role
|
The path of the group of agents, whose position is |
required |
alpha
|
Annotated[float, closed_open(0, oo)]
|
The propulsion. |
1.0
|
beta
|
Annotated[float, open(0, oo)]
|
The friction at high speed. |
1.0
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import RayleighPropulsionInteraction
>>> RayleighPropulsionInteraction('propulsion', target='agents', alpha=2.0).alpha
2.0
Source code in src/skcomplex/systems/biology/collective_behaviour/_many_body_swarms.py
54 55 56 57 58 59 60 61 62 63 64 65 | |
SchoolingInteraction(label, *, target, phase=None)
Bases: ZoneCouplingInteraction
Couzin's turn of every fish toward a desired direction, by at most max_turn.
Zones around the fish set the desired direction.
When neighbours lie within the zone of repulsion alpha, the desired direction points away from them, and
repulsion overrides the other zones. Otherwise it points toward the neighbours within the zone of interaction rho
and along their headings and the fish's own, and an informed fish adds its preferred direction with the weight
omega. A fish with no neighbour keeps its heading. Offsets are the shortest ones on the torus of side side. It
is the agent form of coupling over two radii of different priorities, with a clamp of the turn.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction, unique among the components of its system. |
required |
target
|
Role
|
The path of the group of fish. |
required |
phase
|
str | None
|
The phase of the step the turn runs in, or |
None
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import SchoolingInteraction
>>> [spec.priority for spec in SchoolingInteraction('schooling', target='shoal').radii]
[1, 0, 0]
Source code in src/skcomplex/systems/biology/collective_behaviour/_leadership.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
SelfPropulsionInteraction(label, *, target, alpha=1.0, beta=0.5)
Bases: Interaction
Rayleigh self-propulsion, x'' = (alpha - beta |x'|²) x', which drives points to the speed sqrt(alpha / beta).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
target
|
Role
|
The path of the particle or group. |
required |
alpha
|
Annotated[float, closed_open(0, oo)]
|
The propulsion. |
1.0
|
beta
|
Annotated[float, open(0, oo)]
|
The friction at high speed. |
0.5
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import SelfPropulsionInteraction
>>> SelfPropulsionInteraction('flight', target='birds', alpha=1.5).alpha
1.5
Source code in src/skcomplex/systems/biology/collective_behaviour/_flocks.py
541 542 543 544 545 546 547 548 549 550 551 552 | |
ShepherdElement(label, *, driving=0.0, values=None, size=None, space=None)
Bases: Element
Shepherd of a flock, an agent with a mode.
The mode records whether it collects stray sheep or drives the flock.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the shepherd or of the group of shepherds. |
required |
driving
|
Annotated[float | Array, FiniteEnumeratedSet([0, 1])]
|
1 while the shepherd drives the flock and 0 while it collects a stray sheep. |
0.0
|
values
|
Mapping[str, object] | None
|
Further values by name, such as the coordinates |
None
|
size
|
int | None
|
The number of shepherds, or |
None
|
space
|
BaseSpace | None
|
The pasture the shepherds move on, whose axes |
None
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import ShepherdElement
>>> ShepherdElement('dog').driving
0.0
Source code in src/skcomplex/systems/biology/collective_behaviour/_herding.py
120 121 122 123 124 125 126 127 128 129 130 | |
ShepherdingInteraction(label, *, sheep, shepherds, goal=(0.0, 0.0), ra=2.0, rs=65.0, n=20, weights=(0.5, 1.05, 2.0, 1.0, 0.3), delta=1.0, delta_s=1.5, graze=0.05)
Bases: Interaction
Herding of a flock towards a goal by a shepherd.
The shepherd collects stray sheep and drives the flock from behind.
Following Strömbom et al. (2014), a sheep within rs of the shepherd moves the distance delta along h H + c C +
rho_a R_a + rho_s R_s + e E, of unit vectors: its last heading H, the attraction C to the centre of its n
nearest neighbours, the repulsion R_a from the sheep closer than ra, the repulsion R_s from the shepherd, and
noise E. A sheep far from the shepherd grazes, a random step with the probability graze, and is repelled by
close sheep. The shepherd collects the sheep furthest from the centre of the flock once it is further than ra
N^(2/3), heading to the point ra behind it, and otherwise drives the flock from the point ra sqrt(N) behind
its centre, opposite the goal. It moves at the speed delta_s with noise, and stands still within 3 ra of a
sheep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
sheep
|
Role
|
The path of the flock. |
required |
shepherds
|
Role
|
The path of the group of the shepherd. |
required |
goal
|
Sequence[float]
|
The position of the goal. |
(0.0, 0.0)
|
ra
|
float
|
The distance of repulsion between sheep. |
2.0
|
rs
|
float
|
The distance at which sheep detect the shepherd. |
65.0
|
n
|
int
|
The number of nearest neighbours whose centre attracts a sheep. |
20
|
weights
|
Sequence[float]
|
The weights |
(0.5, 1.05, 2.0, 1.0, 0.3)
|
delta
|
float
|
The step of a sheep. |
1.0
|
delta_s
|
float
|
The step of the shepherd. |
1.5
|
graze
|
float
|
The probability of a grazing step. |
0.05
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import ShepherdingInteraction
>>> ShepherdingInteraction('herding', sheep='sheep', shepherds='dogs').rs
65.0
Source code in src/skcomplex/systems/biology/collective_behaviour/_herding.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
herd(system)
Move every sheep and every shepherd from the positions at the start of the step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
SystemView
|
The system. |
required |
Source code in src/skcomplex/systems/biology/collective_behaviour/_herding.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
ShepherdingSystem(n_sheep=30, *, size=100.0, goal=(5.0, 5.0), seed=0, label='system', solver='auto', backend='auto')
Bases: BaseSystem
A shepherd that collects a scattered flock and drives it to a goal.
It is the heuristic of Strömbom et al.
The sheep start scattered in the middle of a square field and the shepherd behind them, opposite the goal. Sheep
flee a near shepherd while they keep together and apart, and graze otherwise. The shepherd alternates between
collecting the sheep furthest from the flock and driving the flock from behind towards the goal, and the zig-zag of
this alternation brings the flock to the goal in a few hundred steps. The system has the agent groups sheep and
shepherds, the latter of one shepherd.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_sheep
|
int
|
The number of sheep. |
30
|
size
|
float
|
The side of the square field. |
100.0
|
goal
|
tuple[float, float]
|
The position of the goal. |
(5.0, 5.0)
|
seed
|
int | None
|
The seed 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'
|
backend
|
BaseBackend | str
|
The backend that evaluates the compiled functions, or |
'auto'
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import ShepherdingSystem
>>> [group.label for group in ShepherdingSystem(10).prepare().system_.elements]
['sheep', 'shepherds']
Source code in src/skcomplex/systems/biology/collective_behaviour/_herding.py
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
ShoalMemberElement(label, *, vx=1.0, vy=0.0, omega=0.0, deviation=0.0, preferred=0.0, alpha=1.0, rho=6.0, speed=0.3, max_turn=0.4, noise=0.05, side=100.0, values=None, size=None, space=None)
Bases: Element
Fish of a Couzin shoal that avoids close neighbours and follows farther ones.
It may know a preferred direction.
Every step SchoolingInteraction turns a fish toward its desired direction by at most max_turn. When neighbours
lie within the zone of repulsion alpha, it points away from them. Otherwise it points toward the neighbours
within the zone of interaction rho and along their headings and its own. An informed fish adds its preferred
direction with the weight omega, while a naive fish has omega = 0. It then draws a Gaussian angular noise
deviation of standard deviation noise, and SwimmingInteraction turns it by the noise and moves it at the
speed speed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the fish or the group. |
required |
vx
|
object
|
The horizontal component of the unit heading. |
1.0
|
vy
|
object
|
The vertical component of the unit heading. |
0.0
|
omega
|
Annotated[object, closed_open(0, oo)]
|
The weight of the preferred direction, 0 for a naive fish. |
0.0
|
deviation
|
object
|
The angular noise of the last turn. |
0.0
|
preferred
|
float
|
The angle of the preferred direction of the informed fish. |
0.0
|
alpha
|
Annotated[float, open(0, oo)]
|
The radius of the zone of repulsion. |
1.0
|
rho
|
Annotated[float, open(0, oo)]
|
The radius of the zone of interaction. |
6.0
|
speed
|
Annotated[object, open(0, oo)]
|
The distance a fish swims per step. |
0.3
|
max_turn
|
Annotated[float, open(0, oo)]
|
The largest turning angle per step. |
0.4
|
noise
|
Annotated[float, closed_open(0, oo)]
|
The standard deviation of the angular noise. |
0.05
|
side
|
Annotated[float, open(0, oo)]
|
The side of the square torus. |
100.0
|
values
|
Mapping[str, object] | None
|
Further values by name, such as the coordinates |
None
|
size
|
int | None
|
The number of fish, or |
None
|
space
|
BaseSpace | None
|
The torus the fish swim on, whose axes |
None
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import ShoalMemberElement
>>> ShoalMemberElement('shoal', values={'x': 5.0, 'y': 5.0}, size=10).size
10
Source code in src/skcomplex/systems/biology/collective_behaviour/_leadership.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
SpatialBoidElement(label, *, x=0.0, y=0.0, z=0.0, radius=5.0, separation_radius=1.0, cohesion=0.01, separation=0.1, alignment=0.1, min_speed=0.5, max_speed=1.0, values=None, size=None, space=None)
Bases: Element
Boid in three dimensions, a bird that steers by the boids it sees.
It steers by the velocity and the position of those boids.
Every step HeadingInteraction steers a boid by cohesion, separation, and alignment with the boids within radius,
with its speed between min_speed and max_speed, and SpatialFlightInteraction moves it along its velocity. Its
position is x, y, and z, and its velocity "x'", "y'", and "z'", which its rules read and
change as agent.get_value("x'") and agent.set_value("x'", value).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the agent or group. |
required |
x
|
float | Array
|
The initial position along the first axis. |
0.0
|
y
|
float | Array
|
The initial position along the second axis. |
0.0
|
z
|
float | Array
|
The initial position along the third axis. |
0.0
|
radius
|
Annotated[float, open(0, oo)]
|
The distance within which a boid sees others. |
5.0
|
separation_radius
|
Annotated[float, closed_open(0, oo)]
|
The distance within which a boid avoids others. |
1.0
|
cohesion
|
Annotated[float, closed_open(0, oo)]
|
The weight of cohesion. |
0.01
|
separation
|
Annotated[float, closed_open(0, oo)]
|
The weight of separation. |
0.1
|
alignment
|
Annotated[float, closed_open(0, oo)]
|
The weight of alignment. |
0.1
|
min_speed
|
Annotated[float, closed_open(0, oo)]
|
The smallest speed. |
0.5
|
max_speed
|
Annotated[float, open(0, oo)]
|
The largest speed. |
1.0
|
values
|
Mapping[str, object] | None
|
Further values by name, such as the velocity |
None
|
size
|
int | None
|
The number of boids, or |
None
|
space
|
BaseSpace | None
|
The space of the boids, or |
None
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import SpatialBoidElement
>>> SpatialBoidElement('flock', size=10).size
10
Source code in src/skcomplex/systems/biology/collective_behaviour/_flocks.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 248 249 250 251 252 | |
SpatialFlightInteraction(label, *, target, probability=1.0)
Bases: MovementInteraction
Flight of every boid by its velocity in one step.
The boundary of its space acts on the new position.
Every boid moves in its own activation, after the rules and member interactions listed before it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction, unique among the components of its system. |
required |
target
|
Role
|
The path of the group of boids. |
required |
probability
|
Annotated[float, closed(0, 1)]
|
The probability that a boid moves in a step. |
1.0
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import SpatialFlightInteraction
>>> SpatialFlightInteraction('flight', target='flock').schedule
'member'
Source code in src/skcomplex/systems/biology/collective_behaviour/_flocks.py
591 592 593 594 595 596 597 598 | |
SwimmingInteraction(label, *, target, probability=1.0, phase=None)
Bases: MovementInteraction
Swim of every fish by speed along its heading.
The heading is turned first by its angular noise deviation.
The heading (vx, vy) stays a unit vector, and the fish moves on the torus of side side.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction, unique among the components of its system. |
required |
target
|
Role
|
The path of the group of fish. |
required |
probability
|
Annotated[float, closed(0, 1)]
|
The probability that a fish swims in a step. |
1.0
|
phase
|
str | None
|
The phase of the step the swim runs in, or |
None
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import SwimmingInteraction
>>> SwimmingInteraction('swimming', target='shoal', phase='swim').phase
'swim'
Source code in src/skcomplex/systems/biology/collective_behaviour/_leadership.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
TrailChoiceInteraction(label, *, chosen, other, flux=1.0, k=20.0, h=2.0, stochastic=True)
Bases: ReactionInteraction
Choice of a branch by arriving ants, with Deneubourg's probability.
The probability is (k + x)^h / ((k + x)^h + (k + x')^h).
Ants arrive at the fork at rate flux, and each takes the chosen branch with a probability that grows with its
marks x against the marks x' of the other branch, and marks it. It is a ReactionInteraction that produces one
mark on the chosen branch at that propensity and reads the other branch as a modifier, a rate equation, or in
stochastic mode a random event per ant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
chosen
|
Role
|
The path of the branch this choice marks. |
required |
other
|
Role
|
The path of the competing branch. |
required |
flux
|
Annotated[float, closed_open(0, oo)]
|
The rate at which ants arrive at the fork. |
1.0
|
k
|
Annotated[float, closed_open(0, oo)]
|
The attractiveness of an unmarked branch. |
20.0
|
h
|
Annotated[float, open(0, oo)]
|
The nonlinearity of the choice. |
2.0
|
stochastic
|
bool
|
Whether ants are random events. |
True
|
Examples:
>>> from skcomplex.core.systems import System
>>> from skcomplex.systems.biology.collective_behaviour import BranchElement, TrailChoiceInteraction
>>> choice = TrailChoiceInteraction('choice', chosen='left', other='right', stochastic=False)
>>> system = System(elements=[BranchElement('left'), BranchElement('right')], interactions=[choice])
>>> trajectory = system.prepare().simulate(until=2.0, dt=0.1)
>>> round(float(trajectory.get_values('left.x')[-1]), 3)
1.025
Source code in src/skcomplex/systems/biology/collective_behaviour/_decisions.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
TrailSegmentElement(label, *, tau=1.0, passable=1.0, size=None)
Bases: Element
Segment of a road network between two points.
The segment holds the pheromone ants left on it and whether it is open.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the segment or of the group of segments. |
required |
tau
|
Annotated[float | Array, closed_open(0, oo)]
|
The pheromone level. |
1.0
|
passable
|
Annotated[float | Array, FiniteEnumeratedSet([0, 1])]
|
1 while the segment can be walked and 0 once it is cut. |
1.0
|
size
|
int | None
|
The number of members, or |
None
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import TrailSegmentElement
>>> TrailSegmentElement('segment').tau
1.0
Source code in src/skcomplex/systems/biology/collective_behaviour/_colony_optimization.py
66 67 68 69 70 71 72 73 74 75 76 | |
WalkerElement(label, *, goal_x=0.0, goal_y=0.0, arrived=0.0, directedness=3.0, attraction=1.0, intensity=1.0, G_max=5.0, width=30.0, height=30.0, values=None, size=None, space=None)
Bases: Element
Pedestrian who walks toward a goal, drawn to existing trails.
The walker leaves footprints that build the trails.
The walker heads along the unit vector toward its goal plus attraction times the gradient of the trail potential
V, after Helbing, Keltsch, and Molnár. Every step it moves to a neighbouring cell with a probability that grows
with exp(directedness * cos angle), where the angle is between the step and the heading, and adds the footprint
intensity (1 - G / G_max) to the ground. The step after it arrives at its goal it leaves the green, and a new
walker enters there with another entry point as goal, drawn at random, a renewal in place. The system adds the
step, the footprint, the renewal, and the arrival as member interactions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the agent or group. |
required |
goal_x
|
float | Array
|
The column of the goal. |
0.0
|
goal_y
|
float | Array
|
The row of the goal. |
0.0
|
arrived
|
Annotated[float | Array, FiniteEnumeratedSet([0, 1])]
|
Whether the walker stood at its goal at the end of the previous step, 1 or 0. |
0.0
|
directedness
|
Annotated[float, closed_open(0, oo)]
|
How strongly the walker heads for its goal. |
3.0
|
attraction
|
Annotated[float, closed_open(0, oo)]
|
How strongly trails draw the walker. |
1.0
|
intensity
|
Annotated[float, closed_open(0, oo)]
|
The footprint on bare ground. |
1.0
|
G_max
|
Annotated[float, open(0, oo)]
|
The largest ground structure. |
5.0
|
width
|
Annotated[float, open(0, oo)]
|
The number of columns of the grid. |
30.0
|
height
|
Annotated[float, open(0, oo)]
|
The number of rows of the grid. |
30.0
|
values
|
Mapping[str, object] | None
|
Further values by name, such as the cells |
None
|
size
|
int | None
|
The number of walkers, or |
None
|
space
|
BaseSpace | None
|
The grid the walkers walk on, whose axes |
None
|
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import WalkerElement
>>> WalkerElement('walkers', size=4).size
4
Source code in src/skcomplex/systems/biology/collective_behaviour/_trails.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
ZoneCouplingInteraction(label, *, target, source=None, write='x', combine=None, radii=(), clamp=None, phase=None)
Bases: Interaction
Coupling of every agent of a group to the other agents within priority zones.
Every agent runs the coupling in its own activation in the phase phase. Every RadiusSpec of radii gives the
vector function(agent, other, system) of every other agent of the source group, or of the target group, within
its radius, reduced by its sum or mean. Only the radii of the highest priority that hold another agent count, and
their reductions add up. The states write of the agent form a vector v, which becomes
combine(agent, system, reduction, priority), or v plus the reduction by default, where reduction and
priority are None when no radius holds another agent. clamp then bounds the angle the vector turns and its
norm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction, unique among the components of its system. |
required |
target
|
Role
|
The path of the group of the agents. |
required |
source
|
str | None
|
The path of the group of the other agents, or |
None
|
write
|
str | Sequence[str]
|
The names of the components of the vector of the agent, primed for a derivative, such as |
'x'
|
combine
|
AgentCombination | None
|
The next vector, a callable of the agent, the system view, the reduction, and its priority, or |
None
|
radii
|
Sequence[RadiusSpec]
|
The terms over the radii around an agent. |
()
|
clamp
|
ClampSpec | None
|
The clamp of the turn and the norm of the vector, or |
None
|
phase
|
str | None
|
The phase of the step the agents run in, or |
None
|
Raises:
| Type | Description |
|---|---|
ValidationError
|
The target is not one group, there is no radius, or a radius or the clamp is invalid, raised by |
Examples:
>>> from skcomplex.systems.biology.collective_behaviour import RadiusSpec, ZoneCouplingInteraction
>>> term = RadiusSpec(1.0, lambda agent, other, system: (0, 0))
>>> zones = ZoneCouplingInteraction('zones', target='fish', write=('vx', 'vy'), radii=(term,))
>>> zones.prepare().radii[0].radius
1.0
Source code in src/skcomplex/systems/biology/collective_behaviour/_zones.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |