pydistsim.simulation.Simulation

class Simulation(network: NetworkType, algorithms: tuple[type[BaseAlgorithm] | tuple[type[BaseAlgorithm], dict]] | None = None, check_restrictions: bool = True, **kwargs)[source]

Bases: ObserverManagerMixin

Controls single network algorithm and node algorithms simulation. It is responsible for visualization and logging, also.

Parameters:
  • network (NetworkType) – The network object representing the simulation network.

  • algorithms (AlgorithmsParam, optional) – The algorithms to be executed on the network.

  • check_restrictions (bool, optional) – Whether to check restrictions during the simulation.

  • kwargs – Additional keyword arguments.

Methods

__init__

add_observers

clear_observers

get_current_algorithm

Try to return the current algorithm based on the algorithmState.

get_dic

Return all simulation data in the form of a dictionary.

is_halted

Check if simulation has come to an end or deadlock, i.e. there are no messages to pass and no alarms set.

notify_observers

reset

Reset the simulation.

run

Run simulation from the current state.

run_step

Run a single step of the simulation.

Attributes

algorithms

Set algorithms by passing tuple of Algorithm subclasses.

network

Get the network associated with the simulation.

_run_algorithm(algorithm: BaseAlgorithm)[source]

Run the given algorithm on the given network.

Update stepsLeft and sim.algorithmState[‘step’]. If stepsLeft hit 0 it may return unfinished.

Parameters:

algorithm – The algorithm to run on the network.

property algorithms

Set algorithms by passing tuple of Algorithm subclasses.

>>> sim.algorithms = (Algorithm1, Algorithm2,)

For params pass tuples in form (Algorithm, params) like this

>>> sim.algorithms = ((Algorithm1, {'param1': value,}), Algorithm2)
get_current_algorithm() BaseAlgorithm | None[source]

Try to return the current algorithm based on the algorithmState.

Returns:

The current algorithm.

Return type:

BaseAlgorithm or None

Raises:

NetworkException – If there are no algorithms defined in the network.

get_dic()[source]

Return all simulation data in the form of a dictionary.

Returns:

A dictionary containing the simulation data.

Return type:

dict

is_halted()[source]

Check if simulation has come to an end or deadlock, i.e. there are no messages to pass and no alarms set.

A not-started algorithm is considered halted. If there are no algorithms left to run, the simulation is also considered halted.

Returns:

True if the algorithm is halted, False otherwise.

Return type:

bool

property network

Get the network associated with the simulation.

reset()[source]

Reset the simulation.

run(steps=0)[source]

Run simulation from the current state.

Parameters:

steps (int) – Number of steps to run the simulation. If steps = 0, it runs until all algorithms are finished. If steps > 0, the simulation is in stepping mode. If steps > number of steps to finish the current algorithm, it finishes it.

run_step()[source]

Run a single step of the simulation.

This is equivalent to calling sim.run(1).