Skip to content

Constrained flows ​

Flow(ocp, law; constraint, multiplier) augments the pseudo-Hamiltonian with a constraint term, for OCPs whose Pontryagin's Maximum Principle carries a state or mixed constraint alongside the control. This chapter covers the API, the sign convention, the hamiltonian_type semantics for constrained flows (:total vs :partial, and why they agree on a constrained arc), and two worked examples.


The augmented pseudo-Hamiltonian ​

Given the OCP's own pseudo-Hamiltonian       (see Optimal control), a path constraint   with multiplier augments it as

Pass constraint/multiplier to Flow(ocp, law; …) alongside a DynClosedLoop law — they must be given together:

julia
# OCP: ẋ = -x + u, min ∫ 0.5*u^2 dt  (autonomous, fixed, 1-D — scalar convention)
pre = CTModels.Building.PreModel()
CTModels.Building.time_dependence!(pre; autonomous=true)
CTModels.Building.time!(pre; t0=0.0, tf=1.0)
CTModels.Building.state!(pre, 1)
CTModels.Building.control!(pre, 1)
CTModels.Building.dynamics!(pre, (r, t, x, u, v) -> (r[1] = -x + u; nothing))
CTModels.Building.objective!(pre, :min; lagrange=(t, x, u, v) -> 0.5 * u^2)
ocp = CTModels.Building.build(pre)

law = Data.DynClosedLoop((x, p) -> p)   # u = p (from ∂H̃/∂u = p - u = 0)
c = 0.3
f = Flows.Flow(
    ocp, law;
    multipliers=Data.StateConstraint(x -> x) => Data.Multiplier((x, p) -> c),
    reltol=1e-10,
)

A raw Function is accepted directly as the constraint or the multiplier of a pair (wrapped in a MixedConstraint/Multiplier with the OCP's own time/variable dependence, just like Flow(ocp, u::Function) — see Control laws):

julia
f2 = Flows.Flow(ocp, law; multipliers=((x, u) -> x) => ((x, p) -> c), reltol=1e-10)
xf, pf = f2(0.0, 1.0, 0.5, 1.0)
xf, pf
(0.7925558476344876, 0.8436563657430431)

A constraint of the OCP itself is given by its Symbol label: a path constraint, a state box or a control box (boundary constraints and variable boxes do not enter a flow). The model stores it as   , and the pseudo-Hamiltonian is built by CTModels.Models.pseudo_hamiltonian(ocp; constraints=…), with the multiplier in the dual convention of the solution (ct-conventions SGN2, SGN3, see Constraint duals of a flow): the term of each component is

Bounds stored in the modelTerm adjoined to Sign of
lower only,     
upper only,     
two bounds, or an equality  ,   if  , otherwise  on the lower bound,   on the upper

So x(t) - lb ≥ 0, x(t) ≥ lb, lb - x(t) ≤ 0 and the state box x(t) ≥ lb give the same flow (SGN5). On a two-sided constraint, the sign of carries the active side: the multiplier is the dual a direct solver returns, and can be passed to the flow as is. For a vector constraint, has one component per component of the constraint. (A Tuple of labels is a set of constraints, see Multiple constraints.)

The same constraint  , declared in the model as x ≥ 0.1 and as 0.1 - x ≤ 0, gives the flow of the function form   :

julia
function ocp_with(f!, lb, ub)
    pre = CTModels.Building.PreModel()
    CTModels.Building.time_dependence!(pre; autonomous=true)
    CTModels.Building.time!(pre; t0=0.0, tf=1.0)
    CTModels.Building.state!(pre, 1)
    CTModels.Building.control!(pre, 1)
    CTModels.Building.dynamics!(
        pre, (r, t, x, u, v) -> (r[1] = -x + u; nothing),
    )
    CTModels.Building.objective!(
        pre, :min; lagrange=(t, x, u, v) -> 0.5 * u^2,
    )
    CTModels.Building.constraint!(
        pre, :path; f=f!, lb=lb, ub=ub, label=:xcon,
    )
    return CTModels.Building.build(pre)
end

ocp_lower = ocp_with((r, t, x, u, v) -> (r[1] = x; nothing), 0.1, Inf)
ocp_upper = ocp_with((r, t, x, u, v) -> (r[1] = 0.1 - x; nothing), -Inf, 0.0)
μ(x, p) = c
f_lower = Flows.Flow(ocp_lower, law; multipliers=:xcon => μ)
f_upper = Flows.Flow(ocp_upper, law; multipliers=:xcon => μ)
f_fun = Flows.Flow(ocp, law; multipliers=((x, u) -> x - 0.1) => μ)
f_lower(0.0, 1.0, 0.5, 1.0), f_upper(0.0, 1.0, 0.5, 1.0), f_fun(0.0, 1.0, 0.5, 1.0)
((0.7925558476786874, 0.8436563657634168), (0.7925558476786874, 0.8436563657634168), (0.7925558476786874, 0.8436563657634168))

Sign convention ​

A constraint given by its label uses the dual convention of the solution (table above). A constraint given as a function uses   with     — the convention of a lower bound at . This is the opposite sign from Pontryagin's Maximum Principle as usually stated with a constraint   and multiplier   (Maurer's formulation): the two are related by

Under g ≥ 0, the multiplier is   on an active (boundary) arc, and   off it.


Multiple constraints ​

A single constraint carrier — a :path label or a raw function — can already be vector-valued (returning a vector, paired with a vector multiplier): the   pairing is dimension-agnostic, so this needs no special support.

To combine separately-defined constraints, pass tuples of matched length:

julia
g1(x, u) = x
g2(x, u) = 1.0 - x
μ1(x, p) = 0.4
μ2(x, p) = 0.1
ft = Flows.Flow(ocp, law; multipliers=(g1 => μ1, g2 => μ2), reltol=1e-10)

Each gᵢ/μᵢ is resolved independently; the flow sums  . Pairs of labels, multipliers=(:a => μa, :b => μb), adjoin those constraints of the model, each with its own multiplier, in the order given; a NamedTuple (; a=μa, b=μb), a Dict or a vector of pairs do the same, as for CTModels.Models.hamiltonian(ocp, u; multipliers). Labels and functions cannot be mixed in one call, and a constraint is given once — otherwise Flow throws a CTBase.Exceptions.IncorrectArgument.

One keyword

A constraint and its multiplier always go together, so they are given as one pair. The keywords constraint= and multiplier= of earlier versions are rejected, with the rewrite in the error: constraint=:a, multiplier=μ becomes multipliers=:a => μ. An anonymous function in a pair is written in parentheses, multipliers=((x, u) -> x) => ((x, p) -> 0.3).

For several labels, each multiplier is also checked against the number of rows of its constraint when it is evaluated: a number for a one-row constraint, a vector of components for a -row one. A multiplier of the wrong dimension throws an IncorrectArgument that names its label, even when the other multipliers make the total number of components right.


hamiltonian_type for constrained flows ​

The hamiltonian_type keyword (:total default, :partial) works as for unconstrained flows (see Control laws), extended to the constraint term:

Both modes share the same constrained pseudo-Hamiltonian , with the multiplier as an explicit argument (a CTBase.Data.ConstrainedPseudoHamiltonian):

  • :total: differentiate the CTBase.Data.ComposedConstrainedHamiltonian  , through the law and the multiplier (total derivative in , , ).

  • :partial: freeze both the control   and the multiplier   at their feedback values, then differentiate (CTBase.Data.freeze_multiplier) — the constraint is differentiated in , is not.

The accessors return the same objects in both modes: pseudo_hamiltonian(f) is , with and explicit; hamiltonian(f) is , including the constraint term; and pseudo_hamiltonian_gradient(f) gives the partial derivatives of the PMP at fixed and , called as (t, x, p, u, μ, v) or in the short form of the traits:

julia
fc = Flows.Flow(ocp, law; multipliers=((x, u) -> x) => ((x, p) -> c), reltol=1e-10)
H = CTFlows.Systems.pseudo_hamiltonian(fc)  # H(x, p, u, μ) = p(-x+u) - u²/2 + μx
∇H = CTFlows.Systems.pseudo_hamiltonian_gradient(fc)  # at fixed (u, μ)
H(1.0, 0.5, 0.2, c), ∇H(1.0, 0.5, 0.2, c)
(-0.12000000000000005, (-0.2, -0.8))
julia
ft_ = Flows.Flow(
    ocp, law;
    multipliers=Data.StateConstraint(x -> x) => Data.Multiplier((x, p) -> c),
    hamiltonian_type=:total, reltol=1e-10,
)
fp_ = Flows.Flow(
    ocp, law;
    multipliers=Data.StateConstraint(x -> x) => Data.Multiplier((x, p) -> c),
    hamiltonian_type=:partial, reltol=1e-10,
)
x0, p0, tf = 1.0, 0.5, 1.0
ft_(0.0, x0, p0, tf), fp_(0.0, x0, p0, tf)   # constant μ, stationary law ⇒ agree
((0.7925558476344876, 0.8436563657430431), (0.7925558476344876, 0.8436563657430431))

Why :total and :partial agree on a constrained arc ​

Pontryagin's Maximum Principle with a state constraint   (Maurer's formulation;    in this package's convention — see above) gives a pseudo-Hamiltonian      . On an active arc the true (closed-loop) Hamiltonian bakes in the optimal control and multiplier ( ); its symplectic gradient decomposes as

:partial is exactly — the genuine PMP field with the control and multiplier frozen at their feedback values — correct everywhere. :total adds the two extra chain-rule terms. Both vanish on the arc:

  •   vanishes because   on the arc (any constraint order).

  •   vanishes either because   (an invariant of the constrained dynamics — order 1: ; order 2: a richer invariant), or because   (order 2: a constant control on the arc).

So :total and :partial coincide on a constrained arc, and a shooting method's entry conditions put the trajectory exactly there — this is why both modes converge to the same solution in practice, as the two worked examples below demonstrate.


Plotting a constrained trajectory ​

A constrained Flow(ocp, law; constraint, multiplier) with a DynClosedLoop law is an OptimalControlFlow, so a trajectory call returns a CTModels.Solution with the control reconstructed from the law — plot it directly once Plots is loaded:

julia
sol = f((0.0, 1.0), 1.0, 0.5)   # trajectory call on the constrained flow f
plot(sol)


Constraint duals of a flow ​

The trajectory call of Flow(ocp, law; multipliers=:label => y) returns a CTModels.Solution that carries the duals of the constraints given by label (path constraints, state boxes, control boxes): the multiplier given to the flow is the dual. They are read as for a direct solve, with dual(sol, ocp, :label), and they follow the same convention: the sign convention of the ecosystem, ct-conventions SGN1–SGN6 (see also the Multiplier Convention page of CTSolvers and OptimalControl.jl#1014).

The convention ​

The pseudo-Hamiltonian is always maximised, for a minimisation and for a maximisation:

with    for :min and    for :max (normal case, SGN4). For min ∫ℓ and max ∫(-ℓ) the term is the same, so the control, the costate and the multipliers are the same: only the criterion changes sign. This is the indirect counterpart of the rule of the direct solvers, which return the multipliers of the equivalent minimisation.

The dual of a constraint has a single sign when the constraint has a single bound, however it is written: x ≥ 0.1, x - 0.1 ≥ 0, 0.1 - x ≤ 0 and the state box x ≥ 0.1 have the same dual  . An equality, or a constraint with two finite bounds, has a signed dual, as in the direct solvers (SGN2, SGN3):

Constraint in the modeldual(sol, ocp, :label)
single bound,   or   
equality, or two-sided, lower bound active 
equality, or two-sided, upper bound active 

In every case dual(sol, ocp, :label) is the multiplier given to the flow. The rows of a label are taken from the model, so the label is the key: a constraint given as a function has no label, and its dual cannot be placed (see below). On an arc where a path constraint is not adjoined to the flow, its dual is .

A closed form ​

 ,   ,    , with the mixed constraint    active on the whole arc:   ,    and the multiplier of      is    .

julia
pre = CTModels.Building.PreModel()
CTModels.Building.time_dependence!(pre; autonomous=true)
CTModels.Building.time!(pre; t0=0.0, tf=1.0)
CTModels.Building.state!(pre, 1)
CTModels.Building.control!(pre, 1)
CTModels.Building.dynamics!(pre, (r, t, x, u, v) -> (r[1] = u; nothing))
CTModels.Building.objective!(pre, :min; lagrange=(t, x, u, v) -> -u)
CTModels.Building.constraint!(
    pre, :path; f=(r, t, x, u, v) -> (r[1] = x + u; nothing), lb=-Inf, ub=0.0,
    label=:mixed,
)
ocp_mixed = CTModels.Building.build(pre)

f_mixed = Flows.Flow(
    ocp_mixed, (x, p) -> -x; multipliers=:mixed => ((x, p) -> p + 1),
    reltol=1e-10, abstol=1e-10,
)
sol_mixed = f_mixed((0.0, 1.0), -1.0, exp(-1) - 1)
d = CTModels.dual(sol_mixed, ocp_mixed, :mixed)
[(t, d(t), exp(t - 1)) for t in (0.0, 0.5, 1.0)]
3-element Vector{Tuple{Float64, Float64, Float64}}:
 (0.0, 0.36787944117144233, 0.36787944117144233)
 (0.5, 0.6065306597118532, 0.6065306597126334)
 (1.0, 1.0000000000022098, 1.0)

A direct method returns the same dual, up to the discretisation error.

Several arcs: the dual jumps at the junctions ​

With a multi-phase flow, each phase brings the duals of its own constraints, and the switching structure of the solution stores the left and right limits of the duals at each switching time, so the plot draws the jump of the multiplier at the entry and the exit of a boundary arc. The problem of issue #440:  ,  ,  ,  ,  ,  ; on the boundary arc ,   and   .

julia
pre = CTModels.Building.PreModel()
CTModels.Building.time_dependence!(pre; autonomous=true)
CTModels.Building.time!(pre; t0=0.0, tf=2.0)
CTModels.Building.state!(pre, 1)
CTModels.Building.control!(pre, 1)
CTModels.Building.dynamics!(pre, (r, t, x, u, v) -> (r[1] = u; nothing))
CTModels.Building.objective!(pre, :min; lagrange=(t, x, u, v) -> x^2)
CTModels.Building.constraint!(
    pre, :path; f=(r, t, x, u, v) -> (r[1] = x; nothing), lb=0.1, ub=Inf, label=:xmin,
)
ocp_440 = CTModels.Building.build(pre)

opts = (; reltol=1e-10, abstol=1e-10)
f_minus = Flows.Flow(ocp_440, (x, p) -> -1; label="u = -1", opts...)
f_bound = Flows.Flow(
    ocp_440, (x, p) -> 0; multipliers=:xmin => ((x, p) -> 2x), label="boundary",
    opts...,
)
f_plus = Flows.Flow(ocp_440, (x, p) -> 1; label="u = +1", opts...)
φ = f_minus * (0.9, f_bound) * (1.6, f_plus)
sol_440 = φ((0.0, 2.0), 1.0, -0.99)   # p(0) = -∫₀^0.9 2(1 - t) dt: p vanishes at the entry
d = CTModels.dual(sol_440, ocp_440, :xmin)
d(0.5), d(1.2), d(1.8)
(0.0, 0.20000000000000048, 0.0)
julia
plot(sol_440, :state, :control, :path, :dual)

Without a label, and the other constraints ​

  • A constraint given as a function (or as an explicit StateConstraint, MixedConstraint, …) has no label: the solution carries no dual, and dual(sol, ocp, :label) throws a PreconditionError that says so. When the problem has path constraints, the flow warns once when it is built. Give the constraint by its label, multipliers=:label => y, to get its dual. In a multi-phase flow, one unlabelled phase is enough to lose the duals of the whole solution.

  • A state or control box given to the flow by its label has its dual in the solution. In a multi-phase flow, a state box adjoined in some phases has a zero dual in the others; a control box is known only when every phase adjoins it.

  • The duals of the boundary constraints, of the variable boxes and of the boxes not given to the flow are not rebuilt: dual(sol, ocp, :label) throws a PreconditionError for them. A zero would be wrong: a boundary constraint is always active, and a control box is active on a bang arc, where the law saturates the control. How to compute them is discussed in CTFlows.jl#495.

  • The error says why the dual is missing and how to get it: the flow leaves this explanation in infos(sol)[:missing_duals], which dual uses as its suggestion. An unknown label throws an IncorrectArgument, as for any solution.


Worked example: Goddard problem (order 1) ​

The Goddard rocket problem (maximise final mass under a velocity path constraint, free final time) has four arcs — two bang arcs ( ,  ), a singular arc, and a velocity-boundary arc where the state constraint is active. On the boundary arc,   is a genuine invariant of the order-1 constrained dynamics (): both :total and :partial converge, from the known solution and from a perturbed Newton restart, to the same shooting solution (residual < 1e-7 in both modes). This is also an end-to-end integration test of the whole convenience surface — raw-function control law, raw-function constraint (MixedConstraint,  ), raw-function multiplier, and a NonFixed free final time — built directly from a CTModels.Model and rebuilt across all four arcs with Flow(ocp, law; constraint, multiplier), then reconstructed across phases with the * multi-phase operator (see Multi-phase flows) into a single CTModels.Solution. See test/suite/integration/test_goddard_ocp.jl in the repository.

Worked example: double integrator (order 2) ​

The time-minimal double integrator with a second-order position constraint   ( ) has a boundary arc where the constrained dynamics sit in a different corner of the theory: the boundary control is constant ( ) and the multiplier is identically zero ( ) on the arc — so H_1\,u_c' vanishes because   rather than because  , a distinct reason for the same :total ≡ :partial equivalence. The costate jumps at the two junctions between the interior and boundary arcs (f1 * (t1, jump, f2), see Multi-phase flows), and the reconstructed multi-phase solution's control is exactly on the boundary arc and non-zero on both interior arcs. Ported from the OptimalControl.jl state-constraint example. See test/cron/integration/test_double_integrator_state.jl (part of the weekly test suite).


See also ​