Skip to content

Discrete Event Simulation in Python with Py-DES

In this sectrion we’ll explore the Py-DES basic ojjects and its main capabilities.

Setup

Easiest way to setup Py-DES is to install it using pip with the following command.

$ pip install py-des-lib

Hello-World

from pydes import Component, Simulator

class Process(Component):
    def __init__(self, sim: Simulator):
        self.sim = sim

    def main(self):
        for _ in range(10):
            print(self.sim.now(),"waiting")
            self.sim.sleep(2)
            print(self.sim.now(),"waiting")

Now schedule the main process object and run simulation

1
2
3
4
sim = Simulator()
p = Process(sim)
sim.schedule(p)
sim.run()

Simulator Object

Simulator is the central object of Py-DES and is used to model all the process and events of the system.

Its main objective is to schedule process and events and then execute them in a time-ordered way.

Parameters:

Name Type Description Default
initial_time float | datetime

The initial simulation time specified as a float or datetime object.

0
trace bool

Indicates whether tracing is enabled or not.

True

Simulators can be instantiated either using numeric time (float or int) or datetime time.

To create a Simulator with numeric time units simply ommit the argument or pass a specific until argument.

sim = Simulator(until=10)

If you prefer to use datetime objects, you can pass to the until argument a datetime object.

from datetime import datetime
sim = Simulator(until=datetime.max)

Once you created the Simulator object you can start modeling your procesess using its differents methods.

Methods:

Name Description
sleep

Sleep for the given duration.

sleep_until

Sleep until the given simulation time.

wait_for

Suspends the process until a condition becomes true.

schedule

Activates a process either immediately (if both at and after are None) or after a delay.

run

Starts simulation.

record

records an event by passing a component a value and optionally a description.

records

returns a list with all the recors that were saved during the simulation.

Component Object

Base class for components in the simulation.

All subclasses of Component can define a main method which is the underlying process that this Component is going to be excecuting.

Once the main method is defined, this component can be scheduled into the simulation using the schedule method of Simulator

from pydes.process import Component, Simulator

# define the component with a main method
class Process(Component):
    def __init__(self, sim: Simulator):
        self.sim = sim

    def main(self):
        for _ in range(10):
            print(self.sim.now(),"waiting")
            self.sim.sleep(2)
            print(self.sim.now(),"waiting")

# create the simulator object
sim = Simulator()

# create an instance of the Component
process = Process(sim)

# schedule the process in the simulator
sim.schedule(process)

# now you can run the simulation
sim.run()

Recording Events

The Monitor is simply a convenient class to record different events or moments during the simulation. Simulation without data analytics is useless.

The Monitor is used internally by the Simulator and is not ment to be used outside this context.

To record an event you simply have to call the record method o the simulator and pass 2 required parameters and optionally a third one.

from pydes.process import Component, Simulator

class Process(Component):
    def __init__(self, sim: Simulator):
        self.sim = sim

    def main(self):
        for _ in range(10):
            self.sim.record(self,"start waiting")
            self.sim.sleep(2)
            self.sim.record(self,"end waiting","this is an aditional description")

When the simulation runs, you'll see all you recorded events printed out. Besides, these records can be retreived for further analysis using the records from the Simulator.

To turn off the printing of the records during simulation, you can pass 'trace=False' to the Simulator constructor.

Parameters:

Name Type Description Default
sim Simulator

The simulator instance.

required
trace bool

Indicates whether tracing is enabled or not.

required