Package: simpn
SimPNSimPN (Simulation with Petri Nets) is a package for discrete event simulation in Python.SimPN provides a simple syntax that is based on Python functions and variables, making it familiar for people who already know Python. At the same time, it uses the power of and flexibility ofColored Petri Nets (CPN)for simulation. It also provides prototypes for easy modeling of frequently occurring simulation constructs, such as (customer) arrival, processing tasks, queues, choice, parallelism, etc.InstallationThe SimPN package is available on PyPI and can simply be installed with pip.python -m pip install simpnA Basic TutorialTo illustrate how SimPN works, let’s consider a simulation model of a cash register at a small shop,
which we can initialize as follows. This imports parts from the SimPN library that we use here
and further on in the example.fromsimpn.simulatorimportSimProblem,SimTokenshop=SimProblem()A discrete event simulation is defined by thestateof the system that is simulated and theeventsthat can happen
in the system.Simulation State and VariablesIn case of our shop, the state of the system consists of customers that are waiting in line at
the cash register, resources that are free to help the customer, and resources that are busy helping a customer.
Consequently, we can model the state of our simulation, by defining twovariablesas follows.customers=shop.add_var("customers")resources=shop.add_var("resources")A simulation variable is different from a regular Python variable in two important ways. First, a simulation variable
can contain multiple values, while a regular Python variable can only contain one value. Second, values of a simulation
variable are available from a specific moment in (simulation) time. More about that later.
So, with that in mind, let’s give our variables a value.resources.put("cassier")customers.put("c1")customers.put("c2")customers.put("c3")We now gave theresourcesvariable one value, the stringcassier, but we gave thecustomersvariable three values.
You can probably understand why we did that: we now have one cassier and three customers waiting. This is theinitial stateof our simulation model.Simulation EventsSimulation events define what can happen in the system and how the system (state variables) change when they do.
We define simulation events as Python functions that take a system state and return a new system state.
Remember that the system state is defined in terms of variables, so an event function takes (values of) state variables as
input and produces (values of) state variables as output.defprocess(customer,resource):return[SimToken(resource,delay=0.75)]shop.add_event([customers,resources],[resources],process)In our example we introduce a single event that represents a resource processing a waiting customer.
First, let’s focus onshop.add_eventin the code below. This tells the simulator that our event takes a value from thecustomersvariable and a value from theresourcesvariable as input, produces a value for theresourcesvariable as output, and uses theprocessfunction to change the state variables.
Describing that in natural language: it takes a customer and a resource and, when it is done, returns a resource.Theprocessfunction defines how the event modifies the system state (variables).
Taking a value from thecustomersvariable (and calling itcustomer) and a value from theresourcesvariable
(and calling itresource), the function returns theresourceagain. This return value will be put into theresourcesvariable, as per theshop.add_eventdefinition. However, as you can see, there are several things
going on in the return statement.First, the function does not return a single resource value, but a list of values. This is simply a convention
that you have to remember: event functions return a list of values. The reason for this is that we defined the
simulation event inshop.add_eventas taking a list of values (consisting of one value from customers and one value from
resources) as input and as producing a list of values (consisting of one value for resources) as output.
Accordingly, we must produce a list of values as output, even if there is only one value.Second, the function does not return theresource, but returns aSimTokencontaining the resource.
That is because in simulation, values have a time from which they are available. A value with a time
is called atoken. This represents that the value is only available at, or after, the specified time.
In this case, the resource value is made available after a delay of 0.75. You can consider this the time it takes the resource to
process the customer. Since it takes 0.75 to process a customer, the resource is only made available
again after a delay of 0.75. In the meantime no newprocessevents can happen, because a value fromresources,
which is needed as input for such an event, is not available.Putting it all togetherNow we have modeled the entire system and we can simulate it.
To do that, we call thesimulatefunction on the model.
This function takes two parameters. One is the amount of time for which the simulation will be run.
The other is the reporter that will be used to report the results of the simulation.
In our example we will run the simulation for 10. (Since we only have 3 customers, and each customer
takes 0.75 to process, this should be more than enough.) We will use aSimpleReporterfrom the
reporters package to report the result. This reporter simply prints each event that happens
to the standard output.fromsimpn.reportersimportSimpleReportershop.simulate(10,SimpleReporter())As expected, running this code leads to the following output.
The event of (starting) processing customer c1 happens at time t=0.
It uses valuec2for variablecustomersand valuecassierfor variableresources.
The event of (starting) processing customer c2 happens at time t=0.75.
This is logical, because our definition of theprocessevent that the valuecassieris only available
in the variableresourcesagain after 0.75. Accordingly, processing of c3 happens at time t=1.5.process{customers: c1, resources: cassier}@t=0
process{customers: c2, resources: cassier}@t=0.75
process{customers: c3, resources: cassier}@t=1.5For completeness, the full code of the example is:fromsimpn.simulatorimportSimProblem,SimTokenshop=SimProblem()resources=shop.add_var("resources")customers=shop.add_var("customers")defprocess(customer,resource):return[SimToken(resource,delay=0.75)]shop.add_event([customers,resources],[resources],process)resources.put("cassier")customers.put("c1")customers.put("c2")customers.put("c3")fromsimpn.reportersimportSimpleReportershop.simulate(10,SimpleReporter())Visualizing the ModelTo help check whether the model is correct, it is possible to visualize it. To this end, there is a Visualisation class.
You can simply create an instance of this class and call theshowmethod to show the model as follows.fromsimpn.visualisationimportVisualisationv=Visualisation(shop)v.show()The model will now be shown as a Petri net in a separate window.
The newly opened window will block further execution of the program until it is closed.
You can interact with the model in the newly opened window. Pressing the space bar will advance the simulation by one step.
You can also change the layout of the model by dragging its elements around.
After the model window is closed, you can save the layout of the model to a file, so that you can open it later.
Use the methodsave_layoutto save the model to do so.
You can load the layout of the model from the file later, by passing the saved layout as a parameter to the constructor.
If the layout file does not exist, the model will be shown with an automatically generated layout.v=Visualisation(shop,"layout.txt")v.show()v.save_layout("layout.txt")