Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Solutions · 15. Emergence: traffic jams in a cellular automaton and flocking

School of Engineering, Nazarbayev University

Worked solutions to the three exercises of Lecture 15. Each solution is computed here and checked with assert statements against an exact identity or an independent calculation. The first code cell sets up the notebook and runs Demonstration 1 of the lecture without showing its output, because some of the solutions use its variables.

Exercise 1

Problem. Verify the exact deterministic cellular-automaton flow law J(ρ)=min⁡(ρvmax⁡, 1−ρ)J(\rho) = \min(\rho v_{\max},\, 1 - \rho) at p_slow = 0.0, road_length = 200, and v_max = 4 across 15 densities ρ∈[0.05,0.95]\rho \in [0.05, 0.95] (n_cars = 10, 20, 30, 36, 40, ..., 190). Verify machine-precision agreement (atol = 1e-13) in steady state (t≥150t \ge 150), and verify particle-hole duality: for ρ≤ρc=0.20\rho \le \rho_c = 0.20, zero cars ever brake (gap >= v_max everywhere); for ρ≥ρc\rho \ge \rho_c, zero holes ever bunch (every empty cell advances backward by 1 cell/tick, so J=ρholes⋅1=1−ρJ = \rho_{\mathrm{holes}} \cdot 1 = 1 - \rho).


Exercise 1: Exact 15-density verification of J(rho) = min(rho * v_max, 1 - rho) and particle-hole duality
=========================================================================================================
Tested 15 densities in [0.05, 0.95] at L=200, v_max=4, p_slow=0:
Peak flow at critical density rho_c = 0.20: J_sim = 0.800000000000 vs. exact 4/5 = 0.800000000000
Maximum |J_sim - min(rho*v_max, 1 - rho)| across all 15 densities = 1.110e-16
<Figure size 825x451 with 1 Axes>

Figure S15.1. Steady-state flow of the deterministic model at 15 densities (points) against min⁡(4ρ,1−ρ)\min(4\rho, 1 - \rho) (dashed), with the critical density 0.20 (dotted).

Explanation. When ρ≥ρc=1vmax⁡+1\rho \ge \rho_c = \frac{1}{v_{\max}+1} and pslow=0p_{\mathrm{slow}} = 0, after transient relaxation no gap exceeds vmax⁡v_{\max}, so every car’s new speed equals its exact gap vi(t)=gi(t)v_i(t) = g_i(t). That means car ii steps onto the exact cell right behind car i+1i+1’s previous position—which is equivalent to every empty hole stepping backward by 1 cell per tick without blocking. Since hole density is 1−ρ1 - \rho and every hole shifts 1 cell per tick, exactly 1−ρ1 - \rho cars advance across any detector per tick: J(ρ)=1−ρJ(\rho) = 1 - \rho.


Exercise 2

Problem. Audit two subtle bugs that ruin agent-based simulations: (a) Compare synchronous parallel updates (nagel_schreckenberg_traffic) against asynchronous in-place sequential car updates (pos[i] updated immediately inside for i in range(N)) at ρ=0.50\rho = 0.50, vmax⁡=4v_{\max} = 4, pslow=0p_{\mathrm{slow}} = 0, showing how sequential sweep order artificially inflates throughput above the physical 1−ρ=0.501 - \rho = 0.50 hole-conservation bound. (b) Compare circular vector atan2(sum sin, sum cos) against naive arithmetic mean(theta) on a westward-flying Vicsek flock (θ≈±π\theta \approx \pm\pi), showing how the −π/+π-\pi/+\pi branch cut reverses the flock and its mean velocity Φ\Phi.


Exercise 2: Asynchronous in-place traffic bug and branch-cut angle-averaging collapse
=====================================================================================
[Part A] At rho = 0.50: Exact physical bound 1 - rho = 0.5000 | Synchronous CA = 0.5000 | Buggy In-Place Async = 2.0000 (unphysical, > 0.50)
[Part B] Two westward drones [-179 deg, +179 deg]: Naive mean = +0.0 deg (east, backward) vs. atan2 vector mean = +180.0 deg (west)
Westward flock mean v_x (initial = -0.994 West): Vector atan2 = -0.997 (stays West) vs. Arithmetic mean = +0.955 (reversed, 180 deg east)
<Figure size 1254x440 with 2 Axes>

Figure S15.2. Two simulation bugs. (a) Flow at ρ=0.5\rho = 0.5: the in-place update gives 2.0, four times the physical limit 1−ρ1 - \rho. (b) East–west velocity of a westward flock: the circular mean keeps it at -1, the arithmetic mean of the angles reverses it.

Explanation. (a) In cellular automata and stencil PDEs, updating array entries in-place turns a discrete-time synchronous law sit+1=F(st)s_i^{t+1} = F(\mathbf{s}^t) into a sweep-direction-dependent Gauss-Seidel cascade in which a car can move into a cell freed in the same time step, so empty space travels upstream without delay. (b) Angles live on the circle S1S^1, not the real line R\mathbb{R}: atan2(sum(sin), sum(cos)) embeds headings on the unit circle where [−179∘,+179∘][-179^\circ, +179^\circ] average cleanly to (−0.9998,0)↦180∘(-0.9998, 0) \mapsto 180^\circ independently of where the branch cut lies.


Exercise 3

Problem. Implement Thomas Schelling’s (1971) Spatial Segregation Model schelling_segregation_emergence(L=36, tolerance_tau=0.35, vacancy_frac=0.12, max_sweeps=80, seed=42) in NumPy on an L×LL \times L grid with open boundaries (8-neighbor Moore stencil). An occupied cell is satisfied if it has zero occupied neighbors OR at least fraction tolerance_tau of its occupied neighbors share its own type (+1 or -1). Each sweep relocates all dissatisfied agents in random order into randomly chosen vacant cells (0). Prove that even at mild tolerance τ=0.35\tau = 0.35 (agents welcome up to 65%65\% unlike neighbors), the same-type neighbour similarity rises from about 50 %50\,\% to more than 75 %75\,\%.


Exercise 3: Implement it yourself — Schelling's tipping-point segregation emergence
===================================================================================
Schelling L=36, tau=0.35 (happy with 65% unlike neighbors):
  Initial mixed similarity = 49.1% -> Final segregated similarity = 77.7% (unhappy left = 0)
<Figure size 924x880 with 3 Axes>

Figure S15.3. Schelling’s model on a 36 × 36 grid. (a) The initial random mix. (b) The equilibrium at τ=0.35\tau = 0.35. (c) Final like-neighbour share against the tolerance τ\tau, always well above τ\tau itself (dashed).

Summary. The similarity rises from 49.1 %49.1\,\% to 77.7 %77.7\,\% although each agent is content with 35 %35\,\% like neighbours, so a strongly segregated pattern (or a phase-separated alloy) does not imply that the individuals sought homogeneity. When an agent leaves a borderline cell, it lowers the like-neighbour fraction of the neighbours left behind, which can make them move as well; the moves continue until broad domains with thin interfaces have formed.