Skip to content

Control

Control: feedback loops that keep or drive a system towards a goal.

It also holds the controllers that close them.

BangBangPlantElement(label, *, x=0.0, u=1.0, values=None)

Bases: Element

Second-order linear plant at the position x with the held control u.

Its velocity is the derivative x', and BangBangPlantInteraction moves it.

Parameters:

Name Type Description Default
label str

The label of the plant.

required
x float

The initial position.

0.0
u Annotated[float, FiniteEnumeratedSet([-1, 1])]

The initial control, -1 or 1.

1.0
values Mapping[str, object] | None

Further values by name, such as the initial velocity "x'".

None

Examples:

>>> from skcomplex.systems.engineering.control import BangBangPlantElement
>>> BangBangPlantElement('plant', u=-1.0).u
-1.0
Source code in src/skcomplex/systems/engineering/control/_minimum_time_control.py
56
57
58
59
60
61
62
63
64
65
66
def __init__(
    self,
    label: str,
    *,
    x: float = 0.0,
    u: Annotated[float, FiniteEnumeratedSet([-1, 1])] = 1.0,
    values: Mapping[str, object] | None = None,
) -> None:
    super().__init__(label, values=values)
    self.x = x
    self.u = u

BangBangPlantInteraction(label, *, target, a=0.0)

Bases: Interaction

Motion of a second-order linear plant under a bang-bang control.

The control is held between samples, and it steers the plant to the origin. The plant follows x'' = a x + u with the control u of size 1, a = 0 for a free particle, a = -1 for a harmonic oscillator, and a = 1 for a saddle. The control stays constant between samples, and at every sample takes -1 above the switching curve x' = g(x) of the plant and 1 below it. The curve is made of the two trajectories of the plant under u = 1 and u = -1 that reach the origin: g(x) = -sign(x) sqrt(2 |x|) for the free particle, the half circles g(x) = sqrt(1 - (x + 1)^2) on [-2, 0] and -sqrt(1 - (x - 1)^2) on [0, 2], and 0 outside, for the harmonic oscillator, and g(x) = sqrt(x^2 - 2 x) for x <= 0 and -sqrt(x^2 + 2 x) for x >= 0 for the saddle.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the plant.

required
a Annotated[float, FiniteEnumeratedSet([-1, 0, 1])]

The coefficient of the position in the acceleration, 0, -1, or 1.

0.0

Examples:

>>> from skcomplex.systems.engineering.control import BangBangPlantInteraction
>>> BangBangPlantInteraction('motion', target='plant', a=-1.0).a
-1.0
Source code in src/skcomplex/systems/engineering/control/_minimum_time_control.py
 98
 99
100
101
102
103
104
105
106
107
def __init__(
    self,
    label: str,
    *,
    target: Role,
    a: Annotated[float, FiniteEnumeratedSet([-1, 0, 1])] = 0.0,
) -> None:
    super().__init__(label)
    self.target = target
    self.a = a

CartPendulumInteraction(label, *, target, g=GRAVITY)

Bases: Interaction

Motion of an inverted pendulum on a cart that a bounded horizontal force pushes.

It follows Åström and Murray. The cart of mass M at position x carries a point mass m at the end of a rod of length l, at the angle theta from upright, positive towards negative x. The equations of motion are (M + m) x'' - m l theta'' cos(theta) + m l theta'^2 sin(theta) = force and l theta'' - x'' cos(theta) = g sin(theta), so upright is unstable and a positive force tilts the pendulum back from a positive angle. The cart holds x, theta, the control input force, and its masses and length.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the cart.

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

The acceleration of gravity.

GRAVITY

Examples:

>>> from skcomplex.core.components.elements import Element
>>> from skcomplex.core.systems import System
>>> from skcomplex.systems.engineering.control import CartPendulumInteraction
>>> cart = Element('cart', values={'x': 0.0, 'theta': 0.1, 'force': 0.0, 'M': 1.0, 'm': 0.1, 'l': 0.5})
>>> System(elements=[cart], interactions=[CartPendulumInteraction('motion', target='cart')]).prepare().states_
('cart.x', "cart.x'", 'cart.theta', "cart.theta'")
Source code in src/skcomplex/systems/engineering/control/_regulation.py
63
64
65
66
67
68
69
70
71
72
def __init__(
    self,
    label: str,
    *,
    target: Role,
    g: Annotated[float, RealSet.open(0, oo)] = GRAVITY,
) -> None:
    super().__init__(label)
    self.target = target
    self.g = g

ControlInputElement(label, *, u=0.0)

Bases: Element

Control input that a plant reads, scaled to the interval from -1 to 1.

Parameters:

Name Type Description Default
label str

The label of the input.

required
u Annotated[float, closed(-1, 1)]

The value of the input.

0.0

Examples:

>>> from skcomplex.systems.engineering.control import ControlInputElement
>>> ControlInputElement('input', u=0.5).u
0.5
Source code in src/skcomplex/systems/engineering/control/_koopman.py
158
159
160
def __init__(self, label: str, *, u: Annotated[float, RealSet.closed(-1, 1)] = 0.0) -> None:
    super().__init__(label)
    self.u = u

ConvectionInteraction(label, *, node, h=1.0, fluid=0.0)

Bases: Interaction

Surface of a node that loses the heat flow h (T - fluid) to a fluid of a fixed temperature.

Parameters:

Name Type Description Default
label str

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

required
node Role

The path of the node, an element with the temperature T.

required
h Annotated[float, POSITIVE]

The convective conductance of the surface.

1.0
fluid float

The temperature of the fluid.

0.0

Examples:

>>> from skcomplex.systems.engineering.control import ConvectionInteraction
>>> ConvectionInteraction('outside', node='wall', fluid=5.0).fluid
5.0
Source code in src/skcomplex/systems/engineering/control/_control.py
140
141
142
143
144
145
146
147
148
149
150
151
def __init__(
    self,
    label: str,
    *,
    node: Role,
    h: Annotated[float, POSITIVE] = 1.0,
    fluid: float = 0.0,
) -> None:
    super().__init__(label)
    self.node = node
    self.h = h
    self.fluid = fluid

DrivenOscillatorInteraction(label, *, target)

Bases: Interaction

Motion of a damped harmonic oscillator that a force per unit mass drives.

It follows x'' = -omega^2 x - 2 zeta omega x' + force, with the natural frequency omega, the damping ratio zeta, and the control input force of the element it binds. A controller that sets force, such as StateFeedbackInteraction, closes the loop, and without one force is a parameter that linearization takes as a control.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the oscillator, with the values x, force, omega, and zeta.

required

Examples:

>>> from skcomplex.core.components.elements import Element
>>> from skcomplex.core.systems import System
>>> from skcomplex.systems.engineering.control import DrivenOscillatorInteraction
>>> oscillator = Element('oscillator', values={'x': 1.0, 'force': 0.0, 'omega': 1.0, 'zeta': 0.0})
>>> motion = DrivenOscillatorInteraction('motion', target='oscillator')
>>> System(elements=[oscillator], interactions=[motion]).prepare().states_
('oscillator.x', "oscillator.x'")
Source code in src/skcomplex/systems/engineering/control/_regulation.py
123
124
125
def __init__(self, label: str, *, target: Role) -> None:
    super().__init__(label)
    self.target = target

HeatCapacityInteraction(label, *, node, C=1.0)

Bases: Interaction

Heat capacity C of a node of temperature T, which stores the heat C T' that flows into the node.

The heat flows of the components that join a node add into its balance, which is zero, so with a capacity the node warms at T' = Q / C under the net heat flow Q into it.

Parameters:

Name Type Description Default
label str

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

required
node Role

The path of the node, an element with the temperature T.

required
C Annotated[float, POSITIVE]

The heat capacity.

1.0

Examples:

>>> from skcomplex.systems.engineering.control import HeatCapacityInteraction
>>> HeatCapacityInteraction('air', node='room', C=2e5).C
200000.0
Source code in src/skcomplex/systems/engineering/control/_control.py
56
57
58
59
def __init__(self, label: str, *, node: Role, C: Annotated[float, POSITIVE] = 1.0) -> None:
    super().__init__(label)
    self.node = node
    self.C = C

HeatedRoomSystem(T=10.0, *, setting=20.0, band=1.0, outside=5.0, V=230.0, R=25.0, C=200000.0, wall_capacity=1000000.0, wall_conductance=100.0, surface_conductance=50.0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

A room assembled from thermal and electrical components.

It is heated under a thermostat with hysteresis. A voltage source drives a heating resistor whose heat enters the room, and the room loses heat through a wall of its own heat capacity to the outside air. The thermostat opens the switch of the resistor when the room rises through setting + band / 2 and closes it when the room falls through setting - band / 2. The system has the room temperature room.T and the switch heater.closed, in seconds.

Parameters:

Name Type Description Default
T float

The initial temperature of the room.

10.0
setting float

The temperature the thermostat keeps.

20.0
band float

The width of the hysteresis band around the setting.

1.0
outside float

The temperature of the outside air, which is also the initial temperature of the wall.

5.0
V float

The voltage of the source.

230.0
R float

The resistance of the heater.

25.0
C float

The heat capacity of the room.

200000.0
wall_capacity float

The heat capacity of the wall.

1000000.0
wall_conductance float

The thermal conductance between the room and the wall.

100.0
surface_conductance float

The convective conductance between the wall and the outside air.

50.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.control import HeatedRoomSystem
>>> HeatedRoomSystem(setting=22.0).prepare().system_.interactions[-1].threshold
21.5
Source code in src/skcomplex/systems/engineering/control/_control.py
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
def __init__(
    self,
    T: float = 10.0,
    *,
    setting: float = 20.0,
    band: float = 1.0,
    outside: float = 5.0,
    V: float = 230.0,
    R: float = 25.0,
    C: float = 2e5,
    wall_capacity: float = 1e6,
    wall_conductance: float = 100.0,
    surface_conductance: float = 50.0,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.T = T
    self.setting = setting
    self.band = band
    self.outside = outside
    self.V = V
    self.R = R
    self.C = C
    self.wall_capacity = wall_capacity
    self.wall_conductance = wall_conductance
    self.surface_conductance = surface_conductance

HeatingResistorInteraction(label, *, a, b, heated, switch, R=1.0)

Bases: BranchInteraction

Resistor between the electrical nodes a and b that heats the thermal node heated.

It conducts while the value closed of the element switch is 1. Its current is closed (a.v - b.v) / R, and the power it dissipates, the voltage times the current, flows into the heated node.

Parameters:

Name Type Description Default
label str

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

required
a Role

The path of the electrical node the current leaves.

required
b Role

The path of the electrical node the current enters.

required
heated Role

The path of the thermal node the heat enters, an element with the temperature T.

required
switch Role

The path of the element that holds the switch closed, 1 for closed and 0 for open.

required
R Annotated[float, POSITIVE]

The resistance.

1.0

Examples:

>>> from skcomplex.systems.engineering.control import HeatingResistorInteraction
>>> HeatingResistorInteraction('heating', a='supply', b='neutral', heated='room', switch='heater', R=25.0).R
25.0
Source code in src/skcomplex/systems/engineering/control/_control.py
196
197
198
199
200
201
202
203
204
205
206
207
208
209
def __init__(
    self,
    label: str,
    *,
    a: Role,
    b: Role,
    heated: Role,
    switch: Role,
    R: Annotated[float, POSITIVE] = 1.0,
) -> None:
    super().__init__(label, a=a, b=b)
    self.heated = heated
    self.switch = switch
    self.R = R

KoopmanModelPredictiveControlSystem(plant='duffing', *, controlled=True, dictionary=None, x=0.5, y=0.0, n_runs=100, duration=1000, random_state=0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

A plant that a discrete-time controller steers with model predictive control.

The control runs on a fitted surrogate. The system runs the plant under a random binary input at the limits of its range, drawn every sample, and fits mode_decomposition with the input to the runs. The controller lifts the states with the dictionary, condenses the quadratic tracking cost over the horizon with the matrices of the surrogate, solves the bounded least squares problem with scipy.optimize.lsq_linear, and sets the input to the first value of the solution. The controller updates the input, so the nested system controller steps once per unit of time, and the rates of the nested system plant run in continuous time between the steps, holding the input. The plants are those of figure 6.6 of the review of Brunton et al.: the forced Duffing oscillator x'' = a x + b x^3 + d x' + f cos(w t) + (3 + cos(x)) u, the Van der Pol oscillator x'' = -x - mu (x^2 - 1) x' + u, and the bilinear DC motor, whose second variable is the output that tracks the reference. The motor reads four times the input, as Korda and Mezić scale it. One time unit of the system is one sample of the controller.

The system has the variables plant/state.x and plant/state.y, the input controller/input.u, and the observable reference.

Parameters:

Name Type Description Default
plant str

'duffing', 'van_der_pol', or 'motor'.

'duffing'
controlled bool

Whether the controller solves the tracking problem, or holds the input at 0 with no fit.

True
dictionary TransformerMixin | None

A scikit-learn transformer that computes the observables and keeps the variables among them, or None for the monomials up to degree 3.

None
x float

The initial value of the first variable.

0.5
y float

The initial value of the second variable.

0.0
n_runs int

The number of training runs.

100
duration int

The number of samples of every training run.

1000
random_state int | Generator | None

The seed or generator of the training runs.

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'

Raises:

Type Description
ValidationError

The plant is unknown.

Examples:

>>> from skcomplex.systems.engineering.control import KoopmanModelPredictiveControlSystem
>>> system = KoopmanModelPredictiveControlSystem('motor', controlled=False).prepare()
>>> [subsystem.label for subsystem in system.system_.systems], system.form_
(['controller', 'plant'], 'co-simulation')
Source code in src/skcomplex/systems/engineering/control/_koopman.py
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
def __init__(
    self,
    plant: str = 'duffing',
    *,
    controlled: bool = True,
    dictionary: TransformerMixin | None = None,
    x: float = 0.5,
    y: float = 0.0,
    n_runs: int = 100,
    duration: int = 1000,
    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.plant = plant
    self.controlled = controlled
    self.dictionary = dictionary
    self.x = x
    self.y = y
    self.n_runs = n_runs
    self.duration = duration
    self.random_state = random_state

LqrOscillatorSystem(x=1.0, *, omega=1.0, zeta=0.0, q=1.0, r=0.1, label='system', solver='auto', backend='auto')

Bases: BaseSystem

A harmonic oscillator that a linear-quadratic regulator brings to rest.

The gain K of the regulator minimises ∫ (q (x^2 + x'^2) + r force^2) dt for the linearisation of the oscillator, which linearization computes from the Riccati equation, and the state feedback sets force = -K (x, x'). A smaller weight r of the force gives a larger gain and a faster return to rest. The system has the displacement oscillator.x, the force oscillator.force, and the observable energy.

Parameters:

Name Type Description Default
x float

The initial displacement.

1.0
omega float

The natural frequency.

1.0
zeta float

The damping ratio without control.

0.0
q float

The weight of the displacement and the velocity in the cost.

1.0
r float

The weight of the force in the cost.

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.control import LqrOscillatorSystem
>>> [round(value, 3) for value in LqrOscillatorSystem().prepare().system_.interactions[0].gain]
[2.317, 3.825]
Source code in src/skcomplex/systems/engineering/control/_regulation.py
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
def __init__(
    self,
    x: float = 1.0,
    *,
    omega: float = 1.0,
    zeta: float = 0.0,
    q: float = 1.0,
    r: float = 0.1,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.x = x
    self.omega = omega
    self.zeta = zeta
    self.q = q
    self.r = r

MinimumTimeControlSystem(x=3.0, v=2.0, *, plant='free_particle', interval=0.1, label='system', solver='auto', backend='auto')

Bases: BaseSystem

A linear plant steered to the origin of its phase space in minimum time.

A sampled bang-bang control steers it. The plant follows x'' = a x + u with |u| <= 1: a free particle, x'' = u, a harmonic oscillator, x'' + x = u, or an unstable saddle, x'' - x = u. Pontryagin's maximum principle makes the minimum-time control bang-bang, u = ±1, with the switch on the curve of the two trajectories under u = 1 and u = -1 that reach the origin: the control is -1 above the curve and 1 below it. As in Dilão's examples, the controller samples the position and the velocity every interval and holds the control until the next sample, so the plant reaches a neighbourhood of the origin, whose size shrinks with the interval, and stays there. A free particle from (x, v) above the curve reaches the origin at the time v + 2 sqrt(x + v^2 / 2), about 6.47 for the defaults. Only the states between the lines y = -(x + 1) and y = -(x - 1) of the saddle are controllable, and from the others the plant escapes to infinity.

The system has the position plant.x, the velocity plant.x', and the control plant.u, which the samples sample set.

Parameters:

Name Type Description Default
x float

The initial position.

3.0
v float

The initial velocity.

2.0
plant str

The plant, 'free_particle', 'harmonic_oscillator', or 'saddle'.

'free_particle'
interval float

The time between two samples of the controller.

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'

Raises:

Type Description
ParameterError

The plant is unknown, or the interval is not positive.

Examples:

>>> from skcomplex.systems.engineering.control import MinimumTimeControlSystem
>>> MinimumTimeControlSystem(plant='saddle', x=0.5, v=0.0).prepare().system_.elements[0].u
-1.0
Source code in src/skcomplex/systems/engineering/control/_minimum_time_control.py
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
def __init__(
    self,
    x: float = 3.0,
    v: float = 2.0,
    *,
    plant: str = 'free_particle',
    interval: float = 0.1,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.x = x
    self.v = v
    self.plant = plant
    self.interval = interval

PIDInteraction(label, *, plant, integrator, kp=1.0, ki=0.0, kd=0.0, setpoint=0.0, control='u', measure='y', limits=None)

Bases: Interaction

Proportional-integral-derivative controller of a control input of a plant.

It sets the control input from the error of a measure. The error is e = setpoint - y for the measured state y, and the controller sets the control input to u = kp e + ki I - kd y', clipped to the limits, with the derivative taken on the measure. The integral I' = e of the error is the value value of the element bound to the role integrator, such as Element('memory', values={'value': 0.0}). The controller sets the control input of the plant after every solver step, so it holds within a step like a sampled controller. The derivative term reads the first derivative of the measure, so a measure with a derivative term is a state of order 2.

Parameters:

Name Type Description Default
label str

The label of the controller.

required
plant Role

The path of the element that holds the measure and the control input.

required
integrator Role

The path of the integrator of the error.

required
kp float

The proportional gain.

1.0
ki float

The integral gain.

0.0
kd float

The derivative gain.

0.0
setpoint float

The value the measure is kept at.

0.0
control str

The name of the adaptive parameter of the plant that the controller sets.

'u'
measure str

The name of the measured state of the plant.

'y'
limits tuple[float, float] | None

The lower and upper limit of the control input, or None for no limits.

None

Examples:

>>> from skcomplex.systems.engineering.control import PIDInteraction
>>> PIDInteraction('pid', plant='cart', integrator='memory', control='force', measure='theta', kp=40.0).kp
40.0
Source code in src/skcomplex/systems/engineering/control/_regulation.py
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
def __init__(
    self,
    label: str,
    *,
    plant: Role,
    integrator: Role,
    kp: float = 1.0,
    ki: float = 0.0,
    kd: float = 0.0,
    setpoint: float = 0.0,
    control: str = 'u',
    measure: str = 'y',
    limits: tuple[float, float] | None = None,
) -> None:
    super().__init__(label)
    self.plant = plant
    self.integrator = integrator
    self.kp = kp
    self.ki = ki
    self.kd = kd
    self.setpoint = setpoint
    self.control = control
    self.measure = measure
    self.limits = limits

PendulumSwingUpSystem(theta=0.0, omega0=0.5, *, omega=1.5652475842498528, mu=0.1, label='system', solver='auto', backend='auto')

Bases: DynamicalSystem

A pendulum swung up to its inverted position.

A bang-bang acceleration of its pivot swings it up. The pivot of an acrobat's pendulum moves horizontally with acceleration f'' = u, so the pendulum follows θ'' + ω^2 sin θ + u cos θ = 0. The control u = -μ sign(θ' cos θ) sign(E_s - E) pumps the energy E = θ'^2 / 2 - ω^2 cos θ towards E_s = ω^2, the energy of the inverted position, from below or from above. The system has the angle pendulum.theta, the position of the pivot pendulum.f, and their velocities.

Parameters:

Name Type Description Default
theta float

The initial angle from the hanging position.

0.0
omega0 float

The initial angular velocity.

0.5
omega float

The natural frequency sqrt(g / l), by default for g = 9.8 and l = 4.

1.5652475842498528
mu float

The size of the acceleration of the pivot.

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.control import PendulumSwingUpSystem
>>> PendulumSwingUpSystem(mu=0.2).prepare().system_.elements[0].values['mu']
0.2
Source code in src/skcomplex/systems/engineering/control/_control.py
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
def __init__(
    self,
    theta: float = 0.0,
    omega0: float = 0.5,
    *,
    omega: float = 1.5652475842498528,
    mu: float = 0.1,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label=label, solver=solver, backend=backend)
    self.theta = theta
    self.omega0 = omega0
    self.omega = omega
    self.mu = mu

PidInvertedPendulumSystem(theta=0.1, *, kp=40.0, ki=1.0, kd=8.0, M=1.0, m=0.1, l=0.5, label='system', solver='auto', backend='auto')

Bases: BaseSystem

An inverted pendulum on a cart that a PID controller of its angle keeps upright.

The controller sets the force on the cart to kp e + ki ∫ e dt - kd theta' from the error e = -theta of the angle, within the bounds of the force. Near upright the closed loop is stable when kp > (M + m) g and kd > 0, and the angle returns to upright while the cart drifts. The system has the angle cart.theta, the position cart.x, the force cart.force, and the observable tilt.

Parameters:

Name Type Description Default
theta float

The initial angle from upright.

0.1
kp float

The proportional gain.

40.0
ki float

The integral gain.

1.0
kd float

The derivative gain.

8.0
M float

The mass of the cart.

1.0
m float

The mass at the end of the rod.

0.1
l float

The length of the rod.

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.control import PidInvertedPendulumSystem
>>> PidInvertedPendulumSystem(kp=50.0).prepare().system_.interactions[0].kp
50.0
Source code in src/skcomplex/systems/engineering/control/_regulation.py
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
def __init__(
    self,
    theta: float = 0.1,
    *,
    kp: float = 40.0,
    ki: float = 1.0,
    kd: float = 8.0,
    M: float = 1.0,
    m: float = 0.1,
    l: float = 0.5,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.theta = theta
    self.kp = kp
    self.ki = ki
    self.kd = kd
    self.M = M
    self.m = m
    self.l = l

PidLoopSystem(y=0.0, *, tau=1.0, gain=1.0, Kp=2.0, Ki=1.0, Kd=0.1, setpoint=1.0, change=0.0, disturbance=-0.5, label='system', solver='auto', backend='auto')

Bases: DynamicalSystem

A work process whose output a PID controller keeps at a set point.

The controller acts against a disturbance. The output follows the first-order process τ y' = -y + gain u + d(t), and the controller sets u = Kp e + Ki ∫ e dt - Kd y' from the error e = r(t) - y, with the derivative taken on the output, so (τ + gain Kd) y' = -y + gain (Kp e + Ki ∫ e dt) + d(t). The disturbance d steps in at time 10, and a coordinator moves the set point r by change at time 20. The integral action removes the error that the disturbance leaves under proportional control alone. The system has the output process.y and the integral of the error process.integral.

Parameters:

Name Type Description Default
y float

The initial output.

0.0
tau float

The time constant of the process.

1.0
gain float

The gain of the actuator.

1.0
Kp float

The proportional gain.

2.0
Ki float

The integral gain.

1.0
Kd float

The derivative gain.

0.1
setpoint float

The initial set point.

1.0
change float

The change of the set point at time 20.

0.0
disturbance float

The size of the disturbance from time 10.

-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.control import PidLoopSystem
>>> PidLoopSystem(Ki=0.0).prepare().system_.elements[0].values['Ki']
0.0
Source code in src/skcomplex/systems/engineering/control/_control.py
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
def __init__(
    self,
    y: float = 0.0,
    *,
    tau: float = 1.0,
    gain: float = 1.0,
    Kp: float = 2.0,
    Ki: float = 1.0,
    Kd: float = 0.1,
    setpoint: float = 1.0,
    change: float = 0.0,
    disturbance: float = -0.5,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label=label, solver=solver, backend=backend)
    self.y = y
    self.tau = tau
    self.gain = gain
    self.Kp = Kp
    self.Ki = Ki
    self.Kd = Kd
    self.setpoint = setpoint
    self.change = change
    self.disturbance = disturbance

PlantInteraction(label, *, plant, control, kind='duffing')

Bases: Interaction

Rates of the two variables of a plant that reads a bounded control input.

The plant is the forced Duffing oscillator, the Van der Pol oscillator, or the bilinear DC motor of figure 6.6 of the review of Brunton et al., with one time unit per sample of the controller. The plant reads limit * u, so the input u of the control element stays between -1 and 1.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
plant Role

The path of the plant, with the variables x and y.

required
control Role

The path of the control input, with the value u.

required
kind str

'duffing', 'van_der_pol', or 'motor'.

'duffing'

Examples:

>>> from skcomplex.core.systems import System
>>> from skcomplex.systems.engineering.control import ControlInputElement, PlantInteraction, PlantStateElement
>>> elements = [PlantStateElement('plant'), ControlInputElement('input')]
>>> rates = PlantInteraction('rates', plant='plant', control='input')
>>> System(elements=elements, interactions=[rates]).prepare().states_
('plant.x', 'plant.y')
Source code in src/skcomplex/systems/engineering/control/_koopman.py
217
218
219
220
221
def __init__(self, label: str, *, plant: Role, control: Role, kind: str = 'duffing') -> None:
    super().__init__(label)
    self.plant = plant
    self.control = control
    self.kind = kind

PlantStateElement(label, *, x=0.0, y=0.0)

Bases: Element

State of a plant with two variables, whose rates PlantInteraction gives.

Parameters:

Name Type Description Default
label str

The label of the plant.

required
x float

The value of the first variable.

0.0
y float

The value of the second variable, the output that tracks the reference.

0.0

Examples:

>>> from skcomplex.systems.engineering.control import PlantStateElement
>>> PlantStateElement('plant', x=0.5).x
0.5
Source code in src/skcomplex/systems/engineering/control/_koopman.py
182
183
184
185
def __init__(self, label: str, *, x: float = 0.0, y: float = 0.0) -> None:
    super().__init__(label)
    self.x = x
    self.y = y

PredictiveControlInteraction(label, *, control, plant, policy=None)

Bases: Interaction

Controller that sets a control input once per sample from the two variables of a plant.

The next input is policy(t, x, y), such as the first input of a tracking problem solved over a horizon, or 0 without a policy. The input is the target of an update, so a system that holds the controller steps it once per unit of time while a continuous plant beside it holds the input between samples.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
control Role

The path of the control input, with the value u.

required
plant Role

The path of the plant, with the variables x and y.

required
policy Callable[[float, float, float], float] | None

The function f(t, x, y) of the next input, or None to hold the input at 0.

None

Examples:

>>> from skcomplex.core.systems import System
>>> from skcomplex.systems.engineering.control import ControlInputElement, PredictiveControlInteraction
>>> from skcomplex.systems.engineering.control import PlantStateElement
>>> elements = [ControlInputElement('input'), PlantStateElement('plant')]
>>> policy = PredictiveControlInteraction('policy', control='input', plant='plant')
>>> System(elements=elements, interactions=[policy]).prepare().form_
'discrete map'
Source code in src/skcomplex/systems/engineering/control/_koopman.py
348
349
350
351
352
353
354
355
356
357
358
359
def __init__(
    self,
    label: str,
    *,
    control: Role,
    plant: Role,
    policy: Callable[[float, float, float], float] | None = None,
) -> None:
    super().__init__(label)
    self.control = control
    self.plant = plant
    self.policy = policy

SatelliteTransferSystem(s=0.314, velocity=0.0, theta=0.0, *, Lz=0.7, epsilon=0.1, H_final=-0.2, label='system', solver='auto', backend='auto')

Bases: DynamicalSystem

A satellite moved between Kepler orbits by radial thrust.

The orbits have equal angular momentum. In normalised polar coordinates the satellite follows s'' = L_z^2 / s^3 - 1 / s^2 - ε sigma and θ' = L_z / s^2. Radial thrust keeps the angular momentum and changes the effective energy H = s'^2 / 2 + L_z^2 / (2 s^2) - 1 / s at the rate -ε sigma s', so the controller pushes inwards, sigma = 1, while the satellite falls and outwards, sigma = -1, while it rises, until H reaches the final energy. From the periapsis of the orbit with H = -0.7, the transfer to H = -0.2 takes about 9.81.

The system has the distance satellite.s, the radial velocity satellite.s', the angle satellite.theta, and the observable energy.

Parameters:

Name Type Description Default
s float

The initial distance to the planet.

0.314
velocity float

The initial radial velocity.

0.0
theta float

The initial angle.

0.0
Lz float

The angular momentum.

0.7
epsilon float

The thrust.

0.1
H_final float

The effective energy of the final orbit.

-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.control import SatelliteTransferSystem
>>> SatelliteTransferSystem().prepare().system_.elements[0].values['Lz']
0.7
Source code in src/skcomplex/systems/engineering/control/_control.py
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
def __init__(
    self,
    s: float = 0.314,
    velocity: float = 0.0,
    theta: float = 0.0,
    *,
    Lz: float = 0.7,
    epsilon: float = 0.1,
    H_final: float = -0.2,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label=label, solver=solver, backend=backend)
    self.s = s
    self.velocity = velocity
    self.theta = theta
    self.Lz = Lz
    self.epsilon = epsilon
    self.H_final = H_final

StateFeedbackInteraction(label, *, plant, control='u', states=(), gain=(), limits=None)

Bases: Interaction

Linear state feedback u = -K x of a control input of a plant.

It sets the control input from the states of the plant. The control input is an adaptive parameter of the plant, which the controller sets after every solver step, so it holds within a step like a sampled controller. A gain from linearization of the plant makes the controller a linear-quadratic regulator.

Parameters:

Name Type Description Default
label str

The label of the controller.

required
plant Role

The path of the element that holds the states and the control input.

required
control str

The name of the adaptive parameter of the plant that the controller sets.

'u'
states Sequence[str]

The names of the fed back states of the plant, with a prime for a derivative, such as ('x', "x'") for a position and its velocity.

()
gain Sequence[float]

The gain of every state, in the order of states.

()
limits tuple[float, float] | None

The lower and upper limit of the control input, or None for no limits.

None

Examples:

>>> from skcomplex.systems.engineering.control import StateFeedbackInteraction
>>> StateFeedbackInteraction(
...     'regulator', plant='mass', control='force', states=('x', "x'"), gain=(1.0, 1.7)
... ).gain
(1.0, 1.7)
Source code in src/skcomplex/systems/engineering/control/_regulation.py
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
def __init__(
    self,
    label: str,
    *,
    plant: Role,
    control: str = 'u',
    states: Sequence[str] = (),
    gain: Sequence[float] = (),
    limits: tuple[float, float] | None = None,
) -> None:
    super().__init__(label)
    self.plant = plant
    self.control = control
    self.states = states
    self.gain = gain
    self.limits = limits

SwitchInteraction(label, *, sensor, switch, threshold=0.0, value=0.0, direction=1)

Bases: Interaction

Switch that opens or closes when the temperature of a sensor crosses a threshold.

When the temperature T of sensor crosses threshold in direction, the value closed of switch becomes value, as a thermostat switches a heater or a pressure switch a pump.

Parameters:

Name Type Description Default
label str

The label of the interaction, which names its events.

required
sensor Role

The path of the element whose temperature T the switch watches.

required
switch Role

The path of the element that holds the switch closed.

required
threshold float

The temperature at which the switch acts.

0.0
value float

The value the switch takes, 1 for closed and 0 for open.

0.0
direction int

The direction of the crossing, 1 when the temperature rises and -1 when it falls.

1

Examples:

>>> from skcomplex.systems.engineering.control import SwitchInteraction
>>> SwitchInteraction('open_switch', sensor='room', switch='heater', threshold=20.5, value=0.0).direction
1
Source code in src/skcomplex/systems/engineering/control/_control.py
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
def __init__(
    self,
    label: str,
    *,
    sensor: Role,
    switch: Role,
    threshold: float = 0.0,
    value: float = 0.0,
    direction: int = 1,
) -> None:
    super().__init__(label)
    self.sensor = sensor
    self.switch = switch
    self.threshold = threshold
    self.value = value
    self.direction = direction

ThermalConductorInteraction(label, *, a, b, G=1.0)

Bases: Interaction

Conductor of the heat flow G (a.T - b.T) from the node a to the node b.

Parameters:

Name Type Description Default
label str

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

required
a Role

The path of the node the heat leaves.

required
b Role

The path of the node the heat enters.

required
G Annotated[float, POSITIVE]

The thermal conductance.

1.0

Examples:

>>> from skcomplex.systems.engineering.control import ThermalConductorInteraction
>>> ThermalConductorInteraction('wall', a='room', b='outside', G=2.0).G
2.0
Source code in src/skcomplex/systems/engineering/control/_control.py
 97
 98
 99
100
101
def __init__(self, label: str, *, a: Role, b: Role, G: Annotated[float, POSITIVE] = 1.0) -> None:
    super().__init__(label)
    self.a = a
    self.b = b
    self.G = G

ThermostatSystem(T=10.0, *, setting=18.0, furnace=5.0, leak=0.1, outside=10.0, swing=0.0, label='system', solver='auto', backend='auto')

Bases: DynamicalSystem

A room heated by a furnace under a thermostat while heat leaks to the outside.

The furnace adds the gap between the setting and the room temperature, at most furnace degrees per hour, and nothing when the room is warmer than the setting. The room loses leak of its difference to the outside temperature per hour, so it settles below the setting, at (setting + leak * outside) / (1 + leak) for a constant outside. The outside temperature swings by swing around outside over a day, warmest at noon. The system has the room temperature room.T in hours.

Parameters:

Name Type Description Default
T float

The initial room temperature.

10.0
setting float

The thermostat setting.

18.0
furnace float

The largest heating rate of the furnace, in degrees per hour.

5.0
leak float

The fraction of the difference to the outside lost per hour.

0.1
outside float

The mean outside temperature.

10.0
swing float

The amplitude of the daily swing of the outside temperature.

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.control import ThermostatSystem
>>> ThermostatSystem(leak=0.3).prepare().system_.elements[0].values['leak']
0.3
Source code in src/skcomplex/systems/engineering/control/_control.py
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
def __init__(
    self,
    T: float = 10.0,
    *,
    setting: float = 18.0,
    furnace: float = 5.0,
    leak: float = 0.1,
    outside: float = 10.0,
    swing: float = 0.0,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label=label, solver=solver, backend=backend)
    self.T = T
    self.setting = setting
    self.furnace = furnace
    self.leak = leak
    self.outside = outside
    self.swing = swing