Phantom Traffic Jam

Why a highway jams with no crash and no bottleneck: past a critical reaction time, one brake tap becomes a phantom stop-and-go wave that creeps backward.

Level: Advanced

trafficagent-basedemergencetime-delaycontrolbalancing-loop

  • Stocks: speed of each car, gap to the car ahead
  • Flows: acceleration, gap change
  • Feedback Loops: headway control (balancing), reaction delay (makes that loop overshoot)
  • Probes: speed_spread, slowest_car, min_gap

Emergence

Discover how interactions between parts can create properties and behaviors that no individual component possesses alone.

Take the Quiz

Delays

Learn about delays in systems, how they create oscillations, and their impact on system behavior and decision-making.

Take the Quiz
simulation.py

Phantom traffic jams on a ring road

Twenty-two identical cars circle a loop. Each speeds up when the gap ahead opens and eases off when it closes, but only after a reaction delay. Nudge one car: past a critical reaction time that brake tap grows into a stop-and-go wave that creeps backward, with no crash and no bottleneck. Shorten the reaction and the same tap dies.


from math import atan2, cos, cosh, pi, sin, tanh

from tys import frame, probe, progress

Run one ring of drivers and report whether a jam emerged.

def simulate(cfg: dict):

    num_cars        = cfg["num_cars"]         # identical drivers on the loop
    spacing         = cfg["spacing"]          # uniform centre-to-centre gap at the start
    max_speed       = cfg["max_speed"]        # free-flow speed on an open road
    desired_headway = cfg["desired_headway"]  # gap where the speed-gap curve is steepest
    response_width  = cfg["response_width"]   # how gently target speed tracks the gap
    reaction_time   = cfg["reaction_time"]    # KNOB: how sluggishly a driver adjusts speed
    perturbation    = cfg["perturbation"]     # one-off speed dip on a single car
    sim_time        = cfg["sim_time"]
    dt              = cfg["dt"]

A zero reaction time would make sensitivity infinite. Keep a tiny floor.

    reaction_time = max(float(reaction_time), 1e-3)
    ring_length = num_cars * spacing

Quick reactions mean high sensitivity; a sluggish driver has low sensitivity.

    sensitivity = 1.0 / reaction_time

The one local rule

Each driver aims for an optimal velocity set by the gap ahead: zero when bumper to bumper, rising through the desired headway, saturating at free-flow speed. This is the Bando optimal-velocity curve. The whole jam is twenty-two lagged copies of just this curve.

    norm = 1.0 + tanh(desired_headway / response_width)

    def target_speed(gap):
        rise = tanh((gap - desired_headway) / response_width)
        return max_speed * (rise + tanh(desired_headway / response_width)) / norm

Slope of that curve at the cruising gap. Uniform flow is linearly stable only while sensitivity > 2 * slope, i.e. reaction_time below the value returned as critical_reaction_time.

    sech = 1.0 / cosh((spacing - desired_headway) / response_width)
    slope = max_speed / response_width * sech * sech / norm
    critical_reaction_time = 1.0 / (2.0 * slope)

Start in a perfectly uniform stream, then tap the brakes on one car.

    pos = [i * spacing for i in range(num_cars)]
    cruise = target_speed(spacing)
    vel = [cruise] * num_cars
    vel[0] = cruise * (1.0 - perturbation)

Space-time canvas. Each row is a later moment; a dark cell is a car well below cruise, so a jam is a stripe and smooth flow stays blank.

    grid_cols = 48
    slow_mark = 0.25 * cruise
    rows = []
    spread_series, slowest_series = [], []

(time, ring fraction) of the stopped cluster, for the upstream-drift fit.

    jam_samples = []

    steps = int(round(sim_time / dt))
    record_every = max(1, steps // 100)
    told_amplifying = False
    told_wave = False

No passing, and why the stripe moves backward

A car that brakes late must not leapfrog the one ahead — that would be a numerical crash, not a jam — so each step is capped at the current gap. Cars still move forward. The slow patch moves backward: each driver enters it slightly after the car ahead and leaves it slightly later, so the patch drifts upstream.

    for step in range(steps):
        gaps = [(pos[(i + 1) % num_cars] - pos[i]) % ring_length for i in range(num_cars)]
        new_vel = []
        for i in range(num_cars):
            v = vel[i] + sensitivity * (target_speed(gaps[i]) - vel[i]) * dt
            new_vel.append(v if v > 0.0 else 0.0)
        new_pos = []
        for i in range(num_cars):
            travel = vel[i] * dt
            if travel > gaps[i]:
                travel = gaps[i]
            new_pos.append((pos[i] + travel) % ring_length)
        pos = new_pos
        vel = new_vel

        if step % record_every == 0:
            slowest = min(vel)
            spread = max(vel) - slowest
            spread_series.append(spread)
            slowest_series.append(slowest)
            t = step * dt
            probe("speed_spread", t, spread)   # ~0 while uniform; climbs as the wave opens
            probe("slowest_car", t, slowest)   # stays near cruise, or falls into the jam
            probe("min_gap", t, min(gaps))     # stays near the spacing, or collapses
            row = [0] * grid_cols
            stopped = []
            for i in range(num_cars):
                if vel[i] < slow_mark:
                    frac = pos[i] / ring_length
                    row[int(frac * grid_cols) % grid_cols] = 1
                    stopped.append(frac)
            rows.append(row)
            if len(stopped) >= 2:

Circular mean of the stopped cars: where the jam sits on the ring.

                sx = 0.0
                sy = 0.0
                for frac in stopped:
                    ang = 2.0 * pi * frac
                    sx += cos(ang)
                    sy += sin(ang)
                jam_samples.append((t, atan2(sy, sx) / (2.0 * pi) % 1.0))

            pct = int(100 * step / steps)
            if not told_amplifying and spread > 0.4:
                progress(pct, "The brake tap is amplifying: each car behind brakes a little harder.")
                told_amplifying = True
            elif not told_wave and slowest < slow_mark:
                progress(pct, "A stop-and-go wave has formed and is creeping backward around the ring.")
                told_wave = True
            else:
                progress(pct)

    if told_wave:
        progress(100, "The jam took the ring. Nothing was blocking the road.")
    else:
        progress(100, "The nudge died out. Every car is back near one cruise speed.")
    frame(0, rows)

Lab-frame speed of the stopped cluster. Negative means upstream.

    jam_drift = 0.0
    if len(jam_samples) >= 4:
        unwrapped = [jam_samples[0][1]]
        for _, frac in jam_samples[1:]:
            delta = (frac - unwrapped[-1] + 0.5) % 1.0 - 0.5
            unwrapped.append(unwrapped[-1] + delta)
        xs = [sample[0] for sample in jam_samples]
        n = len(xs)
        mean_t = sum(xs) / n
        mean_x = sum(unwrapped) / n
        numer = sum((xs[i] - mean_t) * (unwrapped[i] - mean_x) for i in range(n))
        denom = sum((xs[i] - mean_t) ** 2 for i in range(n))
        if denom > 0.0:
            jam_drift = numer / denom * ring_length

    tail = max(1, len(spread_series) * 2 // 3)
    wave_amplitude = sum(spread_series[tail:]) / len(spread_series[tail:])
    min_speed = min(slowest_series[tail:])
    return {
        "jam_formed": wave_amplitude > 0.5,
        "wave_amplitude": wave_amplitude,
        "min_speed": min_speed,
        "cruise_speed": cruise,
        "critical_reaction_time": critical_reaction_time,
        "jam_drift": jam_drift,
    }

Pure-Python agent model — no external solver needed.

def requirements():
    return {"external": []}
Phantom jam (0.80 s).yaml
# jam.yaml — the default, just past the point where a full stop-and-go wave
# shows up inside this run. The linear stability line sits near a 0.49s
# reaction (see critical_reaction_time). At 0.80s the same brake tap grows
# into a wave that creeps backward. Drop reaction_time to 0.48 (smooth.yaml)
# and the tap dies.
num_cars: 22
spacing: 2.0
max_speed: 2.0
desired_headway: 2.0
response_width: 1.0
reaction_time: 0.80
perturbation: 0.12
sim_time: 240
dt: 0.05
Charts (Phantom jam (0.80 s))

speed_spread

How far apart the fastest and slowest cars are. Near zero means the brake tap died and everyone shares one speed. A climb toward free-flow speed means a stop-and-go wave has opened up.

Samples100 @ 0.00–237.60
Valuesmin 0.04, mean 1.21, median 1.70, max 1.74, σ 0.67

slowest_car

Speed of the slowest car. Smooth flow recovers to the cruise speed. Past the stability line this falls as cars pile into the jam.

Samples100 @ 0.00–237.60
Valuesmin 0.11, mean 0.37, median 0.14, max 0.97, σ 0.34

min_gap

The tightest gap on the ring. It stays near the initial spacing when flow is uniform, and collapses as cars bunch inside the wave.

Samples100 @ 0.00–237.60
Valuesmin 0.68, mean 1.11, median 0.74, max 2.00, σ 0.52
Final Frame (Phantom jam (0.80 s))
final frame
Final Results (Phantom jam (0.80 s))
MetricValue
jam_formedtrue
wave_amplitude1.73
min_speed0.11
cruise_speed0.98
critical_reaction_time0.49
jam_drift-0.27
Smooth flow (0.48 s).yaml
# smooth.yaml — the contrast. Identical to jam.yaml except reaction_time:
# 0.48s sits just under the ~0.49s stability line, so the same brake tap
# damps out and the ring returns to one cruise speed.
num_cars: 22
spacing: 2.0
max_speed: 2.0
desired_headway: 2.0
response_width: 1.0
reaction_time: 0.48
perturbation: 0.12
sim_time: 240
dt: 0.05
Charts (Smooth flow (0.48 s))

speed_spread

How far apart the fastest and slowest cars are. Near zero means the brake tap died and everyone shares one speed. A climb toward free-flow speed means a stop-and-go wave has opened up.

Samples100 @ 0.00–237.60
Valuesmin 0.00, mean 0.01, median 0.00, max 0.11, σ 0.01

slowest_car

Speed of the slowest car. Smooth flow recovers to the cruise speed. Past the stability line this falls as cars pile into the jam.

Samples100 @ 0.00–237.60
Valuesmin 0.88, mean 0.98, median 0.98, max 0.98, σ 0.01

min_gap

The tightest gap on the ring. It stays near the initial spacing when flow is uniform, and collapses as cars bunch inside the wave.

Samples100 @ 0.00–237.60
Valuesmin 1.99, mean 2.00, median 2.00, max 2.00, σ 0.00
Final Frame (Smooth flow (0.48 s))
final frame
Final Results (Smooth flow (0.48 s))
MetricValue
jam_formedfalse
wave_amplitude0.00
min_speed0.98
cruise_speed0.98
critical_reaction_time0.49
jam_drift0.00
FAQ
What sets the tipping point?
Uniform flow is linearly stable only while sensitivity (1 / reaction_time) stays above twice the slope of the speed-gap curve at the cruising gap. With these cars that boundary is a reaction time of about 0.49 seconds, reported as critical_reaction_time. The smooth config sits just under it at 0.48 seconds and the brake tap dies. The default is 0.80 seconds. A reaction of 0.55–0.60 seconds is already past the line, but out to 300 time units that disturbance is still only a ripple with no upstream drift, so 0.80 seconds is as close as this run can sit and still show the backward jam.
Why does the jam move backward while the cars move forward?
Each driver enters the slow patch slightly after the car ahead and leaves it slightly later, so the patch itself drifts upstream, against the flow, even while every car is still pointed forward. jam_drift is that lab-frame speed and is negative only when the wave has formed. The cars are the medium; the jam is the pattern moving through them.
What stabilizes traffic?
A faster reaction. Drop reaction_time under about 0.49 seconds and the same tap damps, which is what adaptive cruise control does by responding in a fraction of a human reaction time. A larger desired_headway or a wider spacing also flattens the speed-gap slope and raises the reaction time you can get away with.
What is the real-world analog?
In 2008 Sugiyama, Fukui, and colleagues put real cars on a circular track at even spacing and asked drivers only to follow the car ahead. With no obstacle, a stop-and-go wave still formed and traveled upstream. The same late correction shows up wherever a balancing loop arrives after the state has already moved: a thermostat that rings, a supply chain that bullwhips, a crowd that bunches.
Doesn't a jam need a crash, a hill, or a bottleneck?
No. The jam is not a place on the road, and it does not need a first cause you can point at. Once reaction time is past the stability line, a tiny speed dip is enough. Clearing an imagined obstacle does not clear the wave — the wave is how this crowd propagates a disturbance.