Private API

This page lists non-exported (internal) symbols of CTModels.


From CTModels

__build_dynamics_from_parts

CTModels.__build_dynamics_from_partsFunction
__build_dynamics_from_parts(
    parts::Vector{<:Tuple{var"#s38", var"#s37"} where {var"#s38"<:(AbstractRange{<:Int64}), var"#s37"<:Function}}
) -> CTModels.var"#dyn!#__build_dynamics_from_parts##0"{Vector{var"#s43"}} where var"#s43"<:(Tuple{var"#s38", var"#s37"} where {var"#s38"<:(AbstractRange{<:Int64}), var"#s37"<: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]

criterion

CTModels.criterionFunction
criterion(model::CTModels.MayerObjectiveModel) -> Symbol

Return the criterion (:min or :max).

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

Return the criterion (:min or :max).

criterion(model::CTModels.BolzaObjectiveModel) -> Symbol

Return the criterion (:min or :max).

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

Return the type of criterion (:min or :max).

dynamics!

CTModels.dynamics!Function
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.

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])
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])

has_lagrange_cost

CTModels.has_lagrange_costFunction
has_lagrange_cost(_::CTModels.MayerObjectiveModel) -> Bool

Return false.

has_lagrange_cost(
    _::CTModels.LagrangeObjectiveModel
) -> Bool

Return true.

has_lagrange_cost(_::CTModels.BolzaObjectiveModel) -> Bool

Return true.

has_lagrange_cost(ocp::CTModels.Model) -> Bool

Check if the model has a Lagrange cost.

has_mayer_cost

CTModels.has_mayer_costFunction
has_mayer_cost(_::CTModels.MayerObjectiveModel) -> Bool

Return true.

has_mayer_cost(_::CTModels.LagrangeObjectiveModel) -> Bool

Return false.

has_mayer_cost(_::CTModels.BolzaObjectiveModel) -> Bool

Return true.

has_mayer_cost(ocp::CTModels.Model) -> Bool

Check if the model has a Mayer cost.

lagrange

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

Return the Lagrange function.

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

Return the Lagrange function.

lagrange(ocp::CTModels.AbstractModel) -> Function

Throw an error when accessing Lagrange cost on a model without one.

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

Return the Lagrange cost.

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

Return the Lagrange cost.

mayer

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

Return the Mayer function.

mayer(
    model::CTModels.BolzaObjectiveModel{M<:Function}
) -> Function

Return the Mayer function.

mayer(ocp::CTModels.AbstractModel) -> Any

Throw an error when accessing Mayer cost on a model without one.

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

Return the Mayer cost.

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

Return the Mayer cost.

objective!

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
julia> function lagrange(t, x, u, v)
           return x[1] + u[1] + v[1]
       end
julia> objective!(ocp, :min, mayer=mayer, lagrange=lagrange)