Traffic Light Tamer
Cars are piling up at your crossroads! Tap it (or press Space) to switch the lights. Can you make cars wait less than the boring timer light next door?
You
Timer light (12 s each)
Big number = average wait per car this round, in pretend seconds. Both crossroads get exactly the same cars.
Robo vs the timer, round by round
Average wait per car (seconds, lower is better · 1 round = 10 pretend minutes)
Checkpoints — Robo's cheat-sheet saved along the way. Tap one to watch that old Robo run today's traffic.
How it works
Look
Robo sees a few things: how many cars wait at green, how many at red, how long the light has been green, and whether an ambulance is waiting.
Check the cheat-sheet
For that situation its table has two scores: "keep green" and "switch". It picks the higher one, or sometimes tries a random move to learn more.
Feel the result
Every second, each waiting car is one point of "ouch". A waiting ambulance is 25 points! Yellow lights cost time because nobody moves.
Fix the score
It nudges the score of the move it made toward "the ouch I got, plus the best score of where I ended up". Thousands of nudges later, the table is smart.
What just happened?
- A timer light switches every 12 seconds no matter what. It never looks at the cars, so it gives green to empty roads and red to long queues.
- Robo started with an empty cheat-sheet. At first it switched at silly moments and cars piled up.
- Every second it felt a little "ouch" for each waiting car. It wrote down which move (keep or switch) led to fewer ouches later, in each situation.
- After a few rounds its cheat-sheet said things like "lots of cars at red, few at green: switch!", and it made cars wait less than the timer.
- When you changed the rules (rush hour, ambulances, long yellows) the old cheat-sheet was partly wrong. Robo kept learning and adapted, while the timer stayed the same.
For grown-ups: what's really going on
This is tabular Q-learning for adaptive traffic signal control, compared against a fixed-time controller on identical demand.
- Simulation: discrete time, 1 tick = 1 s. Each approach (N, S, E, W) gets Poisson arrivals with rate λ per tick (the sliders show 60λ cars/min). Two phases: N–S green or E–W green. During green, each green approach discharges one car per tick (saturation flow 3600 veh/h/lane). A switch inserts a yellow/all-red of Y ticks (2 by default, 5 in the challenge) in which nobody moves. Straight-through movements only. An approach holds at most 40 cars; further arrivals detour (balk) and are not counted, which keeps a hopeless controller's numbers finite.
- State (152 discrete): without an ambulance, (queue on the green phase, queue on the red phase, time since green started) binned as {0, 1–2, 3–4, 5–7, 8–11, 12+} × {0, 1–2, 3–4, 5–7, 8–11, 12+} × {4–6, 7–10, 11–17, 18+} s = 144 states. With an ambulance waiting, (which phase it is on) × time bin = 8 states. The state is relative to the current phase, so N–S and E–W share one table.
- Actions: keep or switch, decided every tick once the minimum green of 4 s has passed. A safety rule forces a switch after 60 s of green (a real controller's max-green).
- Reward:
r = −(cars waiting) − 25·(ambulances waiting)per tick (ambulance term only in that challenge). Ambulances jump to the front of their queue, so they need only a green. - Update (semi-Markov Q-learning): a switch lasts through the whole yellow, so for an action that took k ticks:
Q(s,a) ← Q(s,a) + α [ Σi<k γi ri + γk maxa' Q(s',a') − Q(s,a) ], with step size max(α, 1/n(s,a)). Defaults α = 0.1, γ = 0.9, ε = 0.05. Q starts at 0, which is optimistic because all rewards are negative, so untried moves get tried. - Exploration: ε-greedy with a per-state fade, εs = ε · 100 / (100 + n(s)). In a never-visited state the agent defaults to serving the longer queue.
- Metric: average delay per car = total vehicle-seconds of queueing in the round ÷ cars that arrived (a Little's-law style delay; it also counts cars still stuck at the end).
- Measured in Node (8 seeds, default 18 + 18 cars/min, rounds of 600 ticks): Robo's first round averages about 18 s per car (it is exploring) against the timer's 8 s, it first beats the timer in round 3–6, and over rounds 13–20 it averages about 6.7 s vs 8.0 s. A frozen trained table also wins on unseen traffic (6.2 s vs 7.9 s). In rush hour (25 vs 6 cars/min) the 12 s timer cannot carry the north–south demand and its north–south queues fill up to the 40-car limit (about 31 s per car), while Robo stays near 5 s. With ambulances, Robo's average ambulance wait falls to about 2.7 s vs the timer's 4.8 s. With 5 s yellows, Robo cuts its switches from about 60 to 33 per round and edges out the timer (14.9 s vs 15.6 s; a 12 s fixed time is close to the best fixed plan here).
- Real world: deployed adaptive systems (SCOOT, SCATS and newer RL research controllers) use loop detectors or cameras for the queue counts, coordinate many junctions, and must handle turning traffic, pedestrians and fairness. Deep RL replaces this small table with a neural network once the state is too big to list.