Algorithm Search⌘K

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 nn people. Person ii has a position xi\mathbf x_i (in m), a velocity vi\mathbf v_i (in m/s), a mass mim_i, a radius rir_i, a desired speed vi0v_i^0 and a goal; ei\mathbf e_i is the unit vector from xi\mathbf x_i toward the goal. The equation of motion of Helbing, Farkas and Vicsek is

midvidt=mivi0ei−viτi+∑j≠ifij+∑WfiW,dxidt=vi.m_i \frac{d\mathbf v_i}{dt} = m_i \frac{v_i^0 \mathbf e_i - \mathbf v_i}{\tau_i} + \sum_{j \ne i} \mathbf f_{ij} + \sum_W \mathbf f_{iW}, \qquad \frac{d\mathbf x_i}{dt} = \mathbf v_i .

The first term is the driving force: without other forces, the velocity relaxes to vi0eiv_i^0 \mathbf e_i with the time constant τi\tau_i. For two people with dij=∣xi−xj∣d_{ij} = |\mathbf x_i - \mathbf x_j|, the unit normal nij=(xi−xj)/dij\mathbf n_{ij} = (\mathbf x_i - \mathbf x_j) / d_{ij} from jj to ii, the tangent tij=(−nijy,nijx)\mathbf t_{ij} = (-n_{ij}^y, n_{ij}^x), the sum of radii rij=ri+rjr_{ij} = r_i + r_j and g(z)=max⁡(z,0)g(z) = \max(z, 0), the pair force is

fij=(A e(rij−dij)/B+k g(rij−dij))nij+κ g(rij−dij) ((vj−vi)⋅tij) tij.\mathbf f_{ij} = \Big(A\, e^{(r_{ij} - d_{ij}) / B} + k\, g(r_{ij} - d_{ij})\Big) \mathbf n_{ij} + \kappa\, g(r_{ij} - d_{ij})\, \big((\mathbf v_j - \mathbf v_i) \cdot \mathbf t_{ij}\big)\, \mathbf t_{ij} .

The exponential term is the social repulsion. The term with kk is a body force that resists compression, and the term with κ\kappa is a sliding friction that resists the relative tangential motion; both act only when the bodies overlap, dij<rijd_{ij} < r_{ij}. A wall acts in the same way, with diWd_{iW} the distance to the wall, niW\mathbf n_{iW} the normal from the wall to the person, and the friction term −κ g(ri−diW) (vi⋅tiW) tiW-\kappa\, g(r_i - d_{iW})\, (\mathbf v_i \cdot \mathbf t_{iW})\, \mathbf t_{iW}.

The constants of the 2000 paper are A=2⋅103A = 2 \cdot 10^3 N, B=0.08B = 0.08 m, k=1.2⋅105k = 1.2 \cdot 10^5 kg/s2^2, κ=2.4⋅105\kappa = 2.4 \cdot 10^5 kg/(m s), τ=0.5\tau = 0.5 s, m=80m = 80 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 AA and BB 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 (1−p) ei+p ⟨ej⟩i(1 - p)\, \mathbf e_i + p\, \langle \mathbf e_j \rangle_i of the own direction and the mean direction of the neighbors, with a panic parameter p∈[0,1]p \in [0, 1]. 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 Δt\Delta t:

  1. SOCIAL-FORCE-STEP(X, V, R, goals, v⁰, Δt)
  2. 1Fᵢ ← m (v⁰ᵢ eᵢ - vᵢ) / τ for every person i ▷ the driving force
  3. for every pair i < j
  4. 2f ← f_ij(xᵢ, xⱼ, vᵢ, vⱼ, rᵢ, rⱼ) ▷ repulsion, body force, friction
  5. 3Fᵢ ← Fᵢ + f; Fⱼ ← Fⱼ - f ▷ the reaction on j
  6. for every person i
  7. 4vᵢ ← vᵢ + Δt Fᵢ / m
  8. 5xᵢ ← xᵢ + Δt vᵢ
  9. 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 AA and BB 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 Θ(n2)\Theta(n^2) time for all pairs and Θ(n)\Theta(n) space. The repulsion decays by the factor ee every B=0.08B = 0.08 m, so implementations cut it off at a distance dcd_c and find the near pairs with a uniform grid or spatial hashing, which costs O(n)O(n) 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

  1. Antisymmetric pair forces (proved). Exchanging ii and jj changes the sign of nij\mathbf n_{ij}, tij\mathbf t_{ij} and vj−vi\mathbf v_j - \mathbf v_i, so fji=−fij\mathbf f_{ji} = -\mathbf f_{ij}. 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 10−910^{-9}, and the zero sum for 12 people.
  2. Relaxation (proved, with its step limit). A single person with no other forces has, under symplectic Euler, vn=v0e+(v0−v0e)(1−Δt/τ)n\mathbf v_n = v^0 \mathbf e + (\mathbf v_0 - v^0 \mathbf e)(1 - \Delta t / \tau)^n. The exact solution has e−t/τe^{-t/\tau} in place of the power. The step converges for 0<Δt<2τ0 < \Delta t < 2 \tau and does not overshoot for Δt≤τ\Delta t \le \tau. The test checks the formula to 10−1210^{-12} for 100 steps at Δt=0.01\Delta t = 0.01 s.
  3. Step limit of the body force (proved for this term alone). Two overlapping people, with only the body force, are a linear spring kk on the reduced mass m/2m/2, with ω=2k/m=54.8\omega = \sqrt{2k/m} = 54.8 rad/s. Symplectic Euler is stable for this spring when Δt<2/ω=0.037\Delta t < 2/\omega = 0.037 s. The friction and the stiffness A/BA/B of the repulsion add to this, so the code uses Δt=0.01\Delta t = 0.01 s.
  4. Standing distance (proved). A person at rest who pushes toward a fixed person feels the driving force mv0/τm v^0 / \tau and the repulsion. The two balance at d∗=rij+Bln⁡(Aτ/(mv0))d^* = r_{ij} + B \ln(A \tau / (m v^0)) if mv0/τ<Am v^0 / \tau < A. For v0=1v^0 = 1 m/s and rij=0.6r_{ij} = 0.6 m, the driving force is 160 N and d∗=0.6+0.08ln⁡12.5=0.802d^* = 0.6 + 0.08 \ln 12.5 = 0.802 m. So with these constants one person alone does not touch another, and contact forces come only when several people push in a row.
  5. Two crossing groups (measured). In the test, two groups of 5 people walk through each other in opposite directions with v0=1v^0 = 1 m/s, Δt=0.01\Delta t = 0.01 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.
  6. 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.
  7. 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.

social_force_model.pyI0RawGitHub
# 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

  1. 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)
  2. 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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/4BAJB2/text.yaml