10 Oct 2018

Dynamic Traffic Lights

Although my last post regarding speedmode seems like a good bet for emergency vehicles I decided to take a look at the other solution of dynamically changing traffic lights as this will likely be of great use down the road. I could probably use this to model rail crossings as well if I have trouble using built in SUMO features.

The main reason to dynamically change traffic lights is so that you can let the simulation state determine when to change the lights. For this to be useful we need a way of detecting traffic. SUMO comes with several different inductive-loop traffic detectors.

TraCI allows you to query the detector by ID.

The inductor can be set up to detect many variables including vehicle speed, vehicle volume over time. We are most interested in detecting a specific vehicle by ID. This way when a certain vehicle is detected we can change the traffic light phase to green for that vehicle to allwo it to pass.

Here is a snippet of the relevant Python code which contains the main TraCI control loop:

def run():
    """TraCI control loop"""
    step = 0
    traci.trafficlight.setPhase("0", 2)
    while traci.simulation.getMinExpectedNumber() > 0:
        traci.simulationStep()
        if traci.trafficlight.getPhase("0") == 2:
            if traci.inductionloop.getLastStepVehicleNumber("0") == "emerg":
                # there is an emergency vehicle approaching, switch phase
                traci.trafficlight.setPhase("0", 3)
            else:
                # otherwise try to keep green for other direction
                traci.trafficlight.setPhase("0", 2)
        step += 1
    traci.close()

This solution seems to work great and could possibly be used to model rail crossings as well.

Thank You For Reading
Andrew Vaillancourt

Undergraduate Computer Science student at The University of Manitoba