Inspiration
In rapidly growing urban areas, traffic congestion at roundabouts and intersections is a major daily bottleneck—causing lost time, fuel waste, and increased emissions. Traditional traffic light controllers operate on rigid, pre-programmed timers that remain completely blind to real-time traffic conditions and weather disruptions.
Living and observing traffic dynamics in Antananarivo, Madagascar inspired the vision for Smart Traffic: an adaptive, AI-driven traffic regulation system that acts as an intelligent digital traffic agent, dynamically prioritizing congested lanes based on real-time computer simulation, meteorology, and LLM-powered decision logic.
How we built it
Smart Traffic is built as a full-stack, real-time simulation architecture consisting of two primary layers:1. Interactive Front-End Engine (HTML5 Canvas & JS)Custom Kinematic Physics Engine: Developed a 2D physics simulation on HTML5 Canvas. Each vehicle obeys distance-keeping laws, entry priorities inside the roundabout ring, acceleration curves, and indicator signaling.Traffic Flow Mechanics: Vehicle spawning rates follow a Poisson distribution model, where the average arrival rate $\lambda$ (vehicles per minute) dictates time intervals between new cars:$$P(k \text{ arrivals in time } t) = \frac{(\lambda t)^k e^{-\lambda t}}{k!}$$Live Telemetry Dashboard: Built using modern CSS (OKLCH color space) displaying real-time metrics such as queue lengths, ring density, average wait time $\bar{W}$, and maximum wait time $W_{\max}$.2. Intelligent Back-End Controller (Node.js & Express)API Middleware & Validation: Built with Node.js and Zod schema validation to ensure strong typing and reliable payload handling.Live Weather Integration: Fetches real-time weather metrics (precipitation, wind speed, humidity) via the Open-Meteo API.Gemini LLM Arbitrator: Leverages Google Gemini 2.5 Flash as an expert reasoning engine. When queue length on any branch exceeds a user-defined threshold $T_{\text{queue}}$, telemetry data is sent to Gemini to compute the optimal priority direction and green phase duration $G_{\text{duration}} \in [10, 60]$ seconds.Deterministic Fallback Algorithm: If API connectivity drops or rate limits are reached, the server instantly falls back to an internal mathematical algorithm:$$G_{\text{duration}} = \min\left(60, \max\left(10, \left\lfloor 10 + 2.5 \cdot L_{\max} + \delta_{\text{rain}} \right\rfloor\right)\right)$$where $L_{\max}$ is the maximum branch load and $\delta_{\text{rain}} = 6$ if precipitation $> 0.2\text{ mm/h}$.
Challenges we ran into
Ring Priority & Collision Avoidance Geometry: Translating smooth circular motion into code required precise trigonometric calculations. Ensuring that vehicles entering the roundabout yielded properly to cars already inside the ring—without overlapping or freezing the queue—demanded exact gap-acceptance thresholds.
LLM Latency & Real-Time Flow Synchronization: Asynchronous API calls to LLMs can introduce a delay of 500ms to 2s. To prevent the simulation from stuttering during analysis, we implemented non-blocking asynchronous state updates and visual loading states in the UI.
Ensuring Structured LLM Outputs: Forcing an AI model to return strictly valid JSON without conversational wrapper text required combining explicit system prompts with JSON schema response modes and back-end Zod parsing.
What we learned
LLMs as Decision Engines: Large Language Models are not just for text generation; when supplied with structured JSON context, models like Gemini 2.5 Flash excel at multi-variable decision arbitration under complex environment constraints.
Hybrid Resiliency: Designing systems with a local fallback algorithm ensures 100% system uptime, proving that high-availability Smart City applications must combine deterministic rules with AI intelligence.
Canvas Kinematics: Building a custom JavaScript physics engine from scratch provided deep insights into vector mathematics, dynamic rendering loops, and frame-rate independent updates (requestAnimationFrame).
Built With
- css3
- express.js
- html5
- javascript;
- node.js


Log in or sign up for Devpost to join the conversation.