Skip to content

mayurigade-hub/SignalSync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

27 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

SignalSync ๐Ÿšฆ

Smart Traffic Signal Control System

A high-fidelity intersection simulation that benchmarks Deep Reinforcement Learning against Mathematical Predictive Modeling โ€” in real time.

Python PyTorch Pygame License: MIT

๐Ÿš€ Run the Simulation ยท ๐Ÿ“ Architecture ยท ๐Ÿ“Š How Benchmarking Works


๐Ÿง  What is SignalSync?

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.


๐Ÿ“ธ Screenshots

โš™๏ธ Math Model โ€” Live Simulation

Mathematical model running on BALANCED scenario. Traffic Console shows per-lane queue, wait time, and density. Active lane highlighted in green. Progress: 26s / 60s.

Math Model Running


๐Ÿค– DRL Agent โ€” Live Simulation

DRL agent takes over under identical traffic conditions. Green progress bar indicates the AI run in progress. South lane currently green with queue clearing.

DRL Agent Running


๐Ÿ“Š Simulation Experiment Dashboard

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.

Results Dashboard


โœจ Key Features

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

โš™๏ธ How It Works

The Two Competing Models

Model A โ€” Mathematical Predictive Model

A deterministic scorer inside mathematical_model/ that evaluates every lane on every tick:

  • scoring_model.py computes a weighted score per direction from current queue length + accumulated wait time
  • prediction.py estimates 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.py defines the DQN policy network and action selection logic
  • environment.py wraps the simulation as a Gym-style environment with state, reward, and step logic
  • train.py handles the training loop with experience replay and epsilon-greedy exploration
  • dqn_agent.pth holds 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

๐Ÿ“Š Benchmarking System

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

๐Ÿ› ๏ธ Tech Stack

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)

๐Ÿ“‚ Directory Structure

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

๐Ÿš€ Getting Started

Prerequisites

  • Python 3.10+

Setup

# 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

๐ŸŽฎ Usage

Running a Benchmark

  1. Launch python main.py โ€” the SignalSync landing screen appears
  2. Click Get Started and set your duration and traffic scenario (LOW / BALANCED / PEAK)
  3. The Mathematical Model runs first โ€” the Traffic Console shows live queue, wait, and density per lane with an amber progress bar
  4. The intersection auto-resets with the same random seed applied
  5. The DRL Agent runs under identical conditions โ€” green progress bar indicates the AI run
  6. The Simulation Experiment Dashboard appears โ€” all four metrics compared side-by-side with a final conclusion declaring the winner

๐Ÿ—๏ธ Architecture Notes

  • State vector โ€” agent.py observes 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() and numpy.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.pth lets the agent run inference immediately; re-training can be triggered via train.py
  • Decoupled evaluation โ€” evaluation/ has zero imports from model internals; metrics.py hooks into vehicle lifecycle events only

๐Ÿ“„ License

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.

About

SignalSync is a real-time traffic simulation platform that benchmarks AI-driven decision-making against traditional mathematical models for smarter urban flow control.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages