CTModels.jl

The CTModels.jl package is part of the control-toolbox ecosystem.

flowchart TD B(<a href='https://control-toolbox.org/OptimalControl.jl/stable/dev-ctbase.html'>CTBase</a>) M(<a href='https://control-toolbox.org/OptimalControl.jl/stable/dev-ctmodels.html'>CTModels</a>) P(<a href='https://control-toolbox.org/OptimalControl.jl/stable/dev-ctparser.html'>CTParser</a>) O(<a href='https://control-toolbox.org/OptimalControl.jl/stable/dev-optimalcontrol.html'>OptimalControl</a>) D(<a href='https://control-toolbox.org/OptimalControl.jl/stable/dev-ctdirect.html'>CTDirect</a>) F(<a href='https://control-toolbox.org/OptimalControl.jl/stable/dev-ctflows.html'>CTFlows</a>) O --> D O --> M O --> F O --> P F --> M O --> B F --> B D --> B D --> M P --> M P --> B M --> B style M fill:#FBF275

Index

Documentation

Public

CTModels.CTModelsModule

CTModels module.

Lists all the imported modules and packages:

  • Base
  • Core
  • DocStringExtensions
  • Interpolations
  • MLStyle
  • Parameters
  • PrettyTables

List of all the exported names:

source
RecipesBase.plotMethod
plot(
    sol::CTModels.AbstractSolution,
    description::Symbol...;
    kwargs...
)

Plot a solution from an optimal control problem.

This function dispatches on a solution type that inherits from AbstractSolution. It is intended to visualize various components of the solution (such as state trajectories, controls, costates, or any other variables defined in the model).

Note

This function requires the Plots.jl package to be available. If it is not loaded, a CTBase.ExtensionError(:Plots) is thrown.

Arguments

  • sol::AbstractSolution: A solution object returned by solving a control problem.
  • description::Symbol...: Optional symbols specifying what to plot (e.g., :state, :control, :costate, etc.). If empty, a default set of components is plotted.
  • kwargs...: Additional keyword arguments passed to the underlying plotting routines (e.g., xlabel, ylabel, legend, etc.).

Returns

  • A plot object (if Plots.jl is available) visualizing the selected components of the solution.

Example

julia> using Plots
julia> plot(sol, :state, :control, xlabel = "Time", layout = (2,1))

Throws

  • CTBase.ExtensionError if the Plots package is not available or not loaded.
source

Private

CTModels.TimesDiscType

Type alias for a grid of times. This is used to define a discretization of time interval given to solvers.

julia> const TimesDisc = Union{Times, StepRangeLen}

See also: Time, Times.

source
CTModels.AbstractTagType
abstract type AbstractTag

Abstract type for export/import functions, used to choose between JSON or JLD extensions.

source
CTModels.BolzaObjectiveModelType
struct BolzaObjectiveModel{TM<:Function, TL<:Function} <: CTModels.AbstractObjectiveModel

Fields

  • mayer::Function

  • lagrange::Function

  • criterion::Symbol

source
CTModels.ConstraintsModelType
struct ConstraintsModel{TP<:Tuple, TB<:Tuple, TS<:Tuple, TC<:Tuple, TV<:Tuple} <: CTModels.AbstractConstraintsModel

Fields

  • path_nl::Tuple

  • boundary_nl::Tuple

  • state_box::Tuple

  • control_box::Tuple

  • variable_box::Tuple

source
CTModels.ControlModelSolutionType
struct ControlModelSolution{TS<:Function} <: CTModels.AbstractControlModel

Fields

  • name::String

  • components::Vector{String}

  • value::Function

source
CTModels.DimensionType

Type alias for a dimension. This is used to define the dimension of the state space, the costate space, the control space, etc.

julia> const Dimension = Integer
source
CTModels.DualModelType
struct DualModel{PC_Dual<:Union{Nothing, Function}, BC_Dual<:Union{Nothing, AbstractVector{<:Real}}, SC_LB_Dual<:Union{Nothing, Function}, SC_UB_Dual<:Union{Nothing, Function}, CC_LB_Dual<:Union{Nothing, Function}, CC_UB_Dual<:Union{Nothing, Function}, VC_LB_Dual<:Union{Nothing, AbstractVector{<:Real}}, VC_UB_Dual<:Union{Nothing, AbstractVector{<:Real}}} <: CTModels.AbstractDualModel

Fields

  • path_constraints_dual::Union{Nothing, Function}

  • boundary_constraints_dual::Union{Nothing, AbstractVector{<:Real}}

  • state_constraints_lb_dual::Union{Nothing, Function}

  • state_constraints_ub_dual::Union{Nothing, Function}

  • control_constraints_lb_dual::Union{Nothing, Function}

  • control_constraints_ub_dual::Union{Nothing, Function}

  • variable_constraints_lb_dual::Union{Nothing, AbstractVector{<:Real}}

  • variable_constraints_ub_dual::Union{Nothing, AbstractVector{<:Real}}

source
CTModels.InitType

Initial guess for OCP, contains

  • functions of time for the state and control variables
  • vector for optimization variables

Initialization data for each field can be left to default or:

  • vector for optimization variables
  • constant / vector / function for state and control
  • existing solution ('warm start') for all fields

Constructors:

  • Init(): default initialization
  • Init(state, control, variable, time): constant vector, function handles and / or matrices / vectors interpolated along given time grid
  • Init(sol): from existing solution

Examples

julia> init = Init()
julia> init = Init(state=[0.1, 0.2], control=0.3)
julia> init = Init(state=[0.1, 0.2], control=0.3, variable=0.5)
julia> init = Init(state=[0.1, 0.2], controlt=t->sin(t), variable=0.5)
julia> init = Init(state=[[0, 0], [1, 2], [5, -1]], time=[0, .3, 1.], controlt=t->sin(t))
julia> init = Init(sol)
source
CTModels.ModelType
struct Model{TD<:CTModels.TimeDependence, TimesModelType<:CTModels.AbstractTimesModel, StateModelType<:CTModels.AbstractStateModel, ControlModelType<:CTModels.AbstractControlModel, VariableModelType<:CTModels.AbstractVariableModel, DynamicsModelType<:Function, ObjectiveModelType<:CTModels.AbstractObjectiveModel, ConstraintsModelType<:CTModels.AbstractConstraintsModel, BuildExaModelType<:Union{Nothing, Function}} <: CTModels.AbstractModel

Fields

  • times::CTModels.AbstractTimesModel

  • state::CTModels.AbstractStateModel

  • control::CTModels.AbstractControlModel

  • variable::CTModels.AbstractVariableModel

  • dynamics::Function

  • objective::CTModels.AbstractObjectiveModel

  • constraints::CTModels.AbstractConstraintsModel

  • definition::Expr

  • build_examodel::Union{Nothing, Function}

source
CTModels.PreModelType
mutable struct PreModel <: CTModels.AbstractModel

Fields

  • times::Union{Nothing, CTModels.AbstractTimesModel}: Default: nothing

  • state::Union{Nothing, CTModels.AbstractStateModel}: Default: nothing

  • control::Union{Nothing, CTModels.AbstractControlModel}: Default: nothing

  • variable::CTModels.AbstractVariableModel: Default: EmptyVariableModel()

  • dynamics::Union{Nothing, Function, Vector{<:Tuple{var"#s52", var"#s53"} where {var"#s52"<:(AbstractRange{<:Int64}), var"#s53"<:Function}}}: Default: nothing

  • objective::Union{Nothing, CTModels.AbstractObjectiveModel}: Default: nothing

  • constraints::OrderedCollections.OrderedDict{Symbol, Tuple{Symbol, Union{Function, OrdinalRange{<:Int64}}, AbstractVector{<:Real}, AbstractVector{<:Real}}}: Default: ConstraintsDictType()

  • definition::Union{Nothing, Expr}: Default: nothing

  • autonomous::Union{Nothing, Bool}: Default: nothing

source
CTModels.SolutionType
struct Solution{TimeGridModelType<:CTModels.AbstractTimeGridModel, TimesModelType<:CTModels.AbstractTimesModel, StateModelType<:CTModels.AbstractStateModel, ControlModelType<:CTModels.AbstractControlModel, VariableModelType<:CTModels.AbstractVariableModel, CostateModelType<:Function, ObjectiveValueType<:Real, DualModelType<:CTModels.AbstractDualModel, SolverInfosType<:CTModels.AbstractSolverInfos, ModelType<:CTModels.AbstractModel} <: CTModels.AbstractSolution

Fields

  • time_grid::CTModels.AbstractTimeGridModel

  • times::CTModels.AbstractTimesModel

  • state::CTModels.AbstractStateModel

  • control::CTModels.AbstractControlModel

  • variable::CTModels.AbstractVariableModel

  • costate::Function

  • objective::Real

  • dual::CTModels.AbstractDualModel

  • solver_infos::CTModels.AbstractSolverInfos

  • model::CTModels.AbstractModel

source
CTModels.SolverInfosType
struct SolverInfos{TI<:Dict{Symbol, Any}} <: CTModels.AbstractSolverInfos

Fields

  • iterations::Int64

  • stopping::Symbol

  • message::String

  • success::Bool

  • constraints_violation::Float64

  • infos::Dict{Symbol, Any}

source
CTModels.StateModelType
struct StateModel <: CTModels.AbstractStateModel

Fields

  • name::String

  • components::Vector{String}

source
CTModels.StateModelSolutionType
struct StateModelSolution{TS<:Function} <: CTModels.AbstractStateModel

Fields

  • name::String

  • components::Vector{String}

  • value::Function

source
CTModels.TimeGridModelType
struct TimeGridModel{T<:Union{StepRangeLen, AbstractVector{<:Real}}} <: CTModels.AbstractTimeGridModel

Fields

  • value::Union{StepRangeLen, AbstractVector{<:Real}}
source
CTModels.TimesModelType
struct TimesModel{TI<:CTModels.AbstractTimeModel, TF<:CTModels.AbstractTimeModel} <: CTModels.AbstractTimesModel

Fields

  • initial::CTModels.AbstractTimeModel

  • final::CTModels.AbstractTimeModel

  • time_name::String

source
CTModels.VariableModelSolutionType
struct VariableModelSolution{TS<:Union{Real, AbstractVector{<:Real}}} <: CTModels.AbstractVariableModel

Fields

  • name::String

  • components::Vector{String}

  • value::Union{Real, AbstractVector{<:Real}}

source
Base.isemptyMethod
isempty(model::CTModels.ConstraintsModel) -> Bool

Return if the constraints model is empty or not.

Arguments

  • model: The constraints model to check for emptiness.

Returns

  • Bool: Returns true if the model has no constraints, false otherwise.

Example

# Example of checking if a constraints model is empty
julia> model = ConstraintsModel(...)
julia> isempty(model)  # Returns true if there are no constraints
source
Base.showMethod
show(
    io::IO,
    _::MIME{Symbol("text/plain")},
    ocp::CTModels.PreModel
)

Print the optimal control problem.

source
Base.showMethod
show(
    io::IO,
    _::MIME{Symbol("text/plain")},
    sol::CTModels.Solution
)

Prints the solution.

source
Base.showMethod
show(io::IO, _::MIME{Symbol("text/plain")}, ocp::Model)

Print the optimal control problem.

source
CTModels.__build_dynamics_from_partsMethod
__build_dynamics_from_parts(
    parts::Vector{<:Tuple{var"#s53", var"#s52"} where {var"#s53"<:(AbstractRange{<:Int64}), var"#s52"<:Function}}
) -> CTModels.var"#dyn!#64"{Vector{var"#s54"}} where var"#s54"<:(Tuple{var"#s53", var"#s52"} where {var"#s53"<:(AbstractRange{<:Int64}), var"#s52"<:Function})

Build a combined dynamics function from multiple parts.

This function constructs an in-place dynamics function dyn! by composing several sub-functions, each responsible for updating a specific segment of the output vector.

Arguments

  • parts::Vector{<:Tuple{<:AbstractRange{<:Int}, <:Function}}: A vector of tuples, where each tuple contains:
    • A range specifying the indices in the output vector val that the corresponding function updates.
    • A function f with the signature (output_segment, t, x, u, v), which updates the slice of val indicated by the range.

Returns

  • dyn!: A function with signature (val, t, x, u, v) that updates the full output vector val in-place by applying each part function to its assigned segment.

Details

  • The returned dyn! function calls each part function with a view of val restricted to the assigned range. This avoids unnecessary copying and allows efficient updates of sub-vectors.
  • Each part function is expected to modify its output segment in-place.

Example

# Define two sub-dynamics functions
julia> f1(out, t, x, u, v) = out .= x[1:2] .+ u[1:2]
julia> f2(out, t, x, u, v) = out .= x[3] * v

# Combine them into one dynamics function affecting different parts of the output vector
julia> parts = [(1:2, f1), (3:3, f2)]
julia> dyn! = __build_dynamics_from_parts(parts)

val = zeros(3)
julia> dyn!(val, 0.0, [1.0, 2.0, 3.0], [0.5, 0.5], 2.0)
julia> println(val)  # prints [1.5, 2.5, 6.0]
source
CTModels.__constraint!Method
__constraint!(
    ocp_constraints::OrderedCollections.OrderedDict{Symbol, Tuple{Symbol, Union{Function, OrdinalRange{<:Int64}}, AbstractVector{<:Real}, AbstractVector{<:Real}}},
    type::Symbol,
    n::Int64,
    m::Int64,
    q::Int64;
    rg,
    f,
    lb,
    ub,
    label,
    codim_f
)

Add a constraint to a dictionary of constraints.

Arguments

  • ocp_constraints: The dictionary of constraints to which the constraint will be added.
  • type: The type of the constraint. It can be :state, :control, :variable, :boundary, or :path.
  • n: The dimension of the state.
  • m: The dimension of the control.
  • q: The dimension of the variable.
  • 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 dictionary of constraints.

Requirements

  • The constraint must not be set before.
  • The lower bound lb and the upper bound ub cannot be both nothing.
  • The lower bound lb and the upper bound ub must have the same length, if both provided.

If rg and f are not provided then,

  • type must be :state, :control, or :variable.
  • lb and ub must be of dimension n, m, or q respectively, when provided.

If rg is provided, then:

  • f must not be provided.
  • type must be :state, :control, or :variable.
  • rg must be a range of integers, and must be contained in 1:n, 1:m, or 1:q respectively.

If f is provided, then:

  • rg must not be provided.
  • type must be :boundary or :path.
  • f must be a function that returns a vector of the same dimension as the constraint.
  • lb and ub must be of the same dimension as the output of f, when provided.

Example

# Example of adding a state constraint
julia> ocp_constraints = Dict()
__constraint!(ocp_constraints, :state, 3, 2, 1, lb=[0.0], ub=[1.0], label=:my_constraint)
source
CTModels.__constraint_labelMethod
__constraint_label() -> Symbol

Used to set the default value of the label of a constraint. A unique value is given to each constraint using the gensym function and prefixing by :unamed.

source
CTModels.__control_componentsMethod
__control_components(
    m::Int64,
    name::String
) -> Vector{String}

Used to set the default value of the names of the controls. The default value is ["u"] for a one dimensional control, and ["u₁", "u₂", ...] for a multi dimensional control.

source
CTModels.__control_nameMethod
__control_name() -> String

Used to set the default value of the names of the control. The default value is "u".

source
CTModels.__criterion_typeMethod
__criterion_type() -> Symbol

Used to set the default value of the type of criterion. Either :min or :max. The default value is :min. The other possible criterion type is :max.

source
CTModels.__formatMethod
__format() -> Symbol

Used to set the default value of the format of the file to be used for export and import.

source
CTModels.__is_consistentMethod
__is_consistent(ocp::CTModels.PreModel) -> Bool

Return true if all the required fields are set in the PreModel.

source
CTModels.__state_componentsMethod
__state_components(n::Int64, name::String) -> Vector{String}

Used to set the default value of the names of the states. The default value is ["x"] for a one dimensional state, and ["x₁", "x₂", ...] for a multi dimensional state.

source
CTModels.__state_nameMethod
__state_name() -> String

Used to set the default value of the name of the state. The default value is "x".

source
CTModels.__time_nameMethod
__time_name() -> String

Used to set the default value of the name of the time. The default value is t.

source
CTModels.__variable_componentsMethod
__variable_components(
    q::Int64,
    name::String
) -> Vector{String}

Used to set the default value of the names of the variables. The default value is ["v"] for a one dimensional variable, and ["v₁", "v₂", ...] for a multi dimensional variable.

source
CTModels.__variable_nameMethod
__variable_name(q::Int64) -> String

Used to set the default value of the names of the variables. The default value is "v".

source
CTModels.append_box_constraints!Method
append_box_constraints!(
    inds,
    lbs,
    ubs,
    labels,
    rg,
    lb,
    ub,
    label
)

Appends box constraint data to the provided vectors.

Arguments

  • inds::Vector{Int}: Vector of indices to which the range rg will be appended.
  • lbs::Vector{<:Real}: Vector of lower bounds to which lb will be appended.
  • ubs::Vector{<:Real}: Vector of upper bounds to which ub will be appended.
  • labels::Vector{String}: Vector of labels to which the label will be repeated and appended.
  • rg::AbstractVector{Int}: Index range corresponding to the constraint variables.
  • lb::AbstractVector{<:Real}: Lower bounds associated with rg.
  • ub::AbstractVector{<:Real}: Upper bounds associated with rg.
  • label::String: Label describing the constraint block (e.g., "state", "control").

Notes

  • All input vectors (rg, lb, ub) must have the same length.
  • The function modifies the inds, lbs, ubs, and labels vectors in-place.
source
CTModels.boundary_constraints_dualMethod
boundary_constraints_dual(
    sol::CTModels.Solution
) -> Union{Nothing, AbstractVector{<:Real}}

Return the dual of the boundary constraints of the optimal control solution.

source
CTModels.boundary_constraints_dualMethod
boundary_constraints_dual(
    model::CTModels.DualModel{<:Union{Nothing, Function}, BC_Dual<:Union{Nothing, AbstractVector{<:Real}}}
) -> Union{Nothing, AbstractVector{<:Real}}

Return the dual vector associated with the boundary constraints.

Arguments

  • model::DualModel: A model including dual variables for boundary constraints.

Returns

A vector of dual values, or nothing if not set.

source
CTModels.boundary_constraints_nlMethod
boundary_constraints_nl(
    model::CTModels.ConstraintsModel{<:Tuple, TB}
) -> Any

Get the nonlinear boundary constraints from the model.

Arguments

  • model: The constraints model from which to retrieve the boundary constraints.

Returns

  • The nonlinear boundary constraints.

Example

# Example of retrieving nonlinear boundary constraints
julia> model = ConstraintsModel(...)
julia> boundary_constraints = boundary_constraints_nl(model)
source
CTModels.boundary_constraints_nlMethod
boundary_constraints_nl(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.TimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.AbstractVariableModel, <:Function, <:CTModels.AbstractObjectiveModel, <:CTModels.ConstraintsModel{<:Tuple, TB<:Tuple}}
) -> Any

Get the nonlinear boundary constraints from the model.

source
CTModels.buildMethod
build(
    pre_ocp::CTModels.PreModel;
    build_examodel
) -> Model{TD, var"#s182", var"#s1821", var"#s1822", var"#s1823", var"#s1824", var"#s1825", CTModels.ConstraintsModel{TP, TB, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}}, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}}, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}}}, Nothing} where {TD<:CTModels.TimeDependence, var"#s182"<:CTModels.AbstractTimesModel, var"#s1821"<:CTModels.AbstractStateModel, var"#s1822"<:CTModels.AbstractControlModel, var"#s1823"<:CTModels.AbstractVariableModel, var"#s1824"<:Function, var"#s1825"<:CTModels.AbstractObjectiveModel, TP<:Tuple{Vector{Float64}, Any, Vector{Float64}, Vector{Symbol}}, TB<:Tuple{Vector{Float64}, Any, Vector{Float64}, Vector{Symbol}}}

Converts a mutable PreModel into an immutable Model.

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

Arguments

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

Returns

  • Model: A fully constructed model ready for solving.

Example

pre_ocp = PreModel()
times!(pre_ocp, 0.0, 1.0, 100)
state!(pre_ocp, 2, "x", ["x1", "x2"])
control!(pre_ocp, 1, "u", ["u1"])
dynamics!(pre_ocp, (dx, t, x, u, v) -> dx .= x + u)
model = build(pre_ocp)
source
CTModels.buildMethod
build(
    constraints::OrderedCollections.OrderedDict{Symbol, Tuple{Symbol, Union{Function, OrdinalRange{<:Int64}}, AbstractVector{<:Real}, AbstractVector{<:Real}}}
) -> CTModels.ConstraintsModel{TP, TB, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}}, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}}, Tuple{Vector{Float64}, Vector{Int64}, Vector{Float64}, Vector{Symbol}}} where {TP<:Tuple{Vector{Float64}, Any, Vector{Float64}, Vector{Symbol}}, TB<:Tuple{Vector{Float64}, Any, Vector{Float64}, Vector{Symbol}}}

Constructs a 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 ConstraintsModel.

Arguments

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

Returns

  • ConstraintsModel: A structured model encapsulating all provided constraints.

Example

constraints = OrderedDict(
    :c1 => (:path, f1, [0.0], [1.0]),
    :c2 => (:state, 1:2, [-1.0, -1.0], [1.0, 1.0])
)
model = build(constraints)
source
CTModels.buildFunctionalInitMethod
buildFunctionalInit(
    data::Function,
    time,
    dim
) -> CTModels.var"#27#28"{<:Function}

Build functional initialization: function case

source
CTModels.buildFunctionalInitMethod
buildFunctionalInit(
    data::Union{Real, AbstractVector{<:Real}},
    time,
    dim
) -> Union{CTModels.var"#29#31", CTModels.var"#30#32"}

Build functional initialization: constant / 1D interpolation

source
CTModels.build_solutionMethod
build_solution(
    ocp::Model,
    T::Vector{Float64},
    X::Union{Function, Matrix{Float64}},
    U::Union{Function, Matrix{Float64}},
    v::Vector{Float64},
    P::Union{Function, Matrix{Float64}};
    objective,
    iterations,
    constraints_violation,
    message,
    stopping,
    success,
    path_constraints_dual,
    boundary_constraints_dual,
    state_constraints_lb_dual,
    state_constraints_ub_dual,
    control_constraints_lb_dual,
    control_constraints_ub_dual,
    variable_constraints_lb_dual,
    variable_constraints_ub_dual
)

Build a solution from the optimal control problem, the time grid, the state, control, variable, and dual variables.

Arguments

  • ocp::Model: the optimal control problem.
  • T::Vector{Float64}: the time grid.
  • X::Matrix{Float64}: the state trajectory.
  • U::Matrix{Float64}: the control trajectory.
  • v::Vector{Float64}: the variable trajectory.
  • P::Matrix{Float64}: the costate trajectory.
  • objective::Float64: the objective value.
  • iterations::Int: the number of iterations.
  • constraints_violation::Float64: the constraints violation.
  • message::String: the message associated to the stopping criterion.
  • stopping::Symbol: the stopping criterion.
  • success::Bool: the success status.
  • path_constraints_dual::Matrix{Float64}: the dual of the path constraints.
  • boundary_constraints_dual::Vector{Float64}: the dual of the boundary constraints.
  • state_constraints_lb_dual::Matrix{Float64}: the lower bound dual of the state constraints.
  • state_constraints_ub_dual::Matrix{Float64}: the upper bound dual of the state constraints.
  • control_constraints_lb_dual::Matrix{Float64}: the lower bound dual of the control constraints.
  • control_constraints_ub_dual::Matrix{Float64}: the upper bound dual of the control constraints.
  • variable_constraints_lb_dual::Vector{Float64}: the lower bound dual of the variable constraints.
  • variable_constraints_ub_dual::Vector{Float64}: the upper bound dual of the variable constraints.

Returns

  • sol::Solution: the optimal control solution.
source
CTModels.checkDimMethod
checkDim(actual_dim, target_dim)

Check if actual dimension is equal to target dimension, error otherwise

source
CTModels.componentsMethod
components(
    model::CTModels.ControlModelSolution
) -> Vector{String}

Get the names of the control components from the solution.

Arguments

  • model::ControlModelSolution: The control model solution.

Returns

  • Vector{String}: A list of control component names.
source
CTModels.componentsMethod
components(model::CTModels.ControlModel) -> Vector{String}

Get the names of the control components.

Arguments

  • model::ControlModel: The control model.

Returns

  • Vector{String}: A list of control component names.

Example

julia> components(controlmodel)
["u₁", "u₂"]
source
CTModels.componentsMethod
components(_::CTModels.EmptyVariableModel) -> Vector{String}

Return an empty vector since there are no variable components defined.

source
CTModels.componentsMethod
components(
    model::CTModels.StateModelSolution
) -> Vector{String}

Get the components names of the state from the state model solution.

source
CTModels.componentsMethod
components(model::CTModels.StateModel) -> Vector{String}

Get the components names of the state from the state model.

source
CTModels.componentsMethod
components(
    model::CTModels.VariableModelSolution
) -> Vector{String}

Return the names of the components from the variable solution.

source
CTModels.componentsMethod
components(model::CTModels.VariableModel) -> Vector{String}

Return the names of the components of the variable.

source
CTModels.constraint!Method
constraint!(
    ocp::CTModels.PreModel,
    type::Symbol;
    rg,
    f,
    lb,
    ub,
    label,
    codim_f
)

Add a constraint to a pre-model. See __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.

Example

# Example of adding a control constraint to a pre-model
julia> ocp = PreModel()
julia> constraint!(ocp, :control, rg=1:2, lb=[0.0], ub=[1.0], label=:control_constraint)
source
CTModels.constraintMethod
constraint(
    model::Model,
    label::Symbol
) -> Tuple{Symbol, Any, Any, Any}

Get a labelled constraint from the model. Returns a tuple of the form (type, f, lb, ub) where type is the type of the constraint, f is the function of the constraint, lb is the lower bound of the constraint and ub is the upper bound of the constraint.

The function returns an exception if the label is not found in the model.

Arguments

  • model: The model from which to retrieve the constraint.
  • label: The label of the constraint to retrieve.

Returns

  • Tuple: A tuple containing the type, function, lower bound, and upper bound of the constraint.

Example

julia> # Example of getting a labelled constraint from the model
julia> model = Model(...)
julia> constraint_info = constraint(model, :my_constraint)
source
CTModels.constraintsMethod
constraints(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.AbstractTimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.AbstractVariableModel, <:Function, <:CTModels.AbstractObjectiveModel, C<:CTModels.AbstractConstraintsModel}
) -> CTModels.AbstractConstraintsModel

Get the constraints from the model.

source
CTModels.control!Method
control!(ocp::CTModels.PreModel, m::Int64)
control!(
    ocp::CTModels.PreModel,
    m::Int64,
    name::Union{String, Symbol}
)
control!(
    ocp::CTModels.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> control!(ocp, 1)
julia> control_dimension(ocp)
1
julia> control_components(ocp)
["u"]

julia> control!(ocp, 1, "v")
julia> control_components(ocp)
["v"]

julia> control!(ocp, 2)
julia> control_components(ocp)
["u₁", "u₂"]

julia> control!(ocp, 2, :v)
julia> control_components(ocp)
["v₁", "v₂"]

julia> control!(ocp, 2, "v", ["a", "b"])
julia> control_components(ocp)
["a", "b"]
source
CTModels.controlMethod
control(
    sol::CTModels.Solution{<:CTModels.AbstractTimeGridModel, <:CTModels.AbstractTimesModel, <:CTModels.AbstractStateModel, <:CTModels.ControlModelSolution{TS<:Function}}
) -> Function

Return the control (function of time) of the optimal control solution.

julia> t0 = time_grid(sol)[1]
julia> u  = control(sol)
julia> u0 = u(t0) # control at initial time
source
CTModels.controlMethod
control(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.TimesModel, <:CTModels.AbstractStateModel, T<:CTModels.AbstractControlModel}
) -> CTModels.AbstractControlModel

Get the control from the model.

source
CTModels.control_componentsMethod
control_components(sol::CTModels.Solution) -> Vector{String}

Return the names of the components of the control of the optimal control solution.

source
CTModels.control_constraints_boxMethod
control_constraints_box(
    model::CTModels.ConstraintsModel{<:Tuple, <:Tuple, <:Tuple, TC}
) -> Any

Get the control box constraints from the model.

Arguments

  • model: The constraints model from which to retrieve the control box constraints.

Returns

  • The control box constraints.

Example

# Example of retrieving control box constraints
julia> model = ConstraintsModel(...)
julia> control_constraints = control_constraints_box(model)
source
CTModels.control_constraints_boxMethod
control_constraints_box(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.TimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.AbstractVariableModel, <:Function, <:CTModels.AbstractObjectiveModel, <:CTModels.ConstraintsModel{<:Tuple, <:Tuple, <:Tuple, TC<:Tuple}}
) -> Any

Get the box constraints on control from the model.

source
CTModels.control_constraints_lb_dualMethod
control_constraints_lb_dual(
    sol::CTModels.Solution
) -> Union{Nothing, Function}

Return the lower bound dual of the control constraints of the optimal control solution.

source
CTModels.control_constraints_lb_dualMethod
control_constraints_lb_dual(
    model::CTModels.DualModel{<:Union{Nothing, Function}, <:Union{Nothing, AbstractVector{<:Real}}, <:Union{Nothing, Function}, <:Union{Nothing, Function}, CC_LB_Dual<:Union{Nothing, Function}}
) -> Union{Nothing, Function}

Return the dual function associated with the lower bounds of control constraints.

Arguments

  • model::DualModel: A model including dual variables for control lower bounds.

Returns

A function mapping time t to a vector of dual values, or nothing if not set.

source
CTModels.control_constraints_ub_dualMethod
control_constraints_ub_dual(
    sol::CTModels.Solution
) -> Union{Nothing, Function}

Return the upper bound dual of the control constraints of the optimal control solution.

source
CTModels.control_constraints_ub_dualMethod
control_constraints_ub_dual(
    model::CTModels.DualModel{<:Union{Nothing, Function}, <:Union{Nothing, AbstractVector{<:Real}}, <:Union{Nothing, Function}, <:Union{Nothing, Function}, <:Union{Nothing, Function}, CC_UB_Dual<:Union{Nothing, Function}}
) -> Union{Nothing, Function}

Return the dual function associated with the upper bounds of control constraints.

Arguments

  • model::DualModel: A model including dual variables for control upper bounds.

Returns

A function mapping time t to a vector of dual values, or nothing if not set.

source
CTModels.control_dimensionMethod
control_dimension(sol::CTModels.Solution) -> Int64

Return the dimension of the control of the optimal control solution.

source
CTModels.control_nameMethod
control_name(sol::CTModels.Solution) -> String

Return the name of the control of the optimal control solution.

source
CTModels.costateMethod
costate(
    sol::CTModels.Solution{<:CTModels.AbstractTimeGridModel, <:CTModels.AbstractTimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.AbstractVariableModel, Co<:Function}
) -> Function

Return the costate of the optimal control solution.

julia> t0 = time_grid(sol)[1]
julia> p  = costate(sol)
julia> p0 = p(t0)
source
CTModels.criterionMethod
criterion(model::CTModels.BolzaObjectiveModel) -> Symbol

Get the criterion (:min or :max) of the Bolza objective model.

source
CTModels.criterionMethod
criterion(model::CTModels.LagrangeObjectiveModel) -> Symbol

Get the criterion (:min or :max) of the Lagrange objective model.

source
CTModels.criterionMethod
criterion(model::CTModels.MayerObjectiveModel) -> Symbol

Get the criterion (:min or :max) of the Mayer objective model.

source
CTModels.criterionMethod
criterion(ocp::Model) -> Symbol

Get the type of criterion (:min or :max) from the model.

source
CTModels.ctinterpolateMethod
ctinterpolate(x, f) -> Any

Return a linear interpolation function for the data f defined at points x.

This function creates a one-dimensional linear interpolant using the Interpolations.jl package, with linear extrapolation beyond the bounds of x.

Arguments

  • x: A vector of points at which the values f are defined.
  • f: A vector of values to interpolate.

Returns

A callable interpolation object that can be evaluated at new points.

Example

julia> x = 0:0.5:2
julia> f = [0.0, 1.0, 0.0, -1.0, 0.0]
julia> interp = ctinterpolate(x, f)
julia> interp(1.2)
source
CTModels.definition!Method
definition!(ocp::CTModels.PreModel, definition::Expr)

Set the model definition of the optimal control problem.

source
CTModels.definitionMethod
definition(ocp::CTModels.PreModel) -> Union{Nothing, Expr}

Return the model definition of the optimal control problem or nothing.

source
CTModels.definitionMethod
definition(ocp::Model) -> Expr

Return the model definition of the optimal control problem.

source
CTModels.dim_boundary_constraints_nlMethod
dim_boundary_constraints_nl(
    model::CTModels.ConstraintsModel
) -> Int64

Return the dimension of nonlinear boundary constraints.

Arguments

  • model: The constraints model from which to retrieve the dimension of boundary constraints.

Returns

  • Dimension: The dimension of the nonlinear boundary constraints.

Example

# Example of getting the dimension of nonlinear boundary constraints
julia> model = ConstraintsModel(...)
julia> dim_boundary = dim_boundary_constraints_nl(model)
source
CTModels.dim_control_constraints_boxMethod
dim_control_constraints_box(
    model::CTModels.ConstraintsModel
) -> Int64

Return the dimension of control box constraints.

Arguments

  • model: The constraints model from which to retrieve the dimension of control box constraints.

Returns

  • Dimension: The dimension of the control box constraints.

Example

julia> # Example of getting the dimension of control box constraints
julia> model = ConstraintsModel(...)
julia> dim_control = dim_control_constraints_box(model)
source
CTModels.dim_path_constraints_nlMethod
dim_path_constraints_nl(
    model::CTModels.ConstraintsModel
) -> Int64

Return the dimension of nonlinear path constraints.

Arguments

  • model: The constraints model from which to retrieve the dimension of path constraints.

Returns

  • Dimension: The dimension of the nonlinear path constraints.

Example

# Example of getting the dimension of nonlinear path constraints
julia> model = ConstraintsModel(...)
julia> dim_path = dim_path_constraints_nl(model)
source
CTModels.dim_state_constraints_boxMethod
dim_state_constraints_box(
    model::CTModels.ConstraintsModel
) -> Int64

Return the dimension of state box constraints.

Arguments

  • model: The constraints model from which to retrieve the dimension of state box constraints.

Returns

  • Dimension: The dimension of the state box constraints.

Example

julia> # Example of getting the dimension of state box constraints
julia> model = ConstraintsModel(...)
julia> dim_state = dim_state_constraints_box(model)
source
CTModels.dim_variable_constraints_boxMethod
dim_variable_constraints_box(
    model::CTModels.ConstraintsModel
) -> Int64

Return the dimension of variable box constraints.

Arguments

  • model: The constraints model from which to retrieve the dimension of variable box constraints.

Returns

  • Dimension: The dimension of the variable box constraints.

Example

julia> # Example of getting the dimension of variable box constraints
julia> model = ConstraintsModel(...)
julia> dim_variable = dim_variable_constraints_box(model)
source
CTModels.dimensionMethod
dimension(model::CTModels.ControlModelSolution) -> Int64

Get the control input dimension from the solution.

Arguments

  • model::ControlModelSolution: The control model solution.

Returns

  • Dimension: The number of control components.
source
CTModels.dimensionMethod
dimension(model::CTModels.ControlModel) -> Int64

Get the control input dimension.

Arguments

  • model::ControlModel: The control model.

Returns

  • Dimension: The number of control components.
source
CTModels.dimensionMethod
dimension(_::CTModels.EmptyVariableModel) -> Int64

Return 0 since no variable is defined.

source
CTModels.dimensionMethod
dimension(model::CTModels.StateModelSolution) -> Int64

Get the dimension of the state from the state model solution.

source
CTModels.dimensionMethod
dimension(model::CTModels.StateModel) -> Int64

Get the dimension of the state from the state model.

source
CTModels.dimensionMethod
dimension(model::CTModels.VariableModelSolution) -> Int64

Return the number of components in the variable solution.

source
CTModels.dimensionMethod
dimension(model::CTModels.VariableModel) -> Int64

Return the dimension (number of components) of the variable.

source
CTModels.dualMethod
dual(
    sol::CTModels.Solution,
    model::Model,
    label::Symbol
) -> Any

Return the dual variable associated with a constraint identified by its label.

Searches through all constraint types (path, boundary, state, control, and variable constraints) defined in the model and returns the corresponding dual value(s) from the solution. If the label is found multiple times, a vector of values is returned.

Arguments

  • sol::Solution: Solution object containing dual variables.
  • model::Model: Model containing constraint definitions.
  • label::Symbol: Symbol corresponding to a constraint label.

Returns

A function of time t for time-dependent constraints, or a scalar/vector for time-invariant duals. If the label is not found, throws an IncorrectArgument exception.

Examples

julia> dual_fun = dual(sol, model, :velocity_limit)
julia> dual_value_at_t1 = dual_fun(1.0)
source
CTModels.dual_modelMethod
dual_model(
    sol::CTModels.Solution{<:CTModels.AbstractTimeGridModel, <:CTModels.AbstractTimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.AbstractVariableModel, <:Function, <:Real, DM<:CTModels.AbstractDualModel}
) -> CTModels.AbstractDualModel
source
CTModels.dynamics!Method
dynamics!(
    ocp::CTModels.PreModel,
    rg::AbstractRange{<:Int64},
    f::Function
)

Add a partial dynamics function f to the optimal control problem ocp, applying to the subset of state indices specified by the range rg.

Arguments

  • ocp::PreModel: The optimal control problem being defined.
  • rg::AbstractRange{<:Int}: Range of state indices to which f applies.
  • f::Function: A function describing the dynamics over the specified state indices.

Preconditions

  • The state, control, and times must be set before calling this function.
  • The full dynamics must not yet be complete.
  • No overlap is allowed between rg and existing dynamics index ranges.

Behavior

This function appends the tuple (rg, f) to the list of partial dynamics. It ensures that the specified indices are not already covered and that the system is in a valid configuration for adding partial dynamics.

Errors

Throws CTBase.UnauthorizedCall if:

  • The state, control, or times are not yet set.
  • The dynamics are already defined completely.
  • Any index in rg overlaps with an existing dynamics range.

Example

julia> dynamics!(ocp, 1:2, (out, t, x, u, v) -> out .= x[1:2] .+ u[1:2])
julia> dynamics!(ocp, 3:3, (out, t, x, u, v) -> out .= x[3] * v[1])
source
CTModels.dynamics!Method
dynamics!(ocp::CTModels.PreModel, f::Function)

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

Arguments

  • ocp::PreModel: The optimal control problem being defined.
  • f::Function: A function that defines the complete system dynamics.

Preconditions

  • The state, control, and times must be set before calling this function.
  • No dynamics must have been set previously.

Behavior

This function assigns f as the complete dynamics of the system. It throws an error if any of the required fields (state, control, times) are not yet set, or if dynamics have already been set.

Errors

Throws CTBase.UnauthorizedCall if called out of order or in an invalid state.

source
CTModels.dynamics!Method
dynamics!(ocp::CTModels.PreModel, i::Integer, f::Function)

Define partial dynamics for a single state variable index in an optimal control problem.

This is a convenience method for defining dynamics affecting only one element of the state vector. It wraps the scalar index i into a range i:i and delegates to the general partial dynamics method.

Arguments

  • ocp::PreModel: The optimal control problem being defined.
  • i::Integer: The index of the state variable to which the function f applies.
  • f::Function: A function of the form (out, t, x, u, v) -> ..., which updates the scalar output out[1] in-place.

Behavior

This is equivalent to calling:

julia> dynamics!(ocp, i:i, f)

Errors

Throws the same errors as the range-based method if:

  • The model is not properly initialized.
  • The index i overlaps with existing dynamics.
  • A full dynamics function is already defined.

Example

julia> dynamics!(ocp, 3, (out, t, x, u, v) -> out[1] = x[3]^2 + u[1])
source
CTModels.dynamicsMethod
dynamics(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.AbstractTimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.AbstractVariableModel, D<:Function}
) -> Function

Get the dynamics from the model.

source
CTModels.export_ocp_solutionMethod
export_ocp_solution(
    sol::CTModels.AbstractSolution;
    format,
    filename
)

Export a solution in JLD or JSON formats.

Examples

julia> CTModels.export_ocp_solution(sol; filename="solution", format=:JSON)
julia> CTModels.export_ocp_solution(sol; filename="solution", format=:JLD)
source
CTModels.finalMethod
final(
    model::CTModels.TimesModel{<:CTModels.AbstractTimeModel, TF<:CTModels.AbstractTimeModel}
) -> CTModels.AbstractTimeModel

Get the final time from the times model.

source
CTModels.final_timeMethod
final_time(
    model::CTModels.TimesModel{<:CTModels.AbstractTimeModel, <:CTModels.FixedTimeModel{T<:Real}}
) -> Real

Get the final time from the times model, from a fixed final time model.

source
CTModels.final_timeMethod
final_time(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.TimesModel{<:CTModels.AbstractTimeModel, CTModels.FixedTimeModel{T<:Real}}}
) -> Real

Get the final time from the model, for a fixed final time.

source
CTModels.final_timeMethod
final_time(
    model::CTModels.TimesModel{<:CTModels.AbstractTimeModel, CTModels.FreeTimeModel},
    variable::AbstractArray{T<:Real, 1}
) -> Any

Get the final time from the times model, from a free final time model.

source
CTModels.final_timeMethod
final_time(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.TimesModel{<:CTModels.AbstractTimeModel, CTModels.FreeTimeModel}},
    variable::AbstractArray{T<:Real, 1}
) -> Any

Get the final time from the model, for a free final time.

source
CTModels.final_timeMethod
final_time(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.TimesModel{<:CTModels.AbstractTimeModel, CTModels.FreeTimeModel}},
    variable::Real
) -> Real

Get the final time from the model, for a free final time.

source
CTModels.final_time_nameMethod
final_time_name(sol::CTModels.Solution) -> String

Return the name of the final time of the optimal control solution.

source
CTModels.get_build_examodelMethod
get_build_examodel(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.AbstractTimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.AbstractVariableModel, <:Function, <:CTModels.AbstractObjectiveModel, <:CTModels.AbstractConstraintsModel, <:Function}
) -> Function

Get the build_examodel from the model.

source
CTModels.get_build_examodelMethod
get_build_examodel(
    _::Model{<:CTModels.TimeDependence, <:CTModels.AbstractTimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.AbstractVariableModel, <:Function, <:CTModels.AbstractObjectiveModel, <:CTModels.AbstractConstraintsModel, <:Nothing}
)

Return an error (UnauthorizedCall) since the model is not built with the :exa backend.

source
CTModels.has_fixed_final_timeMethod
has_fixed_final_time(
    times::CTModels.TimesModel{<:CTModels.AbstractTimeModel, CTModels.FreeTimeModel}
) -> Bool

Check if the final time is free. Return false.

source
CTModels.has_fixed_final_timeMethod
has_fixed_final_time(
    times::CTModels.TimesModel{<:CTModels.AbstractTimeModel, <:CTModels.FixedTimeModel{T<:Real}}
) -> Bool

Check if the final time is fixed. Return true.

source
CTModels.has_fixed_initial_timeMethod
has_fixed_initial_time(
    times::CTModels.TimesModel{<:CTModels.FixedTimeModel{T<:Real}}
) -> Bool

Check if the initial time is fixed. Return true.

source
CTModels.has_lagrange_costMethod
has_lagrange_cost(
    model::CTModels.BolzaObjectiveModel
) -> Bool

Check if the Bolza objective model has a Lagrange function. Return true.

source
CTModels.has_lagrange_costMethod
has_lagrange_cost(
    model::CTModels.LagrangeObjectiveModel
) -> Bool

Check if the Lagrange objective model has a Lagrange function. Return true.

source
CTModels.has_lagrange_costMethod
has_lagrange_cost(
    model::CTModels.MayerObjectiveModel
) -> Bool

Check if the Mayer objective model has a Lagrange function. Return false.

source
CTModels.has_mayer_costMethod
has_mayer_cost(model::CTModels.BolzaObjectiveModel) -> Bool

Check if the Bolza objective model has a Mayer function. Return true.

source
CTModels.has_mayer_costMethod
has_mayer_cost(
    model::CTModels.LagrangeObjectiveModel
) -> Bool

Check if the Lagrange objective model has a Mayer function. Return false.

source
CTModels.has_mayer_costMethod
has_mayer_cost(model::CTModels.MayerObjectiveModel) -> Bool

Check if the Mayer objective model has a Mayer function. Return true.

source
CTModels.import_ocp_solutionMethod
import_ocp_solution(
    ocp::CTModels.AbstractModel;
    format,
    filename
)

Import a solution from a JLD or JSON file.

Examples

julia> sol = CTModels.import_ocp_solution(ocp; filename="solution", format=:JSON)
julia> sol = CTModels.import_ocp_solution(ocp; filename="solution", format=:JLD)
source
CTModels.indexMethod
index(model::CTModels.FreeTimeModel) -> Int64

Get the index of the time variable from the free time model.

source
CTModels.infosMethod
infos(sol::CTModels.Solution) -> Dict{Symbol, Any}

Return a dictionary of additional infos depending on the solver or nothing.

source
CTModels.initialMethod
initial(
    model::CTModels.TimesModel{TI<:CTModels.AbstractTimeModel}
) -> CTModels.AbstractTimeModel

Get the initial time from the times model.

source
CTModels.initial_timeMethod
initial_time(
    model::CTModels.TimesModel{<:CTModels.FixedTimeModel{T<:Real}}
) -> Real

Get the initial time from the times model, from a fixed initial time model.

source
CTModels.initial_timeMethod
initial_time(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.TimesModel{CTModels.FixedTimeModel{T<:Real}}}
) -> Real

Get the initial time from the model, for a fixed initial time.

source
CTModels.initial_timeMethod
initial_time(
    model::CTModels.TimesModel{CTModels.FreeTimeModel},
    variable::AbstractArray{T<:Real, 1}
) -> Any

Get the initial time from the times model, from a free initial time model.

source
CTModels.initial_timeMethod
initial_time(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.TimesModel{CTModels.FreeTimeModel}},
    variable::AbstractArray{T<:Real, 1}
) -> Any

Get the initial time from the model, for a free initial time.

source
CTModels.initial_timeMethod
initial_time(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.TimesModel{CTModels.FreeTimeModel}},
    variable::Real
) -> Real

Get the initial time from the model, for a free initial time.

source
CTModels.initial_time_nameMethod
initial_time_name(sol::CTModels.Solution) -> String

Return the name of the initial time of the optimal control solution.

source
CTModels.is_autonomousMethod
is_autonomous(
    _::Model{CTModels.Autonomous, <:CTModels.TimesModel}
) -> Bool

Return true if the model is autonomous.

source
CTModels.iterationsMethod
iterations(sol::CTModels.Solution) -> Int64

Return the number of iterations (if solved by an iterative method) of the optimal control solution.

source
CTModels.lagrangeMethod
lagrange(
    model::CTModels.BolzaObjectiveModel{<:Function, L<:Function}
) -> Function

Get the Lagrange function of the Bolza objective model.

source
CTModels.lagrangeMethod
lagrange(
    model::CTModels.LagrangeObjectiveModel{L<:Function}
) -> Function

Get the Lagrange function of the Lagrange objective model.

source
CTModels.lagrangeMethod
lagrange(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.AbstractTimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.AbstractVariableModel, <:Function, <:CTModels.BolzaObjectiveModel{<:Function, L<:Function}}
) -> Any

Get the Lagrange cost from the model.

source
CTModels.lagrangeMethod
lagrange(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.AbstractTimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.AbstractVariableModel, <:Function, CTModels.LagrangeObjectiveModel{L<:Function}}
) -> Function

Get the Lagrange cost from the model.

source
CTModels.matrix2vecFunction
matrix2vec(A::Matrix{<:Real}) -> Vector{<:Vector{<:Real}}
matrix2vec(
    A::Matrix{<:Real},
    dim::Int64
) -> Vector{<:Vector{<:Real}}

Transform a matrix into a vector of vectors along the specified dimension.

Each row or column of the matrix A is extracted and stored as an individual vector, depending on dim.

Arguments

  • A: A matrix of elements of type <:ctNumber.
  • dim: The dimension along which to split the matrix (1 for rows, 2 for columns). Defaults to 1.

Returns

A Vector of Vectors extracted from the rows or columns of A.

Note

This is useful when data needs to be represented as a sequence of state or control vectors in optimal control problems.

Example

julia> A = [1 2 3; 4 5 6]
julia> matrix2vec(A, 1)  # splits into rows: [[1, 2, 3], [4, 5, 6]]
julia> matrix2vec(A, 2)  # splits into columns: [[1, 4], [2, 5], [3, 6]]
source
CTModels.mayerMethod
mayer(
    model::CTModels.BolzaObjectiveModel{M<:Function}
) -> Function

Get the Mayer function of the Bolza objective model.

source
CTModels.mayerMethod
mayer(
    model::CTModels.MayerObjectiveModel{M<:Function}
) -> Function

Get the Mayer function of the Mayer objective model.

source
CTModels.mayerMethod
mayer(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.AbstractTimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.AbstractVariableModel, <:Function, <:CTModels.BolzaObjectiveModel{M<:Function}}
) -> Any

Get the Mayer cost from the model.

source
CTModels.mayerMethod
mayer(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.AbstractTimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.AbstractVariableModel, <:Function, <:CTModels.MayerObjectiveModel{M<:Function}}
) -> Any

Get the Mayer cost from the model.

source
CTModels.messageMethod
message(sol::CTModels.Solution) -> String

Return the message associated to the stopping criterion of the optimal control solution.

source
CTModels.modelMethod
model(
    sol::CTModels.Solution{<:CTModels.AbstractTimeGridModel, <:CTModels.AbstractTimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.AbstractVariableModel, <:Function, <:Real, <:CTModels.AbstractDualModel, <:CTModels.AbstractSolverInfos, TM<:CTModels.AbstractModel}
) -> CTModels.AbstractModel
source
CTModels.nameMethod
name(model::CTModels.ControlModelSolution) -> String

Get the name of the control variable from the solution.

Arguments

  • model::ControlModelSolution: The control model solution.

Returns

  • String: The name of the control.
source
CTModels.nameMethod
name(model::CTModels.ControlModel) -> String

Get the name of the control variable.

Arguments

  • model::ControlModel: The control model.

Returns

  • String: The name of the control.

Example

julia> name(controlmodel)
"u"
source
CTModels.nameMethod
name(_::CTModels.EmptyVariableModel) -> String

Return an empty string, since no variable is defined.

source
CTModels.nameMethod
name(model::CTModels.FixedTimeModel) -> String

Get the name of the time from the fixed time model.

source
CTModels.nameMethod
name(model::CTModels.FreeTimeModel) -> String

Get the name of the time from the free time model.

source
CTModels.nameMethod
name(model::CTModels.StateModelSolution) -> String

Get the name of the state from the state model solution.

source
CTModels.nameMethod
name(model::CTModels.StateModel) -> String

Get the name of the state from the state model.

source
CTModels.nameMethod
name(model::CTModels.VariableModelSolution) -> String

Return the name of the variable stored in the model solution.

source
CTModels.nameMethod
name(model::CTModels.VariableModel) -> String

Return the name of the variable stored in the model.

source
CTModels.objective!Function
objective!(ocp::CTModels.PreModel; ...)
objective!(
    ocp::CTModels.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 or :max. 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, control and variable must be set before the objective.
  • 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> function mayer(x0, xf, v)
           return x0[1] + xf[1] + v[1]
       end
juila> function lagrange(t, x, u, v)
           return x[1] + u[1] + v[1]
       end
julia> objective!(ocp, :min, mayer=mayer, lagrange=lagrange)
source
CTModels.objectiveMethod
objective(
    sol::CTModels.Solution{<:CTModels.AbstractTimeGridModel, <:CTModels.AbstractTimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.AbstractVariableModel, <:Function, O<:Real}
) -> Real

Return the objective value of the optimal control solution.

source
CTModels.objectiveMethod
objective(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.AbstractTimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.AbstractVariableModel, <:Function, O<:CTModels.AbstractObjectiveModel}
) -> CTModels.AbstractObjectiveModel

Get the objective from the model.

source
CTModels.path_constraints_dualMethod
path_constraints_dual(
    sol::CTModels.Solution
) -> Union{Nothing, Function}

Return the dual of the path constraints of the optimal control solution.

source
CTModels.path_constraints_dualMethod
path_constraints_dual(
    model::CTModels.DualModel{PC_Dual<:Union{Nothing, Function}}
) -> Union{Nothing, Function}

Return the dual function associated with the nonlinear path constraints.

Arguments

  • model::DualModel: A model including dual variables for path constraints.

Returns

A function mapping time t to the vector of dual values, or nothing if not set.

source
CTModels.path_constraints_nlMethod
path_constraints_nl(
    model::CTModels.ConstraintsModel{TP}
) -> Any

Get the nonlinear path constraints from the model.

Arguments

  • model: The constraints model from which to retrieve the path constraints.

Returns

  • The nonlinear path constraints.

Example

# Example of retrieving nonlinear path constraints
julia> model = ConstraintsModel(...)
julia> path_constraints = path_constraints_nl(model)
source
CTModels.path_constraints_nlMethod
path_constraints_nl(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.TimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.AbstractVariableModel, <:Function, <:CTModels.AbstractObjectiveModel, <:CTModels.ConstraintsModel{TP<:Tuple}}
) -> Any

Get the nonlinear path constraints from the model.

source
CTModels.state!Method
state!(ocp::CTModels.PreModel, n::Int64)
state!(
    ocp::CTModels.PreModel,
    n::Int64,
    name::Union{String, Symbol}
)
state!(
    ocp::CTModels.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.

Examples

julia> state!(ocp, 1)
julia> state_dimension(ocp)
1
julia> state_components(ocp)
["x"]

julia> state!(ocp, 1, "y")
julia> state_dimension(ocp)
1
julia> state_components(ocp)
["y"]

julia> state!(ocp, 2)
julia> state_dimension(ocp)
2
julia> state_components(ocp)
["x₁", "x₂"]

julia> state!(ocp, 2, :y)
julia> state_dimension(ocp)
2
julia> state_components(ocp)
["y₁", "y₂"]

julia> state!(ocp, 2, "y")
julia> state_dimension(ocp)
2
julia> state_components(ocp)
["y₁", "y₂"]

julia> state!(ocp, 2, "y", ["u", "v"])
julia> state_dimension(ocp)
2
julia> state_components(ocp)
["u", "v"]

julia> state!(ocp, 2, "y", [:u, :v])
julia> state_dimension(ocp)
2
julia> state_components(ocp)
["u", "v"]
source
CTModels.stateMethod
state(
    sol::CTModels.Solution{<:CTModels.AbstractTimeGridModel, <:CTModels.AbstractTimesModel, <:CTModels.StateModelSolution{TS<:Function}}
) -> Function

Return the state (function of time) of the optimal control solution.

julia> t0 = time_grid(sol)[1]
julia> x  = state(sol)
julia> x0 = x(t0)
source
CTModels.stateMethod
state(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.TimesModel, T<:CTModels.AbstractStateModel}
) -> CTModels.AbstractStateModel

Get the state from the model.

source
CTModels.state_componentsMethod
state_components(sol::CTModels.Solution) -> Vector{String}

Return the names of the components of the state of the optimal control solution.

source
CTModels.state_constraints_boxMethod
state_constraints_box(
    model::CTModels.ConstraintsModel{<:Tuple, <:Tuple, TS}
) -> Any

Get the state box constraints from the model.

Arguments

  • model: The constraints model from which to retrieve the state box constraints.

Returns

  • The state box constraints.

Example

# Example of retrieving state box constraints
julia> model = ConstraintsModel(...)
julia> state_constraints = state_constraints_box(model)
source
CTModels.state_constraints_boxMethod
state_constraints_box(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.TimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.AbstractVariableModel, <:Function, <:CTModels.AbstractObjectiveModel, <:CTModels.ConstraintsModel{<:Tuple, <:Tuple, TS<:Tuple}}
) -> Any

Get the box constraints on state from the model.

source
CTModels.state_constraints_lb_dualMethod
state_constraints_lb_dual(
    sol::CTModels.Solution
) -> Union{Nothing, Function}

Return the lower bound dual of the state constraints of the optimal control solution.

source
CTModels.state_constraints_lb_dualMethod
state_constraints_lb_dual(
    model::CTModels.DualModel{<:Union{Nothing, Function}, <:Union{Nothing, AbstractVector{<:Real}}, SC_LB_Dual<:Union{Nothing, Function}}
) -> Union{Nothing, Function}

Return the dual function associated with the lower bounds of state constraints.

Arguments

  • model::DualModel: A model including dual variables for state lower bounds.

Returns

A function mapping time t to a vector of dual values, or nothing if not set.

source
CTModels.state_constraints_ub_dualMethod
state_constraints_ub_dual(
    sol::CTModels.Solution
) -> Union{Nothing, Function}

Return the upper bound dual of the state constraints of the optimal control solution.

source
CTModels.state_constraints_ub_dualMethod
state_constraints_ub_dual(
    model::CTModels.DualModel{<:Union{Nothing, Function}, <:Union{Nothing, AbstractVector{<:Real}}, <:Union{Nothing, Function}, SC_UB_Dual<:Union{Nothing, Function}}
) -> Union{Nothing, Function}

Return the dual function associated with the upper bounds of state constraints.

Arguments

  • model::DualModel: A model including dual variables for state upper bounds.

Returns

A function mapping time t to a vector of dual values, or nothing if not set.

source
CTModels.state_dimensionMethod
state_dimension(sol::CTModels.Solution) -> Int64

Return the dimension of the state of the optimal control solution.

source
CTModels.state_nameMethod
state_name(sol::CTModels.Solution) -> String

Return the name of the state of the optimal control solution.

source
CTModels.stoppingMethod
stopping(sol::CTModels.Solution) -> Symbol

Return the stopping criterion (a Symbol) of the optimal control solution.

source
CTModels.successMethod
success(sol::CTModels.Solution) -> Bool

Return the success status of the optimal control solution.

source
CTModels.time!Method
time!(ocp::CTModels.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.

Examples

julia> time!(ocp, t0=0,   tf=1  ) # Fixed t0 and fixed tf
julia> time!(ocp, t0=0,   indf=2) # Fixed t0 and free  tf
julia> time!(ocp, ind0=2, tf=1  ) # Free  t0 and fixed tf
julia> time!(ocp, ind0=2, indf=3) # Free  t0 and free  tf

When you plot a solution of an optimal control problem, the name of the time variable appears. By default, the name is "t". Consider you want to set the name of the time variable to "s".

julia> time!(ocp, t0=0, tf=1, time_name="s") # time_name is a String
# or
julia> time!(ocp, t0=0, tf=1, time_name=:s ) # time_name is a Symbol  
source
CTModels.timeMethod
time(model::CTModels.FixedTimeModel{T<:Real}) -> Real

Get the time from the fixed time model.

source
CTModels.timeMethod
time(
    model::CTModels.FreeTimeModel,
    variable::AbstractArray{T<:Real, 1}
) -> Any

Get the time from the free time model.

Exceptions

  • If the index of the time variable is not in [1, length(variable)], throw an error.
source
CTModels.time_gridMethod
time_grid(
    sol::CTModels.Solution{<:CTModels.TimeGridModel{T<:Union{StepRangeLen, AbstractVector{<:Real}}}}
) -> Union{StepRangeLen, AbstractVector{<:Real}}

Return the time grid of the optimal control solution.

source
CTModels.time_nameMethod
time_name(sol::CTModels.Solution) -> String

Return the name of the time component of the optimal control solution.

source
CTModels.time_nameMethod
time_name(model::CTModels.TimesModel) -> String

Get the name of the time variable from the times model.

source
CTModels.timesMethod
times(
    ocp::Model{<:CTModels.TimeDependence, T<:CTModels.TimesModel}
) -> CTModels.TimesModel

Get the times from the model.

source
CTModels.to_out_of_placeMethod
to_out_of_place(
    f!,
    n;
    T
) -> Union{Nothing, CTModels.var"#f#13"{CTModels.var"#f#12#14"{DataType, _A, _B}} where {_A, _B}}

Convert an in-place function f! to an out-of-place function f.

The resulting function f returns a vector of type T and length n by first allocating memory and then calling f! to fill it.

Arguments

  • f!: An in-place function of the form f!(result, args...).
  • n: The length of the output vector.
  • T: The element type of the output vector (default is Float64).

Returns

An out-of-place function f(args...; kwargs...) that returns the result as a vector or scalar, depending on n.

Example

julia> f!(r, x) = (r[1] = sin(x); r[2] = cos(x))
julia> f = to_out_of_place(f!, 2)
julia> f(π/4)  # returns approximately [0.707, 0.707]
source
CTModels.valueMethod
value(
    model::CTModels.ControlModelSolution{TS<:Function}
) -> Function

Get the control function associated with the solution.

Arguments

  • model::ControlModelSolution{TS}: The control model solution.

Returns

  • TS: A function giving the control value at a given time or state.
source
CTModels.valueMethod
value(
    model::CTModels.StateModelSolution{TS<:Function}
) -> Function

Get the state function from the state model solution.

source
CTModels.valueMethod
value(
    model::CTModels.VariableModelSolution{TS<:Union{Real, AbstractVector{<:Real}}}
) -> Union{Real, AbstractVector{<:Real}}

Return the value stored in the variable solution model.

source
CTModels.variable!Method
variable!(ocp::CTModels.PreModel, q::Int64)
variable!(
    ocp::CTModels.PreModel,
    q::Int64,
    name::Union{String, Symbol}
)
variable!(
    ocp::CTModels.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

variable!(ocp, 1, "v")
variable!(ocp, 2, "v", ["v₁", "v₂"])
source
CTModels.variableMethod
variable(
    sol::CTModels.Solution{<:CTModels.AbstractTimeGridModel, <:CTModels.AbstractTimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.VariableModelSolution{TS<:Union{Real, AbstractVector{<:Real}}}}
) -> Union{Real, AbstractVector{<:Real}}

Return the variable of the optimal control solution or nothing.

julia> v  = variable(sol)
source
CTModels.variableMethod
variable(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.TimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, T<:CTModels.AbstractVariableModel}
) -> CTModels.AbstractVariableModel

Get the variable from the model.

source
CTModels.variable_componentsMethod
variable_components(
    sol::CTModels.Solution
) -> Vector{String}

Return the names of the components of the variable of the optimal control solution.

source
CTModels.variable_constraints_boxMethod
variable_constraints_box(
    model::CTModels.ConstraintsModel{<:Tuple, <:Tuple, <:Tuple, <:Tuple, TV}
) -> Any

Get the variable box constraints from the model.

Arguments

  • model: The constraints model from which to retrieve the variable box constraints.

Returns

  • The variable box constraints.

Example

# Example of retrieving variable box constraints
julia> model = ConstraintsModel(...)
julia> variable_constraints = variable_constraints_box(model)
source
CTModels.variable_constraints_boxMethod
variable_constraints_box(
    ocp::Model{<:CTModels.TimeDependence, <:CTModels.TimesModel, <:CTModels.AbstractStateModel, <:CTModels.AbstractControlModel, <:CTModels.AbstractVariableModel, <:Function, <:CTModels.AbstractObjectiveModel, <:CTModels.ConstraintsModel{<:Tuple, <:Tuple, <:Tuple, <:Tuple, TV<:Tuple}}
) -> Any

Get the box constraints on variable from the model.

source
CTModels.variable_constraints_lb_dualMethod
variable_constraints_lb_dual(
    sol::CTModels.Solution
) -> Union{Nothing, AbstractVector{<:Real}}

Return the lower bound dual of the variable constraints of the optimal control solution.

source
CTModels.variable_constraints_lb_dualMethod
variable_constraints_lb_dual(
    model::CTModels.DualModel{<:Union{Nothing, Function}, <:Union{Nothing, AbstractVector{<:Real}}, <:Union{Nothing, Function}, <:Union{Nothing, Function}, <:Union{Nothing, Function}, <:Union{Nothing, Function}, VC_LB_Dual<:Union{Nothing, AbstractVector{<:Real}}}
) -> Union{Nothing, AbstractVector{<:Real}}

Return the dual vector associated with the lower bounds of variable constraints.

Arguments

  • model::DualModel: A model including dual variables for variable lower bounds.

Returns

A vector of dual values, or nothing if not set.

source
CTModels.variable_constraints_ub_dualMethod
variable_constraints_ub_dual(
    sol::CTModels.Solution
) -> Union{Nothing, AbstractVector{<:Real}}

Return the upper bound dual of the variable constraints of the optimal control solution.

source
CTModels.variable_constraints_ub_dualMethod
variable_constraints_ub_dual(
    model::CTModels.DualModel{<:Union{Nothing, Function}, <:Union{Nothing, AbstractVector{<:Real}}, <:Union{Nothing, Function}, <:Union{Nothing, Function}, <:Union{Nothing, Function}, <:Union{Nothing, Function}, <:Union{Nothing, AbstractVector{<:Real}}, VC_UB_Dual<:Union{Nothing, AbstractVector{<:Real}}}
) -> Union{Nothing, AbstractVector{<:Real}}

Return the dual vector associated with the upper bounds of variable constraints.

Arguments

  • model::DualModel: A model including dual variables for variable upper bounds.

Returns

A vector of dual values, or nothing if not set.

source
CTModels.variable_dimensionMethod
variable_dimension(sol::CTModels.Solution) -> Int64

Return the dimension of the variable of the optimal control solution.

source
CTModels.variable_nameMethod
variable_name(sol::CTModels.Solution) -> String

Return the name of the variable of the optimal control solution.

source
CTModels.@ensureMacro
@ensure condition exception

Throws the provided exception if condition is false.

Usage

julia> @ensure x > 0 CTBase.IncorrectArgument("x must be positive")

Arguments

  • condition: A Boolean expression to test.
  • exception: An instance of an exception to throw if condition is false.

Throws

  • The provided exception if the condition is not satisfied.
source