phyelds.simulator¶
phyelds.simulator
¶
A simple simulator for aggregate computing systems in phyelds. This simulator allows you to create nodes, set their positions and data, and define their neighborhoods. It also provides a way to schedule events and run the simulation.
Node(position, data=None, node_id=None)
¶
SpatialIndex
¶
Bases: Protocol
An index of the nodes of an environment by their position, which it keeps up to date.
Environment(neighborhood_function=None)
¶
A class to represent the environment in which nodes exist. It manages the nodes and their neighborhoods. It also provides a way to set the neighborhood function.
add_data_for_all_nodes(data)
¶
Add data to all nodes in the environment
node_list()
¶
Return a list of all nodes in the environment
add_node(node)
¶
Add a node to the environment
remove_node(node_id)
¶
Remove a node from the environment
node_moved(node, old_position)
¶
Keep the spatial indexes up to date: a node calls it when its position changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
The node that moved. |
required |
old_position
|
Tuple[float, ...]
|
Where it was. |
required |
spatial_index(key, build)
¶
The spatial index of key, built on the first request and then kept up to date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
What the index is, e.g. the kind and size of its cells. |
required |
build
|
Callable[[List[Node]], SpatialIndex]
|
How to build the index from the nodes. |
required |
Returns:
| Type | Description |
|---|---|
SpatialIndex
|
The index, rebuilt if nodes were added or removed other than with |
SpatialIndex
|
and |
set_neighborhood_function(func)
¶
Set the function that determines neighborhoods
get_neighbors(node)
¶
Get neighbors for a node using the neighborhood function
no_neighbors(node, all_node)
staticmethod
¶
Default neighborhood function (no neighbors)
Event(time, action, *args, **kwargs)
¶
A class to represent an event in the simulation. An event has a time, an action (function), and optional arguments.
execute()
¶
Execute the event's action
Monitor(simulator)
¶
Simulator(environment=None)
¶
A class to represent the simulator for a simple aggregate computing system.
schedule_event(time_delta, action, *args, **kwargs)
¶
Schedule an event to occur after time_delta
run(until_time=None)
¶
Run the simulation until the specified time or until no more events
continue_run(until_time=None)
¶
Continue running the simulation until the specified time or until no more events
add_monitor(monitor)
¶
Add a monitor to the simulator
stop()
¶
Stop the simulation
cancel_event(event)
¶
Cancel a scheduled event
reset()
¶
Reset the simulator
create_node(position, data=None, node_id=None)
¶
Helper method to create and add a node to the environment
phyelds.simulator.deployments
¶
This module contains various functions to generate different types of node distributions in a simulation environment. These functions can be used to create specific patterns or random distributions of nodes for testing and simulation purposes.
grid_generation(simulator, width, height, spacing)
¶
Generate a grid of nodes in the simulator's environment.
deformed_lattice(simulator, width, height, spacing, deformation_factor)
¶
Generate a deformed lattice of nodes in the simulator's environment.
random_walk(simulator, num_steps, step_size)
¶
Perform a random walk and create nodes at each step.
random_in_circle(simulator, num_nodes, radius)
¶
Generate nodes randomly distributed within a circle.
phyelds.simulator.neighborhood
¶
Neighborhood functions for the phyelds simulator. A neighborhood function is simply a function that takes a node and an environment, and returns a list of neighbors.
AdaptiveRange(radius, *, neighbors, tolerance, step=1.0, min_range=1.0, max_range=100.0)
¶
A neighborhood where each node has its own communication range, which adapts to keep
about neighbors neighbors: a node with too many shrinks its range, one with too
few grows it. A crowded swarm keeps its messages few, and a scattered one stays
connected. Two nodes are neighbors if each is in range of the other.
The rule of Alchemist's AdaptiveRange linking rule. A range adapts once per round
of its node, a new value of its time, so that drawing the edges changes nothing.
range_of(node)
¶
The current communication range of node.
radius_neighborhood(radius)
¶
Create a neighborhood function that includes nodes within a certain radius.
The nodes are found through a uniform grid that the environment keeps up to date as nodes move, join and leave, so a query reads only the nodes nearby. The neighbors are those of a scan of every node, in the order of the environment.
k_nearest_neighbors(k)
¶
Create a neighborhood function that returns the k closest nodes.
Ties in distance are broken by the order of the nodes in the environment. Queries use the environment's spatial grid, expanding outward from the node's cell only until no unvisited cell can contain a closer neighbor.
full_neighborhood(node, environment)
¶
Include all nodes as neighbors
adaptive_range(radius, *, neighbors, tolerance, step=1.0, min_range=1.0, max_range=100.0)
¶
Create a neighborhood whose ranges adapt to keep about neighbors neighbors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
radius
|
float
|
The range each node starts with. |
required |
neighbors
|
int
|
The number of neighbors each node aims at. |
required |
tolerance
|
int
|
How many more or fewer neighbors are fine: a node shrinks its range
above |
required |
step
|
float
|
How much a range changes in a round. |
1.0
|
min_range
|
float
|
The shortest range. |
1.0
|
max_range
|
float
|
The longest range. |
100.0
|
Returns:
| Type | Description |
|---|---|
AdaptiveRange
|
The neighborhood function, whose |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
phyelds.simulator.runner
¶
This module contains the event to run the aggregate program in the simulator.
SimulatorNodeContext(node_id, sensors=None, outputs=None, *, default_position=None)
¶
Bases: NodeContext, ABC
A class to represent the context of a node in the simulator.
This class exposes the node's data and position as sensors without copying node.data.
from_node(node)
staticmethod
¶
Create the context of a simulated node backed by node.data and node.position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
The node to create the context from. |
required |
Returns:
| Type | Description |
|---|---|
SimulatorNodeContext
|
The node's |
aggregate_program_runner(simulator, time_delta, node, program, **kwargs)
¶
Run program once for node and reschedule its next round after time_delta.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulator
|
Simulator
|
The simulator owning |
required |
time_delta
|
float
|
Period until the next round of |
required |
node
|
Node
|
The simulated node executing the round. |
required |
program
|
Callable[..., Any]
|
The aggregate program to run. |
required |
**kwargs
|
Any
|
Keyword arguments forwarded to |
{}
|
schedule_program_for_all(simulator, start, frequency, program, **kwargs)
¶
Run an aggregate program on every node of the simulator, round after round.
Each node runs its first round start time units from now and then one every
frequency time units; its result, outgoing messages, state and outputs are
kept in its data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulator
|
Simulator
|
The simulator whose nodes run the program. |
required |
start
|
float
|
The delay before the first round. |
required |
frequency
|
float
|
The time between two rounds of a node (a period, not a rate). |
required |
program
|
Callable[..., Any]
|
The aggregate program to run. |
required |
**kwargs
|
Any
|
Keyword arguments passed to |
{}
|
phyelds.simulator.actions
¶
Set of actions related to node movements and interactions in the simulator.
move_with_velocity(simulator, delta_time, node, velocity)
¶
Move the node with a given velocity.
gaussian_movement(simulator, node, mean, stddev)
¶
Move a node according to a Gaussian distribution.
follow_velocity(simulator, delta_time, node, output=velocity_actuator.name, max_speed=None)
¶
Move a node, every delta_time, by the velocity its program sends.
The velocity is what the program sends to the velocity actuator
(velocity.send(v)), with as many components as the position or more: a planar
node ignores a height. A node whose program has sent nothing yet stays still.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulator
|
Simulator
|
The simulator. |
required |
delta_time
|
float
|
The time between two moves, and the time each move lasts. |
required |
node
|
Node
|
The node to move. |
required |
output
|
str
|
The name of the output holding the velocity, if not |
name
|
max_speed
|
Optional[float]
|
The speed the node cannot exceed, if any. |
None
|
phyelds.simulator.effects
¶
This module contains the effects that can be applied to the simulation rendering.
Link(node1, node2)
¶
A class to represent a link between two nodes. The link is represented as a tuple of two nodes. Note! Link(node1, node2) == Link(node2, node1)
Effect
¶
Bases: BaseModel, ABC
Abstract base class for effects.
apply(ax, environment)
abstractmethod
¶
Apply the effect to the matplotlib axis.
DrawEdges
¶
DrawNodes
¶
Bases: Effect
Draw nodes.
With color_from alone, node.data[color_from] is used as a matplotlib color.
With cmap too, it is read as a number and mapped onto that colormap between
vmin and vmax (by default, the finite values on screen); nodes whose value is
missing, infinite or not a number are drawn in missing_color.
apply(ax, environment)
¶
Draw nodes.
DrawValues
¶
RenderMode
¶
Bases: str, Enum
Enum for render modes.
RenderConfig
¶
validate_effect(v)
¶
Validate the effect type.
phyelds.simulator.render
¶
Render the nodes in the simulator's environment.
make_writer(config)
¶
Create the movie writer matching the extension of config.save_as.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
RenderConfig
|
the render configuration. |
required |
Returns:
| Type | Description |
|---|---|
AbstractMovieWriter
|
A |
phyelds.simulator.exporter
¶
Exporter module for saving simulation metrics to CSV during simulation.
ExporterConfig(output_directory, experiment_name, metrics, aggregators, precision, initial=True)
dataclass
¶
Configuration for CSV exporting of simulation metrics.
csv_exporter(simulator, time_delta, config, **kwargs)
¶
Append aggregated node outputs to a CSV file and reschedule the next export.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulator
|
Simulator
|
The running simulator whose nodes hold |
required |
time_delta
|
float
|
Delay before the next export event. |
required |
config
|
ExporterConfig
|
Export destination, metric names, aggregators and rounding precision. |
required |
**kwargs
|
Any
|
Extra keyword arguments forwarded when rescheduling. |
{}
|
aggregate_values(values, precision, aggregator)
¶
Aggregate values with aggregator and round to precision decimals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Sequence[float]
|
Numeric metric values collected across nodes. |
required |
precision
|
int
|
Number of decimal digits passed to |
required |
aggregator
|
str
|
One of |
required |
Returns:
| Type | Description |
|---|---|
float
|
The rounded aggregate value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |