Optimal velocity
The optimal velocity system of cars on a ring road.
- Class:
OptimalVelocitySystem - Import:
from skcomplex.systems.engineering.traffic import OptimalVelocitySystem - Field:
engineering/traffic - Topics:
traffic,jams,ode,car following - References:
- Bando et al. 1995
- Chowdhury, Santen, and Schadschneider 2000
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 OptimalVelocitySystem
trajectory = OptimalVelocitySystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
n_cars |
20 |
length |
40.0 |
a |
1.0 |
V0 |
1.0 |
hc |
2.0 |
perturbation |
0.1 |
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 | rate |
| Randomness | deterministic |
| Domains | real |
| Structure | fixed |
Class
The source of the class shows how it builds its components from its model parameters.
class OptimalVelocitySystem(BaseSystem):
"""The optimal velocity system of cars on a ring road.
Uniform flow breaks into stop-and-go waves. Car `i` relaxes its speed towards the optimal velocity of its headway
`h`, `x_i'' = a (V(h) - x_i')` with `V(h) = V0 (tanh(h - hc) + tanh(hc))`. Uniform flow with headway
`b = length / n_cars` is unstable when `V'(b) > a / 2`, and then a jam forms that moves against the direction of
travel. The cars are one group, and the headway of car `i` is the distance along the ring to member `i + 1`. 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.
a:
The sensitivity of drivers.
V0:
The scale of the optimal velocity, which tends to `V0 (1 + tanh(hc))` on an empty road.
hc:
The headway at which the optimal velocity changes fastest.
perturbation:
The forward shift of the first car from uniform spacing.
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 OptimalVelocitySystem
>>> system = OptimalVelocitySystem(n_cars=5).prepare().system_
>>> system.elements[0].size, len(system.interactions)
(5, 1)
"""
field = 'engineering/traffic'
topics = ('traffic', 'jams', 'ode', 'car following')
references = ('Bando et al. 1995', 'Chowdhury, Santen, and Schadschneider 2000')
until = 200.0
dt = 1.0
@staticmethod
def _get_ring_positions(n_cars: int, length: float, perturbation: float) -> list[float]:
"""Return evenly spaced positions on a ring.
The first car is moved forward by a perturbation.
"""
positions = np.arange(n_cars) * length / n_cars
positions[0] += perturbation
return [float(position) for position in positions]
def __init__(
self,
n_cars: int = 20,
length: float = 40.0,
*,
a: float = 1.0,
V0: float = 1.0,
hc: float = 2.0,
perturbation: float = 0.1,
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.a = a
self.V0 = V0
self.hc = hc
self.perturbation = perturbation
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)
headway = self.length / self.n_cars
speed = self.V0 * (np.tanh(headway - self.hc) + np.tanh(self.hc))
cars = CarElement(
'cars',
OptimalVelocitySystem._get_ring_positions(self.n_cars, self.length, self.perturbation),
values={"x'": float(speed)},
size=self.n_cars,
space=ContinuousSpace(extent=(self.length,)),
)
following = OptimalVelocityInteraction(
'follow',
target='cars',
a=self.a,
V0=self.V0,
hc=self.hc,
road_length=self.length,
)
self.system_ = System(self.label, elements=[cars], interactions=[following])
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
CarElement, fromElement
Interactions:
OptimalVelocityInteraction, 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 OptimalVelocityInteraction(SpeedAdaptationInteraction):
"""Relaxation of the speed `x'` of every car of a ring road.
The speed follows `x'' = a (V(h) - x')` with the optimal velocity `V(h) = V0 (tanh(h - hc) + tanh(hc))` of the
headway `h` to the next car, a relaxation over the time `1 / a`.
Args:
label:
The label of the interaction.
target:
The path of the group of cars, in the order they drive.
a:
The sensitivity, the inverse of the relaxation time.
V0:
The scale of the optimal velocity.
hc:
The safe headway, where the optimal velocity turns.
road_length:
The length of the ring road.
Examples:
>>> from skcomplex.systems.engineering.common.traffic import OptimalVelocityInteraction
>>> OptimalVelocityInteraction('follow', target='cars').state
"x'"
"""
def __init__(
self,
label: str,
*,
target: Role,
a: Annotated[float, RealSet.open(0, oo)] = 1.0,
V0: Annotated[float, RealSet.open(0, oo)] = 1.0,
hc: Annotated[float, RealSet.open(0, oo)] = 2.0,
road_length: Annotated[float, RealSet.open(0, oo)] = 40.0,
) -> None:
super().__init__(label, target=target, state="x'")
self.a = a
self.V0 = V0
self.hc = hc
self.road_length = road_length
def _get_goal(self, namespace: SimpleNamespace) -> object:
"""Return the optimal velocity of the headway of every car."""
headway = OptimalVelocityInteraction._get_headway(namespace.target.x, namespace.road_length)
return namespace.V0 * (tanh(headway - namespace.hc) + tanh(namespace.hc))
def _get_time(self, namespace: SimpleNamespace) -> object:
"""Return the relaxation time `1 / a`."""
return 1 / namespace.a
@staticmethod
def _get_headway(x: Expression, road_length: Expression) -> Expression:
"""Return the distance from every car of a ring road to the car ahead.
The car ahead is the next member of the group.
"""
return mod(shift_member(x, 1) - x, road_length)