Langevin car-following toward a density-dependent speed
Cars on a ring road whose speeds relax with noise towards a desired speed.
- Class:
LangevinTrafficSystem - Import:
from skcomplex.systems.engineering.traffic import LangevinTrafficSystem - Field:
engineering/traffic - Topics:
traffic,sde,langevin,car following - References:
- Bertin 2021
Use this system
The block imports the class and simulates the system with its default parameters and its tuned run settings.
from skcomplex.systems.engineering.traffic import LangevinTrafficSystem
trajectory = LangevinTrafficSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
n_cars |
50 |
length |
100.0 |
gamma |
3.0 |
u0 |
1.0 |
rho_max |
1.0 |
D1 |
0.05 |
solver |
'auto' |
backend |
'auto' |
Tags
The system with its default parameters has these capability tags, which solvers and analyses check.
| Tag | Values |
|---|---|
| Time | continuous |
| Space | continuous |
| Laws | noise, rate |
| Randomness | stochastic |
| Domains | real |
| Structure | fixed |
Class
The source of the class shows how it builds its components from its model parameters.
class LangevinTrafficSystem(BaseSystem):
"""Cars on a ring road whose speeds relax with noise towards a desired speed.
The local density sets the desired speed. Car `i` follows `dv_i = -gamma (v_i - u(rho_i)) dt + sqrt(2 D(rho_i)) dW`
with the desired speed `u(rho) = u0 (1 - rho / rho_max)` and the noise intensity `D(rho) = D1 rho`, where the local
density `rho_i` is the inverse of the headway to the car ahead. Cars start evenly spaced at the speed `u(rho)` of
the mean density `rho = n_cars / length`. The mean speed stays at `u(rho)`, so the mean flow follows the fundamental
diagram `rho u(rho)`, and the speeds fluctuate with the variance `D1 rho / gamma`. The uniform flow is linearly
stable while `u0 rho^2 / rho_max < gamma / 2`, as for the optimal velocity system. Strong noise lets cars pass each
other, and the headway of a car that has passed the car ahead wraps around the ring.
The system has the group `cars` of positions `cars.x` and speeds `cars.x'`, car `i + 1` ahead of car `i`.
Args:
n_cars:
The number of cars.
length:
The length of the ring road.
gamma:
The relaxation rate of the speed.
u0:
The desired speed on an empty road.
rho_max:
The jam density, at which the desired speed vanishes.
D1:
The noise intensity per unit density.
label:
The label of the system, which names it when it is nested in another one.
solver:
The solver of the problem, or `'auto'` for the one the system chooses.
backend:
The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.
Examples:
>>> from skcomplex.systems.engineering.traffic import LangevinTrafficSystem
>>> float(LangevinTrafficSystem(n_cars=10, length=20.0).prepare().system_.elements[0].values["x'"][0])
0.5
"""
field = 'engineering/traffic'
topics = ('traffic', 'sde', 'langevin', 'car following')
references = ('Bertin 2021',)
until = 200.0
dt = 1.0
def __init__(
self,
n_cars: int = 50,
length: float = 100.0,
*,
gamma: float = 3.0,
u0: float = 1.0,
rho_max: float = 1.0,
D1: float = 0.05,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.n_cars = n_cars
self.length = length
self.gamma = gamma
self.u0 = u0
self.rho_max = rho_max
self.D1 = D1
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set `system_` to the components of the system built from its model parameters."""
super()._prepare(context)
density = self.n_cars / self.length
positions = np.arange(self.n_cars) * self.length / self.n_cars
cars = CarElement(
'cars',
positions,
values={"x'": np.full(self.n_cars, self.u0 * (1 - density / self.rho_max))},
size=self.n_cars,
space=ContinuousSpace(extent=(self.length,)),
)
driving = LangevinDrivingInteraction(
'drive',
target='cars',
gamma=self.gamma,
u0=self.u0,
rho_max=self.rho_max,
D1=self.D1,
road_length=self.length,
)
self.system_ = System(self.label, elements=[cars], interactions=[driving], solver=EulerMaruyamaSolver(dt=0.01))
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
CarElement, fromElement
Interactions:
LangevinDrivingInteraction, fromInteraction
class CarElement(Element):
"""Car on a road at the position `x`, whose speed is the derivative `x'`.
The interactions that drive the car give the acceleration `x''`, and the speed `x'` starts at zero unless `values`
gives it.
Args:
label:
The label of the car or the group of cars.
x:
The initial position, one value per car or one for all.
values:
Further values by name, such as the starting speed `"x'"`.
size:
The number of cars, or `None` for one.
space:
The road, a continuous space of one dimension whose axis `x` makes the position of a car its place, or
`None` for none.
Examples:
>>> from skcomplex.systems.engineering.common.traffic import CarElement
>>> CarElement('cars', x=[0.0, 10.0], values={"x'": 1.0}, size=2).x
[0.0, 10.0]
"""
def __init__(
self,
label: str,
x: object = 0.0,
*,
values: Mapping[str, object] | None = None,
size: int | None = None,
space: BaseSpace | None = None,
) -> None:
super().__init__(label, values=values, size=size, space=space)
self.x = x
class LangevinDrivingInteraction(SpeedAdaptationInteraction):
"""Relaxation of the speed of every car of a ring road, with noise.
The speed relaxes towards the speed of the local density. The speed `v = x'` follows
`dv = -gamma (v - u(rho)) dt + sqrt(2 D1 rho) dW`, with the desired speed `u(rho) = u0 (1 - rho / rho_max)` of the
density `rho`, the inverse of the headway to the next car, an Ornstein-Uhlenbeck relaxation over the time
`1 / gamma` with a noise that grows with the density.
Args:
label:
The label of the interaction.
target:
The path of the group of cars, in the order they drive.
gamma:
The relaxation rate.
u0:
The desired speed on an empty road.
rho_max:
The jam density.
D1:
The strength of the noise.
road_length:
The length of the ring road.
Examples:
>>> from skcomplex.systems.engineering.traffic import LangevinDrivingInteraction
>>> LangevinDrivingInteraction('drive', target='cars', gamma=2.0).state
"x'"
"""
def __init__(
self,
label: str,
*,
target: Role,
gamma: Annotated[float, RealSet.open(0, oo)] = 3.0,
u0: Annotated[float, RealSet.open(0, oo)] = 1.0,
rho_max: Annotated[float, RealSet.open(0, oo)] = 1.0,
D1: Annotated[float, RealSet.closed_open(0, oo)] = 0.05,
road_length: Annotated[float, RealSet.open(0, oo)] = 100.0,
) -> None:
super().__init__(label, target=target, state="x'")
self.gamma = gamma
self.u0 = u0
self.rho_max = rho_max
self.D1 = D1
self.road_length = road_length
def _get_goal(self, namespace: SimpleNamespace) -> object:
"""Return the desired speed of the local density of every car."""
return LangevinDrivingInteraction._get_target_speed(
LangevinDrivingInteraction._get_local_density(namespace.target.x, namespace.road_length),
namespace,
)
def _get_time(self, namespace: SimpleNamespace) -> object:
"""Return the relaxation time `1 / gamma`."""
return 1 / namespace.gamma
def _get_noise(self, namespace: SimpleNamespace) -> object:
"""Return the amplitude `sqrt(2 D1 rho)` of the noise of the local density."""
return sqrt(
2 * namespace.D1 * LangevinDrivingInteraction._get_local_density(namespace.target.x, namespace.road_length),
)
@staticmethod
def _get_target_speed(density: Expression, s: SimpleNamespace) -> Expression:
"""Return the desired speed `u(rho) = u0 (1 - rho / rho_max)` at a density."""
return s.u0 * (1 - density / s.rho_max)
@staticmethod
def _get_local_density(x: Expression, road_length: Expression) -> Expression:
"""Return the density seen by every car of a ring road, the inverse of its headway."""
return 1 / OptimalVelocityInteraction._get_headway(x, road_length)