Cucker-Smale flocking
Cucker-Smale flocking.
- Class:
CuckerSmaleFlockingSystem - Import:
from skcomplex.systems.physics.active_matter import CuckerSmaleFlockingSystem - Field:
physics/active_matter - Topics:
flocking,ode,alignment,pair interaction - References:
- Cucker and Smale 2007
- Vicsek and Zafeiris 2012
- Bellomo et al. 2017
Use this system
The block imports the class and simulates the system with its default parameters and its tuned run settings.
from skcomplex.systems.physics.active_matter import CuckerSmaleFlockingSystem
trajectory = CuckerSmaleFlockingSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
n_agents |
50 |
K |
1.0 |
beta |
0.25 |
size |
10.0 |
random_state |
0 |
solver |
'auto' |
backend |
'auto' |
Tags
The system with its default parameters has these capability tags, which solvers and analyses check.
| Tag | Values |
|---|---|
| Time | continuous |
| Space | continuous |
| Laws | rate |
| Randomness | deterministic |
| Domains | vector |
| Structure | fixed |
Class
The source of the class shows how it builds its components from its model parameters.
class CuckerSmaleFlockingSystem(BaseSystem):
"""Cucker-Smale flocking.
Every agent aligns its velocity with all others, weighted by distance.
The mean velocity is conserved, and for `beta <= 1/2` the velocities converge to it from any start. The system has
the positions `birds.x` and velocities `birds.x'`.
Args:
n_agents:
The number of agents.
K:
The coupling strength.
beta:
The decay exponent of the communication weight.
size:
The side of the square in which the agents start.
random_state:
The seed or generator of the initial positions and velocities.
label:
The label of the system, which names it when it is nested in another one.
solver:
The solver of the problem, or `'auto'` for Runge-Kutta 4 with the step 0.05.
backend:
The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.
Examples:
>>> from skcomplex.systems.physics.active_matter import CuckerSmaleFlockingSystem
>>> CuckerSmaleFlockingSystem(5).prepare().system_.interactions[0].n_agents
5
"""
field = 'physics/active_matter'
topics = ('flocking', 'ode', 'alignment', 'pair interaction')
references = ('Cucker and Smale 2007', 'Vicsek and Zafeiris 2012', 'Bellomo et al. 2017')
until = 20.0
dt = 0.5
def __init__(
self,
n_agents: int = 50,
*,
K: float = 1.0,
beta: float = 0.25,
size: float = 10.0,
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_agents = n_agents
self.K = K
self.beta = beta
self.size = size
self.random_state = random_state
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set `system_` to agents at random places and velocities that align with all others.
Args:
context:
The observables to compile with the problem, or `None` for none.
"""
super()._prepare(context)
rng = np.random.default_rng(self.random_state)
plane = ContinuousSpace(n_dim=2)
birds = BirdElement(
'birds',
x=rng.uniform(0.0, self.size, (self.n_agents, 2)),
values={"x'": rng.normal(0.0, 1.0, (self.n_agents, 2))},
size=self.n_agents,
space=plane,
)
alignment = CuckerSmaleAlignmentInteraction(
'align',
target='birds',
cutoff=1e6,
K=self.K,
beta=self.beta,
n_agents=self.n_agents,
)
self.system_ = System(
self.label,
elements=[birds],
interactions=[alignment],
space=plane,
solver=RK4Solver(dt=0.05),
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
BirdElement, fromElement, through the shared recipePointElement
Interactions:
CuckerSmaleAlignmentInteraction, fromInteraction
class BirdElement(PointElement):
"""Point drawn at its position and coloured by its velocity.
Examples:
>>> from skcomplex.systems.physics.active_matter import BirdElement
>>> BirdElement('bird', x=[0.0, 1.0]).x
[0.0, 1.0]
"""
visual = VisualSpec('points', {'position': 'x', 'color_by': "x'", 'states': ()})
class CuckerSmaleAlignmentInteraction(Interaction):
"""Cucker-Smale alignment, `x_i'' += K / N sum_j psi(r_ij) (x_j' - x_i')` with `psi(r) = (1 + r^2)^(-beta)`.
Every member pulls its velocity `x'` towards those of the members within the cutoff, with a weight that decays with
their distance. It is a flock of birds, a school of fish, or a fleet of drones that agree on a heading.
Args:
label:
The label of the interaction, unique among the components of its system.
target:
The path of the group of agents.
cutoff:
The distance beyond which pairs do not align, large for the all-to-all system.
K:
The strength of the alignment.
beta:
The decay exponent of the weight.
n_agents:
The number of agents `N` that normalizes the alignment.
Examples:
>>> from skcomplex.systems.physics.active_matter import CuckerSmaleAlignmentInteraction
>>> CuckerSmaleAlignmentInteraction('align', target='birds', beta=0.25).beta
0.25
"""
law_classes = (RateLaw,)
def __init__(
self,
label: str,
*,
target: Role,
cutoff: float = 1.0,
K: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
beta: Annotated[float, RealSet.closed_open(0, oo)] = 0.25,
n_agents: int = 1,
) -> None:
super().__init__(label)
self.target = target
self.cutoff = cutoff
self.K = K
self.beta = beta
self.n_agents = n_agents
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the pull of the velocity of every agent towards those of the others.
Args:
context:
The namespace of the target, the time `t`, and the parameters.
"""
super()._prepare(context)
s, t = context.target, context.t
separation = s.x - s.other.x
weight = (1 + separation.dot_product(separation)) ** (-context.beta)
pull = neighbors(weight * (s.other.x.diff(t) - s.x.diff(t)), over=RadiusNeighborhood(self.cutoff))
self.laws_ = [RateLaw(s.x.diff(t, 2), context.K / self.n_agents * pull)]