Traffic light algorithms

random trip report

Berkeley has lots of intersections of streets A and B where

  • A has much more traffic than B.
  • There's a traffic light.
  • There are traffic sensors (inductive or ultrasound).

At most of these intersections, the traffic light is timed: X seconds for A, Y seconds for B, repeat. The traffic sensors are apparently not used. X and Y are about the same even though the arrival rates are much different.

This is sub-optimal, of course. A lot of the time cars are stopped by a red light (usually on A), and there's no traffic in the other direction. This pisses me off. Time is the most precious thing I have, and it's being wasted by a lazy and dumb algorithm.

My algorithm

Here's my attempt at a better algorithm for such intersections. I assume no left-turn lanes or left-turn signals.


  • G(A) means the light is green for A.
  • C(A) means one of the two traffic sensors for A is activated.
  • TA and TB are the durations of greens for A and B resp. when there is traffic in both directions. These should be proportional to the average traffic rates, in the 30 second range.
  • T1 is the max time between sensor activations when there's continuous traffic. 5 sec or so.
  • T2 is the max time a car turning right activates a sensor if there is no cross traffic. 10 sec or so.

G(A) initially.

If G(A), switch to G(B) if either of the following is true:

  1. Not C(A) for at least T1, and C(B) for at least T2.
  2. G(A) for at least TA and C(B) for at least T2.

1) means that if there hasn't been any traffic on A for a while, and a car arrives on B, it will get a green pretty much immediately. The T2 pause is so that if the car turns right, the light won't change.

2) means that if there is steady traffic on A, and a car arrives on B, it will get a green after a while.

If G(B), switch to G(A) if one of the following is true:

  1. Not C(B) for the last T1 sec.
  2. G(B) for the last TB sec.

Hence B's green light will end after TB sec, or when there is no more traffic on B.

If there is unlimited traffic in both directions, the lights will alternate with periods of TA and TB. If there is no traffic, G(A) will always hold.

I'm omitting some stuff:

  • We may want to have a max for G(A) to handle situations like a bike failing to trigger a sensor, or some other failure mode.
  • Pedestrians: there should be buttons, and peds should have priority over cars.

And of course things are more complex for intersections of 4-lane streets with left- and right- turn lanes.

The field of traffic engineering

What's a good formalism for expressing traffic-light algorithms? I use pseudocode above. Is there something better, maybe involving temporal logic or constraint programming?

It turns out there's an entire academic field that deals with stuff like this!

People write research papers like this and this.

UC Berkeley has an Institute of Traffic Studies.

Here's a survey of traffic sensor technology as of 2007.

I don't have time right now to read a lot of papers and see if the algorithm I sketch above has been studied, or improved on. If you want to do this, please let me know what you find.

People have written simulators to study traffic control algorithms. Here's one with a web-based GUI.

Copyright 2024 © David P. Anderson