Skip to main content
Distributed Systems · Statistical Simulation

Adaptive Traffic Signal & Stochastic Flow Simulator

Test how Fixed-Timer and Sensor-Adaptive traffic lights handle changing rush-hour demand across dozens of randomized trials, and watch stop-and-go shockwaves unfold live at 60 FPS.

  • C++23
  • gRPC
  • Python
  • TypeScript
  • Google Cloud Run
  • Pub/Sub
  • Monte Carlo

Interactive Traffic Simulator

Signal ControllerLoop-Detector Gap-Out
Poisson Arrival Demand
0.20 veh/s
0.20 veh/s
Signal Phase Bounds
10 s
45 s
Monte Carlo Ensemble
50 runs
Advanced IDM Physics
13.9 m/s
2.0 m/s²
1.5 s

2D Microscopic Shockwave Replay

Run-0 trajectory interpolated from 5 Hz server snapshots to 60 FPS

Standby

Interactive Viewport

Configure arrival rates and signal control on the left, then click Run Simulation to watch 60 FPS shockwaves.

Stopped (0 m/s)Cruising (13.9 m/s)

Backend Highlights

High-Speed C++23 Engine

Compiled Physics Backend for Parallel Runs

Every click dispatches up to 200 independent intersection simulations to a compiled C++23 physics engine over internal gRPC—updating individual cars every 0.1 seconds and returning full wait-time distributions and a 60 FPS replay in moments.

Serverless Cloud Run

Scales to Zero When Idle

The entire cloud pipeline runs event-driven on Google Cloud Run and automatically spins down to zero instances when nobody is using the demo—waking up on demand when a new simulation is submitted.

Mathematical Model

How the Simulation Works

Instead of treating traffic like a uniform fluid, the simulator models each individual car's acceleration, braking, and queue behavior from first principles.

1. Random Vehicle Arrivals

Cars enter from North, South, East, and West at an average arrival rate λd\lambda_d (veh/s\text{veh/s}). The time gap Δt\Delta t between consecutive cars is drawn randomly from an exponential distribution:
Δtk(d)∼Exp(λd),f(Δt;λd)=λde−λdΔt\Delta t_k^{(d)} \sim \mathrm{Exp}(\lambda_d), \qquad f(\Delta t; \lambda_d) = \lambda_d e^{-\lambda_d \Delta t}

If a queue backs up all the way to the entry point, newly arriving cars wait upstream until space opens up.

2. Car-Following & Braking (IDM)

Each driver accelerates toward their desired speed v0v_0 while braking smoothly based on the bumper-to-bumper gap snets_{\text{net}} and closing speed Δvα\Delta v_\alpha to the car (or red light) ahead:
v˙α=a[1−(vαv0)4−(s∗(vα,Δvα)snet)2]\dot{v}_\alpha = a \left[ 1 - \left(\frac{v_\alpha}{v_0}\right)^4 - \left(\frac{s^*(v_\alpha, \Delta v_\alpha)}{s_{\text{net}}}\right)^2 \right]
s∗(vα,Δvα)=s0+max⁡ ⁣(0,  vαT+vα Δvα2ab)s^*(v_\alpha, \Delta v_\alpha) = s_0 + \max\!\left(0,\; v_\alpha T + \frac{v_\alpha \, \Delta v_\alpha}{2\sqrt{a b}}\right)
Speeds and positions advance every Δt=0.1 s\Delta t = 0.1\text{ s} via vα(t+Δt)=max⁡(0,vα+v˙αΔt)v_\alpha(t+\Delta t) = \max(0, v_\alpha + \dot{v}_\alpha \Delta t) and sα(t+Δt)=sα+vα(t+Δt)Δts_\alpha(t+\Delta t) = s_\alpha + v_\alpha(t+\Delta t)\Delta t, creating realistic stop-and-go waves.

3. Fixed-Timer vs. Adaptive Signals

Both controllers cycle safely through green, yellow, and all-red clearance intervals, differing only in when green ends:

  • Fixed-Timer: Holds green for a fixed TgreenT_{\text{green}} duration every cycle, even when no cars are approaching.
  • Adaptive: Stays within Tmin⁡≤tphase≤Tmax⁡T_{\min} \le t_{\text{phase}} \le T_{\max} and monitors road sensors near the stop line—ending green early whenever the active road clears and cross-traffic is waiting.

4. Monte Carlo Variation

Real traffic is unpredictable, so a single run isn't enough. Across MM parallel trials, the simulator slightly varies arrival rates, driver reaction times, and cruising speeds around your settings:
θ(m)=clip ⁣(θ0⋅ξ(m),  θmin⁡,  θmax⁡),ξ(m)∼N(1,σθ2)\theta^{(m)} = \mathrm{clip}\!\Big(\theta_0 \cdot \xi^{(m)},\; \theta_{\min},\; \theta_{\max}\Big), \qquad \xi^{(m)} \sim \mathcal{N}(1, \sigma_\theta^2)
Pooling wait times across all MM runs reveals both typical (P50P_{50}) and heavy-congestion (P95P_{95}) delays.

Want the full system architecture & engineering details?

See the GitHub repository for the microservice architecture, Protobuf/gRPC schemas, Cloud Run security setup, and local Docker quickstart.

Read Full Docs on GitHub