A car moves only when the road ahead is empty
Run Rule 184 on a closed ring road, watch a jam form from a local rule, and sweep density to recover a fundamental diagram the code never encoded.
Rule 184 is a one-dimensional ring road where 1 is a car and 0 is empty
road, and the whole rule is one sentence: a car moves one cell to the right
when the cell ahead is empty. Nothing about lanes, speeds, queues or
congestion is written down.
This program runs that rule: it prints the road before and after ten steps, checks that the car count has not changed, and sweeps the density to measure how much traffic actually moves.
Run it
Save this as rule184.py and run it with Python 3.11 or later. It needs NumPy
only.
import numpy as np
def traffic_step_with_flow(road):
cars = road == 1
empty_ahead = (
np.roll(road, -1) == 0
)
moving = cars & empty_ahead
next_road = road.copy()
next_road[moving] = 0
next_road[
np.roll(moving, 1)
] = 1
return next_road, int(moving.sum())
def make_road(length=240, density=0.35, seed=42):
rng = np.random.default_rng(seed)
return (
rng.random(length) < density
).astype(np.uint8)
def average_flow(density, steps=700, warmup=200, length=600, seed=1):
road = make_road(length, density, seed)
values = []
for t in range(steps):
road, moving = traffic_step_with_flow(road)
if t >= warmup:
values.append(moving / length)
return float(np.mean(values))
road = make_road(length=60, density=0.6)
cars_before = int(road.sum())
print("t=0 :", "".join("#" if c else "." for c in road))
for _ in range(10):
road, moving = traffic_step_with_flow(road)
print("t=10:", "".join("#" if c else "." for c in road))
print("cars:", cars_before, "->", int(road.sum()))
for density in (0.1, 0.3, 0.5, 0.7, 0.9):
print(f"density {density:.1f} -> flow {average_flow(density):.3f}")The program is the chapter’s own code composed into one file: the traffic
mechanism in its flow-counting form, make_road, average_flow, and a print
block that renders the ring as text. The rule is Rule 184 (10111000) in
traffic words.
Expected outcome
t=0 : .#..#...###...####...#...####...#######.#..#..##.###....####
t=10: .#.#.#.#.#.#.#.#.#.#.#.#.####.#.#.#.#.#.##.#.#.#.#.#.#.#.#.#
cars: 32 -> 32
density 0.1 -> flow 0.095
density 0.3 -> flow 0.303
density 0.5 -> flow 0.483
density 0.7 -> flow 0.293
density 0.9 -> flow 0.098The car count is unchanged, which is the point of the ring: cars move, they do
not appear or vanish. The road settles into separated cars with two packed
clusters where the initial layout was densest, and that is a jam. Nobody
computed a queue length. The flow sweep then produces the fundamental
diagram: flow peaks near half-filling and tracks min(density, 1 − density)
to within 0.02.
One local condition does all the work
The whole mechanism, in the chapter’s simplest form, is a condition and a move:
def traffic_step(road):
cars = road == 1
empty_ahead = (
np.roll(road, -1) == 0
)
moving = cars & empty_ahead
next_road = road.copy()
next_road[moving] = 0
next_road[
np.roll(moving, 1)
] = 1
return next_roadThree properties of that code carry the chapter’s argument. The np.roll
makes the road a closed ring, so the car count is fixed by construction. The
movement is a simultaneous mask update, so a car cannot swap places with the
car in front of it. And the update is synchronous.
Conservation is the invariant worth testing, and it is stronger than checking
a few cells. The chapter puts it as an assertion,
road.sum() == next_road.sum(), and the program prints the same quantity
before and after ten steps: the two numbers agree. That is what makes density
a clean control parameter: the road always holds the same number of cars, so
only how many of them get to move changes between measurements.
Mechanism and limitations
Documented: the chapter states that a car moves one cell right when the
destination is empty; that this is exactly Rule 184 underneath; that the car
count never changes under the deterministic rule or the stochastic variant;
that the closed ring is deliberate, so that “density becomes a clean control
parameter”; and that the steady-state flow follows approximately
min(density, 1 − density).
Observed: the run above. The car count was 32 before and 32 after ten
steps. The road relaxed from a random layout into free-flowing traffic with
two packed clusters. The measured flows were 0.095, 0.303, 0.483, 0.293 and
0.098 at densities 0.1, 0.3, 0.5, 0.7 and 0.9, matching the chapter’s own
verified figures and the min(density, 1 − density) shape to within 0.02.
Not shown. The chapter’s own extensions are not exercised. The stochastic hesitation variant is not implemented, so no claim is made about the two sources of lost flow, and the entry into the Nagel-Schreckenberg model is not taken. The model’s assumptions (one lane, one cell per vehicle, maximum speed one cell per step, no overtaking, closed ring) are all in force, and the chapter’s guardrail stands: a conservation law is not a realistic traffic model and not a predictive transportation model. No open-boundary run is compared, so the sweep is conditional on the closed system.
Understand this example
Copy this prompt into your AI tool. No code runs here.
Apply this example
Copy this prompt into your AI tool. No code runs here.