simpype.simulation

SimPype’s simulation.

# Import SimPype module
import simpype
# Import python random module
import random

# [Mandatory] Create a SimPype simulation object
sim = simpype.Simulation(id = 'simple')
# [Optional] Fix the seed for the pseudo-random generator
sim.seed = 42
# [Optional] Configure the log directory.
# [Default] Log are stored by default in the 'current working directory/log'
sim.log.dir = 'mylog'

# [Mandatory] Add at least one generator to the simulation
gen0 = sim.add_generator(id = 'gen0')
# [Mandatory] Assign an arrival time
# Generator.random is a custom dictionary accepting the following format as values:
# generator.random[<some_id>] = {
#       <initial_time> : lambda: <value>/<random_function>
#       ...
# }
# Random values can be generated in the following way:
#       generator.random[<some_id>].value
# The random value is:
#       <value>/<random_function> the simulation time is equal or
# greater than (>=) <initial_time>, 0 otherwise
gen0.random['arrival'] = {
        # From t=0 to t=10, arrival is constant every 3s
        0       : lambda: 3.0,
        # From t=10 to t=20, arrival is uniform between 2.5 and 3.5
        10      : lambda: random.uniform(2.5, 3.5),
        # From t=20 to t=inf, arrival is expovariate with lambda 0.20
        20      : lambda: random.expovariate(0.20)
}

# [Mandatory] Add at least one resource to the simulation
res0 = sim.add_resource(id = 'res0')
# [Mandatory] Assign a service time
# Resource.random is a dictionary accepting the same Generator.random format
res0.random['service'] = {
        # From t=0 to t=10, service is constant at 1.5s
        0       : lambda: 1.5,
        # From t=10 to t=20, service is uniform between 1.5 and 2.5
        10      : lambda: random.uniform(1.5, 2.5),
        # From t=20 to t=inf, arrival is expovariate with lambda 2.0
        20      : lambda: random.expovariate(2.0)
}

# [Mandatory] Add a pipeline connecting the generator and the resource
p0 = sim.add_pipeline(gen0, res0)

# [Mandatory] Run the simulation e.g. until t=30
#             sim.run calls Simpy's env.run
#             Any arg passed to sim.run is then passed to env.run
sim.run(until = 30)

The log directory structure is the following:

log.dir
|-- <simulation #1>
|   |-- <run #1>
|   |   |-- sim.cfg
|   |   `-- sim.log
|   |-- <run #2>
|   |   |-- sim.cfg
|   |   `-- sim.log
|-- <simulation #2>
|   |-- <run #1>
|   |   |-- sim.cfg
|   |   `-- sim.log
|   |-- <run #2>
|   |   |-- sim.cfg
|   |   `-- sim.log
|   ...
...

sim.cfg contains information about the simulation environment and has the following format:

Simulation Seed: 42
Simulation Time: 30.000000000
Execution Time: 0.003298451

sim.log contains the actual log of the simulation events and has the following format:

timestamp,message,seq_num,resource,event
0.000000000,gen0,0,res0,pipe.in
0.000000000,gen0,0,res0,pipe.out
1.500000000,gen0,0,res0,resource.serve
3.000000000,gen0,1,res0,pipe.in
3.000000000,gen0,1,res0,pipe.out
4.500000000,gen0,1,res0,resource.serve
6.000000000,gen0,2,res0,pipe.in
6.000000000,gen0,2,res0,pipe.out
7.500000000,gen0,2,res0,resource.serve
9.000000000,gen0,3,res0,pipe.in
9.000000000,gen0,3,res0,pipe.out
10.500000000,gen0,3,res0,resource.serve
12.000000000,gen0,4,res0,pipe.in
12.000000000,gen0,4,res0,pipe.out
13.525010755,gen0,4,res0,resource.serve
15.139426798,gen0,5,res0,pipe.in
15.139426798,gen0,5,res0,pipe.out
16.862637537,gen0,5,res0,resource.serve
17.914456117,gen0,6,res0,pipe.in
17.914456117,gen0,6,res0,pipe.out
20.091155604,gen0,6,res0,resource.serve
21.150927331,gen0,7,res0,pipe.in
21.150927331,gen0,7,res0,pipe.out
21.196403533,gen0,7,res0,resource.serve
class simpype.simulation.Log(sim)

Class storing the simulation parameters regarding the dynamic models.

Parameters:sim (Simulation) – The SimPype simulation object.
sim

The SimPype simulation object.

Type:Simulation
env

The SimPy environment object.

Type:simpy.Environment
date

The real world creation time of the simulation environment.

Type:datetime.now()
file

Write the logs to a file if True. Default value is True.

Type:bool
print

Print the logs to the console if True. Default value is False.

Type:bool
dir

The folder where the simulation logs are written.

init()

Initialize the log folder and the simulation loggers.

property(property)

Enable the logging of a message property.

Parameters:property (str) – The property to log.
write(entry)

Write a log entry.

Parameters:entry (Timestamp)(str) – The entry to be logged. If the entry is Timestamp
class simpype.simulation.Model(sim)

Class storing the simulation parameters regarding the dynamic models.

Parameters:sim (Simulation) – The SimPype simulation object.
sim

The SimPype simulation object.

Type:Simulation
env

The SimPy environment object.

Type:simpy.Environment
dir

The folder containing the customs models to be loaded by the SimPype simulation environment.

class simpype.simulation.Simulation(id)

Class implementing the SimPype’s simulation environment.

Parameters:id (str) – The simulation environment id.
env

The SimPy environment object.

Type:simpy.Environment
id

The simulation environment id.

Type:str
resource

The dictionary storing all the Resource objects of the simulation.

Type:dict
generator

The dictionary storing all the Resource objects implementing generator functionalities of the simulation.

Type:dict
pipeline

The dictionary storing all the Pipeline objects of the simulation.

Type:dict
log

The log class storing the logging parameters.

Type:Log
model

The model class storing the custom model parameters.

Type:Model
add_generator(id, model=None)

Add a Resource implementing generator funcionalities to the simulation environment.

Parameters:
  • id (str) – The generator id
  • model (str) – The model of the generator. If model is None, the default model is used.
Returns:

Resource

add_pipeline(*args)

Chain multiples Resource or Pipeline objects into a pipeline..

Parameters:*args (Resource)(Pipeline) – Create a pipeline by chaining 2 or more Resource or Pipeline objects.
Returns:Pipeline
add_resource(id, model=None, capacity=1, pipe=None)

Add a Resource object to the simulation environment.

Parameters:
  • id (str) – The resource id
  • model (str) – The model of the resource. If model is None, the default model is used.
  • capacity (int) – The capacity of the resource, that is the number of Message objects that the resource can simultaneously serve.
  • pipe (str) – The model of the pipe associated to the resource. If model is None, the default model is used.
Returns:

Resource

merge_pipeline(*args)

Merge varioues Pipeline objects into a single pipeline.

Parameters:*args (Pipeline) – Create a pipeline by merging 2 or more Pipeline objects.
Returns:Pipeline
run(*args, **kwargs)

Run the simulation environment using SimPy environment.

seed

The seed of the pseudo-random number generator used by this simulation.