pydistsim.network.generator.NetworkGenerator

class NetworkGenerator(n_count=None, n_min=None, n_max=None, enforce_connected=True, degree=None, comm_range=None, method='random_network', degree_tolerance=0.5, directed=False, **kwargs)[source]

Bases: object

Class for generating networks with specified properties.

Instance mode

Usage example:

>>> net_gen = NetworkGenerator(n_count=45, degree=4, directed=False, enforce_connected=False)
>>> net = net_gen.generate_random_network()

The generated network is returned as a pydistsim.network.rangenetwork.RangeNetwork or pydistsim.network.rangenetwork.BidirectionalRangeNetwork object.

Class mode

Here instancing the class will have no effect at all. The only parameters considered are the ones passed to the class methods. The class methods are:

  1. generate_complete_network()

  2. generate_ring_network()

  3. generate_star_network()

  4. generate_hypercube_network()

  5. generate_mesh_network()

Usage example:

>>> net = NetworkGenerator.generate_complete_network(5)
param n_count:

int, number of nodes, if None, 100 is used

param n_min:

int, minimum number of nodes, if not set it is equal to n_count

param n_max:

int, maximum number of nodes, if not set it is equal to n_count

param enforce_connected:

bool, if True network must be fully connected

param degree:

int, average number of neighbors per node

param comm_range:

int, nodes communication range, if None, 100 is used and it is a signal that this value can be changed if needed to satisfy other wanted properties (connected and degree)

param method:

str, sufix of the name of the method used to generate network

param degree_tolerance:

float, tolerance for degree parameter

param directed:

bool, if True generated network is directed

param kwargs:

network and node __init__ kwargs i.e.: - environment: Environment, environment in which the network should be created, if None Environment2D is used - rangeType: RangeType - algorithms: tuple - commRange: int, overrides comm_range - sensors: tuple

Methods

__init__

generate_complete_network

Generate a complete network with n nodes.

generate_grid_network

Generate a mesh (or torus) network with n nodes (or a x b nodes).

generate_homogeneous_network

Generates a network where nodes are located approximately homogeneous.

generate_hypercube_network

Generate a hypercube network of the given dimension (or node count).

generate_mesh_network

Generate a mesh (or torus) network with n nodes (or a x b nodes).

generate_neigborhood_network

Generates a network where all nodes are in one hop neighborhood of at least one node.

generate_random_network

Generates a random network with randomly positioned nodes.

generate_ring_network

Generate a ring network with n nodes.

generate_star_network

Generate a star network with n nodes.

DIRECTED_NETWORK_T

alias of RangeNetwork

UNDIRECTED_NETWORK_T

alias of BidirectionalRangeNetwork

__get_ring_pos(env: Environment2D) list[tuple[float, float]]

A helper method for generating positions of nodes on a circle.

Parameters:
  • n (int) – The number of nodes.

  • env (Environment2D) – The environment object.

Returns:

The list of positions.

Return type:

list[tuple[int, int]]

_are_conditions_satisfied(net: RangeNetworkType)[source]

Check if the conditions for the network are satisfied.

Parameters:

net (Network) – The network to check the conditions for.

Returns:

The condition value.

Return type:

int

_create_modify_network(net: RangeNetworkType | None = None, step=1) RangeNetworkType | None[source]

Helper method for creating a new network or modifying a given network.

Parameters:
  • net – NetworkType object, optional The network to modify. If None, create a new network from scratch.

  • step – int, optional If step > 0, the new network should be more dense. If step < 0, the new network should be less dense.

Returns:

NetworkType object or None The modified network if successful, None otherwise.

classmethod generate_complete_network(n: int, network_type: type[T] = None, directed_network: bool = None) T[source]

Generate a complete network with n nodes. The nodes are placed on a circle.

Examples:

>>> net1 = NetworkGenerator.generate_complete_network(5)
<BidirectionalNetwork object with 5 nodes and 10 edges>
>>> net2 = NetworkGenerator.generate_complete_network(5, directed_network=True)
<DirectedNetwork object with 5 nodes and 20 edges>
>>> net3 = NetworkGenerator.generate_complete_network(5, network_type=BidirectionalNetwork)
<BidirectionalNetwork object with 5 nodes and 10 edges>

DO NOT instantiate the class, this is a class method.

Parameters:
  • n (int) – The number of nodes in the network.

  • network_type (type[NetworkType]) – The type of network to generate.

  • directed_network (bool) – Only if True, the network is directed.

Returns:

The generated network.

Return type:

NetworkType

classmethod generate_grid_network(n: int = None, a: int = None, b: int = None, torus: bool = False, network_type: type[T] = None, directed_network: bool = None) T

Generate a mesh (or torus) network with n nodes (or a x b nodes). If network_type is a directed type, the links are only north and east. Only with torus, this gives a fully connected network nonetheless.

Example:

>>> net = NetworkGenerator.generate_mesh_network(a=4, b=7)

DO NOT instantiate the class, this is a class method.

Parameters:
  • n (int) – The number of nodes in the mesh/torus network. Optional.

  • a (int) – Width of the mesh/torus. Optional.

  • b (int) – Height of the mesh/torus. Optional.

  • torus (bool) – Whether or not to add “returning” edges.

  • network_type (type[NetworkType]) – The type of network to generate.

  • directed_network (bool) – Only if True, the network is directed.

Returns:

The generated network.

Return type:

NetworkType

generate_homogeneous_network(randomness=0.11) RangeNetworkType | None[source]

Generates a network where nodes are located approximately homogeneous.

Parameters:

randomness (float) – Controls random perturbation of the nodes. It is given as a part of the environment size.

Returns:

The generated random network.

Return type:

RangeNetworkType

static generate_hypercube_network(n: int | None = None, dimension: int | None = None, use_binary_labels: bool = True, network_type: type[T] = None, directed_network: bool = None) T[source]

Generate a hypercube network of the given dimension (or node count). The nodes are placed in a hypercube structure.

Examples:

>>> net = NetworkGenerator.generate_hypercube_network(dimension=3)
<BidirectionalNetwork object with 8 nodes and 12 edges>
>>> net2 = NetworkGenerator.generate_hypercube_network(dimension=3, directed_network=True)
<DirectedNetwork object with 8 nodes and 24 edges>
>>> net3 = NetworkGenerator.generate_complete_network(n=8)
<BidirectionalNetwork object with 8 nodes and 12 edges>

DO NOT instantiate the class, this is a class method.

Parameters:
  • n (int) – The number of nodes in the network. If None, the dimension parameter must be set.

  • dimension (int) – The dimension of the hypercube. If None, the n parameter must be set.

  • network_type (type[NetworkType]) – The type of network to generate.

  • directed_network (bool) – Only if True, the network is directed.

Returns:

The generated network.

Return type:

NetworkType

classmethod generate_mesh_network(n: int = None, a: int = None, b: int = None, torus: bool = False, network_type: type[T] = None, directed_network: bool = None) T[source]

Generate a mesh (or torus) network with n nodes (or a x b nodes). If network_type is a directed type, the links are only north and east. Only with torus, this gives a fully connected network nonetheless.

Example:

>>> net = NetworkGenerator.generate_mesh_network(a=4, b=7)

DO NOT instantiate the class, this is a class method.

Parameters:
  • n (int) – The number of nodes in the mesh/torus network. Optional.

  • a (int) – Width of the mesh/torus. Optional.

  • b (int) – Height of the mesh/torus. Optional.

  • torus (bool) – Whether or not to add “returning” edges.

  • network_type (type[NetworkType]) – The type of network to generate.

  • directed_network (bool) – Only if True, the network is directed.

Returns:

The generated network.

Return type:

NetworkType

generate_neigborhood_network() RangeNetworkType[source]

Generates a network where all nodes are in one hop neighborhood of at least one node.

Finds out the node in the middle, which is the node with the minimum maximum distance to all other nodes, and sets that distance as the new commRange.

This generator ignores all other parameters except comm_range and n counts.

Returns:

The generated network.

Return type:

RangeNetworkType

generate_random_network(net: RangeNetworkType | None = None, max_steps=1000) RangeNetworkType | None[source]

Generates a random network with randomly positioned nodes.

Parameters:
  • net (Optional[RangeNetworkType]) – The network to modify. If not provided, a new network will be created.

  • max_steps (int) – The maximum number of steps to take.

Returns:

The generated network, optional.

Return type:

Optional[RangeNetworkType]

classmethod generate_ring_network(n: int, network_type: type[T] = None, directed_network: bool = None) T[source]

Generate a ring network with n nodes. The nodes are placed on a circle. If network_type is a directed type, the links between the nodes are one-way only so the network is fully connected.

Example:

>>> net = NetworkGenerator.generate_ring_network(6)
<BidirectionalNetwork object with 6 nodes and 6 edges>
>>> net = NetworkGenerator.generate_ring_network(6, directed_network=False)
<DirectedNetwork object with 6 nodes and 6 edges>

DO NOT instantiate the class, this is a class method.

Parameters:
  • n (int) – The number of nodes in the network.

  • network_type (type[NetworkType]) – The type of network to generate.

  • directed_network (bool) – Only if True, the network is directed.

Returns:

The generated network.

Return type:

NetworkType

classmethod generate_star_network(n: int, network_type: type[T] = None, directed_network: bool = None) T[source]

Generate a star network with n nodes. The nodes are placed on a circle with one node in the center. If network_type is a directed type, the links are only from the center node to the other nodes.

Example:

>>> net = NetworkGenerator.generate_star_network(5)

DO NOT instantiate the class, this is a class method.

Parameters:
  • n (int) – The number of nodes in the network.

  • network_type (type[NetworkType]) – The type of network to generate.

  • directed_network (bool) – Only if True, the network is directed.

Returns:

The generated network.

Return type:

NetworkType