A high-fidelity intersection simulation that benchmarks Deep Reinforcement Learning against Mathematical Predictive Modeling โ in real time.
๐ Run the Simulation ยท ๐ Architecture ยท ๐ How Benchmarking Works
SignalSync is a physics-based traffic intersection simulator built to answer one question:
Can a Deep Reinforcement Learning agent outperform a hand-crafted mathematical model at controlling traffic signals?
It runs both models on the same intersection, under identical traffic conditions, and produces a live side-by-side performance comparison โ tracking wait times, throughput, and queue lengths across all four lanes.
No fixed timers. No guesswork. Just two algorithms competing for the fastest flow.
Mathematical model running on BALANCED scenario. Traffic Console shows per-lane queue, wait time, and density. Active lane highlighted in green. Progress: 26s / 60s.
DRL agent takes over under identical traffic conditions. Green progress bar indicates the AI run in progress. South lane currently green with queue clearing.
Head-to-head results after both 60s runs. Math model wins on all four metrics โ avg wait, max wait, throughput, and queue length โ in this benchmark.
| Feature | Description | |
|---|---|---|
| ๐ | Physics-Based Simulation | Vehicles with realistic acceleration, deceleration, and collision avoidance at a 4-way intersection |
| ๐ | Mathematical Model | Weighted scoring + prediction algorithm using real-time queue lengths and vehicle arrivals |
| ๐ค | DRL Agent (DQN) | PyTorch Deep Q-Network observing a 12-parameter intersection state to learn long-term optimization |
| ๐ก๏ธ | Hybrid Safety Override | Prevents traffic starvation โ steps in if the AI makes dangerously inefficient decisions |
| ๐ | Live Traffic Console | Real-time queue lengths, wait times, and density tracking across N/S/E/W lanes |
| ๐ฆ | Scenario Selector | Switch between LOW, BALANCED, and PEAK traffic scenarios mid-experiment |
| โ๏ธ | Fair Benchmarking | Fixed random seed ensures both models face identical traffic across sequential runs |
| ๐ | Results Dashboard | Programmatic winner declaration based on avg wait, max wait, throughput, and queue length |
Model A โ Mathematical Predictive Model
A deterministic scorer inside mathematical_model/ that evaluates every lane on every tick:
scoring_model.pycomputes a weighted score per direction from current queue length + accumulated wait timeprediction.pyestimates incoming vehicle arrivals to anticipate congestion before it peaks- Applies a fairness threshold โ forces a lane switch if a direction has waited too long
- Applies an emergency threshold โ overrides everything if one lane is critically congested
- Fully transparent: every decision is traceable to a formula
Model B โ Deep Reinforcement Learning Agent (DQN)
A PyTorch-powered neural network inside drl_model/:
agent.pydefines the DQN policy network and action selection logicenvironment.pywraps the simulation as a Gym-style environment with state, reward, and step logictrain.pyhandles the training loop with experience replay and epsilon-greedy explorationdqn_agent.pthholds the pre-trained weights โ no training required to run inference- Protected by a Hybrid Safety Override that vetoes decisions which would starve a congested lane
SignalSync uses a reproducible, sequential experiment flow to ensure a scientifically fair comparison:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ EXPERIMENT FLOW โ
โ โ
โ 1. User sets duration (e.g. 60s, 120s) โ
โ โ
โ 2. Math Model runs โ metrics tracked via metrics.py โ
โ โ โ
โ 3. Full intersection reset + fixed random seed applied โ
โ โ โ
โ 4. DRL Agent runs โ same traffic, same seed โ
โ โ โ
โ 5. comparator.py ranks both models across 4 metrics โ
โ โ declares winner on the results dashboard โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Metrics tracked independently per model via evaluation/metrics.py:
| Metric | Description |
|---|---|
| Average Waiting Time | Mean seconds a vehicle waits at red across the full run |
| Maximum Waiting Time | Worst-case wait experienced by any single vehicle |
| Total Throughput | Total vehicles that successfully cleared the intersection |
| Average Queue Length | Mean vehicles queued across all four lanes |
| Layer | Technology |
|---|---|
| Simulation Engine | Python, Pygame |
| Machine Learning | PyTorch (DQN Architecture) |
| Data & Analytics | NumPy, Pandas, Matplotlib |
| Traffic Generation | Custom weighted probability engine (LOW / BALANCED / PEAK) |
SignalSync/
โ
โโโ main.py # Entry point โ landing screen + experiment orchestrator
โโโ requirements.txt # Python dependencies
โโโ package-lock.json
โโโ README.md
โ
โโโ drl_model/ # Deep Reinforcement Learning
โ โโโ agent.py # DQN policy network + action selection
โ โโโ dqn_agent.pth # Pre-trained agent weights
โ โโโ environment.py # Gym-style simulation wrapper (state/reward/step)
โ โโโ train.py # Training loop โ replay buffer, epsilon-greedy
โ
โโโ mathematical_model/ # Deterministic scoring engine
โ โโโ prediction.py # Vehicle arrival prediction
โ โโโ scoring_model.py # Weighted queue + wait-time scorer
โ
โโโ simulation/ # Physics & intersection logic
โ โโโ intersection.py # Signal state machine + phase management
โ โโโ traffic_generator.py # Weighted vehicle spawn (LOW/BALANCED/PEAK)
โ โโโ vehicle.py # Movement, stopping, collision avoidance
โ
โโโ evaluation/ # Benchmarking pipeline
โ โโโ comparator.py # Side-by-side model comparison + winner logic
โ โโโ metrics.py # MetricsTracker โ wait time, queue, throughput
โ
โโโ visualization/ # Rendering layer
โโโ pygame_display.py # Roads, vehicles, signals, traffic console
- Python
3.10+
# 1. Clone the repository
git clone https://github.com/your-username/SignalSync.git
cd SignalSync
# 2. Create and activate a virtual environment
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
# 3. Install dependencies
pip install -r requirements.txt
# 4. Launch
python main.py- Launch
python main.pyโ the SignalSync landing screen appears - Click Get Started and set your duration and traffic scenario (LOW / BALANCED / PEAK)
- The Mathematical Model runs first โ the Traffic Console shows live queue, wait, and density per lane with an amber progress bar
- The intersection auto-resets with the same random seed applied
- The DRL Agent runs under identical conditions โ green progress bar indicates the AI run
- The Simulation Experiment Dashboard appears โ all four metrics compared side-by-side with a final conclusion declaring the winner
- State vector โ
agent.pyobserves 12 normalized parameters: queue lengths (ร4), cumulative wait times (ร4), and instantaneous traffic density (ร4) for N/S/E/W - Hybrid Safety Override โ monitors every DQN action before it is applied; substitutes the safest valid alternative if the chosen action would critically starve any lane
- Fixed-seed fairness โ
random.seed()andnumpy.seed()are reset to identical values before each model's run, making the vehicle spawn sequence byte-for-byte identical - Pre-trained weights โ
dqn_agent.pthlets the agent run inference immediately; re-training can be triggered viatrain.py - Decoupled evaluation โ
evaluation/has zero imports from model internals;metrics.pyhooks into vehicle lifecycle events only
This project is licensed under the MIT License โ see the LICENSE file for details.
Built with Python, PyTorch, and a lot of red lights.
SignalSync โ Two models enter. One light turns green.


