Car following with delay
A chain of cars that react to the speed difference to the car ahead.
- Class:
DelayedCarFollowingSystem - Import:
from skcomplex.systems.engineering.traffic import DelayedCarFollowingSystem - Field:
engineering/traffic - Topics:
traffic,delay,car following,instability - References:
- Gros 2015
- Chandler, Herman, and Montroll 1958
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 DelayedCarFollowingSystem
trajectory = DelayedCarFollowingSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
n_cars |
8 |
spacing |
10.0 |
u |
1.0 |
amplitude |
0.1 |
omega |
0.5 |
reaction |
1.0 |
T |
1.0 |
power |
0.0 |
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 | none |
| Laws | rate |
| Randomness | deterministic |
| Domains | real |
| Structure | fixed |
Class
The source of the class shows how it builds its components from its model parameters.
class DelayedCarFollowingSystem(BaseSystem):
"""A chain of cars that react to the speed difference to the car ahead.
They react after a reaction time. Car `j + 1` follows car `j` as `x''_{j+1}(t) = λ Δv(t - T) / Δx(t - T)^power`,
where `Δv` and `Δx` are the speed and position differences to the car ahead. The lead car drives at
`u (1 - amplitude sin(ω t))`. Oscillations grow along the chain when `ω < 2 λ sin(ω T)` for the linear system,
`power = 0`. The system has the lead car `car_0` and the cars `car_<j>` behind it, with positions `x` and speeds
`x'`.
Args:
n_cars:
The number of cars, the lead car included.
spacing:
The initial distance between neighbouring cars.
u:
The mean speed of the lead car.
amplitude:
The relative amplitude of the speed oscillation of the lead car.
omega:
The angular frequency of the speed oscillation of the lead car.
reaction:
The reaction constant `λ`.
T:
The reaction time.
power:
The power of the distance in the reaction, 0 for the linear system.
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 DelayedCarFollowingSystem
>>> DelayedCarFollowingSystem(n_cars=3).prepare().system_.elements[2].x
-20.0
"""
field = 'engineering/traffic'
topics = ('traffic', 'delay', 'car following', 'instability')
references = ('Gros 2015', 'Chandler, Herman, and Montroll 1958')
until = 60.0
dt = 0.5
def __init__(
self,
n_cars: int = 8,
*,
spacing: float = 10.0,
u: float = 1.0,
amplitude: float = 0.1,
omega: float = 0.5,
reaction: float = 1.0,
T: float = 1.0,
power: float = 0.0,
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.spacing = spacing
self.u = u
self.amplitude = amplitude
self.omega = omega
self.reaction = reaction
self.T = T
self.power = power
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)
cars = [
CarElement(f'car_{index}', -index * self.spacing, values={"x'": self.u}) for index in range(self.n_cars)
]
lead = LeadCarInteraction('lead', target='car_0', u=self.u, amplitude=self.amplitude, omega=self.omega)
followers = [
DelayedFollowingInteraction(
f'follow_{index}',
target=f'car_{index}',
leader=f'car_{index - 1}',
reaction=self.reaction,
T=self.T,
power=self.power,
)
for index in range(1, self.n_cars)
]
self.system_ = System(self.label, elements=cars, interactions=[lead, *followers])
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
CarElement, fromElement
Interactions:
LeadCarInteraction, fromInteractionDelayedFollowingInteraction, 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 LeadCarInteraction(Interaction):
"""Swing of the speed of a lead car about a cruising speed `u`.
The speed `x'` follows `u (1 - amplitude sin(omega t))`, so the acceleration is
`x'' = -u amplitude omega cos(omega t)`.
Args:
label:
The label of the interaction.
target:
The path of the lead car.
u:
The cruising speed.
amplitude:
The relative amplitude of the swing.
omega:
The angular frequency of the swing.
Examples:
>>> from skcomplex.systems.engineering.traffic import LeadCarInteraction
>>> LeadCarInteraction('lead', target='car_0').omega
0.5
"""
law_classes = (RateLaw,)
def __init__(
self,
label: str,
*,
target: Role,
u: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
amplitude: Annotated[float, RealSet.closed_open(0, oo)] = 0.1,
omega: Annotated[float, RealSet.closed_open(0, oo)] = 0.5,
) -> None:
super().__init__(label)
self.target = target
self.u = u
self.amplitude = amplitude
self.omega = omega
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the forced swing of the speed of the lead car.
Args:
context:
The namespace of the target, the time `t`, and the parameters.
"""
super()._prepare(context)
s, t = context, context.t
self.laws_: list[BaseLaw] = [
RateLaw(s.target.x.diff(t, 2), -s.u * s.amplitude * s.omega * cos(s.omega * t)),
]
class DelayedFollowingInteraction(Interaction):
"""Acceleration of a follower car by the speed difference to its leader.
Both cars are seen a reaction time late. The speed of the follower follows the Gazis-Herman-Rothery law
`x_f''(t) = λ (x_l' - x_f')(t - T) / (x_l - x_f)(t - T)^m` of the sensitivity `λ`, the reaction time `T`, and the
power `m` of the gap.
Args:
label:
The label of the interaction.
target:
The path of the follower car.
leader:
The path of the car ahead of the follower.
reaction:
The sensitivity `λ`.
T:
The reaction time.
power:
The power `m` of the gap.
Examples:
>>> from skcomplex.systems.engineering.traffic import DelayedFollowingInteraction
>>> DelayedFollowingInteraction('follow', target='car_1', leader='car_0').T
1.0
"""
law_classes = (RateLaw,)
def __init__(
self,
label: str,
*,
target: Role,
leader: Role,
reaction: Annotated[float, RealSet.open(0, oo)] = 1.0,
T: Annotated[float, RealSet.open(0, oo)] = 1.0,
power: Annotated[float, RealSet.closed_open(0, oo)] = 0.0,
) -> None:
super().__init__(label)
self.target = target
self.leader = leader
self.reaction = reaction
self.T = T
self.power = power
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the delayed response of the follower to its leader.
Args:
context:
The namespace of the cars, the time `t`, and the parameters.
"""
super()._prepare(context)
s, t = context, context.t
leader, follower = s.leader.x, s.target.x
gap = (leader - follower)(t=t - s.T)
approach = (leader.diff(t) - follower.diff(t))(t=t - s.T)
self.laws_: list[BaseLaw] = [RateLaw(follower.diff(t, 2), s.reaction * approach / gap**s.power)]
Equations
The equations are those that symbolic returns for the system with its default parameters.
car_0.x: diff(x_car_0(t), t, t) == -amplitude_lead*omega_lead*u_lead*cos(omega_lead*t)
car_1.x: diff(x_car_1(t), t, t) == reaction_follow_1*(D[0](x_car_0)(-T_follow_1 + t) - D[0](x_car_1)(-T_follow_1 + t))/(x_car_0(-T_follow_1 + t) - x_car_1(-T_follow_1 + t))^power_follow_1
car_2.x: diff(x_car_2(t), t, t) == reaction_follow_2*(D[0](x_car_1)(-T_follow_2 + t) - D[0](x_car_2)(-T_follow_2 + t))/(x_car_1(-T_follow_2 + t) - x_car_2(-T_follow_2 + t))^power_follow_2
car_3.x: diff(x_car_3(t), t, t) == reaction_follow_3*(D[0](x_car_2)(-T_follow_3 + t) - D[0](x_car_3)(-T_follow_3 + t))/(x_car_2(-T_follow_3 + t) - x_car_3(-T_follow_3 + t))^power_follow_3
car_4.x: diff(x_car_4(t), t, t) == reaction_follow_4*(D[0](x_car_3)(-T_follow_4 + t) - D[0](x_car_4)(-T_follow_4 + t))/(x_car_3(-T_follow_4 + t) - x_car_4(-T_follow_4 + t))^power_follow_4
car_5.x: diff(x_car_5(t), t, t) == reaction_follow_5*(D[0](x_car_4)(-T_follow_5 + t) - D[0](x_car_5)(-T_follow_5 + t))/(x_car_4(-T_follow_5 + t) - x_car_5(-T_follow_5 + t))^power_follow_5
car_6.x: diff(x_car_6(t), t, t) == reaction_follow_6*(D[0](x_car_5)(-T_follow_6 + t) - D[0](x_car_6)(-T_follow_6 + t))/(x_car_5(-T_follow_6 + t) - x_car_6(-T_follow_6 + t))^power_follow_6
car_7.x: diff(x_car_7(t), t, t) == reaction_follow_7*(D[0](x_car_6)(-T_follow_7 + t) - D[0](x_car_7)(-T_follow_7 + t))/(x_car_6(-T_follow_7 + t) - x_car_7(-T_follow_7 + t))^power_follow_7