gossipy.simul module#
Module contents#
- class gossipy.simul.GossipSimulator(nodes, data_dispatcher, delta, protocol, drop_prob=0.0, online_prob=1.0, delay=ConstantDelay(0), sampling_eval=0.0)#
Bases:
gossipy.simul.SimulationEventSender
Class that implements a vanilla gossip learning simulation.
The simulation is divided into “rounds” and each round consists of a
delta
timesteps. A single time step represent the time unit of the simulation. At each time step, the nodes that timed out act according to the gossip protocol, e.g., in the case of the PUSH protocol, the nodes send a message (i.e., its model) to a random neighbor. The message arrives at the destination node with adelay
(seegossipy.simul.Delay
). Messages can also drop according to a probability defined by thedrop_prob
parameter. Similarly, nodes can drop according to a probability equals to1 - online_prob
. Nodes are considered in a random order even if they time out in the same timestep.The simulator implements the design pattern Observer (actually Event Receiver) extending the
gossipy.simul.SimulationEventSender
class. The events are:update_message()
: a message has been sent or dropped;update_evaluation()
: an evaluation has been computed;update_timestep()
: a timestep has been performed;update_end()
: the simulation has ended.
- Parameters
nodes (dict[int, GossipNode]) – The nodes participating in the simulation. The keys are the node ids, and the values are the corresponding nodes (instances of the class
GossipNode
).data_dispatcher (DataDispatcher) – The data dispatcher. Useful if the evaluation is performed on a separate test set, i.e., not on the nodes.
delta (int) – The number of timesteps of a round.
protocol (AntiEntropyProtocol) – The protocol of the gossip simulation.
drop_prob (float, default=0.) – The probability of a message being dropped.
online_prob (float, default=1.) – The probability of a node to be online.
delay (Delay, default=ConstantDelay(0)) – The (potential) function delay of the messages.
sampling_eval (float, default=0.) – The percentage of nodes to use during evaluate. If 0 or 1, all nodes are considered.
- init_nodes(seed=98765)#
Initializes the nodes.
The initialization of the nodes usually involves the initialization of the local model (see
GossipNode.init_model()
).
- classmethod load(filename)#
Loads the state of the simulator (including the models’ cache).
- Parameters
filename (str) – The name of the file to load the state.
- Returns
The simulator loaded from the file.
- Return type
- save(filename)#
Saves the state of the simulator (including the models’ cache).
- start(n_rounds=100)#
Starts the simulation.
The simulation handles the messages exchange between the nodes for
n_rounds
rounds. If attached to aSimulationReport
, the report is updated at each time step, sent/fail message and evaluation.
- class gossipy.simul.SimulationEventReceiver#
Bases:
abc.ABC
The event receiver interface declares all the update methods, used by the event sender.
- update_evaluation(round, on_user, evaluation)#
Receives an update about an evaluation.
- abstract update_message(failed, msg=None)#
Receives an update about a sent or a failed message.
- class gossipy.simul.SimulationEventSender#
Bases:
abc.ABC
The event sender interface declares methods for managing receviers.
- add_receiver(receiver)#
Attaches an event receiver to the event sender.
- Parameters
receiver (SimulationEventReceiver) – The receiver to attach.
- Return type
- notify_evaluation(round, on_user, evaluation)#
Notifies all receivers about a performed evaluation.
- notify_message(falied, msg=None)#
Notifies all receivers about a sent message or a failed message.
- Parameters
falied (bool) – Whether the message was sent or not.
msg_size (Message or None, default=None) – The message.
msg (Optional[gossipy.core.Message]) –
- Return type
- notify_timestep(t)#
Notifies all receivers that a timestep has happened.
- Parameters
t (int) – The timestep number.
- remove_receiver(receiver)#
Detaches an event receiver from the event sender.
If the
receiver
is not attached to the event sender, nothing happens.- Parameters
receiver (SimulationEventReceiver) – The receiver to detach.
- Return type
- class gossipy.simul.SimulationReport#
Bases:
gossipy.simul.SimulationEventReceiver
Class that implements a basic simulation report.
The report traces the number of sent messages, the number of failed messages, the total size of the sent messages, and the evaluation metrics (both global and local).
The report is updated according to the design pattern Observer (actually Event Receiver). Thus, the report must be created and attached to the simulation before starting it.
Examples
>>> from gossipy.simul import SimulationReport >>> from gossipy.simul import GossipSimulator >>> simulator = GossipSimulator(...) >>> report = SimulationReport() >>> simulator.add_receiver(report) >>> simulator.start(...)
The
report
object is now attached to the simulation and it will be notified about the events.See also
gossipy.Sizeable
- update_evaluation(round, on_user, evaluation)#
Receives an update about an evaluation.
- update_message(failed, msg=None)#
Receives an update about a sent or a failed message.
- class gossipy.simul.TokenizedGossipSimulator(nodes, data_dispatcher, token_account, utility_fun, delta, protocol, drop_prob=0.0, online_prob=1.0, delay=ConstantDelay(0), sampling_eval=0.0)#
Bases:
gossipy.simul.GossipSimulator
Class that implements a gossip learning simulation using token account [].
The simulation happens similary to the
GossipSimulator
, but the communication pattern is handled by a token account algorithm (seeTokenAccount
). Token account based flow control mechanism can be useful in case of bursty communication [].The simulator implements the design pattern Observer (actually Event Receiver) extending the
gossipy.simul.SimulationEventSender
class. The events are:update_message()
: a message has been sent or dropped;update_evaluation()
: an evaluation has been computed;update_timestep()
: a timestep has been performed;update_end()
: the simulation has ended.
- Parameters
nodes (dict[int, GossipNode]) – The nodes participating in the simulation. The keys are the node ids, and the values are the corresponding nodes (instances of the class
GossipNode
).data_dispatcher (DataDispatcher) – The data dispatcher. Useful if the evaluation is performed on a separate test set, i.e., not on the nodes.
token_account (TokenAccount) – The token account strategy.
utility_fun (Callable[[ModelHandler, ModelHandler, Message], int]) – Function defining the usefulness of a message. The usefulness expresses the notion that some messages are more important than others in most applications. For example, in the broadcast application, the received message is useful if and only if it contains new information for the node. The signatue of the function is:
utility_fun(model_handler_1, model_handler_2, msg) -> int
wheremodel_handler_1
andmodel_handler_2
are the model handlers of the nodes involved in the message exchange, andmsg
is the message. The function must return an integer value.delta (int) – The number of timesteps of a round.
protocol (AntiEntropyProtocol) – The protocol of the gossip simulation.
drop_prob (float, default=0.) – The probability of a message being dropped.
online_prob (float, default=1.) – The probability of a node to be online.
delay (Delay, default=ConstantDelay(0)) – The (potential) function delay of the messages.
sampling_eval (float, default=0.) – The percentage of nodes to use during evaluate. If 0 or 1, all nodes are considered.
- init_nodes(seed=98765)#
Initializes the nodes.
The initialization of the nodes usually involves the initialization of the local model (see
GossipNode.init_model()
).
- start(n_rounds=100)#
Starts the simulation.
The simulation handles the messages exchange between the nodes for
n_rounds
rounds. If attached to aSimulationReport
, the report is updated at each time step, sent/fail message and evaluation.