Skip to content

Intelligent driver

The intelligent driver system of cars on a ring road.

  • Class: IntelligentDriverSystem
  • Import: from skcomplex.systems.engineering.traffic import IntelligentDriverSystem
  • Field: engineering/traffic
  • Topics: traffic, jams, ode, car following
  • References:
    • Treiber, Hennecke, and Helbing 2000
    • 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 IntelligentDriverSystem

trajectory = IntelligentDriverSystem().prepare().simulate()

Parameters

The model parameters take these defaults, and set_params changes them.

Parameter Default
n_cars 20
length 500.0
v0 30.0
T 1.5
a 1.0
b 1.5
s0 2.0
delta 4.0
car_length 5.0
perturbation 1.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 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 IntelligentDriverSystem(BaseSystem):
    """The intelligent driver system of cars on a ring road.

    Car `i` of speed `v = x'` accelerates as `v' = a (1 - (v / v0)^δ - (s* / s)^2)` with the gap `s` to the car ahead
    and the desired gap `s* = s0 + v T + v Δv / (2 sqrt(a b))`, where `Δv` is the speed difference to the car ahead.
    Cars start at rest. The cars are one group, and the car ahead of car `i` is member `i + 1` along 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.

        v0:
            The desired speed.

        T:
            The safe time headway.

        a:
            The largest acceleration.

        b:
            The comfortable deceleration.

        s0:
            The jam distance.

        delta:
            The acceleration exponent.

        car_length:
            The length of a car, subtracted from the headway to give the gap.

        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 IntelligentDriverSystem
        >>> IntelligentDriverSystem(n_cars=4).prepare().system_.interactions[0].road_length
        500.0
    """

    field = 'engineering/traffic'
    topics = ('traffic', 'jams', 'ode', 'car following')
    references = ('Treiber, Hennecke, and Helbing 2000', 'Chowdhury, Santen, and Schadschneider 2000')
    until = 100.0
    dt = 1.0

    def __init__(
        self,
        n_cars: int = 20,
        length: float = 500.0,
        *,
        v0: float = 30.0,
        T: float = 1.5,
        a: float = 1.0,
        b: float = 1.5,
        s0: float = 2.0,
        delta: float = 4.0,
        car_length: float = 5.0,
        perturbation: float = 1.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.length = length
        self.v0 = v0
        self.T = T
        self.a = a
        self.b = b
        self.s0 = s0
        self.delta = delta
        self.car_length = car_length
        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)
        cars = CarElement(
            'cars',
            OptimalVelocitySystem._get_ring_positions(self.n_cars, self.length, self.perturbation),
            size=self.n_cars,
            space=ContinuousSpace(extent=(self.length,)),
        )
        following = IntelligentDriverInteraction(
            'follow',
            target='cars',
            v0=self.v0,
            T=self.T,
            a=self.a,
            b=self.b,
            s0=self.s0,
            delta=self.delta,
            length=self.car_length,
            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:

Interactions:

  • IntelligentDriverInteraction, from Interaction
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 IntelligentDriverInteraction(Interaction):
    """Acceleration of every car of a ring road by the intelligent driver system.

    The acceleration depends on its speed and the car ahead. The speed `v = x'` follows
    `v' = a (1 - (v / v0)^δ - (s*(v, Δv) / (h - l))^2)`, with the desired gap `s* = s0 + v T + v Δv / (2 sqrt(a b))`,
    the approach rate `Δv` to the next car, and the headway `h` less the car length `l`.

    Args:
        label:
            The label of the interaction.

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

        v0:
            The desired speed.

        T:
            The safe time headway.

        a:
            The largest acceleration.

        b:
            The comfortable deceleration.

        s0:
            The jam distance.

        delta:
            The acceleration exponent.

        length:
            The length of a car.

        road_length:
            The length of the ring road.

    Examples:
        >>> from skcomplex.systems.engineering.traffic import IntelligentDriverInteraction
        >>> IntelligentDriverInteraction('follow', target='cars').v0
        30.0
    """

    law_classes = (RateLaw,)

    def __init__(
        self,
        label: str,
        *,
        target: Role,
        v0: Annotated[float, RealSet.open(0, oo)] = 30.0,
        T: Annotated[float, RealSet.closed_open(0, oo)] = 1.5,
        a: Annotated[float, RealSet.open(0, oo)] = 1.0,
        b: Annotated[float, RealSet.open(0, oo)] = 1.5,
        s0: Annotated[float, RealSet.closed_open(0, oo)] = 2.0,
        delta: Annotated[float, RealSet.open(0, oo)] = 4.0,
        length: Annotated[float, RealSet.closed_open(0, oo)] = 5.0,
        road_length: Annotated[float, RealSet.open(0, oo)] = 500.0,
    ) -> None:
        super().__init__(label)
        self.target = target
        self.v0 = v0
        self.T = T
        self.a = a
        self.b = b
        self.s0 = s0
        self.delta = delta
        self.length = length
        self.road_length = road_length

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the acceleration of the intelligent driver system.

        Args:
            context:
                The namespace of the cars, the time `t`, and the parameters.
        """
        super()._prepare(context)
        s, t = context, context.t
        x = s.target.x
        v = x.diff(t)
        gap = s.s0 + v * s.T + v * (v - shift_member(v, 1)) / (2 * sqrt(s.a * s.b))
        headway = OptimalVelocityInteraction._get_headway(x, s.road_length)
        self.laws_: list[BaseLaw] = [
            RateLaw(x.diff(t, 2), s.a * (1 - (v / s.v0) ** s.delta - (gap / (headway - s.length)) ** 2)),
        ]