pydistsim.network.network.NetworkMixin
- class NetworkMixin(incoming_graph_data=None, environment: Environment | None = None, behavioral_properties: NetworkBehaviorModel | None = None, **kwargs)[source]
Bases:
ObserverManagerMixin,objectMixin to extend Graph and DiGraph. The result represents a network in a distributed simulation.
The Network classes (
DirectedNetworkandBidirectionalNetwork) 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 a message to the lost messages from node u to node v.
Add node to network.
add_observersAdd a message to the in-transit messages from node u to node v.
Calculate the average degree of the network.
clear_observersPass all messages from node's outboxes to its neighbors inboxes.
Return a copy of the graph.
Deliver a message to a destination node in the network.
Return all network data in the form of a dictionary.
Get the figure object representing the network visualization.
Get lost messages from node u to node v.
Returns network width and height based on nodes positions.
Get messages in transit from node u to node v.
Returns a new network with edges that are not in a tree removed.
in_neighborsIncrement the clock of all nodes in the network.
is_connectedReturns the first node with the given id.
nodesReturned sorted nodes by id.
notify_observersout_neighborsRemove a node from the network.
Reset the network to its initial state.
Reset all nodes in the network.
savefigGet and show the figure object representing the network visualization.
Returns a NetworkType instance with the specified nodes and edges.
to_directed_classto_undirected_classValidate 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.
- 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:
- 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.
- 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:
- deliver_to(destination: Node, message: Message)[source]
Deliver a message to a destination node 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:
- get_transit_messages(u: Node, v: Node) dict[Message, int][source]
Get messages in transit from node u to node v.
- 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:
- 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:
- 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.
- 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: