ExampleSnippets
In [15]:
import simpy
import random
In [16]:
def car(env):
while True:
print(‘Start parking at %d’ % env.now)
parking_duration = 5
yield env.timeout(parking_duration)
print(‘Start driving at %d’ % env.now)
trip_duration = 2
yield env.timeout(trip_duration)
In [17]:
env = simpy.Environment()
env.process(car(env))
env.run(until=15)
Start parking at 0
Start driving at 5
Start parking at 7
Start driving at 12
Start parking at 14
In [5]:
class Car(object):
def __init__(self, env):
self.env = env
# Start the run process everytime an instance is created.
self.action = env.process(self.run())
def run(self):
while True:
print(‘Start parking and charging at %d’ % self.env.now)
charge_duration = 5
# We yield the process that process() returns
# to wait for it to finish
yield self.env.process(self.charge(charge_duration))
# The charge process has finished and
# we can start driving again.
print(‘Start driving at %d’ % self.env.now)
trip_duration = 2
yield self.env.timeout(trip_duration)
def charge(self, duration):
yield self.env.timeout(duration)
In [18]:
env = simpy.Environment()
car = Car(env)
env.run(until=15)
Start parking and charging at 0
Start driving at 5
Start parking and charging at 7
Start driving at 12
Start parking and charging at 14
In [36]:
def driver(env, car):
while True:
yield env.timeout(3)
car.action.interrupt()
In [37]:
class Car(object):
def __init__(self, env):
self.env = env
# Start the run process everytime an instance is created.
self.action = env.process(self.run())
def run(self):
while True:
print(‘Start parking and charging at %d’ % self.env.now)
charge_duration = 5
# We yield the process that process() returns
# to wait for it to finish
try:
yield self.env.process(self.charge(charge_duration))
except simpy.Interrupt:
# When we received an interrupt, we stop charging and
# switch to the “driving” state
print(‘Was interrupted, hope battery is full enough’)
# The charge process has finished and
# we can start driving again.
print(‘Start driving at %d’ % self.env.now)
trip_duration = 5
try:
yield self.env.process(self.drive(trip_duration))
except simpy.Interrupt:
print(“Can’t interrupt me, I’m already driving”)
def charge(self, duration):
yield self.env.timeout(duration)
def drive(self, duration):
yield self.env.timeout(duration)
In [38]:
env = simpy.Environment()
car = Car(env)
env.process(driver(env, car))
env.run(until=15)
Start parking and charging at 0
Was interrupted, hope battery is full enough
Start driving at 3
Can’t interrupt me, I’m already driving
Start parking and charging at 6
Was interrupted, hope battery is full enough
Start driving at 9
Can’t interrupt me, I’m already driving
Start parking and charging at 12
In [43]:
def car(env, name, bcs, driving_time, charge_duration):
# Simulate driving to the BCS
yield env.timeout(driving_time)
# Request one of its charging spots
print(‘%s arriving at %d’ % (name, env.now))
with bcs.request() as req:
yield req
# Charge the battery
print(‘%s starting to charge at %s’ % (name, env.now))
yield env.timeout(charge_duration)
print(‘%s leaving the bcs at %s’ % (name, env.now))
In [44]:
env = simpy.Environment()
bcs = simpy.Resource(env, capacity = 2)
In [45]:
for i in range(5):
env.process(car(env, ‘Car %d’ % i, bcs, i, 5))
In [46]:
env.run()
Car 0 arriving at 0
Car 0 starting to charge at 0
Car 1 arriving at 1
Car 1 starting to charge at 1
Car 2 arriving at 2
Car 3 arriving at 3
Car 4 arriving at 4
Car 0 leaving the bcs at 5
Car 2 starting to charge at 5
Car 1 leaving the bcs at 6
Car 3 starting to charge at 6
Car 2 leaving the bcs at 10
Car 4 starting to charge at 10
Car 3 leaving the bcs at 11
Car 4 leaving the bcs at 15
In [ ]: