Skip to content

Public API

This page lists exported symbols of CTModels.Building.


From CTModels.Building

CTModels.Building [Module]

CTModels.Building Module
julia
Building

Building module for CTModels — assembles CTModels.Building.PreModel (mutable problem under construction), all component mutators, and the CTModels.Building.build / CTModels.Building.build_model functions that convert a finished CTModels.Building.PreModel into an immutable CTModels.Models.Model.

Organisation

See also: CTModels.Components, CTModels.Models.

PreModel [Struct]

CTModels.Building.PreModel Type
julia
mutable struct PreModel <: CTModels.Models.AbstractModel

Mutable optimal control problem model under construction.

A PreModel is used to incrementally define an optimal control problem before building it into an immutable CTModels.Models.Model. Fields can be set in any order and the model is validated before building.

Fields

  • times::Union{AbstractTimesModel,Nothing}: Initial and final time specification.

  • state::Union{AbstractStateModel,Nothing}: State variable structure.

  • control::AbstractControlModel: Control variable structure (defaults to EmptyControlModel(), i.e. no control).

  • variable::AbstractVariableModel: Optimisation variable (defaults to empty).

  • dynamics::Union{Function,Vector,Nothing}: System dynamics (function or component-wise).

  • objective::Union{AbstractObjectiveModel,Nothing}: Cost functional.

  • constraints::ConstraintsDictType: Dictionary of constraints being built.

  • definition::AbstractDefinition: Symbolic definition; defaults to CTModels.Components.EmptyDefinition and becomes a CTModels.Components.Definition when definition! is called with a real expression.

  • autonomous::Union{Bool,Nothing}: Whether the system is autonomous.

Example

julia
julia> using CTModels

julia> pre = CTModels.PreModel()
julia> # Set fields incrementally...

append_box_constraints! [Function]

CTModels.Building.append_box_constraints! Function
julia
append_box_constraints!(
    inds,
    lbs,
    ubs,
    labels,
    rg,
    lb,
    ub,
    label
)

Append box constraint data to the provided flat vectors.

This is an internal helper used by CTModels.Building.build. It simply accumulates declarations. Deduplication (one entry per component with intersection semantics) and associated warnings are handled later by CTModels.Building._dedup_box_constraints!.

Arguments

  • inds::Vector{Int}: Vector of component indices to append to.

  • lbs::Vector{<:Real}: Vector of lower bounds to append to.

  • ubs::Vector{<:Real}: Vector of upper bounds to append to.

  • labels::Vector{Symbol}: Vector of labels (one entry per declared component).

  • rg::AbstractVector{Int}: Component indices declared by the new constraint.

  • lb::AbstractVector{<:Real}: Lower bounds associated with rg.

  • ub::AbstractVector{<:Real}: Upper bounds associated with rg.

  • label::Symbol: Label describing the declaration.

Notes

Returns

  • Nothing

build [Function]

CTModels.Building.build Function
julia
build(
    constraints::OrderedCollections.OrderedDict{Symbol, Tuple{Symbol, Union{Function, OrdinalRange{<:Int64}}, AbstractVector{<:Real}, AbstractVector{<:Real}}}
) -> CTModels.Components.ConstraintsModel{TP, TB, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}, Vector{Vector{Symbol}}}, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}, Vector{Vector{Symbol}}}, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}, Vector{Vector{Symbol}}}} where {TP<:Tuple{Vector{Float64}, Function, Vector{Float64}, Vector{Symbol}}, TB<:Tuple{Vector{Float64}, Function, Vector{Float64}, Vector{Symbol}}}

Constructs a CTModels.Components.ConstraintsModel from a dictionary of constraints.

This function processes a dictionary where each entry defines a constraint with its type, function or index range, lower and upper bounds, and label. It categorizes constraints into path, boundary, state, control, and variable constraints, assembling them into a structured CTModels.Components.ConstraintsModel.

Arguments

  • constraints::CTModels.Components.ConstraintsDictType: A dictionary mapping constraint labels to tuples of the form (type, function_or_range, lower_bound, upper_bound).

Returns

  • CTModels.Components.ConstraintsModel: A structured model encapsulating all provided constraints.

Example

julia
using CTModels.Building
using OrderedCollections

f1(t, x, u, v) = x[1]
constraints = OrderedDict(
    :c1 => (:path, f1, [0.0], [1.0]),
    :c2 => (:state, 1:2, [-1.0, -1.0], [1.0, 1.0])
)
model = build(constraints)

Throws

  • CTBase.Exceptions.IncorrectArgument: If an unknown constraint type is encountered

See also: CTModels.Building.append_box_constraints!, CTModels.Building._dedup_box_constraints!

julia
build(
    pre_ocp::CTModels.Building.PreModel;
    build_examodel
) -> CTModels.Models.Model{TD, var"#s179", var"#s1791", var"#s1792", var"#s1793", var"#s1794", var"#s1795", CTModels.Components.ConstraintsModel{TP, TB, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}, Vector{Vector{Symbol}}}, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}, Vector{Vector{Symbol}}}, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}, Vector{Vector{Symbol}}}}, <:CTModels.Components.AbstractDefinition, Nothing} where {TD<:CTBase.Traits.TimeDependence, var"#s179"<:CTModels.Components.AbstractTimesModel, var"#s1791"<:CTModels.Components.AbstractStateModel, var"#s1792"<:CTModels.Components.AbstractControlModel, var"#s1793"<:CTModels.Components.AbstractVariableModel, var"#s1794"<:Function, var"#s1795"<:CTModels.Components.AbstractObjectiveModel, TP<:Tuple{Vector{Float64}, Function, Vector{Float64}, Vector{Symbol}}, TB<:Tuple{Vector{Float64}, Function, Vector{Float64}, Vector{Symbol}}}

Converts a mutable CTModels.Building.PreModel into an immutable CTModels.Models.Model.

This function finalizes a pre-defined optimal control problem (CTModels.Building.PreModel) by verifying that all necessary components (times, state, dynamics, objective) are set. It then constructs a CTModels.Models.Model instance, incorporating optional components like control, variable, and constraints.

Note

Control is optional: calling CTModels.Building.control! is not required. When omitted, the model is built with control_dimension == 0 (an CTModels.Components.EmptyControlModel). This is useful for problems where the dynamics depend only on the state, such as pure state-space systems.

Arguments

  • pre_ocp::CTModels.Building.PreModel: The pre-defined optimal control problem to be finalized.

  • build_examodel=nothing: Optional ExaModel builder function for GPU acceleration.

Returns

  • CTModels.Models.Model: A fully constructed model ready for solving.

Examples

Minimal Mayer problem (no control):

julia
using CTModels

pre = CTModels.PreModel()
CTModels.variable!(pre, 0)
CTModels.time!(pre; t0=0.0, tf=1.0)
CTModels.state!(pre, 2, "x", ["x1", "x2"])
CTModels.dynamics!(pre, (r, t, x, u, v) -> (r[1] = -x[2]; r[2] = x[1]; nothing))
CTModels.objective!(pre, :min; mayer=(x0, xf, v) -> xf[1]^2)
CTModels.time_dependence!(pre; autonomous=true)
model = CTModels.build(pre)
CTModels.control_dimension(model)  # 0

Bolza problem with control:

julia
using CTModels

pre = CTModels.PreModel()
CTModels.variable!(pre, 0)
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[1]; nothing))
CTModels.objective!(pre, :min; lagrange=(t, x, u, v) -> u[1]^2)
CTModels.time_dependence!(pre; autonomous=true)
model = CTModels.build(pre)

Throws

  • CTBase.Exceptions.PreconditionError: If times, state, dynamics, objective, or time dependence are not set

  • CTBase.Exceptions.PreconditionError: If dynamics are incomplete

See also: CTModels.Building.build_model, CTModels.Building.PreModel, CTModels.Models.Model

build_model [Function]

CTModels.Building.build_model Function
julia
build_model(
    pre_ocp::CTModels.Building.PreModel;
    build_examodel
) -> CTModels.Models.Model{TD, var"#s179", var"#s1791", var"#s1792", var"#s1793", var"#s1794", var"#s1795", CTModels.Components.ConstraintsModel{TP, TB, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}, Vector{Vector{Symbol}}}, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}, Vector{Vector{Symbol}}}, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}, Vector{Vector{Symbol}}}}, <:CTModels.Components.AbstractDefinition, Nothing} where {TD<:CTBase.Traits.TimeDependence, var"#s179"<:CTModels.Components.AbstractTimesModel, var"#s1791"<:CTModels.Components.AbstractStateModel, var"#s1792"<:CTModels.Components.AbstractControlModel, var"#s1793"<:CTModels.Components.AbstractVariableModel, var"#s1794"<:Function, var"#s1795"<:CTModels.Components.AbstractObjectiveModel, TP<:Tuple{Vector{Float64}, Function, Vector{Float64}, Vector{Symbol}}, TB<:Tuple{Vector{Float64}, Function, Vector{Float64}, Vector{Symbol}}}

Build a complete optimal control problem model from a pre-model.

This function is an alias for CTModels.Building.build and constructs a fully validated CTModels.Models.Model from a CTModels.Building.PreModel by extracting and organizing all components (times, state, control, variable, dynamics, objective, constraints).

Arguments

  • pre_ocp::CTModels.Building.PreModel: The pre-model containing all problem components

  • build_examodel=nothing: Optional ExaModel builder function for GPU acceleration

Returns

  • CTModels.Models.Model: A complete, validated optimal control problem model

Throws

Example

julia
using CTModels.Building

# Create and configure a pre-model
pre_ocp = PreModel()
time_dependence!(pre_ocp, autonomous=true)
state!(pre_ocp, 2)
control!(pre_ocp, 1)
dynamics!(pre_ocp, (x, u) -> [x[2], u[1]])
objective!(pre_ocp, :mayer, (x0, xf) -> xf[1]^2)

# Build the model
ocp = build_model(pre_ocp)

See also: CTModels.Building.build, CTModels.Building.PreModel, CTModels.Models.Model, CTModels.Building.time_dependence!.

constraint! [Function]

CTModels.Building.constraint! Function
julia
constraint!(
    ocp::CTModels.Building.PreModel,
    type::Symbol;
    rg,
    f,
    lb,
    ub,
    label,
    codim_f
)

Add a constraint to a pre-model. See CTModels.Building.__constraint! for more details.

Arguments

  • ocp: The pre-model to which the constraint will be added.

  • type: The type of the constraint. It can be :state, :control, :variable, :boundary, or :path.

  • rg: The range of the constraint. It can be an integer or a range of integers.

  • f: The function that defines the constraint. It must return a vector of the same dimension as the constraint.

  • lb: The lower bound of the constraint. It can be a number or a vector.

  • ub: The upper bound of the constraint. It can be a number or a vector.

  • label: The label of the constraint. It must be unique in the pre-model.

Examples

julia
julia> using CTModels

julia> ocp = CTModels.PreModel(); CTModels.variable!(ocp, 0); CTModels.time!(ocp; t0=0, tf=1);

julia> CTModels.state!(ocp, 2); CTModels.control!(ocp, 2);

julia> CTModels.constraint!(ocp, :control; rg=1:2, lb=[-1.0, -1.0], ub=[1.0, 1.0], label=:u_box);

Throws

  • Exceptions.PreconditionError: If state has not been set

  • Exceptions.PreconditionError: If times has not been set

  • Exceptions.PreconditionError: If control has not been set and type == :control

  • Exceptions.PreconditionError: If variable has not been set (when type=:variable)

  • Exceptions.PreconditionError: If constraint with same label already exists

  • Exceptions.PreconditionError: If both lb and ub are nothing

  • Exceptions.IncorrectArgument: If lb and ub have different lengths

  • Exceptions.IncorrectArgument: If lb > ub element-wise

  • Exceptions.IncorrectArgument: If dimensions don't match expected sizes

Returns

  • Nothing

See also: CTModels.Building.state!, CTModels.Building.control!, CTModels.Building.variable!.

Note

Control is only required for type == :control constraints. All other types (:state, :boundary, :path, :variable) are valid even when no control is defined (control dimension 0).

control! [Function]

CTModels.Building.control! Function
julia
control!(ocp::CTModels.Building.PreModel, m::Int64)
control!(
    ocp::CTModels.Building.PreModel,
    m::Int64,
    name::Union{String, Symbol}
)
control!(
    ocp::CTModels.Building.PreModel,
    m::Int64,
    name::Union{String, Symbol},
    components_names::Array{T2<:Union{String, Symbol}, 1}
)

Define the control input for a given optimal control problem model.

This function sets the control dimension and optionally allows specifying the control name and the names of its components.

Note

This function should be called only once per model. Calling it again will raise an error.

Arguments

  • ocp::PreModel: The model to which the control will be added.

  • m::Dimension: The control input dimension (must be greater than 0).

  • name::Union{String,Symbol} (optional): The name of the control variable (default: "u").

  • components_names::Vector{<:Union{String,Symbol}} (optional): Names of the control components (default: automatically generated).

Examples

julia
julia> using CTModels

julia> ocp = CTModels.PreModel(); CTModels.control!(ocp, 1);

julia> CTModels.control_dimension(ocp), CTModels.control_components(ocp)
(1, ["u"])

julia> ocp = CTModels.PreModel(); CTModels.control!(ocp, 1, "v");

julia> CTModels.control_components(ocp)
["v"]

julia> ocp = CTModels.PreModel(); CTModels.control!(ocp, 2);

julia> CTModels.control_components(ocp)
["u₁", "u₂"]

julia> ocp = CTModels.PreModel(); CTModels.control!(ocp, 2, "v", ["a", "b"]);

julia> CTModels.control_components(ocp)
["a", "b"]

Throws

  • Exceptions.PreconditionError: If control has already been set

  • Exceptions.IncorrectArgument: If m ≤ 0

  • Exceptions.IncorrectArgument: If number of component names ≠ m

  • Exceptions.IncorrectArgument: If name is empty

  • Exceptions.IncorrectArgument: If any component name is empty

  • Exceptions.IncorrectArgument: If name is one of the component names

  • Exceptions.IncorrectArgument: If component names contain duplicates

  • Exceptions.IncorrectArgument: If name conflicts with existing names in other components

  • Exceptions.IncorrectArgument: If any component name conflicts with existing names

Returns

  • Nothing

See also: CTModels.Building.state!, CTModels.Building.variable!, CTModels.Building.dynamics!, CTModels.Models.control_dimension.

definition! [Function]

CTModels.Building.definition! Function
julia
definition!(
    ocp::CTModels.Building.PreModel,
    definition::Expr
)

Set the model definition of the optimal control problem from a raw Expr.

The expression is wrapped in a CTModels.Components.Definition and stored on the pre-model.

Arguments

  • ocp::PreModel: The pre-model to modify.

  • definition::Expr: The symbolic expression defining the problem.

Returns

  • Nothing
julia
definition!(
    ocp::CTModels.Building.PreModel,
    definition::CTModels.Components.AbstractDefinition
)

Set the model definition of the optimal control problem from an existing CTModels.Components.AbstractDefinition value (either a CTModels.Components.Definition or an CTModels.Components.EmptyDefinition).

Arguments

  • ocp::PreModel: The pre-model to modify.

  • definition::AbstractDefinition: The definition value to store.

Returns

  • Nothing

dynamics! [Function]

CTModels.Building.dynamics! Function
julia
dynamics!(ocp::CTModels.Building.PreModel, f::Function)

Set the full dynamics of the optimal control problem ocp using the in-place function f.

The dynamics have the signature f!(r, t, x, u, v) where r is the output buffer (filled in-place), t is the time, x the state, u the control (or nothing for control-free problems), and v the optimisation variable.

Arguments

  • ocp::PreModel: The optimal control problem being defined.

  • f::Function: In-place function f!(r, t, x, u, v) defining the complete dynamics.

Returns

  • Nothing

Throws

  • Exceptions.PreconditionError: If state has not been set yet.

  • Exceptions.PreconditionError: If times have not been set yet.

  • Exceptions.PreconditionError: If dynamics have already been set.

See also: CTModels.Building.objective!, CTModels.Building.time_dependence!.

julia
dynamics!(
    ocp::CTModels.Building.PreModel,
    rg::AbstractRange{<:Int64},
    f::Function
)

Add a partial dynamics function for a range of state indices in ocp.

The partial right-hand side fills r[1:length(rg)] (local buffer view). Ranges must tile 1:n without overlap; completeness is verified by CTModels.Building.build via CTModels.Building.__is_dynamics_complete.

Arguments

  • ocp::PreModel: The optimal control problem being defined.

  • rg::AbstractRange{<:Int}: State index range covered by f.

  • f::Function: In-place function f!(r, t, x, u, v) updating r[1:length(rg)].

Returns

  • Nothing

Throws

  • Exceptions.PreconditionError: If state or times have not been set yet.

  • Exceptions.PreconditionError: If complete dynamics have already been set.

  • Exceptions.PreconditionError: If rg overlaps with an existing dynamics range.

  • Exceptions.IncorrectArgument: If any index in rg is out of bounds.

See also: CTModels.Building.dynamics!, CTModels.Building.objective!.

julia
dynamics!(
    ocp::CTModels.Building.PreModel,
    i::Integer,
    f::Function
)

Convenience wrapper: add partial dynamics for a single state index i.

Equivalent to CTModels.Building.dynamics!(ocp, i:i, f).

Arguments

  • ocp::PreModel: The optimal control problem being defined.

  • i::Integer: State index covered by f.

  • f::Function: In-place function f!(r, t, x, u, v) updating r[1].

Returns

  • Nothing

Throws

  • Exceptions.PreconditionError: If state, times, or dynamics preconditions are violated.

  • Exceptions.IncorrectArgument: If i is out of bounds.

See also: CTModels.Building.dynamics! (range-based version).

objective! [Function]

CTModels.Building.objective! Function
julia
objective!(ocp::CTModels.Building.PreModel; ...)
objective!(
    ocp::CTModels.Building.PreModel,
    criterion::Symbol;
    mayer,
    lagrange
)

Set the objective of the optimal control problem.

Arguments

  • ocp::PreModel: the optimal control problem.

  • criterion::Symbol: the type of criterion. Either :min, :max, :MIN, or :MAX (case-insensitive). Default is :min.

  • mayer::Union{Function, Nothing}: the Mayer function (inplace). Default is nothing.

  • lagrange::Union{Function, Nothing}: the Lagrange function (inplace). Default is nothing.

Note

  • The state and times must be set before the objective.

  • Control is optional: problems without control input (dimension 0) are fully supported.

  • The objective must not be set before.

  • At least one of the two functions must be given. Please provide a Mayer or a Lagrange function.

Examples

julia
julia> using CTModels

julia> ocp = CTModels.PreModel()

julia> CTModels.state!(ocp, 1); CTModels.control!(ocp, 1); CTModels.variable!(ocp, 1); CTModels.time!(ocp; t0=0, tf=1);

julia> mayer(x0, xf, v)    = x0[1] + xf[1] + v[1]
julia> lagrange(t, x, u, v) = x[1]  + u[1]  + v[1]

julia> CTModels.objective!(ocp, :min; mayer=mayer, lagrange=lagrange);

Throws

  • Exceptions.PreconditionError: If state has not been set

  • Exceptions.PreconditionError: If times has not been set

  • Exceptions.PreconditionError: If objective has already been set

  • Exceptions.IncorrectArgument: If criterion is not :min, :max, :MIN, or :MAX

  • Exceptions.IncorrectArgument: If neither mayer nor lagrange function is provided

Returns

  • Nothing

See also: CTModels.Building.dynamics!, CTModels.Building.state!, CTModels.Building.time!.

state! [Function]

CTModels.Building.state! Function
julia
state!(ocp::CTModels.Building.PreModel, n::Int64)
state!(
    ocp::CTModels.Building.PreModel,
    n::Int64,
    name::Union{String, Symbol}
)
state!(
    ocp::CTModels.Building.PreModel,
    n::Int64,
    name::Union{String, Symbol},
    components_names::Array{T2<:Union{String, Symbol}, 1}
)

Define the state dimension and possibly the names of each component.

Note

You must use state! only once to set the state dimension.

Arguments

  • ocp::PreModel: The optimal control problem model.

  • n::Dimension: The state dimension (number of state components).

  • name::Union{String,Symbol} (optional): The name of the state variable (default: "x").

  • components_names::Vector{<:Union{String,Symbol}} (optional): Names of the state components (default: automatically generated).

Examples

Each call below starts from a fresh PreModel (state! may be used only once per problem). The forms illustrate the default name, a custom name, and explicit component names:

julia
julia> using CTModels

julia> ocp = CTModels.PreModel(); CTModels.state!(ocp, 1);

julia> CTModels.state_dimension(ocp), CTModels.state_components(ocp)
(1, ["x"])

julia> ocp = CTModels.PreModel(); CTModels.state!(ocp, 2);

julia> CTModels.state_dimension(ocp), CTModels.state_components(ocp)
(2, ["x₁", "x₂"])

julia> ocp = CTModels.PreModel(); CTModels.state!(ocp, 2, "y");

julia> CTModels.state_dimension(ocp), CTModels.state_components(ocp)
(2, ["y₁", "y₂"])

julia> ocp = CTModels.PreModel(); CTModels.state!(ocp, 2, "y", ["u", "v"]);

julia> CTModels.state_dimension(ocp), CTModels.state_components(ocp)
(2, ["u", "v"])

Throws

  • Exceptions.PreconditionError: If state has already been set

  • Exceptions.IncorrectArgument: If n ≤ 0

  • Exceptions.IncorrectArgument: If number of component names ≠ n

  • Exceptions.IncorrectArgument: If name is empty

  • Exceptions.IncorrectArgument: If any component name is empty

  • Exceptions.IncorrectArgument: If name is one of the component names

  • Exceptions.IncorrectArgument: If component names contain duplicates

  • Exceptions.IncorrectArgument: If name conflicts with existing names in other components

  • Exceptions.IncorrectArgument: If any component name conflicts with existing names

Returns

  • Nothing

See also: CTModels.Building.control!, CTModels.Building.variable!, CTModels.Building.time!, CTModels.Models.state_dimension.

time! [Function]

CTModels.Building.time! Function
julia
time!(
    ocp::CTModels.Building.PreModel;
    t0,
    tf,
    ind0,
    indf,
    time_name
)

Set the initial and final times. We denote by t0 the initial time and tf the final time. The optimal control problem is denoted ocp. When a time is free, then, one must provide the corresponding index of the ocp variable.

Note

You must use time! only once to set either the initial or the final time, or both.

Arguments

  • ocp::PreModel: The optimal control problem model.

  • t0::Union{Time,Nothing} (keyword): The initial time (fixed). Must not be provided with ind0.

  • tf::Union{Time,Nothing} (keyword): The final time (fixed). Must not be provided with indf.

  • ind0::Union{Int,Nothing} (keyword): The variable index for free initial time. Must not be provided with t0.

  • indf::Union{Int,Nothing} (keyword): The variable index for free final time. Must not be provided with tf.

  • time_name::Union{String,Symbol} (keyword): The name of the time variable (default: "t").

Examples

time! may be used only once per problem; each form below applies to a fresh ocp:

julia
julia> using CTModels

julia> ocp = CTModels.PreModel(); CTModels.variable!(ocp, 0); CTModels.time!(ocp; t0=0, tf=1)   # Fixed t0 and fixed tf

julia> ocp = CTModels.PreModel(); CTModels.variable!(ocp, 2); CTModels.time!(ocp; t0=0, indf=2) # Fixed t0 and free  tf

julia> ocp = CTModels.PreModel(); CTModels.variable!(ocp, 2); CTModels.time!(ocp; ind0=1, tf=1) # Free  t0 and fixed tf

When a solution is plotted, the name of the time variable appears ("t" by default). To name the time variable "s":

julia
julia> ocp = CTModels.PreModel(); CTModels.variable!(ocp, 0); CTModels.time!(ocp; t0=0, tf=1, time_name="s") # time_name as a String

julia> ocp = CTModels.PreModel(); CTModels.variable!(ocp, 0); CTModels.time!(ocp; t0=0, tf=1, time_name=:s)  # time_name as a Symbol

Throws

  • Exceptions.PreconditionError: If time has already been set

  • Exceptions.PreconditionError: If variable must be set before (when t0 or tf is free)

  • Exceptions.IncorrectArgument: If ind0 or indf is out of bounds

  • Exceptions.IncorrectArgument: If both t0 and ind0 are provided

  • Exceptions.IncorrectArgument: If neither t0 nor ind0 is provided

  • Exceptions.IncorrectArgument: If both tf and indf are provided

  • Exceptions.IncorrectArgument: If neither tf nor indf is provided

  • Exceptions.IncorrectArgument: If time_name is empty

  • Exceptions.IncorrectArgument: If time_name conflicts with existing names

  • Exceptions.IncorrectArgument: If t0 ≥ tf (when both are fixed)

Returns

  • Nothing

See also: CTModels.Building.state!, CTModels.Building.time_dependence!, CTModels.Components.time_name.

time_dependence! [Function]

CTModels.Building.time_dependence! Function
julia
time_dependence!(
    ocp::CTModels.Building.PreModel;
    autonomous
)

Set the time dependence of the optimal control problem ocp.

Must be called exactly once, after declaring the spaces and dynamics but before calling CTModels.Building.build.

Arguments

  • ocp::PreModel: The optimal control problem being defined.

  • autonomous::Bool: true for an autonomous system  , false for a non-autonomous system  .

Returns

  • Nothing

Throws

  • Exceptions.PreconditionError: If time dependence has already been set.

Examples

julia
julia> using CTModels

julia> ocp = CTModels.PreModel(); CTModels.time_dependence!(ocp; autonomous=true);

See also: CTModels.Building.time!, CTModels.Building.dynamics!.

variable! [Function]

CTModels.Building.variable! Function
julia
variable!(ocp::CTModels.Building.PreModel, q::Int64)
variable!(
    ocp::CTModels.Building.PreModel,
    q::Int64,
    name::Union{String, Symbol}
)
variable!(
    ocp::CTModels.Building.PreModel,
    q::Int64,
    name::Union{String, Symbol},
    components_names::Array{T2<:Union{String, Symbol}, 1}
)

Define a new variable in the optimal control problem ocp with dimension q.

This function registers a named variable (e.g. "state", "control", or other) to be used in the problem definition. You may optionally specify a name and individual component names.

Note

You can call variable! only once. It must be called before setting the objective or dynamics.

Arguments

  • ocp: The PreModel where the variable is registered.

  • q: The dimension of the variable (number of components).

  • name: A name for the variable (default: auto-generated from q).

  • components_names: A vector of strings or symbols for each component (default: ["v₁", "v₂", ...]).

Examples

julia
julia> using CTModels

julia> ocp = CTModels.PreModel(); CTModels.variable!(ocp, 1, "v");

julia> ocp = CTModels.PreModel(); CTModels.variable!(ocp, 2, "v", ["v₁", "v₂"]);

Throws

  • Exceptions.PreconditionError: If variable has already been set

  • Exceptions.PreconditionError: If objective has already been set

  • Exceptions.PreconditionError: If dynamics has already been set

  • Exceptions.IncorrectArgument: If number of component names ≠ q (when q > 0)

  • Exceptions.IncorrectArgument: If name is empty (when q > 0)

  • Exceptions.IncorrectArgument: If any component name is empty (when q > 0)

  • Exceptions.IncorrectArgument: If name is one of the component names (when q > 0)

  • Exceptions.IncorrectArgument: If component names contain duplicates (when q > 0)

  • Exceptions.IncorrectArgument: If name conflicts with existing names in other components (when q > 0)

  • Exceptions.IncorrectArgument: If any component name conflicts with existing names (when q > 0)

Returns

  • Nothing

See also: CTModels.Building.state!, CTModels.Building.control!, CTModels.Models.variable_dimension.