Modelcore
Social force model
Also called Helbing social force model.
Move each pedestrian by a driving force toward its goal plus repulsive forces from other people and walls, integrated as a particle system.
The social force model moves pedestrians as particles in the plane. Each person has a goal and a desired speed, and three kinds of force act on the person: a driving force that relaxes the velocity toward the desired velocity, a repulsion from every other person and from every wall that grows fast as they come close, and, when bodies touch, a body force and a sliding friction. Dirk Helbing and Péter Molnár introduced the model in 1995; the form on this page, with the body force and the friction, is the one of Helbing, Farkas and Vicsek (2000). The forces are "social" in the sense that they describe the wish to keep a distance; only the contact forces stand for real forces between bodies. All constants are fitted or tuned values, not laws.
The machine
The state is a set of people. Person has a position (in m), a velocity (in m/s), a mass , a radius , a desired speed and a goal; is the unit vector from toward the goal. The equation of motion of Helbing, Farkas and Vicsek is
The first term is the driving force: without other forces, the velocity relaxes to with the time constant . For two people with , the unit normal from to , the tangent , the sum of radii and , the pair force is
The exponential term is the social repulsion. The term with is a body force that resists compression, and the term with is a sliding friction that resists the relative tangential motion; both act only when the bodies overlap, . A wall acts in the same way, with the distance to the wall, the normal from the wall to the person, and the friction term .
The constants of the 2000 paper are N, m, kg/s, kg/(m s), s, kg and radii from 0.25 m to 0.35 m. The desired speed is about 0.6 m/s for relaxed walking, about 1 m/s for normal walking and up to 1.5 m/s for nervous people. The paper chose and to fit a measured flow of about 0.73 persons per second through a door of 1 m. These are fitted and tuned values.
In escape panic the paper also adds herding: the desired direction becomes a normalized mix of the own direction and the mean direction of the neighbors, with a panic parameter . The 1995 model has the same driving term, with repulsive potentials from other people and from borders, attraction to friends or to attractive places, and a weaker effect of what is behind a person than of what is in front of the person.
The code with this page keeps the pair force of the 2000 paper and leaves out the walls, the herding and the attraction. It steps the equations with symplectic Euler, velocity first, with a time step :
SOCIAL-FORCE-STEP(X, V, R, goals, v⁰, Δt)- 1
Fᵢ ← m (v⁰ᵢ eᵢ - vᵢ) / τ for every person i ▷ the driving force for every pair i < j- 2
f ← f_ij(xᵢ, xⱼ, vᵢ, vⱼ, rᵢ, rⱼ) ▷ repulsion, body force, friction - 3
Fᵢ ← Fᵢ + f; Fⱼ ← Fⱼ - f ▷ the reaction on j for every person i- 4
vᵢ ← vᵢ + Δt Fᵢ / m - 5
xᵢ ← xᵢ + Δt vᵢ return X, V
The standing assumptions are those of a particle system: people are disks in the plane with no body orientation, the forces add, and every person sees every other person. In the code all people have the same mass.
What it counts
The model gives the trajectories of all people, and from them the quantities of crowd studies: the flow through a door or a corridor (persons per second), the density (persons per square meter), the speeds, and the time to leave a room. It is a model of collective behavior, and the papers compare its output with observed patterns, not with single trajectories. The fit of and to a door flow is a measurement on real crowds; that the simulated crowd shows a pattern is a measurement on the model.
The direct cost of one step is time for all pairs and space. The repulsion decays by the factor every m, so implementations cut it off at a distance and find the near pairs with a uniform grid or spatial hashing, which costs expected time per step at a bounded density.
Relation to other models
- A particle system has the same state and update loop but no forces between particles. The social force model adds pair forces, so it is closer to molecular dynamics, with social potentials in place of physical ones.
- Boids steer each agent by rules on the velocities of its neighbors (separation, alignment, cohesion) for flocks, not by forces with a goal for each agent.
- Velocity obstacles, reciprocal velocity obstacles and ORCA choose a new velocity outside the set of velocities that lead to a collision within a time window. They are geometric and predict the motion of others, while social forces react only to the present distance; ORCA proves collision free motion under its assumptions, and the social force model has no such guarantee.
- Continuum crowds replace the people by a density field per group and plan with a potential over the whole space; they avoid local minima of the goal direction, which the social force model, with only a straight line to the goal, does not.
- Cellular automaton models of pedestrians move people between cells of a grid with rules, in discrete time and space.
Known results in the model
- Antisymmetric pair forces (proved). Exchanging and changes the sign of , and , so . So the pair forces of a group sum to zero, and only the driving forces and the walls change its total momentum. Line L3 applies the reaction by this rule. The test checks the rule on random pairs to a relative , and the zero sum for 12 people.
- Relaxation (proved, with its step limit). A single person with no other forces has, under symplectic Euler, . The exact solution has in place of the power. The step converges for and does not overshoot for . The test checks the formula to for 100 steps at s.
- Step limit of the body force (proved for this term alone). Two overlapping people, with only the body force, are a linear spring on the reduced mass , with rad/s. Symplectic Euler is stable for this spring when s. The friction and the stiffness of the repulsion add to this, so the code uses s.
- Standing distance (proved). A person at rest who pushes toward a fixed person feels the driving force and the repulsion. The two balance at if . For m/s and m, the driving force is 160 N and m. So with these constants one person alone does not touch another, and contact forces come only when several people push in a row.
- Two crossing groups (measured). In the test, two groups of 5 people walk through each other in opposite directions with m/s, s and 2000 steps (seed 4). They pass in 20 s, and the smallest gap between two bodies is about 0.068 m, with no overlap. This is a measurement of one toy, not a bound.
- Lanes and oscillations (observed in simulation). Helbing and Molnár (1995) report that people walking in opposite directions in a corridor form lanes of one walking direction, and that the passing direction at a narrow bottleneck oscillates. Both are self organized patterns of the model, also seen in real crowds.
- Panic effects (observed in simulation). Helbing, Farkas and Vicsek (2000) report that people with a high desired speed jam at a door and leave more slowly than calm people ("faster is slower"), that the crowd forms arches before the exit, and that in a smoke filled room with strong herding the crowd uses the exits badly, while a mix of individual and herding behavior works best. These are measurements on the model, with the constants above.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/social-force-model/. The authors are listed in prov/4BAJB2.jsonl.
"""The social force model in the form of Helbing, Farkas and Vicsek (2000).
Each person i has a position x_i, a velocity v_i, a radius r_i, a mass m,
a desired speed v0 and a goal. The force on i is
F_i = m (v0 e_i - v_i) / tau + sum over j != i of f_ij,
with e_i the unit vector from x_i to the goal, and for d = |x_i - x_j|,
n = (x_i - x_j) / d, t = (-n_y, n_x), r = r_i + r_j and g(z) = max(z, 0):
f_ij = (A exp((r - d) / B) + k g(r - d)) n + kappa g(r - d) ((v_j - v_i) . t) t.
The first part is the social repulsion, the second the body force and the
third the sliding friction; the last two act only when the bodies touch.
Walls are left out. The defaults are the constants of the 2000 paper;
they are fitted or tuned values, not laws. The step is symplectic Euler
(velocity first). The plane is x-y; there is no gravity in the model.
"""
import math
PARAMS = {
"A": 2.0e3, # N
"B": 0.08, # m
"k": 1.2e5, # kg / s^2
"kappa": 2.4e5, # kg / (m s)
"tau": 0.5, # s
"m": 80.0, # kg
}
def pair_force(xi, xj, vi, vj, ri, rj, p=PARAMS):
dx, dy = xi[0] - xj[0], xi[1] - xj[1]
d = math.hypot(dx, dy)
nx, ny = dx / d, dy / d
tx, ty = -ny, nx
over = max(ri + rj - d, 0.0)
a = p["A"] * math.exp((ri + rj - d) / p["B"]) + p["k"] * over
dvt = (vj[0] - vi[0]) * tx + (vj[1] - vi[1]) * ty
f = p["kappa"] * over * dvt
return (a * nx + f * tx, a * ny + f * ty)
def driving_force(x, v, goal, v0, p=PARAMS):
ex, ey = goal[0] - x[0], goal[1] - x[1]
n = math.hypot(ex, ey)
if n > 0:
ex, ey = ex / n, ey / n
return (p["m"] * (v0 * ex - v[0]) / p["tau"], p["m"] * (v0 * ey - v[1]) / p["tau"])
def forces(X, V, R, goals, v0, p=PARAMS):
n = len(X)
F = [list(driving_force(X[i], V[i], goals[i], v0[i], p)) for i in range(n)] # @line L1
for i in range(n):
for j in range(i + 1, n):
fx, fy = pair_force(X[i], X[j], V[i], V[j], R[i], R[j], p) # @line L2
F[i][0] += fx
F[i][1] += fy
F[j][0] -= fx # @line L3
F[j][1] -= fy
return F
def step(X, V, R, goals, v0, dt, p=PARAMS):
F = forces(X, V, R, goals, v0, p)
V = [(v[0] + dt * f[0] / p["m"], v[1] + dt * f[1] / p["m"]) for v, f in zip(V, F)] # @line L4
X = [(x[0] + dt * v[0], x[1] + dt * v[1]) for x, v in zip(X, V)] # @line L5
return X, V
def min_gap(X, R):
"""The smallest d_ij - r_i - r_j over all pairs (negative if bodies overlap)."""
best = math.inf
for i in range(len(X)):
for j in range(i + 1, len(X)):
best = min(best, math.dist(X[i], X[j]) - R[i] - R[j])
return best
def simulate(X, V, R, goals, v0, dt, steps, p=PARAMS):
gap = min_gap(X, R)
for _ in range(steps):
X, V = step(X, V, R, goals, v0, dt, p)
gap = min(gap, min_gap(X, R))
return X, V, gap
test_social_force_model.py 81 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/social-force-model/. The authors are listed in prov/4BAJB2.jsonl.
"""Tests of the social force model toy.
Pair forces are antisymmetric, so the interaction forces sum to zero. A
single person relaxes to the desired velocity by the factor (1 - dt / tau)
per step under symplectic Euler. Two groups that walk through each other
keep a minimum gap, which is a measurement of this toy and not a bound.
"""
import math
import random
import social_force_model as sf
def test_pair_forces_are_antisymmetric():
rng = random.Random(1)
for _ in range(300):
xi = (rng.uniform(-1, 1), rng.uniform(-1, 1))
xj = (rng.uniform(-1, 1), rng.uniform(-1, 1))
vi = (rng.uniform(-2, 2), rng.uniform(-2, 2))
vj = (rng.uniform(-2, 2), rng.uniform(-2, 2))
ri, rj = rng.uniform(0.25, 0.35), rng.uniform(0.25, 0.35)
a = sf.pair_force(xi, xj, vi, vj, ri, rj)
b = sf.pair_force(xj, xi, vj, vi, rj, ri)
s = max(1.0, abs(a[0]), abs(a[1]))
assert abs(a[0] + b[0]) < 1e-9 * s and abs(a[1] + b[1]) < 1e-9 * s
# the net interaction force of a group is zero
X = [(rng.uniform(0, 3), rng.uniform(0, 3)) for _ in range(12)]
V = [(rng.uniform(-1, 1), rng.uniform(-1, 1)) for _ in range(12)]
R = [0.3] * 12
F = sf.forces(X, V, R, X, [0.0] * 12)
D = [sf.driving_force(x, v, x, 0.0) for x, v in zip(X, V)]
scale = max(abs(c) for f in F for c in f)
assert abs(sum(f[0] - d[0] for f, d in zip(F, D))) < 1e-9 * scale
assert abs(sum(f[1] - d[1] for f, d in zip(F, D))) < 1e-9 * scale
def test_relaxation_to_desired_velocity():
dt, tau, v0 = 0.01, sf.PARAMS["tau"], 1.3
# along the goal direction: v_n = v0 + (v_0 - v0) (1 - dt / tau)^n
X, V = [(0.0, 0.0)], [(0.4, 0.0)]
goal = [(1000.0, 0.0)]
for n in range(1, 101):
X, V = sf.step(X, V, [0.3], goal, [v0], dt)
c = (1.0 - dt / tau) ** n
assert abs(V[0][0] - (v0 + (0.4 - v0) * c)) < 1e-12
assert V[0][1] == 0.0
# with v0 = 0 any velocity decays by the same factor
X, V = [(0.0, 0.0)], [(0.7, -0.4)]
for n in range(1, 101):
X, V = sf.step(X, V, [0.3], goal, [0.0], dt)
c = (1.0 - dt / tau) ** n
assert abs(V[0][0] - 0.7 * c) < 1e-12 and abs(V[0][1] + 0.4 * c) < 1e-12
def test_far_apart_repulsion_is_small():
f = sf.pair_force((0.0, 0.0), (5.0, 0.0), (0.0, 0.0), (0.0, 0.0), 0.3, 0.3)
assert abs(f[0]) < 2e3 * math.exp((0.6 - 5.0) / 0.08) * 1.0001 and f[0] < 0
g = sf.pair_force((0.0, 0.0), (0.5, 0.0), (0.0, 0.0), (0.0, 0.0), 0.3, 0.3)
assert g[0] < -sf.PARAMS["k"] * 0.1
def test_measured_gap_of_crossing_groups():
rng = random.Random(4)
X, goals, R = [], [], []
for k in range(5):
y = 0.8 * k + rng.uniform(-0.05, 0.05)
X.append((0.0, y))
goals.append((100.0, y))
X.append((8.0, y + 0.4))
goals.append((-92.0, y + 0.4))
R = [rng.uniform(0.25, 0.35) for _ in X]
V = [(0.0, 0.0)] * len(X)
v0 = [1.0] * len(X)
X, V, gap = sf.simulate(X, V, R, goals, v0, dt=0.01, steps=2000)
# measured on this seed: the groups pass each other in 20 s and the
# smallest gap between two bodies is about 0.068 m (no overlap)
assert 0.0 < gap < 0.2
assert all(x[0] > 8.0 for x in X[0::2]) and all(x[0] < 0.0 for x in X[1::2])
References
- Dirk Helbing and Péter Molnár, Social force model for pedestrian dynamics, Physical Review E 51(5), 4282-4286, 1995, the whole paper (the driving term, the repulsion from other people and from borders, the attraction terms, and the simulated lanes and oscillations at bottlenecks; general locator)
- Dirk Helbing, Illés Farkas and Tamás Vicsek, Simulating dynamical features of escape panic, Nature 407, 487-490, 2000, the equation of motion with the driving term, the pair force with repulsion, body force and sliding friction, the wall force, the herding rule, and the constants of the model
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag 4BAJB2 Edit this page History Report an error Markdown