Skip to content

Constraints ​

constraint! adds one constraint at a time to the pre-model. The first positional argument is the kind; the keywords give the bounds, the function or range, and a label used later to read back the constraint and its dual.

KindFormKeywords
:pathnonlinear   f, lb, ub, label
:boundarynonlinear   f, lb, ub, label
:statebox on state componentsrg, lb, ub, label
:controlbox on control componentsrg, lb, ub, label
:variablebox on variable componentsrg, lb, ub, label

Nonlinear constraint functions are written in place like the dynamics: f(r, t, x, u, v) for :path, f(r, x0, xf, v) for :boundary.

julia
using CTModels

pre = CTModels.PreModel()
CTModels.variable!(pre, 2, "v")
CTModels.time!(pre; t0=0.0, tf=1.0)
CTModels.state!(pre, 2)
CTModels.control!(pre, 1)
CTModels.dynamics!(pre, (r, t, x, u, v) -> (r[1] = x[2]; r[2] = u; nothing))
CTModels.objective!(pre, :min; lagrange=(t, x, u, v) -> u^2)

# Nonlinear path and boundary constraints
CTModels.constraint!(pre, :path;
    f=(r, t, x, u, v) -> (r[1] = x[1] + u; nothing), lb=[0.0], ub=[1.0], label=:p)
CTModels.constraint!(pre, :boundary;
    f=(r, x0, xf, v) -> (r[1] = x0[1]; r[2] = xf[1]; nothing),
    lb=[0.0, 0.0], ub=[0.0, 0.0], label=:bc)

# Box constraints on state, control and variable components
CTModels.constraint!(pre, :state;    rg=1:1, lb=[-1.0], ub=[1.0], label=:x1)
CTModels.constraint!(pre, :control;  rg=1:1, lb=[-10.0], ub=[10.0], label=:u)
CTModels.constraint!(pre, :variable; rg=1:2, lb=[0.0, 0.0], ub=[1.0, 1.0], label=:vbox)

CTModels.time_dependence!(pre; autonomous=true)
ocp = CTModels.build(pre)
The (autonomous) optimal control problem is of the form:

    minimize  J(x, u, v) = ∫ f⁰(x(t), u(t), v) dt, over [0.0, 1.0]

    subject to

        ẋ(t) = f(x(t), u(t), v), t in [0.0, 1.0] a.e.,

        ψ₋ ≤ ψ(x(t), u(t), v) ≤ ψ₊, 
        ϕ₋ ≤ ϕ(x(0.0), x(1.0), v) ≤ ϕ₊, 
        x₋ ≤ x(t) ≤ x₊, 
        u₋ ≤ u(t) ≤ u₊, 
        v₋ ≤ v ≤ v₊, 

    where x(t) ∈ R², u(t) ∈ R and v ∈ R².

Reading constraints back ​

On the built Model, the constraints are grouped in a ConstraintsModel and queried by dimension or by label:

julia
julia> CTModels.dim_path_constraints_nl(ocp)
1

julia> CTModels.dim_boundary_constraints_nl(ocp)
2

julia> CTModels.dim_state_constraints_box(ocp)
1

julia> CTModels.dim_control_constraints_box(ocp)
1

julia> CTModels.dim_variable_constraints_box(ocp)
2

The raw constraint tuples can also be retrieved:

julia> CTModels.path_constraints_nl(ocp)
([0.0], CompositeConstraint{:path}(n=1, dims=[1]), [1.0], [:p])

julia> CTModels.boundary_constraints_nl(ocp)
([0.0, 0.0], CompositeConstraint{:boundary}(n=1, dims=[2]), [0.0, 0.0], [:bc, :bc])

julia> CTModels.state_constraints_box(ocp)
([-1.0], [1], [1.0], [:x1], [[:x1]])

julia> CTModels.control_constraints_box(ocp)
([-10.0], [1], [10.0], [:u], [[:u]])

julia> CTModels.variable_constraints_box(ocp)
([0.0, 0.0], [1, 2], [1.0, 1.0], [:vbox, :vbox], [[:vbox], [:vbox]])

Each nonlinear tuple has the form (lb, f!, ub, labels): f! is the stacked in-place function of the group, which fills the rows of all its constraints in declaration order. It is always a CompositeConstraint, even when the group holds a single declaration, as :p here: its type and display do not depend on the number of constraints. n counts the declarations and dims gives the dimension of each.

Use isempty_constraints to check whether the model has any constraints at all.

Reading a constraint by label ​

constraint returns one declared constraint as a tuple (type, f, lb, ub). Unlike the stacked f! above, f is out of place: it returns the value of that constraint alone. It is a named functor, so it prints what it does:

julia> CTModels.constraint(ocp, :p)
(:path, SubPathConstraint(n=1, indices=1), 0.0, 1.0)

julia> CTModels.constraint(ocp, :bc)
(:boundary, SubBoundaryConstraint(n=2, indices=[1, 2]), [0.0, 0.0], [0.0, 0.0])

julia> CTModels.constraint(ocp, :x1)
(:state, BoxProjection{:state}(1), -1.0, 1.0)

julia> CTModels.constraint(ocp, :vbox)
(:variable, BoxProjection{:variable}([1, 2]), [0.0, 0.0], [1.0, 1.0])
typefCall
:pathSubPathConstraint: rows indices of the stacked path constraint, which has n rowsf(t, x, u, v)
:boundarySubBoundaryConstraint: same for the boundary constraintf(x0, xf, v)
:state, :controlBoxProjection: the bounded componentsf(t, x, u, v)
:variableBoxProjectionf(x0, xf, v)

A constraint of dimension one gives a scalar, and so do its bounds:

julia
julia> _, f, lb, ub = CTModels.constraint(ocp, :p);

julia> f(0.0, [0.25, 0.0], 0.5, [0.0, 0.0])  # x[1] + u
0.75

julia> (lb, ub)
(0.0, 1.0)

julia> _, g, _, _ = CTModels.constraint(ocp, :bc);

julia> g([1.0, 0.0], [3.0, 0.0], [0.0, 0.0])  # [x0[1], xf[1]]
2-element Vector{Float64}:
 1.0
 3.0

Labels and aliases ​

Before building, constraints live in a ConstraintsDictType keyed by label. Box constraints obey a per-component uniqueness invariant: if several declarations touch the same component, their bounds are intersected and all their labels are kept as aliases. This is what lets constraint and dual resolve any of the original labels to the merged component — see the Duals guide.

One declaration, one label

Always pass an explicit label. It is the stable handle for the constraint across build, solution reconstruction, and dual extraction; auto-generated labels are harder to track in downstream packages.

Error cases ​

Passing the same label twice raises a PreconditionError immediately:

julia
pre_dup = CTModels.PreModel()
CTModels.variable!(pre_dup, 0)
CTModels.time!(pre_dup; t0=0.0, tf=1.0)
CTModels.state!(pre_dup, 2)
CTModels.control!(pre_dup, 1)
CTModels.constraint!(pre_dup, :state; rg=1:1, lb=[-1.0], ub=[1.0], label=:x1)
julia> CTModels.constraint!(pre_dup, :control; rg=1:1, lb=[-10.0], ub=[10.0], label=:x1)  # duplicate
ERROR: PreconditionError
│
│  Constraint already exists
│
│  Reason   constraint with label 'x1' is already defined
│
│  Context  constraint! function - duplicate label validation
│  Hint     Use a different label or remove the existing constraint first
└─

Bounds with lb > ub are rejected on the spot:

julia> CTModels.constraint!(pre_dup, :state; rg=2:2, lb=[1.0], ub=[0.0], label=:bad)
ERROR: IncorrectArgument
│
│  Invalid bounds: lower > upper
│
│  Got       some lb[i] > ub[i] violations
│  Expected  lb[i] ≤ ub[i] for all i
│
│  Context   constraint!(ocp, type=:state, lb=[...], ub=[...]) - validating bounds order
│  Hint      Check bounds values: lb=[1.0,...] ≤ ub=[0.0,...]
└─