SimPy is a process-based discrete-event simulation framework based on standard Python. Its event dispatcher is based on the time of events and the processes it contains. It provides the modeller with components of a simulation model, including processes, for active components like vehicles or customers, and resources, for passive components like servers or counters. SimPy also provides monitors to aid in understanding the model.
Features of SimPy:
To install:
pip intall simpy
import simpy
print('simpy version: ' + simpy.__version__)
simpy version: 4.1.1
class Car(object):
''' stop and drive the car '''
def __init__(self, env):
# reference to current simulation environment
self.env = env
# Start the park_and_drive process everytime an instance is created
self.action = env.process(self.park_and_drive())
def park_and_drive(self):
while True:
print('Start parking at %d' % self.env.now)
parking_duration = 5
yield self.env.timeout(parking_duration)
print('Start driving at %d' % self.env.now)
trip_duration = 2
yield self.env.timeout(trip_duration)
# create an environment
env = simpy.Environment()
# create an instance of the Car class
# add car process to the environment
car = Car(env)
# start simulation
# run for 15 seconds (note the number could mean anything, i.e. seconds/hours/days/years/etc.)
env.run(until=15)
Start parking at 0 Start driving at 5 Start parking at 7 Start driving at 12 Start parking at 14
This tutorial was created by HEDARO