Skip to content

Traffic

Traffic: cars on roads.

ArrivalInteraction(label, *, target, demand=1.0, width=30, height=30, phase='arrival')

Bases: BirthDeathInteraction

Arrival of new riders at random cells with random destinations.

It is a life cycle of arrivals only. Every step a Poisson number of riders, with mean demand, appear at random cells with random destinations.

Parameters:

Name Type Description Default
label str

The label of the arrivals.

required
target Role

The path of the group of riders.

required
demand Annotated[float, closed_open(0, oo)]

The mean number of new riders in a step.

1.0
width int

The number of columns of the grid.

30
height int

The number of rows of the grid.

30
phase str | None

The phase the riders arrive in.

'arrival'

Examples:

>>> from skcomplex.systems.engineering.traffic import ArrivalInteraction
>>> ArrivalInteraction('arrival', target='riders').reports
('births', 'deaths', 'arrivals')
Source code in src/skcomplex/systems/engineering/traffic/_ridesharing.py
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
def __init__(
    self,
    label: str,
    *,
    target: Role,
    demand: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
    width: int = 30,
    height: int = 30,
    phase: str | None = 'arrival',
) -> None:
    super().__init__(
        label,
        target=target,
        placement=self.place,
        values=self.destine,
        arrival=self.expect,
        phase=phase,
    )
    self.demand = demand
    self.width = width
    self.height = height

destine(agent, system)

Return a random destination on the grid for a new rider.

Parameters:

Name Type Description Default
agent RiderView | None

The parent, None for an arrival.

required
system SystemView

The system view of the arrivals.

required

Returns:

Name Type Description
values dict[str, object]

The coordinates of the destination by state name.

Source code in src/skcomplex/systems/engineering/traffic/_ridesharing.py
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
def destine(self, agent: RiderView | None, system: SystemView) -> dict[str, object]:
    """Return a random destination on the grid for a new rider.

    Args:
        agent:
            The parent, `None` for an arrival.

        system:
            The system view of the arrivals.

    Returns:
        values:
            The coordinates of the destination by state name.
    """
    to_x, to_y = system.rng.integers((self.width, self.height))
    return {'destination_x': float(to_x), 'destination_y': float(to_y)}

expect(system)

Return the mean number of new riders in a step.

Parameters:

Name Type Description Default
system SystemView

The system view of the arrivals.

required

Returns:

Name Type Description
demand float

The demand.

Source code in src/skcomplex/systems/engineering/traffic/_ridesharing.py
372
373
374
375
376
377
378
379
380
381
382
383
def expect(self, system: SystemView) -> float:
    """Return the mean number of new riders in a step.

    Args:
        system:
            The system view of the arrivals.

    Returns:
        demand:
            The demand.
    """
    return system.get_parameter('demand')

place(agent, system)

Return a random cell of the grid for a new rider.

Parameters:

Name Type Description Default
agent RiderView | None

The parent, None for an arrival.

required
system SystemView

The system view of the arrivals.

required

Returns:

Name Type Description
cell tuple[int, int]

The coordinates of the cell.

Source code in src/skcomplex/systems/engineering/traffic/_ridesharing.py
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
def place(self, agent: RiderView | None, system: SystemView) -> tuple[int, int]:
    """Return a random cell of the grid for a new rider.

    Args:
        agent:
            The parent, `None` for an arrival.

        system:
            The system view of the arrivals.

    Returns:
        cell:
            The coordinates of the cell.
    """
    x, y = system.rng.integers((self.width, self.height))
    return int(x), int(y)

CabDrivingInteraction(label, *, target, riders, base_fare=2.0, block_fare=0.5, phase='drive')

Bases: Interaction

Driving rule of the cabs of the ridesharing system.

A cab drives one block a step to its rider, collects the fare, and drives the rider to its destination. The fare is a base fare plus a fare per block of the trip, paid as a transfer from rider to cab.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the group of cabs.

required
riders Role

The path of the group of riders.

required
base_fare Annotated[float, closed_open(0, oo)]

The fare of every trip.

2.0
block_fare Annotated[float, closed_open(0, oo)]

The fare per block of a trip.

0.5
phase str | None

The phase the cabs drive in.

'drive'

Examples:

>>> from skcomplex.systems.engineering.traffic import CabDrivingInteraction
>>> CabDrivingInteraction('drive', target='cabs', riders='riders').block_fare
0.5
Source code in src/skcomplex/systems/engineering/traffic/_ridesharing.py
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
def __init__(
    self,
    label: str,
    *,
    target: Role,
    riders: Role,
    base_fare: Annotated[float, RealSet.closed_open(0, oo)] = 2.0,
    block_fare: Annotated[float, RealSet.closed_open(0, oo)] = 0.5,
    phase: str | None = 'drive',
) -> None:
    super().__init__(label)
    self.target = target
    self.riders = riders
    self.base_fare = base_fare
    self.block_fare = block_fare
    self.phase = phase

drive(agent, system)

Drive one block toward the target, and pick up or drop off the rider there.

Parameters:

Name Type Description Default
agent CabView

The cab.

required
system SystemView

The system.

required
Source code in src/skcomplex/systems/engineering/traffic/_ridesharing.py
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
def drive(self, agent: CabView, system: SystemView) -> None:
    """Drive one block toward the target, and pick up or drop off the rider there.

    Args:
        agent:
            The cab.

        system:
            The system.
    """
    if agent.task == IDLE:
        return
    x, y = CabDrivingInteraction._get_cell(agent.location)
    target = (int(agent.target_x), int(agent.target_y))
    if (x, y) != target:
        step = (int(np.sign(target[0] - x)), 0) if x != target[0] else (0, int(np.sign(target[1] - y)))
        agent.move_to((x + step[0], y + step[1]))
    if CabDrivingInteraction._get_cell(agent.location) != target:
        return
    if agent.task == PICKING_UP:
        self._pick_up(agent, system)
    else:
        agent.task = IDLE
        agent.seats = 1.0

CabElement(label, *, task=IDLE, target_x=0.0, target_y=0.0, destination_x=0.0, destination_y=0.0, rider=-1.0, seats=1.0, money=0.0, values=None, size=None, space=None)

Bases: Element

Cab that drives a rider to its destination for a fare.

It drives one block a step to its rider, collects the fare, and drives the rider to its destination, by the rule of CabDrivingInteraction.

Parameters:

Name Type Description Default
label str

The label of the agent or group.

required
task Annotated[float, NN]

0 when idle, 1 on the way to a rider, and 2 on the way to a destination.

IDLE
target_x Annotated[float, NN]

The column of the cell the cab drives to.

0.0
target_y Annotated[float, NN]

The row of the cell the cab drives to.

0.0
destination_x Annotated[float, NN]

The column of the destination of the rider.

0.0
destination_y Annotated[float, NN]

The row of the destination of the rider.

0.0
rider float

The id of the rider the cab is sent to.

-1.0
seats Annotated[float, closed_open(0, oo)]

The free seats of the cab, one when idle and zero on a trip.

1.0
money float

The fares the cab collected.

0.0
values Mapping[str, object] | None

Further values by name, such as the cell x and y of every member.

None
size int | None

The number of members, or None for one.

None
space BaseSpace | None

The grid whose axes x and y place the members, or None for none.

None

Examples:

>>> from skcomplex.systems.engineering.traffic import CabElement
>>> CabElement('cabs', size=3).size
3
Source code in src/skcomplex/systems/engineering/traffic/_ridesharing.py
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
def __init__(
    self,
    label: str,
    *,
    task: Annotated[float, NN] = IDLE,
    target_x: Annotated[float, NN] = 0.0,
    target_y: Annotated[float, NN] = 0.0,
    destination_x: Annotated[float, NN] = 0.0,
    destination_y: Annotated[float, NN] = 0.0,
    rider: float = -1.0,
    seats: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
    money: float = 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.task = task
    self.target_x = target_x
    self.target_y = target_y
    self.destination_x = destination_x
    self.destination_y = destination_y
    self.rider = rider
    self.seats = seats
    self.money = money

CarelessBrakingInteraction(label, *, cars, probability=0.1, gap=1.0)

Bases: Interaction

System rule of Charbonneau's traffic system.

Every step, with probability probability, one random driver brakes by the braking of its group whatever the distance to the car ahead. Then every car moves by its speed, but no closer than gap to the car ahead.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
cars Role

The path of the group of cars.

required
probability Annotated[float, closed_open(0, oo)]

The probability that a random driver brakes in a step.

0.1
gap Annotated[float, closed_open(0, oo)]

The smallest distance a car keeps to the car ahead.

1.0

Examples:

>>> from skcomplex.systems.engineering.traffic import CarelessBrakingInteraction
>>> CarelessBrakingInteraction('bozo', cars='cars').probability
0.1
Source code in src/skcomplex/systems/engineering/traffic/_drivers.py
366
367
368
369
370
371
372
373
374
375
376
377
def __init__(
    self,
    label: str,
    *,
    cars: Role,
    probability: Annotated[float, RealSet.closed_open(0, oo)] = 0.1,
    gap: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
) -> None:
    super().__init__(label)
    self.cars = cars
    self.probability = probability
    self.gap = gap

drive(system)

Make a random driver brake, then move every car.

The driver brakes with the interaction's probability.

Parameters:

Name Type Description Default
system SystemView

The system.

required
Source code in src/skcomplex/systems/engineering/traffic/_drivers.py
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
def drive(self, system: SystemView) -> None:
    """Make a random driver brake, then move every car.

    The driver brakes with the interaction's probability.

    Args:
        system:
            The system.
    """
    cars = sorted(cast(list[MotoristView], system.get_agents('cars')), key=lambda car: car.rank)
    if not cars:
        return
    if system.rng.random() < self.probability:
        careless = cars[int(system.rng.integers(len(cars)))]
        careless.v = max(careless.v - careless.braking, 0.0)
    gap = self.gap
    positions = [car.x for car in cars]
    for index, car in enumerate(cars):
        moved = car.x + car.v
        car.x = min(moved, positions[index + 1] - gap) if index + 1 < len(cars) else moved

CharbonneauTrafficSystem(n_cars=100, *, probability=0.1, safe_distance=5.0, speed_limit=10.0, random_state=0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

Charbonneau's traffic system.

Random braking of single drivers sets off traffic jams. Cars on a one-way road start at rest with random spacings between 3 and 17. Every step, each driver brakes by 3 when the car ahead is closer than the safe distance and accelerates by 1 otherwise, within the speed limit. A random driver brakes with a probability every step, and then every car moves by its speed. The system has the group cars of positions x, speeds v, and ranks rank.

Parameters:

Name Type Description Default
n_cars int

The number of cars.

100
probability float

The probability that a random driver brakes in a step.

0.1
safe_distance float

The distance to the car ahead below which drivers brake.

5.0
speed_limit float

The largest speed.

10.0
random_state int | Generator | None

The seed or generator of the initial spacings.

0
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.engineering.traffic import CharbonneauTrafficSystem
>>> CharbonneauTrafficSystem(5).prepare().system_.elements[0].size
5
Source code in src/skcomplex/systems/engineering/traffic/_drivers.py
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
def __init__(
    self,
    n_cars: int = 100,
    *,
    probability: float = 0.1,
    safe_distance: float = 5.0,
    speed_limit: float = 10.0,
    random_state: int | np.random.Generator | None = 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.probability = probability
    self.safe_distance = safe_distance
    self.speed_limit = speed_limit
    self.random_state = random_state

DelayedCarFollowingSystem(n_cars=8, *, spacing=10.0, u=1.0, amplitude=0.1, omega=0.5, reaction=1.0, T=1.0, power=0.0, label='system', solver='auto', backend='auto')

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

Parameters:

Name Type Description Default
n_cars int

The number of cars, the lead car included.

8
spacing float

The initial distance between neighbouring cars.

10.0
u float

The mean speed of the lead car.

1.0
amplitude float

The relative amplitude of the speed oscillation of the lead car.

0.1
omega float

The angular frequency of the speed oscillation of the lead car.

0.5
reaction float

The reaction constant λ.

1.0
T float

The reaction time.

1.0
power float

The power of the distance in the reaction, 0 for the linear system.

0.0
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.engineering.traffic import DelayedCarFollowingSystem
>>> DelayedCarFollowingSystem(n_cars=3).prepare().system_.elements[2].x
-20.0
Source code in src/skcomplex/systems/engineering/traffic/_car_following.py
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
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

DelayedFollowingInteraction(label, *, target, leader, reaction=1.0, T=1.0, power=0.0)

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

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the follower car.

required
leader Role

The path of the car ahead of the follower.

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

The sensitivity λ.

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

The reaction time.

1.0
power Annotated[float, closed_open(0, oo)]

The power m of the gap.

0.0

Examples:

>>> from skcomplex.systems.engineering.traffic import DelayedFollowingInteraction
>>> DelayedFollowingInteraction('follow', target='car_1', leader='car_0').T
1.0
Source code in src/skcomplex/systems/engineering/traffic/_car_following.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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

DriverElement(label, *, speed=0.0, rank=0.0, acceleration=1.0, min_acc=-10.0, max_acc=1.0, speed_limit=40.0, eps=0.02, values=None, size=None, space=None)

Bases: Element

Driver on a ring road who accelerates as much as allowed.

The driver stops when the speed would cause a collision, by the rule of RingRoadDrivingInteraction.

Parameters:

Name Type Description Default
label str

The label of the agent or group.

required
speed Annotated[float, closed_open(0, oo)]

The speed.

0.0
rank Annotated[object, NN]

The order of the car along the ring.

0.0
acceleration float

The acceleration every driver chooses.

1.0
min_acc float

The largest braking, as a negative acceleration.

-10.0
max_acc float

The largest acceleration.

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

The largest speed.

40.0
eps Annotated[float, closed_open(0, oo)]

The relative amplitude of the random error of speed.

0.02
values Mapping[str, object] | None

Further values by name, such as the position x and y of every driver on the road.

None
size int | None

The number of drivers, or None for one.

None
space BaseSpace | None

The road, a continuous space whose axes x and y place the drivers, or None for none.

None

Examples:

>>> from skcomplex.systems.engineering.traffic import DriverElement
>>> DriverElement('drivers', values={'x': [0.0, 10.0], 'y': 0.5}, size=2).size
2
Source code in src/skcomplex/systems/engineering/traffic/_drivers.py
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
def __init__(
    self,
    label: str,
    *,
    speed: Annotated[float, RealSet.closed_open(0, oo)] = 0.0,
    rank: Annotated[object, NN] = 0.0,
    acceleration: float = 1.0,
    min_acc: float = -10.0,
    max_acc: float = 1.0,
    speed_limit: Annotated[float, RealSet.open(0, oo)] = 40.0,
    eps: Annotated[float, RealSet.closed_open(0, oo)] = 0.02,
    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.speed = speed
    self.rank = rank
    self.acceleration = acceleration
    self.min_acc = min_acc
    self.max_acc = max_acc
    self.speed_limit = speed_limit
    self.eps = eps

GasKineticTrafficElement(label, *, rho=20.0, Q=1800.0, V0=110.0, T=1.8 / 3600, tau=35 / 3600, rho_max=160.0, gamma=1.2, A0=0.008, dA=0.02, rho_c=0.27 * 160, d_rho=0.05 * 160, space=None)

Bases: Element

Cell of a road with the density rho and the flow Q of the GKT system.

The GKT system is the gas-kinetic-based traffic system. The density and the flow are conserved by conservation interactions, and the flow relaxes to a dynamic equilibrium speed that brakes on the density and the speed read ahead at the interaction point. Lengths are in kilometres and times in hours, so T and tau are fractions of an hour.

Parameters:

Name Type Description Default
label str

The label of the cell or the road.

required
rho Annotated[object, open(0, oo)]

The initial density in vehicles per kilometre.

20.0
Q object

The initial flow in vehicles per hour.

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

The desired speed.

110.0
T Annotated[float, open(0, oo)]

The safe time headway.

1.8 / 3600
tau Annotated[float, open(0, oo)]

The relaxation time of the speed.

35 / 3600
rho_max Annotated[float, open(0, oo)]

The maximum density.

160.0
gamma Annotated[float, open(0, oo)]

The anticipation factor, the interaction distance in units of the safe distance 1 / rho_max + T V.

1.2
A0 Annotated[float, open(0, oo)]

The variance prefactor of free traffic.

0.008
dA Annotated[float, closed_open(0, oo)]

The increase of the variance prefactor from free to congested traffic, halved.

0.02
rho_c Annotated[float, open(0, oo)]

The density of the transition of the variance prefactor.

0.27 * 160
d_rho Annotated[float, open(0, oo)]

The width of the transition of the variance prefactor.

0.05 * 160
space BaseSpace | None

The lattice of the cells of the road, or None for one cell.

None

Examples:

>>> from skcomplex.systems.engineering.traffic import GasKineticTrafficElement
>>> GasKineticTrafficElement('cell').gamma
1.2
Source code in src/skcomplex/systems/engineering/traffic/_continuum.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def __init__(
    self,
    label: str,
    *,
    rho: Annotated[object, RealSet.open(0, oo)] = 20.0,
    Q: object = 1800.0,
    V0: Annotated[float, RealSet.open(0, oo)] = 110.0,
    T: Annotated[float, RealSet.open(0, oo)] = 1.8 / 3600,
    tau: Annotated[float, RealSet.open(0, oo)] = 35 / 3600,
    rho_max: Annotated[float, RealSet.open(0, oo)] = 160.0,
    gamma: Annotated[float, RealSet.open(0, oo)] = 1.2,
    A0: Annotated[float, RealSet.open(0, oo)] = 0.008,
    dA: Annotated[float, RealSet.closed_open(0, oo)] = 0.02,
    rho_c: Annotated[float, RealSet.open(0, oo)] = 0.27 * 160,
    d_rho: Annotated[float, RealSet.open(0, oo)] = 0.05 * 160,
    space: BaseSpace | None = None,
) -> None:
    super().__init__(label, space=space)
    self.rho = rho
    self.Q = Q
    self.V0 = V0
    self.T = T
    self.tau = tau
    self.rho_max = rho_max
    self.gamma = gamma
    self.A0 = A0
    self.dA = dA
    self.rho_c = rho_c
    self.d_rho = d_rho

GktTrafficSystem(n=200, *, length=10.0, density=35.0, amplitude=2.0, V0=110.0, T=1.8 / 3600, tau=35 / 3600, rho_max=160.0, gamma=1.2, label='system', solver='auto', backend='auto')

Bases: BaseSystem

The gas-kinetic-based traffic system.

Drivers brake on the traffic at an interaction point ahead. The density and the flow Q = rho V on a ring road follow rho_t + Q_x = 0 and Q_t + (Q**2 / rho + rho A(rho) V**2)_x = rho (Ve* - V) / tau, where the dynamic equilibrium speed

Ve* = V0 (1 - (theta + theta_a) / (2 A(rho_max)) (rho_a T / (1 - rho_a / rho_max))**2 B(dV))

reads the density rho_a and the speed V_a at the interaction point x + gamma (1 / rho_max + T V), with the variances theta = A(rho) V**2, the variance prefactor A(rho) = A0 + dA (tanh((rho - rho_c) / d_rho) + 1), the dimensionless speed difference dV = (V - V_a) / sqrt(theta + theta_a), and the Boltzmann factor B(d) = 2 (d phi(d) + (1 + d**2) Phi(d)) of the standard normal density phi and distribution Phi. Lengths are in kilometres and times in hours, and the defaults are the calibrated values of Treiber, Hennecke, and Helbing. The flow starts in equilibrium with their dipole perturbation of the density, of height amplitude. Homogeneous traffic is unstable between about 24 and 51 vehicles per kilometre, where the perturbation grows into stop-and-go waves whose downstream fronts move against the traffic.

The system has the density road.rho and the flow road.Q.

Parameters:

Name Type Description Default
n int

The number of cells.

200
length float

The length of the ring road in kilometres.

10.0
density float

The mean density in vehicles per kilometre.

35.0
amplitude float

The height of the dipole perturbation in vehicles per kilometre.

2.0
V0 float

The desired speed in kilometres per hour.

110.0
T float

The safe time headway in hours.

1.8 / 3600
tau float

The relaxation time of the speed in hours.

35 / 3600
rho_max float

The maximum density in vehicles per kilometre.

160.0
gamma float

The anticipation factor.

1.2
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.engineering.traffic import GktTrafficSystem
>>> GktTrafficSystem(10).prepare().system_.elements[0].gamma
1.2
Source code in src/skcomplex/systems/engineering/traffic/_continuum.py
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
def __init__(
    self,
    n: int = 200,
    *,
    length: float = 10.0,
    density: float = 35.0,
    amplitude: float = 2.0,
    V0: float = 110.0,
    T: float = 1.8 / 3600,
    tau: float = 35 / 3600,
    rho_max: float = 160.0,
    gamma: float = 1.2,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.n = n
    self.length = length
    self.density = density
    self.amplitude = amplitude
    self.V0 = V0
    self.T = T
    self.tau = tau
    self.rho_max = rho_max
    self.gamma = gamma

IntelligentDriverInteraction(label, *, target, v0=30.0, T=1.5, a=1.0, b=1.5, s0=2.0, delta=4.0, length=5.0, road_length=500.0)

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

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
v0 Annotated[float, open(0, oo)]

The desired speed.

30.0
T Annotated[float, closed_open(0, oo)]

The safe time headway.

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

The largest acceleration.

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

The comfortable deceleration.

1.5
s0 Annotated[float, closed_open(0, oo)]

The jam distance.

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

The acceleration exponent.

4.0
length Annotated[float, closed_open(0, oo)]

The length of a car.

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

The length of the ring road.

500.0

Examples:

>>> from skcomplex.systems.engineering.traffic import IntelligentDriverInteraction
>>> IntelligentDriverInteraction('follow', target='cars').v0
30.0
Source code in src/skcomplex/systems/engineering/traffic/_car_following.py
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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

IntelligentDriverSystem(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, label='system', solver='auto', backend='auto')

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

Parameters:

Name Type Description Default
n_cars int

The number of cars.

20
length float

The length of the ring road.

500.0
v0 float

The desired speed.

30.0
T float

The safe time headway.

1.5
a float

The largest acceleration.

1.0
b float

The comfortable deceleration.

1.5
s0 float

The jam distance.

2.0
delta float

The acceleration exponent.

4.0
car_length float

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

5.0
perturbation float

The forward shift of the first car from uniform spacing.

1.0
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.engineering.traffic import IntelligentDriverSystem
>>> IntelligentDriverSystem(n_cars=4).prepare().system_.interactions[0].road_length
500.0
Source code in src/skcomplex/systems/engineering/traffic/_car_following.py
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
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

KernerKonhauserSystem(n=40, *, length=10.0, density=0.25, bump=0.1, V0=1.0, rho_max=1.0, tau=1.0, c0=0.45, mu=0.2, label='system', solver='auto', backend='auto')

Bases: BaseSystem

The Kerner-Konhäuser system, where viscous traffic forms moving clusters.

The clusters are dense, and the traffic flow is viscous. The density and the flow q = rho v on a ring road follow rho_t + q_x = 0 and q_t + (q^2 / rho + c0^2 rho)_x = (rho V(rho) - q) / τ + μ v_xx, with the equilibrium speed V(rho) = V0 (1 / (1 + exp((rho / rho_max - 0.25) / 0.06)) - 3.72e-6). The flow starts in equilibrium with a bump of density, which grows into a cluster at intermediate densities. The system has the density road.rho and the flow road.q.

Parameters:

Name Type Description Default
n int

The number of cells.

40
length float

The length of the ring road.

10.0
density float

The mean density.

0.25
bump float

The relative height of the density bump.

0.1
V0 float

The scale of the equilibrium speed.

1.0
rho_max float

The jam density.

1.0
tau float

The relaxation time of the speed.

1.0
c0 float

The speed of sound of the traffic pressure c0^2 rho.

0.45
mu float

The viscosity.

0.2
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.engineering.traffic import KernerKonhauserSystem
>>> KernerKonhauserSystem(10).prepare().system_.elements[0].mu
0.2
Source code in src/skcomplex/systems/engineering/traffic/_continuum.py
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
def __init__(
    self,
    n: int = 40,
    *,
    length: float = 10.0,
    density: float = 0.25,
    bump: float = 0.1,
    V0: float = 1.0,
    rho_max: float = 1.0,
    tau: float = 1.0,
    c0: float = 0.45,
    mu: float = 0.2,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.n = n
    self.length = length
    self.density = density
    self.bump = bump
    self.V0 = V0
    self.rho_max = rho_max
    self.tau = tau
    self.c0 = c0
    self.mu = mu

LangevinDrivingInteraction(label, *, target, gamma=3.0, u0=1.0, rho_max=1.0, D1=0.05, road_length=100.0)

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

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
gamma Annotated[float, open(0, oo)]

The relaxation rate.

3.0
u0 Annotated[float, open(0, oo)]

The desired speed on an empty road.

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

The jam density.

1.0
D1 Annotated[float, closed_open(0, oo)]

The strength of the noise.

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

The length of the ring road.

100.0

Examples:

>>> from skcomplex.systems.engineering.traffic import LangevinDrivingInteraction
>>> LangevinDrivingInteraction('drive', target='cars', gamma=2.0).state
"x'"
Source code in src/skcomplex/systems/engineering/traffic/_langevin.py
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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

LangevinTrafficSystem(n_cars=50, length=100.0, *, gamma=3.0, u0=1.0, rho_max=1.0, D1=0.05, label='system', solver='auto', backend='auto')

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

Parameters:

Name Type Description Default
n_cars int

The number of cars.

50
length float

The length of the ring road.

100.0
gamma float

The relaxation rate of the speed.

3.0
u0 float

The desired speed on an empty road.

1.0
rho_max float

The jam density, at which the desired speed vanishes.

1.0
D1 float

The noise intensity per unit density.

0.05
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

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
Source code in src/skcomplex/systems/engineering/traffic/_langevin.py
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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

LeadCarInteraction(label, *, target, u=1.0, amplitude=0.1, omega=0.5)

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

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the lead car.

required
u Annotated[float, closed_open(0, oo)]

The cruising speed.

1.0
amplitude Annotated[float, closed_open(0, oo)]

The relative amplitude of the swing.

0.1
omega Annotated[float, closed_open(0, oo)]

The angular frequency of the swing.

0.5

Examples:

>>> from skcomplex.systems.engineering.traffic import LeadCarInteraction
>>> LeadCarInteraction('lead', target='car_0').omega
0.5
Source code in src/skcomplex/systems/engineering/traffic/_car_following.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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

LighthillWhithamRichardsSystem(n=200, *, upstream=0.3, downstream=0.8, vmax=1.0, rho_max=1.0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

The Lighthill-Whitham-Richards system.

It is the conservation of cars with the Greenshields speed law. The density follows rho_t + (rho V(rho))_x = 0 with V(rho) = vmax (1 - rho / rho_max) on a road of length 1 with open edges. Free traffic at density upstream runs into a queue at density downstream in the second half of the road, and the shock between them moves at vmax (1 - (upstream + downstream) / rho_max). The system has the density road.rho.

Parameters:

Name Type Description Default
n int

The number of cells.

200
upstream float

The density in the first half of the road.

0.3
downstream float

The density in the second half of the road.

0.8
vmax float

The speed on an empty road.

1.0
rho_max float

The jam density.

1.0
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.engineering.traffic import LighthillWhithamRichardsSystem
>>> float(LighthillWhithamRichardsSystem(10).prepare().system_.elements[0].rho[-1])
0.8
Source code in src/skcomplex/systems/engineering/traffic/_continuum.py
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
def __init__(
    self,
    n: int = 200,
    *,
    upstream: float = 0.3,
    downstream: float = 0.8,
    vmax: float = 1.0,
    rho_max: float = 1.0,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.n = n
    self.upstream = upstream
    self.downstream = downstream
    self.vmax = vmax
    self.rho_max = rho_max

MotoristElement(label, *, x=0.0, v=0.0, rank=0.0, safe_distance=5.0, braking=3.0, acceleration=1.0, speed_limit=10.0, size=None)

Bases: Element

Car of Charbonneau's traffic system.

It brakes when it is closer than a safe distance to the car ahead, by the rule of SafeDistanceInteraction.

Parameters:

Name Type Description Default
label str

The label of the agent or group.

required
x object

The position along the road.

0.0
v Annotated[object, closed_open(0, oo)]

The speed.

0.0
rank Annotated[object, NN]

The order of the car along the road, 0 for the last car.

0.0
safe_distance Annotated[float, open(0, oo)]

The distance to the car ahead below which the driver brakes.

5.0
braking Annotated[float, closed_open(0, oo)]

The speed lost when braking.

3.0
acceleration Annotated[float, closed_open(0, oo)]

The speed gained when accelerating.

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

The largest speed.

10.0
size int | None

The number of cars, or None for one.

None

Examples:

>>> from skcomplex.systems.engineering.traffic import MotoristElement
>>> MotoristElement('cars', x=[0.0, 10.0, 20.0], size=3).size
3
Source code in src/skcomplex/systems/engineering/traffic/_drivers.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def __init__(
    self,
    label: str,
    *,
    x: object = 0.0,
    v: Annotated[object, RealSet.closed_open(0, oo)] = 0.0,
    rank: Annotated[object, NN] = 0.0,
    safe_distance: Annotated[float, RealSet.open(0, oo)] = 5.0,
    braking: Annotated[float, RealSet.closed_open(0, oo)] = 3.0,
    acceleration: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
    speed_limit: Annotated[float, RealSet.open(0, oo)] = 10.0,
    size: int | None = None,
) -> None:
    super().__init__(label, size=size)
    self.x = x
    self.v = v
    self.rank = rank
    self.safe_distance = safe_distance
    self.braking = braking
    self.acceleration = acceleration
    self.speed_limit = speed_limit

NagelSchreckenbergInteraction(label, *, target='road', vmax=5)

Bases: Interaction

Driving rules of the Nagel-Schreckenberg road: accelerate, brake, slow down, and move.

Every step, a car accelerates by one up to vmax, brakes to the gap ahead, slows down by one with the probability slowdown of its cell, and then moves forward by its velocity. Both rules read the ordered cells ahead or behind up to vmax cells away.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The cells of the road whose state velocity the rules update.

'road'
vmax int

The largest velocity, in cells per step.

5

Examples:

>>> from skcomplex.systems.engineering.traffic import NagelSchreckenbergInteraction
>>> NagelSchreckenbergInteraction('driving', target='road', vmax=3).vmax
3
Source code in src/skcomplex/systems/engineering/traffic/_traffic.py
87
88
89
90
def __init__(self, label: str, *, target: Role = 'road', vmax: int = 5) -> None:
    super().__init__(label)
    self.target = target
    self.vmax = vmax

NagelSchreckenbergSystem(length=1000, *, density=0.2, slowdown=0.3, vmax=5, random_state=0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

The Nagel-Schreckenberg traffic system on a ring road.

Jams emerge above a critical density. The system has the velocities road.velocity, -1 on empty cells.

Parameters:

Name Type Description Default
length int

The number of cells of the road.

1000
density float

The fraction of cells with a car.

0.2
slowdown float

The probability of random braking.

0.3
vmax int

The largest velocity.

5
random_state int | Generator | None

The seed or generator of the initial positions of the cars.

0
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.engineering.traffic import NagelSchreckenbergSystem
>>> system = NagelSchreckenbergSystem(100, density=0.1).prepare().system_
>>> int((system.elements[0].velocity >= 0).sum())
10
Source code in src/skcomplex/systems/engineering/traffic/_traffic.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
def __init__(
    self,
    length: int = 1000,
    *,
    density: float = 0.2,
    slowdown: float = 0.3,
    vmax: int = 5,
    random_state: int | np.random.Generator | None = 0,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.length = length
    self.density = density
    self.slowdown = slowdown
    self.vmax = vmax
    self.random_state = random_state

OptimalVelocitySystem(n_cars=20, length=40.0, *, a=1.0, V0=1.0, hc=2.0, perturbation=0.1, label='system', solver='auto', backend='auto')

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

Parameters:

Name Type Description Default
n_cars int

The number of cars.

20
length float

The length of the ring road.

40.0
a float

The sensitivity of drivers.

1.0
V0 float

The scale of the optimal velocity, which tends to V0 (1 + tanh(hc)) on an empty road.

1.0
hc float

The headway at which the optimal velocity changes fastest.

2.0
perturbation float

The forward shift of the first car from uniform spacing.

0.1
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.engineering.traffic import OptimalVelocitySystem
>>> system = OptimalVelocitySystem(n_cars=5).prepare().system_
>>> system.elements[0].size, len(system.interactions)
(5, 1)
Source code in src/skcomplex/systems/engineering/traffic/_car_following.py
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
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

PayneSystem(n=200, *, density=0.3, bump=0.2, vmax=1.0, rho_max=1.0, tau=0.5, label='system', solver='auto', backend='auto')

Bases: BaseSystem

The Payne system, where the speed of traffic relaxes to the Greenshields speed.

The speed also anticipates density. The density and the flow q = rho v on a ring road of length 1 follow rho_t + q_x = 0 and q_t + (q^2 / rho + P(rho))_x = (rho V(rho) - q) / τ, with V(rho) = vmax (1 - rho / rho_max) and the anticipation pressure P(rho) = vmax rho / (2 τ rho_max). This is the speed equation v_t + v v_x = (V - v) / τ + V'(rho) rho_x / (2 τ rho) in conservation form. The flow starts in equilibrium with a bump of density. The system has the density road.rho and the flow road.q.

Parameters:

Name Type Description Default
n int

The number of cells.

200
density float

The mean density.

0.3
bump float

The relative height of the density bump.

0.2
vmax float

The speed on an empty road.

1.0
rho_max float

The jam density.

1.0
tau float

The relaxation time of the speed.

0.5
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.engineering.traffic import PayneSystem
>>> PayneSystem(10).prepare().system_.elements[0].tau
0.5
Source code in src/skcomplex/systems/engineering/traffic/_continuum.py
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
def __init__(
    self,
    n: int = 200,
    *,
    density: float = 0.3,
    bump: float = 0.2,
    vmax: float = 1.0,
    rho_max: float = 1.0,
    tau: float = 0.5,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.n = n
    self.density = density
    self.bump = bump
    self.vmax = vmax
    self.rho_max = rho_max
    self.tau = tau

RiderElement(label, *, destination_x=0.0, destination_y=0.0, waited=0.0, seats=1.0, money=100.0, values=None, size=None, space=None)

Bases: Element

Rider who waits at a cell for a cab to a destination.

It pays the fare when the cab picks it up.

Parameters:

Name Type Description Default
label str

The label of the agent or group.

required
destination_x Annotated[float, NN]

The column of the destination.

0.0
destination_y Annotated[float, NN]

The row of the destination.

0.0
waited Annotated[float, closed_open(0, oo)]

The number of steps the rider has waited.

0.0
seats Annotated[float, closed_open(0, oo)]

The seats the rider still requests, one until a cab is on its way and zero after.

1.0
money float

The money of the rider.

100.0
values Mapping[str, object] | None

Further values by name, such as the cell x and y of every member.

None
size int | None

The number of members, or None for one.

None
space BaseSpace | None

The grid whose axes x and y place the members, or None for none.

None

Examples:

>>> from skcomplex.systems.engineering.traffic import RiderElement
>>> RiderElement('riders', money=50.0, size=2).size
2
Source code in src/skcomplex/systems/engineering/traffic/_ridesharing.py
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
def __init__(
    self,
    label: str,
    *,
    destination_x: Annotated[float, NN] = 0.0,
    destination_y: Annotated[float, NN] = 0.0,
    waited: Annotated[float, RealSet.closed_open(0, oo)] = 0.0,
    seats: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
    money: float = 100.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.destination_x = destination_x
    self.destination_y = destination_y
    self.waited = waited
    self.seats = seats
    self.money = money

RidesharingSystem(n_cabs=30, *, demand=1.0, width=30, height=30, base_fare=2.0, block_fare=0.5, random_state=0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

Riders who request trips on a city grid and cabs sent to them.

A stable matching sends the cabs. Every step new riders appear at random cells with random destinations, and the dispatch matches the waiting riders with the idle cabs by a stable matching in which riders prefer nearer cabs and cabs prefer riders who waited longer. Every cab drives one block a step to its rider, collects the fare, and drives the rider to its destination. While the demand stays below the trips the fleet can serve, riders wait about as long as a cab takes to reach them. As the demand approaches that capacity, every cab is busy, the queue of waiting riders grows, and the waiting time rises steeply, as in a queue near saturation.

The system has the groups riders and cabs, the arrivals arrival, and the matching dispatch.

Parameters:

Name Type Description Default
n_cabs int

The number of cabs.

30
demand float

The mean number of new riders in a step.

1.0
width int

The number of columns of the grid.

30
height int

The number of rows of the grid.

30
base_fare float

The fare of every trip.

2.0
block_fare float

The fare per block of a trip.

0.5
random_state int | Generator | None

The seed or generator of the cells of the cabs.

0
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the agent solver with the phases of arrivals, dispatch, and driving.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.engineering.traffic import RidesharingSystem
>>> [group.size for group in RidesharingSystem(5).prepare().system_.elements]
[1, 5]
Source code in src/skcomplex/systems/engineering/traffic/_ridesharing.py
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
def __init__(
    self,
    n_cabs: int = 30,
    *,
    demand: float = 1.0,
    width: int = 30,
    height: int = 30,
    base_fare: float = 2.0,
    block_fare: float = 0.5,
    random_state: int | np.random.Generator | None = 0,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.n_cabs = n_cabs
    self.demand = demand
    self.width = width
    self.height = height
    self.base_fare = base_fare
    self.block_fare = block_fare
    self.random_state = random_state

RingRoadDriversSystem(n_cars=30, length=1000.0, *, eps=0.02, speed_limit=40.0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

Drivers on a circular highway.

Small random errors of speed set off traffic jams. The cars start at rest, equally spaced. Every driver accelerates by 1 per step up to the speed limit, the speed takes a random relative error of up to eps, and a speed that would reach the car ahead becomes 0. The system has the group drivers of speeds speed on a ring road of length length.

Parameters:

Name Type Description Default
n_cars int

The number of cars.

30
length float

The length of the ring road.

1000.0
eps float

The relative amplitude of the random error of speed.

0.02
speed_limit float

The largest speed.

40.0
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.engineering.traffic import RingRoadDriversSystem
>>> RingRoadDriversSystem(10).prepare().system_.space.prepare().extent_[0]
1000.0
Source code in src/skcomplex/systems/engineering/traffic/_drivers.py
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
def __init__(
    self,
    n_cars: int = 30,
    length: float = 1000.0,
    *,
    eps: float = 0.02,
    speed_limit: float = 40.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.eps = eps
    self.speed_limit = speed_limit

RingRoadDrivingInteraction(label, *, target)

Bases: Interaction

Driving rule of the drivers on a ring road.

The speed of a driver changes by its acceleration, bounded by min_acc and max_acc, is multiplied by a random factor between 1 - eps and 1 + eps, and stays between 0 and the speed limit. A speed larger than the distance to the car ahead becomes 0. The car then moves along the ring by its speed, and the ring is as long as the width of the space.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the group of drivers.

required

Examples:

>>> from skcomplex.systems.engineering.traffic import RingRoadDrivingInteraction
>>> RingRoadDrivingInteraction('drive', target='drivers').target
'drivers'
Source code in src/skcomplex/systems/engineering/traffic/_drivers.py
279
280
281
def __init__(self, label: str, *, target: Role) -> None:
    super().__init__(label)
    self.target = target

drive(agent, system)

Choose the speed from the distance to the car ahead and move along the ring.

Parameters:

Name Type Description Default
agent DriverView

The driver.

required
system SystemView

The system.

required
Source code in src/skcomplex/systems/engineering/traffic/_drivers.py
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
def drive(self, agent: DriverView, system: SystemView) -> None:
    """Choose the speed from the distance to the car ahead and move along the ring.

    Args:
        agent:
            The driver.

        system:
            The system.
    """
    cars = cast(list[DriverView], system.get_agents(self.target))
    if system.dimensions is None:
        error_msg = 'Drivers move along a ring road, a continuous space with an extent.'
        raise SimulationError(error_msg)
    length = system.dimensions[0]
    ahead = self._get_car_ahead(agent, system, len(cars))
    x, y = RingRoadDrivingInteraction._get_position(agent)
    distance = (RingRoadDrivingInteraction._get_position(ahead)[0] - x) % length if ahead is not None else length
    acceleration = min(max(agent.acceleration, agent.min_acc), agent.max_acc)
    speed = (agent.speed + acceleration) * system.rng.uniform(1 - agent.eps, 1 + agent.eps)
    speed = min(max(speed, 0.0), agent.speed_limit)
    agent.speed = 0.0 if speed > distance else speed
    agent.move_to(((x + agent.speed) % length, y))

RoadElement(label, *, velocity=-1, slowdown=0.3, space=None)

Bases: Element

Cell of the Nagel-Schreckenberg road.

It holds the velocity of the car on it, or -1 when it is empty. The local rules of the road make cars accelerate by one up to the largest velocity, brake to the gap ahead, slow down by one with probability slowdown, and move forward by their velocity, every step.

Parameters:

Name Type Description Default
label str

The label of the cell or the lattice group.

required
velocity Annotated[object, ZZ]

The initial velocity, or -1 for an empty cell.

-1
slowdown Annotated[float, closed_open(0, oo)]

The probability of random braking.

0.3
space BaseSpace | None

The lattice of the cells of the road, or None for one cell.

None

Examples:

>>> from skcomplex.systems.engineering.traffic import RoadElement
>>> RoadElement('cell').slowdown
0.3
Source code in src/skcomplex/systems/engineering/traffic/_traffic.py
51
52
53
54
55
56
57
58
59
60
61
def __init__(
    self,
    label: str,
    *,
    velocity: Annotated[object, ZZ] = -1,
    slowdown: Annotated[float, RealSet.closed_open(0, oo)] = 0.3,
    space: BaseSpace | None = None,
) -> None:
    super().__init__(label, space=space)
    self.velocity = velocity
    self.slowdown = slowdown

SafeDistanceInteraction(label, *, target)

Bases: Interaction

Speed rule of the cars of Charbonneau's traffic system.

Every step a driver brakes by its braking when the car ahead is closer than its safe distance, and accelerates by its acceleration otherwise, within its speed limit.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the group of cars.

required

Examples:

>>> from skcomplex.systems.engineering.traffic import SafeDistanceInteraction
>>> SafeDistanceInteraction('adjust', target='cars').target
'cars'
Source code in src/skcomplex/systems/engineering/traffic/_drivers.py
132
133
134
def __init__(self, label: str, *, target: Role) -> None:
    super().__init__(label)
    self.target = target

adjust(agent, system)

Brake when the car ahead is too close and accelerate otherwise.

The car brakes when the car ahead is closer than the safe distance. The lead car, which has no car ahead, keeps its distance to the car behind it instead.

Parameters:

Name Type Description Default
agent MotoristView

The car.

required
system SystemView

The system.

required
Source code in src/skcomplex/systems/engineering/traffic/_drivers.py
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
def adjust(self, agent: MotoristView, system: SystemView) -> None:
    """Brake when the car ahead is too close and accelerate otherwise.

    The car brakes when the car ahead is closer than the safe distance. The lead car, which has no car ahead, keeps
    its distance to the car behind it instead.

    Args:
        agent:
            The car.

        system:
            The system.
    """
    cars = cast(list[MotoristView], system.get_agents(self.target))
    ahead = next((car for car in cars if int(car.rank) == int(agent.rank) + 1), None)
    if ahead is None:
        behind = next((car for car in cars if int(car.rank) == int(agent.rank) - 1), None)
        distance = agent.x - behind.x if behind is not None else np.inf
    else:
        distance = ahead.x - agent.x
    change = -agent.braking if distance < agent.safe_distance else agent.acceleration
    agent.v = float(np.clip(agent.v + change, 0.0, agent.speed_limit))

TrafficDensityEquationSystem(n=100, *, length=50.0, density=0.5, gamma=1.0, u0=1.0, rho_max=1.0, D1=0.2, perturbation=0.01, random_state=0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

The density and flux equations of Langevin traffic on a ring road.

Their uniform flow breaks into jams. The density rho and the flux w of cars that relax to the desired speed u(rho) = u0 (1 - rho / rho_max) at the rate gamma, with the diffusion coefficient D(rho) = D1 rho, follow the moment equations rho_t + w_x = (D rho_x)_x and w_t + (2 u w - rho u^2)_x = -gamma (w - rho u) + (D w_x)_x, closed by expanding the velocity distribution around u(rho). A long density wave of the uniform flow grows when the effective diffusion coefficient D(rho) + ((rho u^2)' - (rho u)'^2) / gamma = D(rho) - rho^2 u'(rho)^2 / gamma is negative, so the flow is unstable above the density D1 gamma rho_max^2 / u0^2, and the perturbation grows into dense jams. The uniform density starts with independent Gaussian perturbations of relative size perturbation in every cell, at the flux rho u(rho).

The system has the density road.rho and the flux road.w.

Parameters:

Name Type Description Default
n int

The number of cells.

100
length float

The length of the ring road.

50.0
density float

The mean density.

0.5
gamma float

The relaxation rate of the speed.

1.0
u0 float

The desired speed on an empty road.

1.0
rho_max float

The jam density, at which the desired speed vanishes.

1.0
D1 float

The diffusion coefficient per unit density.

0.2
perturbation float

The relative standard deviation of the initial density perturbations.

0.01
random_state int | Generator | None

The seed or generator of the initial perturbations.

0
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.engineering.traffic import TrafficDensityEquationSystem
>>> TrafficDensityEquationSystem(10).prepare().system_.interactions[0].D1
0.2
Source code in src/skcomplex/systems/engineering/traffic/_langevin.py
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
def __init__(
    self,
    n: int = 100,
    *,
    length: float = 50.0,
    density: float = 0.5,
    gamma: float = 1.0,
    u0: float = 1.0,
    rho_max: float = 1.0,
    D1: float = 0.2,
    perturbation: float = 0.01,
    random_state: int | np.random.Generator | None = 0,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.n = n
    self.length = length
    self.density = density
    self.gamma = gamma
    self.u0 = u0
    self.rho_max = rho_max
    self.D1 = D1
    self.perturbation = perturbation
    self.random_state = random_state

TrafficMomentsElement(label, *, rho=0.5, w=0.25, space=None)

Bases: Element

Density rho and flow w of Langevin traffic on a road.

They are the first two moments of its speeds.

Parameters:

Name Type Description Default
label str

The label of the road.

required
rho Annotated[object, closed_open(0, oo)]

The initial density, one value per cell or one for all.

0.5
w object

The initial flow, one value per cell or one for all.

0.25
space BaseSpace | None

The lattice of the cells of the road, or None for one cell.

None

Examples:

>>> from skcomplex.systems.engineering.traffic import TrafficMomentsElement
>>> TrafficMomentsElement('road').w
0.25
Source code in src/skcomplex/systems/engineering/traffic/_langevin.py
54
55
56
57
58
59
60
61
62
63
64
def __init__(
    self,
    label: str,
    *,
    rho: Annotated[object, RealSet.closed_open(0, oo)] = 0.5,
    w: object = 0.25,
    space: BaseSpace | None = None,
) -> None:
    super().__init__(label, space=space)
    self.rho = rho
    self.w = w

TrafficMomentsInteraction(label, *, target, gamma=1.0, u0=1.0, rho_max=1.0, D1=0.2)

Bases: Interaction

Closed moment equations of the density rho and the flow w of Langevin traffic.

They are rho_t + w_x = (D rho_x)_x and w_t + (2 u w - rho u^2)_x = -gamma (w - rho u) + (D w_x)_x, with the desired speed u(rho) = u0 (1 - rho / rho_max) and the diffusion coefficient D = D1 rho.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the road.

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

The relaxation rate of the speed.

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

The desired speed on an empty road.

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

The jam density.

1.0
D1 Annotated[float, closed_open(0, oo)]

The diffusion coefficient per unit density.

0.2

Examples:

>>> from skcomplex.systems.engineering.traffic import TrafficMomentsInteraction
>>> TrafficMomentsInteraction('moments', target='road').D1
0.2
Source code in src/skcomplex/systems/engineering/traffic/_langevin.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def __init__(
    self,
    label: str,
    *,
    target: Role,
    gamma: Annotated[float, RealSet.open(0, oo)] = 1.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.2,
) -> None:
    super().__init__(label)
    self.target = target
    self.gamma = gamma
    self.u0 = u0
    self.rho_max = rho_max
    self.D1 = D1