Get startedGet started for free

Transportation model: defining the generator

Well done; you have defined your model inputs and outputs and the model processes you characterized using Python methods. Now, it's time to put a generator together that will sequence all your model processes.

This exercise is part of the course

Discrete Event Simulation in Python

View Course

Exercise instructions

  • Call the function road_travel() created before, which calculates the road travel time, and clock in the time it takes to complete it.
  • Call the function wait_traffic_light() created before, which calculates the waiting time at traffic lights, and clock in the time it takes to complete it.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

def all_processes(env, inputs):
    road_stretch, distance_total, traffic_light = 0, 0, 0
    while True:

        road_stretch += 1
        
        # Call function calculates road travel time
        distance, distance_total = ____(inputs, distance_total)
        yield env.____(distance/inputs['Speed_limit_ms'])
        print(f"> Road Stretch #{road_stretch} \nLength = {distance} m , Cumulative distance travelled = {distance_total} m , Total time elapsed = {env.now} sec")

        traffic_light += 1
        
        # Call function that calculates waiting time at a traffic light
        waitTime_traffic_light_sec = ____(inputs, distance_total)
        yield env.____(waitTime_traffic_light_sec)
        print(f"> Traffic Light #{traffic_light} \nWait time = {waitTime_traffic_light_sec} sec, Time lapsed = {env.now} sec")
Edit and Run Code