Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SUMO Cross-Intersection Traffic Simulation

Python · Eclipse SUMO 1.27 · TraCI · Traffic Engineering

Data Science & AI in Intelligent and Sustainable Mobility Systems
M.Eng. Automatisiertes Fahren und Fahrzeugsicherheit — Technische Hochschule Ingolstadt (THI)


What This Is

A Python-scripted traffic simulation of a cross-shaped urban intersection, built with Eclipse SUMO and controlled step-by-step via the TraCI (Traffic Control Interface). The script programmatically generates the full road network (nodes, edges, lanes, connections, traffic light phases) as SUMO XML files, compiles them into a network binary via netconvert, injects 50 vehicles with a defined demand pattern, and then uses TraCI to run the simulation and extract macroscopic traffic flow metrics in real time.

This kind of scripted, Python-controlled simulation pipeline is directly relevant to AV validation and ADAS testing workflows, where traffic environments are generated and instrumented programmatically rather than manually.


Network Architecture

              [N]
               |
             200 m, 2 lanes each direction
               |
[W] ──200 m──[Center TL]──200 m──[E]
               |
             200 m, 2 lanes each direction
               |
              [S]
Property Value
Topology Cross / plus-shaped
Nodes 5 (1 signalized center + 4 outer endpoints)
Directed edges 8 (bidirectional pair per arm)
Edge length 200 m
Lanes per direction 2
Speed limit 50 km/h (13.89 m/s)
Turning Not permitted — straight through only
Intersection control Static traffic lights

Traffic Light Logic

Each incoming direction gets its own green phase. The full cycle runs 120 seconds:

Phase Duration Signal State
North — green 24 s GGrrrrrr
North — yellow 3 s yyrrrrrr
All red 3 s rrrrrrrr
East — green 24 s rrGGrrrr
East — yellow 3 s rryyrrrr
All red 3 s rrrrrrrr
South — green 24 s rrrrGGrr
South — yellow 3 s rrrryyrr
All red 3 s rrrrrrrr
West — green 24 s rrrrrrGG
West — yellow 3 s rrrrrryy
All red 3 s rrrrrrrr
Total cycle 120 s

The signal state string has 8 characters — one per link at the intersection (2 lanes × 4 directions). G = green, y = yellow, r = red.


Traffic Demand

Property Value
Total vehicles 50
Departure interval 1 vehicle every 2 seconds
Spawn pattern Clockwise rotation: N → E → S → W
Vehicle type Car — 5 m length, max 54 km/h, accel 2.0 m/s², decel 3.0 m/s²

Vehicles spawn at the outer endpoints and travel straight through the center intersection to the opposite side. No turns are possible.


How the TraCI Loop Works

After SUMO starts, the Python script takes over step-by-step control:

while traci.simulation.getMinExpectedNumber() > 0:
    traci.simulationStep()                              # advance simulation by 1 second
    for edge in real_edges:
        count = traci.edge.getLastStepVehicleNumber(e)  # query vehicle count per edge
        total_cars += count
    steps += 1

Internal junction edges (prefixed with :) are filtered out so only the 8 real road segments are measured.

Macroscopic metric calculated:

Average Vehicles per Edge = Total vehicle-edge-steps / (Number of Edges × Simulation Steps)

This gives a single number representing how loaded the network is on average — a standard macroscopic traffic flow descriptor alongside density and flow rate.


Results

Metric Value
Simulation duration 202 seconds
Edges monitored 8
Average vehicles per edge 2.156

Interpretation: at any given second during the simulation, roughly 2 vehicles were present on each road segment on average — reflecting a moderately loaded network with vehicles queuing at red phases.


Project Structure

sumo-traffic-simulation/
├── README.md
├── requirements.txt
├── .gitignore
├── simulation.py           # Clean standalone simulation script
├── Exercise_1.ipynb        # Original Jupyter notebook (course submission)
└── network/
    ├── network.nod.xml     # Node definitions (5 nodes)
    ├── network.edg.xml     # Edge definitions (8 directed edges)
    ├── network.tll.xml     # Traffic light phase logic
    ├── network.con.xml     # Connection rules (straight-through only)
    ├── network.rou.xml     # Vehicle routes and departure schedule
    ├── network.net.xml     # Compiled network binary (generated by netconvert)
    └── sumo_config.sumocfg # SUMO run configuration

Note: simulation.py regenerates all XML and the compiled network at runtime. The network/ folder contains the pre-generated reference copies.


How to Run

Prerequisites

  • Eclipse SUMO ≥ 1.27 — Download
  • Set SUMO_HOME environment variable to your SUMO installation folder
    Windows example: C:\Program Files (x86)\Eclipse\Sumo
  • Python ≥ 3.10

Install Python dependencies

pip install -r requirements.txt

Run with GUI

python simulation.py

A SUMO GUI window will open. Press the Play button inside SUMO to start the simulation. Once all vehicles exit the network, the terminal will print the macroscopic results.

Run headless (no GUI, faster)

Change line in simulation.py:

# from:
sumoBinary = checkBinary('sumo-gui')
# to:
sumoBinary = checkBinary('sumo')

Relevance to Autonomous Driving & ADAS

Traffic simulators like SUMO are used throughout the AV development stack:

  • Scenario generation — synthetic traffic environments for perception algorithm testing
  • Closed-loop validation — ADAS functions (ACC, intersection handling, emergency braking) are tested against simulated traffic before road deployment
  • HiL integration — simulation outputs feed into Hardware-in-the-Loop setups alongside real ECU hardware

The TraCI interface used here mirrors how AV engineers script test scenarios: Python communicates with the simulator in real time, reads back state data (vehicle positions, speeds, queue lengths), and can inject commands — exactly how an ADAS function interfaces with a simulation backend in a validation pipeline.


Course Context

This project is part of the course Data Science and AI in Intelligent and Sustainable Mobility Systems at THI Ingolstadt. The course covers traffic data analysis, machine learning for mobility, and reinforcement learning for traffic control — all applied to real-world intelligent transport problems.


About

Cross-intersection traffic simulation using Eclipse SUMO and TraCI | Python-controlled network with traffic light optimization and macroscopic flow analysis

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages