Generator exercise#

To see the solutions please see the generator exercise solutions notebook

Imports#

import simpy
import numpy as np

Example code#

The code below is taken from the simple pharmacy example. In this code arrivals occur with an IAT of exactly 5 minutes.

def prescription_arrival_generator(env):
    '''
    Prescriptions arrive with a fixed duration of
    5 minutes.

    Parameters:
    ------
    env: simpy.Environment
    '''
    
    # don't worry about the infinite while loop, simpy will
    # exit at the correct time.
    while True:
        
        # sample an inter-arrival time.
        inter_arrival_time = 5.0
        
        # we use the yield keyword instead of return
        yield env.timeout(inter_arrival_time)
        
        # print out the time of the arrival
        print(f'Prescription arrives at: {env.now}')
# model parameters
RUN_LENGTH = 25

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

# tell simpy that the `prescription_arrival_generator` is a process
env.process(prescription_arrival_generator(env))

# run the simulation model
env.run(until=RUN_LENGTH)
print(f'end of run. simulation clock time = {env.now}')
Prescription arrives at: 5.0
Prescription arrives at: 10.0
Prescription arrives at: 15.0
Prescription arrives at: 20.0
end of run. simulation clock time = 25

Exercise: modelling a poisson arrival process for prescriptions#

Task:

  • Update prescription_arrival_generator() so that inter-arrival times follow an exponential distribution with a mean of 5.0 minutes between arrivals.

  • Use a run length of 25 minutes.

Bonus: try this initially without setting a random seed. Then update the method choosing an approach to control random sampling.

# your code here.