Traffic Simulator — the lights decide

Every car on the map is a rule — STOP for the car ahead, STOP for the red, GO, WAIT it out, TURN by argmax over the exit-arm queues, REROUTE when the straight queue is long. The signal lights are the decision agents: each re-scores every sim-second from the queue counts, and a genuine near-tie (gap ≤ 120) goes to the engine over keep or switch — under a city-wide batching budget: no token, no question, and the rung says so. Emergency preemption is a rule checked every tick and is never asked. Click a car to follow it; dispatch an emergency and watch the preemptions land.

What decides what

  • Cars are rules, full stop. A car ahead → STOP; a red at the boundary → STOP; 3 s stopped → WAIT (once); clear → GO. Neither state nor transition is ever put on the wire — a test scans every escalated request across a live run and asserts only keep/switch candidates appear.
  • Turns are argmax, reroutes are the argmax flipping. At each intersection entry a car scores straight/left/right from the queue on the exit street ahead (300 − 60 per queued car). When the straight arm wins the argmax and its queue is 5+ deep, the pick flips to a side street — the REROUTE. Still a rule; still never asked.
  • The lights are the decision agents. Each re-scores every sim-second: keep = 300 + 40 per car on green + 200 per car in the box; switch = 300 + 40 per car on red + the red side's waiting time. Rules first — under the 6 s minimum phase nothing switches, past 24 s everything does, and an emergency approaching on a red arm preempts every tick. A genuine near-tie (gap ≤ 120) goes to the engine over {keep, switch} if the city-wide budget has a token; the budget refills at N asks per sim-second.
  • Determinism is test-pinned. Same seed, same city — every spawn, stop, turn, and phase change replays exactly. The spec tests (no gridlock deadlock, emergency preemption fires, minimum phase respected, determinism, batching budget) run in CI.

Honest edges

  • The engine is a budgeted voice, not the boss: 12 lights re-scoring once a sim-second can ask up to 12 times a second, but the default budget is 2 asks/s — most near-ties are decided by the argmax rule, and every one of them says so on the rung. Set the budget to 0 and the city runs entirely on rules.
  • Near-ties are common by design at quiet intersections (two empty arms score 300 vs 300): the ledger shows what the engine actually bought. If honored switches do not move throughput or the wait percentiles, that is what the numbers will show.
  • The exit-arm queue reads the 8 cells AHEAD of where you would land — it is a local measurement, not a route planner. A car that reroutes can still end up behind the jam it fled.
  • The scoreboard is session-only, auto-recorded at every 50th completed trip: close the tab and it is gone. Nothing leaves this page.