Smart Traffic Light Simulation is a real-time traffic simulation featuring adaptive traffic lights controlled by a reinforcement learning (RL) algorithm. It models an intelligent 4-way intersection where vehicles dynamically interact with the traffic signals, and queue-based optimization is applied to improve traffic flow.
- Realistic Traffic Simulation: Supports various vehicle types (cars, trucks, ambulances, taxis, etc.).
- Adaptive Traffic Light System: Optimizes green light duration based on real-time congestion levels.
- SFML-based Graphics: Provides smooth real-time rendering of the intersection.
- Reinforcement Learning Integration: Utilizes Q-learning to adjust light timings dynamically, reducing vehicle wait times and congestion.
- Multi-Phase Traffic Light Management: Manages North-South and East-West green/yellow transitions.
- Vehicle Spawn Control: Includes an on-screen button to cycle through preset vehicle spawn intervals.
- Customizable Q-Table Storage: Saves and loads the RL policy for persistent learning across simulations.
- C++ & SFML: Core simulation logic and graphical interface.
- Python: For training the RL model.
- JSON: For storing and loading the Q-table.
- MinGW/MSYS2 UCRT64: Compiler toolchain used for building the project.
- SFML 2.6.1 – Graphics, window, and system modules.
- MinGW/MSYS2 UCRT64 – GCC toolchain (GCC 14.2.0 or later recommended).
- nlohmann/json – JSON library (via
json.hpp) for Q-table handling. - Python 3.x – (with NumPy) for training the RL model.
-
Configure Build Tasks:
In yourtasks.json, set the compiler path to:"command": "C:/msys64/ucrt64/bin/g++.exe",
Ensure your include paths point to
C:/msys64/ucrt64/includeand your project’sinclude/folder, and that library paths point toC:/msys64/ucrt64/lib. -
Build the Project:
PressCtrl+Shift+Bto run the build task.
The executable (e.g.,SFMLTest.exe) will be generated in yourbin/folder. -
Run the Simulation:
In the integrated terminal, execute:.\bin\SFMLTest.exe
(On Windows, use
.\to run executables from the current directory.)
- Navigate to Your Project Directory:
cd /c/TrafficLightSimulation - Clean Previous Builds (if needed):
rm -rf bin/* rm -rf build/ rm -rf CMakeCache.txt CMakeFiles/ - Compile the Project:
C:/msys64/ucrt64/bin/g++.exe -std=c++17 -g main.cpp TrafficLight.cpp Vehicle.cpp TrafficManager.cpp QTableLoader.cpp -I include -I C:/msys64/ucrt64/include -L C:/msys64/ucrt64/lib -lsfml-graphics -lsfml-window -lsfml-system -o bin/SFMLTest.exe
- Run the Executable:
./bin/SFMLTest.exe
- Navigate to the RL Directory:
cd RL - Run the Training Script:
This will train the RL agent and update
pip install numpy python traffic_rl.py
q_table.json, which is loaded by the simulation.
The TrafficLight class handles the cycling of red, yellow, and green phases based on timers and pre-loaded textures.
The Vehicle class supports various vehicle types and manages their movement and stop-line logic.
The TrafficManager class spawns vehicles, measures queues, and uses a Q-table (loaded from q_table.json) to adapt the green light duration dynamically based on real-time traffic conditions.
An on-screen button allows users to cycle through preset vehicle spawn intervals (1.0f, 3.0f, 0.5f) to simulate different traffic densities.
The RL component is trained in Python using Q-learning to optimize traffic light timings based on a simulated environment, and the resulting Q-table is saved as q_table.json. The C++ simulation loads this Q-table at runtime and uses it to dynamically adjust green light durations in response to real-time traffic conditions.