Introduction to Discrete-Event Simulation in Python

Despite what you may have heard, simulation is as much an art as a science. There are infinite possibilities of how models can be constructed. Don’t let that put you off though – instead see it as an opportunity to really engage the analytical side of your brain. Simulations are abstractions of real life. Deciding on this level of abstraction depends on a decision you will make between granularity of results and the level of approximation you are prepared to make.

Simulations and models will always represent an abstraction of reality. The degree of abstraction depends on a several factors:
1. The question being answered (modelling requirement)
2. The computing power available (technical resource)
3. The time available to build a simulation (human resource)

Simulation is used to virtually test new or existing systems, providing vital development cost savings to projects and ensuring that they are designed right first time. Many projects experience significant waste and costs due to rework and redesign late in the project lifecycle. Simulation can be employed early in the project lifecycle to mitigate this risk.

System requirements can be tested with simulation in order to provide confidence that a project will be successful. This is in particular essential for large infrastructure projects which require extensive verification and validation. Simulation can also be used to help re-design underperforming systems that fall short of performance requirements, by highlighting system do straints or enabling the optimisation of processes.

Python is an incredibly powerful language and can be used for discrete-event simulation. The SimPy library provides a powerful framework to produce all kinds of simulations, its webpage can be found here: https://simpy.readthedocs.io/en/latest/

For now, let’s go straight into an example of the most basic of discrete-event simulations: a clock. This is actually an example that features on the SimPy webpage, and rightly so, because the clock demonstrates the most basic element of a discrete-event simulation: time.

# import the module simpy
import simpy

# describe the process
def clock(env, tick):
    while True:
        print(env.now)
        yield env.timeout(tick)


# create the simpy environment
env = simpy.Environment()

# start the process
env.process(clock(env, 0.5))

# run the process until time = 2
env.run(until=2)

This simulation will run until the “time” reaches a value of 2. We have set the “tick” equal to 0.5, so every 0.5 units of time the simulation will print the current time. Our output looks like:

0
0.5
1
1.5

Note that the simulation does not get a chance to print 2 as we have instructed it to end the moment time 2 is reached.

So there you have it, a clock simulator! Simple. More to come…


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s