topsim.core.simulation.Simulation

class topsim.core.simulation.Simulation(env, config, instrument, planning_model, scheduling, delay=None, timestamp=None, to_file=False, hdf5_path=None, use_task_data=False, use_edge_data=True, **kwargs)[source]

The Simulation class is a wrapper for all Actors; we start the simulation through the simulation class, which in turn invokes the initial Actors and monitoring, and provides the conditions for checking if the simulation has finished.

Parameters:
  • env (simpy.Environment object) – The discrete-event simulation environment. This is the way TOpSim simulation maintains state across the different actors, and interfaces with the simpy processes.

  • config (Path) – Path to the simulation JSOn configuration file

  • instrument (Instrument) – User-defined implementation of the Instrument class.

  • planning_model (Planning object) – User-defined implementation of the planning algorithm class

  • scheduling (Algorithm) – User-defined implementation of the scheduling algorithm abc.ABC.

  • delay (DelayModel, optional) – for the simulation.

  • timestamp (float, optional) – Optional Simulation start-time; this is useful for testing, to ensure we name the file and the tests match up. Also useful if you do not want to use the time of the simulation as the name.

  • to_file (bool, optional) – True if the simulation is to be written to a Pandas pkl file; False will return pandas DataFrame objects at the completion of the run() function.

Notes

If to_file left as False, simulation results and output will be returned as Pandas DataFrames (see run()) . This is designed for running multiple simulations, allowing for the appending of individual simulation results to a ‘global’ DataFrame . Current support for output is limited to Panda’s .pkl files.

Parsing in the option delimiters provides a way of differentiating between multiple simulations within a single HDF5 store (for example, in an experiment). A typical experimental loop may involve the following structure:

>>> for heuristic in list_of_scheduling_heuristics
>>>     for algorithm in list_of_planning_algorithms
>>>         for cfg in list_of_system_configs
>>>             ...
>>>             delimiter = f'{heuristic}/{algorithm}/{cfg}'

This means when querying HDF5 output files, the results of each simulation can be filtered nicely:

>>> store = pd.HDFStore('path/to/output.h5')
>>> # Returns a dataframe of simulation results
>>> store['heuristic_1/algorithm_3/cfg.json']

Examples

Standard simulation with data frame output

>>> env = simpy.environment()
>>> config = Path('path/to/config')
>>> instrument = CustomInstrument()
>>> plan = PlanningModel()
>>> sched = SchedulingModel()
>>> simulation = Simulation(env, config, instrument,plan,sched)

If we want delays in the model:

>>> dm = DelayModel(prob=0.1, dist='normal', dm=DelayModel.DelayDegree.LOW)
>>> simulation =  Simulation(
>>>    env, config, instrument,plan,sched, delay=dm
>>> )

Running a simulation to completion:

>>> simulation.run()

Running a simulation for a specific time period, then resuming:

>>> simulation.run(runtime=100)
>>> ### Check current status of simulatiion
>>> simulation.resume(until=150)

Attributes

env

simpy.Environment object

cluster

Cluster instance

scheduler

Scheduler instance

instrument

User-defined Instrument instance

Methods

__init__(env, config, instrument, ...[, ...])

is_finished()

Check if simulation is finished based on the following finish conditions:

resume(until)

Resume a simulation for a period of time.

start([runtime])

Run the simulation, either for the specified runtime, OR until the exit conditions are reached.

summary()

Produce a formatted summary of events that occured during simulation