pydistsim.network.network.NetworkMixin

class NetworkMixin(incoming_graph_data=None, environment: Environment | None = None, behavioral_properties: NetworkBehaviorModel | None = None, **kwargs)[source]

Bases: ObserverManagerMixin, object

Mixin to extend Graph and DiGraph. The result represents a network in a distributed simulation.

The Network classes (DirectedNetwork and BidirectionalNetwork) extend the Graph class and provides additional functionality for managing nodes and network properties.

Parameters:
  • environment (Environment, optional) – The environment in which the network operates. If not provided, a new Environment instance will be created.

  • graph (NetworkX graph, optional) – The graph representing the network topology. Defaults to None.

  • kwargs – Additional keyword arguments.

Methods

__init__

add_lost_message

Add a message to the lost messages from node u to node v.

add_node

Add node to network.

add_observers

add_transit_message

Add a message to the in-transit messages from node u to node v.

avg_degree

Calculate the average degree of the network.

clear_observers

communicate

Pass all messages from node's outboxes to its neighbors inboxes.

copy

Return a copy of the graph.

deliver_to

Deliver a message to a destination node in the network.

get_dic

Return all network data in the form of a dictionary.

get_fig

Get the figure object representing the network visualization.

get_lost_messages

Get lost messages from node u to node v.

get_size

Returns network width and height based on nodes positions.

get_transit_messages

Get messages in transit from node u to node v.

get_tree_net

Returns a new network with edges that are not in a tree removed.

in_neighbors

increment_node_clocks

Increment the clock of all nodes in the network.

is_connected

node_by_id

Returns the first node with the given id.

nodes

nodes_sorted

Returned sorted nodes by id.

notify_observers

out_neighbors

remove_node

Remove a node from the network.

reset

Reset the network to its initial state.

reset_all_nodes

Reset all nodes in the network.

savefig

show

Get and show the figure object representing the network visualization.

subnetwork

Returns a NetworkType instance with the specified nodes and edges.

to_directed_class

to_undirected_class

validate_params

Validate if given network params match its real params.

Attributes

environment

_set_environment(environment: Environment)[source]

Setter for environment. Override this method if default behavior is not enough.

Parameters:

environment (Environment) – The new environment for the network.

add_lost_message(u: Node, v: Node, message: Message)[source]

Add a message to the lost messages from node u to node v.

Parameters:
  • u (Node) – The source node.

  • v (Node) – The destination node.

  • message (Message) – The message

add_node(node=None, pos=None, ori=None, commRange=None)[source]

Add node to network.

Parameters:
  • node (Node, optional) – node to add, default: new node is created

  • pos (tuple, optional) – position (x,y), default: random free position in environment

  • ori (float, optional) – orientation from 0 to 2*pi, default: random orientation

  • commRange (float, optional) – communication range of the node, default: None

Returns:

the added node

Return type:

Node

Raises:

NetworkException – if the given node is already in another network or the given position is not free space

add_transit_message(u: Node, v: Node, message: Message, delay: int)[source]

Add a message to the in-transit messages from node u to node v.

Parameters:
  • u (Node) – The source node.

  • v (Node) – The destination node.

  • message (Message) – The message

  • delay (int) – The delay of the message

avg_degree()[source]

Calculate the average degree of the network. Uses out_degree for directed networks (amount of outgoing edges) and degree for undirected networks.

Returns:

The average degree of the network.

Return type:

float

communicate()[source]

Pass all messages from node’s outboxes to its neighbors inboxes.

This method collects messages from each node’s outbox and delivers them to the appropriate destination. If the message is a broadcast, it is sent to all neighbors. If the message has a specific next hop, it is sent directly to that node. If the message has a specific destination, it is sent to that neighbor if it is reachable.

Returns:

None

copy(as_view=False)[source]

Return a copy of the graph. Parameters as_view is not used and is only for compatibility with NetworkX.

Parameters:

as_view (bool, optional) – Unused parameter. Defaults to False.

Returns:

A deepcopy of the graph.

Return type:

NetworkMixin

deliver_to(destination: Node, message: Message)[source]

Deliver a message to a destination node in the network.

Parameters:
  • destination (Node) – The destination node to send the message to.

  • message (Message) – The message to be sent.

Raises:

PyDistSimMessageUndeliverable – If the destination is not in the network.

get_dic()[source]

Return all network data in the form of a dictionary.

Returns:

A dictionary containing the network data.

Return type:

dict

get_fig(*args, **kwargs)[source]

Get the figure object representing the network visualization.

Parameters:
  • args – Additional positional arguments to pass to the draw function.

  • kwargs – Additional keyword arguments to pass to the draw function.

Returns:

The figure object representing the network visualization.

Return type:

matplotlib.figure.Figure

get_lost_messages(u: Node, v: Node) list[Message][source]

Get lost messages from node u to node v.

Parameters:
  • u (Node) – The source node.

  • v (Node) – The destination node.

Returns:

A list of lost messages from node u to node v.

Return type:

list[Message]

get_size()[source]

Returns network width and height based on nodes positions.

get_transit_messages(u: Node, v: Node) dict[Message, int][source]

Get messages in transit from node u to node v.

Parameters:
  • u (Node) – The source node.

  • v (Node) – The destination node.

Returns:

A dictionary of messages in transit from node u to node v, with the message as the key and the delay as the value.

Return type:

Dict[Message, int]

get_tree_net(tree_key, return_subnetwork=True)[source]

Returns a new network with edges that are not in a tree removed.

The tree is defined in the nodes’ memory under the specified tree_key. The method iterates over all nodes in the network and checks if the tree_key is present in their memory. If it is, it retrieves the tree neighbors or children and adds the corresponding edges to the tree_edges_ids list. It also adds the tree nodes to the tree_nodes list.

After iterating over all nodes, a subnetwork is created using the tree_nodes. Then, the method removes any edges from the subnetwork that are not present in the tree_edges_ids list.

Finally, the resulting subnetwork, representing the tree, is returned.

Parameters:
  • tree_key (str) – The key in the nodes’ memory that defines the tree. It can be a list of tree neighbors or a dictionary with ‘parent’ (node) and ‘children’ (list) keys.

  • return_subnetwork (bool, optional) – Whether to return a new network or the sets of nodes and edges. Defaults to True.

Returns:

A new network object with only the edges that are part of the tree.

Return type:

NetworkMixin

increment_node_clocks()[source]

Increment the clock of all nodes in the network.

Follows the clock increment function defined in the behavioral properties.

Returns:

None

node_by_id(id_)[source]

Returns the first node with the given id.

Parameters:

id (int) – The id of the node to search for.

Returns:

The node with the given id.

Return type:

Node

Raises:

NetworkException – If the network does not have a node with the given id.

nodes_sorted() list[Node][source]

Returned sorted nodes by id.

Returns:

A sorted tuple of nodes.

Return type:

list[Node]

remove_node(node: Node, skip_check=False)[source]

Remove a node from the network.

Parameters:

node (Node) – The node to be removed.

Raises:

NetworkException – If the node is not in the network.

reset(log=True)[source]

Reset the network to its initial state.

Does not reset the observers of the network nor the observers of the nodes.

reset_all_nodes()[source]

Reset all nodes in the network.

Returns:

None

show(*args, **kwargs)[source]

Get and show the figure object representing the network visualization.

Parameters:
  • args – Additional positional arguments to pass to the draw function.

  • kwargs – Additional keyword arguments to pass to the draw function.

Returns:

The figure object representing the network visualization.

Return type:

matplotlib.figure.Figure

subnetwork(nodes: list[Node], edges: None | Iterable[tuple[Node, Node]] = None)[source]

Returns a NetworkType instance with the specified nodes and edges.

Parameters:
  • nbunch (list[Node]) – A list of nodes to include in the subnetwork.

  • edges (Iterable[tuple[Node, Node]], optional) – A list of edges to include in the subnetwork. Defaults to None.

Returns:

A NetworkType instance representing the subnetwork.

Return type:

NetworkType

validate_params(params: dict)[source]

Validate if given network params match its real params.

Parameters:

params (dict) – A dictionary containing the network parameters to validate.

Raises:

AssertionError – If any of the network parameters do not match the real parameters.