Research project
AI Traffic Light Intersection Simulation
A Pygame four-way intersection simulator comparing Fixed Cycle, Longest Queue First, Q-learning, and genetic search — with a Tkinter runner UI.
Simulator · 1 / 2
Overview
This research project asks whether adaptive controllers can beat fixed traffic-light timing on a simulated intersection. I built a custom two-way intersection in Pygame — inbound/outbound roads, turn curves, vehicles, and signal phases — then wrapped it in an MDP-style environment so four policies can be evaluated on the same metrics: wait time and collisions.
The repo ships pretrained Q-values from 10,000 training episodes, a genetic planner over short action sequences, baseline heuristics, and a Tkinter desktop UI that builds and runs python main.py while streaming episode output.
Evidence
Simulator & environment
TrafficSimulator models a four-way intersection with roads, vehicles, traffic signals, and a Pygame window. The RL Environment exposes a binary action space {0, 1} (hold vs. switch phase) and caps generation at max_gen = 50 vehicles per episode. State is a compact tuple: current signal phase, vehicle counts on each approach, and whether the junction is non-empty. Reward is inbound flow change — clearing cars raises the score; letting queues grow lowers it.
Learning & search
Q-learning uses ε-greedy exploration (ε = 0.1), learning rate α = 0.1, and discount γ = 0.6. Training for 10,000 episodes produced a persisted Q-table with 1,149 state–action values across 649 unique states. At runtime the training loop is commented out and the agent loads that file for evaluation. The Search module runs a genetic planner (Chosen_Length = 5) that proposes short action sequences and scores them on wait time / collisions, with optional MPS/CPU via PyTorch for device selection.
Methods compared
Evaluation UI
A Tkinter app (run_ui.py) lets you pick method, episode count, and live rendering, previews the exact CLI command, and streams stdout with wait-time and collision lines highlighted. That made side-by-side method comparison practical without memorizing flags. Dependencies listed in requirements.txt: scipy, numpy, pygame (plus Tkinter stdlib and torch for search).
Highlights
- Custom Pygame four-way intersection with vehicles, turn curves, and signal phases
- Shared evaluation for Fixed Cycle, Longest Queue First, Q-learning, and genetic search
- Q-learning state = (phase, queue₁, queue₂, junction occupied); reward = inbound flow change
- Persisted Q-table from 10,000 episodes: 1,149 entries over 649 unique states
- Genetic search evolves length-5 action sequences over {0, 1}
- Tkinter runner streams average wait time and collisions per episode for comparison
Technologies used
Core tools and libraries behind this project — click a logo for official docs.




