Mastering SUMO Traffic Simulation with TraCI
Introduction
Once you have installed SUMO on your device and selected the environment or area where you want to test your algorithms for controlling traffic signals, managing vehicle movements, or any other desired scenario within that environment, but before continuing in reading let me tell you something
If you have not done the steps mentioned above:
- Read the article “How to get started with SUMO” to ensure you have the necessary setup.
Else:
Get Taher Almoussali’s stories in your inbox
Join Medium for free to get updates from this writer.
Continue reading this article because you are ready to learn how to control SUMO traffic simulation through TraCI.
What is TraCI?
TraCI (short for Traffic Control Interface) is an API that provides access to a SUMO traffic simulation, enabling controlling the behavior of multiple simulation objects during a live simulation. It allows for external scripts to interact with the simulation and its vehicles, pedestrians, and infrastructure.
Key Topics Covered in This Article:
- How to Control Vehicles
- How to Control Traffic Signals
1. Control of Vehicles
TraCI provides various commands to control individual or groups of vehicles within the SUMO simulation. Here are some fundamental operations you can perform:
Adding Vehicles
You can dynamically add vehicles to the simulation using the traci.vehicle.add() command. This requires specifying the vehicle ID, and route.
Changing Vehicle Speed
To control the speed of a vehicle, use the traci.vehicle.setSpeed(vehicleID, speed) command. This can help simulate different driving behaviors or respond to real-time traffic conditions.
Routing Vehicles
TraCI allows you to change a vehicle’s route during the simulation. Use the traci.vehicle.setRoute(vehicleID, route) command to dynamically adjust the vehicle's path based on changing traffic conditions or objectives.
Example usage:
setRoute(‘1’, [‘1’, ‘2’, ‘4’, ‘6’, ‘7’])
Changes the vehicles route to the route with the given id.
Stopping and Resuming Vehicles
You can stop a vehicle at a specific location using traci.vehicle.setStop(vehicleID, edgeID, pos, laneID, duration). To resume, simply issue a speed command or use traci.vehicle.resume(vehicleID).
2. Control of Traffic Signals
TraCI also provides extensive functionality to control traffic signals, allowing for the implementation of custom traffic management algorithms. Here are some key operations:
Changing Traffic Light States
To change the state of a traffic light, use the traci.trafficlight.setRedYellowGreenState(trafficLightID, state) command. The state parameter is a string representing the desired light states for each lane. more details
Programming Traffic Light Phases
You can set custom traffic light programs using traci.trafficlight.setProgram(trafficLightID, programID). This allows for complex control strategies, such as adaptive traffic lights that respond to real-time traffic conditions.
Querying Traffic Light Information
TraCI enables you to retrieve the current state and program of traffic lights using commands like traci.trafficlight.getRedYellowGreenState(trafficLightID) and traci.trafficlight.getProgram(trafficLightID).
Conclusion
TraCI provides powerful tools to control and customize SUMO simulations, making it an essential interface for researchers and developers working on traffic management systems. By mastering vehicle and traffic light controls, you can simulate complex traffic scenarios and test innovative traffic solutions.







