Skip to content

Traffic

Traffic realizations shared by the engineering packages.

They are cars, the adaptation of a speed or a flow towards a desired one, and the optimal velocity model.

CarElement(label, x=0.0, *, values=None, size=None, space=None)

Bases: 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.

Parameters:

Name Type Description Default
label str

The label of the car or the group of cars.

required
x object

The initial position, one value per car or one for all.

0.0
values Mapping[str, object] | None

Further values by name, such as the starting speed "x'".

None
size int | None

The number of cars, or None for one.

None
space BaseSpace | None

The road, a continuous space of one dimension whose axis x makes the position of a car its place, or None for none.

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]
Source code in src/skcomplex/systems/engineering/common/traffic/_car.py
41
42
43
44
45
46
47
48
49
50
51
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

OptimalVelocityInteraction(label, *, target, a=1.0, V0=1.0, hc=2.0, road_length=40.0)

Bases: 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.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the group of cars, in the order they drive.

required
a Annotated[float, open(0, oo)]

The sensitivity, the inverse of the relaxation time.

1.0
V0 Annotated[float, open(0, oo)]

The scale of the optimal velocity.

1.0
hc Annotated[float, open(0, oo)]

The safe headway, where the optimal velocity turns.

2.0
road_length Annotated[float, open(0, oo)]

The length of the ring road.

40.0

Examples:

>>> from skcomplex.systems.engineering.common.traffic import OptimalVelocityInteraction
>>> OptimalVelocityInteraction('follow', target='cars').state
"x'"
Source code in src/skcomplex/systems/engineering/common/traffic/_optimal_velocity.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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

SpeedAdaptationInteraction(label, *, target, state='x', goal=0.0, time=1.0, noise=None)

Bases: Interaction

Adaptation of a speed or a flow towards a desired one over an adaptation time.

Traffic engineering names it the speed adaptation of a driver, as in the optimal velocity and Payne-Whitham models. The rate is (goal(s) - x) / time(s) of a value x of the target, with optional noise. With a constant goal and time and an additive noise of amplitude sigma, the value follows the Ornstein-Uhlenbeck process dx = (goal - x) / time dt + sigma dW, whose stationary variance is sigma**2 time / 2. The value is a state of the target, such as the flow Q of a road, or the speed "x'" of a car, the derivative of its position. A goal, a time, or a noise is a number or a function of the namespace of the interaction, whose target is s.target.

Parameters:

Name Type Description Default
label str

The label of the interaction, unique among the components of its system.

required
target Role

The path of the element or group whose value relaxes.

required
state str

The name of the value of the target that relaxes, with a prime for the derivative of a value.

'x'
goal Setting

A function of the namespace that returns the goal, or a number.

0.0
time Setting

A function of the namespace that returns the relaxation time, or a positive number.

1.0
noise Setting | None

A function of the namespace that returns the amplitude of the noise, a number, or None for no noise.

None

Raises:

Type Description
ValidationError

A goal, a time, or a noise is neither callable nor a number, raised by prepare.

Examples:

>>> from skcomplex.core.components.elements import Element
>>> from skcomplex.core.systems import System
>>> from skcomplex.systems.engineering.common.traffic import SpeedAdaptationInteraction
>>> relaxation = SpeedAdaptationInteraction('relaxation', target='stock', goal=10.0, time=0.5)
>>> system = System(elements=[Element('stock', values={'x': 0.0})], interactions=[relaxation]).prepare()
>>> round(float(system.simulate(until=5.0, dt=0.01).get_values('stock.x')[-1]), 3)
10.0
Source code in src/skcomplex/systems/engineering/common/traffic/_speed_adaptation.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def __init__(
    self,
    label: str,
    *,
    target: Role,
    state: str = 'x',
    goal: Setting = 0.0,
    time: Setting = 1.0,
    noise: Setting | None = None,
) -> None:
    super().__init__(label)
    self.target = target
    self.state = state
    self.goal = goal
    self.time = time
    self.noise = noise