Flocking¶
cuplan.FlockingSim — Reynolds' Boids (Reynolds 1987, Flocks, herds
and schools: a distributed behavioral model, SIGGRAPH), mirroring the
boids behavior of pymapf.swarm.flocking.
Semantics¶
Within a perception radius, each agent accumulates three steering accelerations:
- separation — inverse-square repulsion from neighbours closer than the separation distance;
- cohesion — toward the mean neighbour offset;
- alignment — toward the mean neighbour velocity.
The command is clamped to a maximum acceleration, integrated with a
capped speed. Works in 2D and 3D. FlockingResult reports Vicsek's
polarization order parameter and a mean-neighbour-distance cohesion
proxy, so "does it flock?" is a number rather than an impression.
Parallelization¶
The per-agent force is a gather over neighbours with no dependencies between agents:
- CPU reference — full pairwise NumPy broadcast.
- CUDA — one thread per agent scanning the swarm.
The O(n²) neighbour scan is deliberate on both backends: at the swarm sizes this library targets (up to a few thousand agents), rebuilding a spatial index every step costs more than it saves, and the two backends stay exactly comparable — CPU and CUDA trajectories agree to floating-point tolerance (tested).
Boids flocking with GPU force accumulation.
Mirrors pymapf.swarm.flocking.Boids (Reynolds 1987): separation as
an inverse-square repulsion inside the separation distance, cohesion
toward the mean neighbour offset, alignment toward the mean neighbour
velocity, all limited to a maximum acceleration and integrated at a
capped speed.
The per-agent force is a sum over neighbours — a gather with no data dependencies between agents — so the CUDA backend runs one thread per agent scanning the swarm. The scan is brute-force O(n^2) on both backends on purpose: at the swarm sizes this library targets (up to a few thousand agents) rebuilding a spatial index every step costs more than it saves, and the two backends stay exactly comparable.
FlockingParams
dataclass
¶
Boids gains and limits, defaults matching pymapf's Boids.
Source code in cuplan/flocking.py
29 30 31 32 33 34 35 36 37 38 39 | |
FlockingResult
dataclass
¶
Trajectories and metrics of a flocking run.
Source code in cuplan/flocking.py
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 | |
polarization ¶
polarization()
Per-frame heading agreement: 1.0 means perfectly aligned.
The norm of the mean unit velocity (Vicsek's order parameter).
Source code in cuplan/flocking.py
52 53 54 55 56 57 58 59 60 | |
mean_neighbor_distance ¶
mean_neighbor_distance()
Per-frame mean pairwise distance (a cohesion proxy).
Source code in cuplan/flocking.py
62 63 64 65 66 67 68 69 70 | |
FlockingSim ¶
Boids swarm simulation (Reynolds 1987).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positions
|
ndarray
|
|
required |
velocities
|
ndarray
|
|
required |
params
|
FlockingParams | None
|
gains and limits; see :class: |
None
|
timestep
|
float
|
integration step in seconds. |
0.05
|
backend
|
Backend
|
|
'auto'
|
Source code in cuplan/flocking.py
73 74 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 109 110 111 112 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 144 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 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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | |
run ¶
run(n_steps)
Integrate n_steps and return trajectories plus metrics.
Source code in cuplan/flocking.py
109 110 111 112 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 144 145 146 147 148 | |