Skip to content

phyelds.libraries

phyelds.libraries.device

Device library: Set of function used to get the device information.

local_id()

Get the local id of the device.

Returns:

Type Description
int

sense(sensor)

Get the value of the sensor.

Parameters:

Name Type Description Default
sensor str

The name of the sensor.

required

Returns:

Type Description
Any

The value of the sensor.

local_position()

Get the position of the device.

Returns:

Type Description
Tuple[float, float]

The position of the device.

store(output, value)

Store the value in the context.

Parameters:

Name Type Description Default
output str

The name of the stored value.

required
value Any

The value to be stored.

required

Returns:

Type Description
None

phyelds.libraries.distances

This module contains functions to calculate distances between nodes in the neighborhood. These functions may be used by other library to compute system-wise properties

neighbors_distances()

Get the distances to the neighbors from the current node.

Returns:

Type Description
NeighborhoodField[float]

the neighborhood representing the distances to the neighbors

hops_distance()

Get the hops distance to the neighbors from the current node.

Returns:

Type Description
NeighborhoodField[int]

the neighborhood representing the hops distance to the neighbors

phyelds.libraries.spreading

Diffusion (information) library for phyelds This library provides functions for diffusion of information in a network. It includes functions for calculating distances and to braodcast information

distance_to(source, distances)

Calculate the distance to a source node in the network.

Parameters:

Name Type Description Default
source bool
required
distances NeighborhoodField[float]
required

Returns:

Type Description
StateT[float]

cast_from(source, data, accumulation, distances)

Cast information from a source node to its neighbors.

Parameters:

Name Type Description Default
source bool

the source node to cast from

required
data T

the data to cast

required
accumulation Callable[[T], T]

the function to accumulate the data

required
distances NeighborhoodField[float]

the distances against the neighbors

required

Returns:

Type Description
StateT[T]

the data cast

broadcast(source, data, distances)

Broadcast information to the network from a source node.

Parameters:

Name Type Description Default
source bool

the source node to broadcast from

required
data T

the data to spread

required
distances NeighborhoodField[float]

the distances again the neighbors

required

Returns:

Type Description
StateT[T]

the data spread

phyelds.libraries.collect

Collect library functions that are used to collect information from the network to source nodes.

find_parent(potential)

Find the parent of a path giving a potential neighborhood.

Parameters:

Name Type Description Default
potential StateT[float]

The potential value (distance/gradient) at the current node.

required

Returns:

Type Description
Optional[int]

The node ID of the parent (neighbor with min potential), or None if local is min.

collect_with(potential, local, accumulation)

Generic collection function that accumulates data from children to parents based on a potential neighborhood.

Parameters:

Name Type Description Default
potential StateT[float]

The potential neighborhood used to determine parent-child relationships.

required
local T

The local value to start with (and add to).

required
accumulation Callable[[T, T], T]

A function to combine two values (e.g., sum, max, union).

required

Returns:

Type Description
StateT[T]

The accumulated state.

count_nodes(potential)

Count the number of nodes in the sub-tree defined by the potential neighborhood.

Parameters:

Name Type Description Default
potential StateT[float]

The potential field.

required

Returns:

Type Description
StateT[int]

The count of nodes.

sum_values(potential, local)

Sum numeric values up the potential gradient.

Parameters:

Name Type Description Default
potential StateT[float]

The potential field.

required
local Union[int, float]

The local numeric value.

required

Returns:

Type Description
StateT[Union[int, float]]

The sum of values.

collect_or(potential, local)

Perform a logical OR collection up the potential gradient. Useful for determining if any node in a subtree satisfies a condition.

Parameters:

Name Type Description Default
potential float

The potential field.

required
local bool

The local boolean condition.

required

Returns:

Type Description
StateT[bool]

True if any node in the subtree is True.

phyelds.libraries.gossip

Gossip Library. Set of operation in order to gossip information between nodes in the network.

gossip_max(value)

Gossip the maximum value among the all system

Parameters:

Name Type Description Default
value float

The value to gossip.

required

Returns:

Type Description
StateT[float]

The maximum value among the all system.

gossip_min(value)

Gossip the minimum value among the all system

Parameters:

Name Type Description Default
value float

The value to gossip.

required

Returns:

Type Description
StateT[float]

The minimum value among the all system.

gossip(value, callback)

Gossip a value in the network using a callable function to choice the value to gossip.

Parameters:

Name Type Description Default
value T

The value to gossip.

required
callback Callable[..., T]

The callback function to apply on the value.

required

Returns:

Type Description
StateT[T]

The gossiped value.

stabilizing_gossip(value, max_diameter, callback)

Gossip a value in the network using a callable function to choice the value to gossip. This function will stabilize the gossip value by applying the callback function until no changes are made.

Parameters:

Name Type Description Default
value T

The value to gossip.

required
max_diameter int

The maximum diameter to consider for gossiping.

required
callback Callable[..., T]

The callback function to apply on the value.

required

Returns:

Type Description
StateT[T]

The stabilized gossiped value.

phyelds.libraries.leader_election

Leader election functionality (self-stabilizing) using UUIDs.

elect_leaders(area, distances)

Elect a leader in the network using a random UUID.

Parameters:

Name Type Description Default
area float

the area of the network

required
distances NeighborhoodField[float]

the distances to the neighbors

required

Returns:

Type Description
bool

the current id of the leader

random_uuid()

Generate a random UUID for the node.

Returns:

Type Description
Tuple[StateT[float], int]

the random UUID

breaking_using_uids(uid, area, distances)

Break the symmetry using the UUID of the node.

Parameters:

Name Type Description Default
uid Tuple[StateT[float], int]

the UUID of the node

required
area float

the area of the network

required
distances NeighborhoodField[float]

the distances to the neighbors

required

Returns:

Type Description
StateT[tuple[float, int]]

the current id of the leader

distance_competition(current_distance, area, uid, lead, distances)

Compare the distance of the current node with the distance of the neighbors. It leverages the fact that the tuple is a symmetric breaker.

Parameters:

Name Type Description Default
current_distance float

current distance to the lead

required
area float

the maximum distance to the lead

required
uid Tuple[StateT[float], int]

the local id of the node

required
lead StateT[Tuple[float, int]]

the current lead

required
distances NeighborhoodField[float]

the distances to the neighbors

required

Returns:

Type Description
StateT[Tuple[float, int]]

the new lead

phyelds.libraries.time

A group of functions based on the notion of time.

local_time()

Get the local time of the node.

Returns:

Type Description
float

the local time of the node.

counter()

Simple counter function that counts the number of times it is called.

Returns:

Type Description
StateT[int]

a counter that counts the number of times it is called.

decay(value, rate)

Apply a decay function to a value

Parameters:

Name Type Description Default
value float | int

the initial value

required
rate float | int

the decay rate

required

Returns:

Type Description
StateT[float | int]

the decayed value

phyelds.libraries.utils

This module contains utility functions and classes for various purposes.

min_with_default(iterable, default=None)

Returns the minimum value from an iterable, or a default value if the iterable is empty.

Parameters:

Name Type Description Default
iterable Any

a list of values (or any iterable)

required
default Any

the default value to return if the iterable is empty

None

Returns:

Type Description
Any

the minimum value from the iterable or the default value