Skip to content

Standard libraries overview

phyelds.libraries provides a rich suite of reusable aggregate building blocks, all written in pure Python on top of @aggregate, remember, neighbors, and exchange. Reading their source code is one of the best ways to learn idiomatic phyelds.


The G–C–S self-organization pattern

In aggregate computing, complex collective behaviors are rarely written from scratch with raw remember and neighbors. Instead, they are composed from three universal coordination operators—historically called G, C, and S:

S — Sparse Choice

elect_leaders() breaks symmetry across the network, picking distributed coordinators separated by a target grain radius.

G — Spreading

distance_to() and broadcast() propagate distances and information outward from leaders or sources.

C — Collection

collect_with() and sum_values() aggregate data inward along a gradient potential toward the sink.

Operator Module Direction Role
G (Gradient / Spreading) phyelds.libraries.spreading Outward (source → network) Computes distances from sources and broadcasts data or functions along shortest paths
C (Collection) phyelds.libraries.collect Inward (network → sink) Aggregates values downhill along a potential field toward one or more sinks
S (Sparse choice / Leaders) phyelds.libraries.leader_election Symmetry breaking Elects spatially distributed leaders separated by a target grain radius

Combining just these three blocks gives you:

  • S → G (Voronoi partitioning): elect sparse leaders with elect_leaders, then broadcast each leader's ID so the network splits into self-healing clusters.
  • G → C → G (Regional / network-wide summarize): build a gradient from a sink (G), collect values from all nodes into the sink (C), and broadcast the aggregated result back out (G).
  • G + G + G (channel): add the gradients from a source and from a target (G, G), and compare the sum with the distance between the two, broadcast from the source (G): where it exceeds that distance by at most width, the devices form a self-healing channel along the shortest path. The tutorial builds a corridor with G → C → G instead.

Explore the library guides

  • Spreading, Collection & Gossip — Gradients (bellman_ford, path_coherent), broadcast, channel, tree/DAG collection (point_to_point, multi_path), summarize, and self-stabilizing gossip.
  • Leader Election & Aggregate Processes — Regional symmetry breaking (elect_leaders) and dynamic, spatially scoped computation bubbles (@process / spawn).
  • Time, Reactive State & Logic — Network clocks, timers, edge detectors, filters, Spatial Logic for Closure Spaces (SLCS), and Past-CTL temporal logic.

Device sensors & actuators: phyelds.libraries.device

Every aggregate program interacts with its host device through phyelds.libraries.device:

Function Returns / Effect
local_id() The unique identifier of the current device
local_position() The position tuple of the current device (sense("position"))
sense(sensor) The current value of sensor sensor (None if missing)
store(output, value) Writes an output named output for the platform/simulator to read (actuation, metrics)
Sensor(name).sense() The value of the sensor name, named once and shared by the program and its host
Actuator(name).send(value) Writes the output name; the host reads it with .read(outputs). check= refuses invalid values
velocity.send(v) The actuator the robots move by: a sequence of finite numbers, one per axis
source, heading, obstacle The sensors robot hosts share: whether the device is the configured source, where it faces, away from what it senses near
led, label The actuators robot hosts share: the RGB colour of the LEDs (0–255) and a text the host shows or logs

A string repeated in the program and in its host is checked by nobody: store("velocty", v) leaves the robot still, without an error. Name sensors and actuators once instead, and share the object:

from phyelds.libraries.device import Actuator, Sensor

alarm: Sensor[bool] = Sensor("alarm")
buzzer: Actuator[bool] = Actuator("buzzer")

buzzer.send(alarm.sense())                  # in the program
buzzer.read(device.outputs, default=False)  # in the host

Neighborhood distances: phyelds.libraries.distances

Spreading and leader-election functions accept a NeighborhoodField of distances to neighbors. When omitted, they default to hops_distance() (1 per hop); for physical distances in 2D, 3D, or N-D space, pass neighbors_distances():

Function Returns
neighbors_distances(position=None) A NeighborhoodField with the Euclidean distance to each neighbor (supports 2D, 3D, and N-D coordinates; defaults to local_position())
hops_distance() A NeighborhoodField with 1 for every neighbor and 0 for the device itself