Model

Optimal control model type

CTBase.OptimalControlModelType
mutable struct OptimalControlModel{time_dependence<:TimeDependence, variable_dependence<:VariableDependence} <: CTBase.AbstractOptimalControlModel

Fields

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

  • initial_time::Union{Nothing, Index, Real}: Default: nothing

  • initial_time_name::Union{Nothing, String}: Default: nothing

  • final_time::Union{Nothing, Index, Real}: Default: nothing

  • final_time_name::Union{Nothing, String}: Default: nothing

  • time_name::Union{Nothing, String}: Default: nothing

  • control_dimension::Union{Nothing, Integer}: Default: nothing

  • control_components_names::Union{Nothing, Vector{String}}: Default: nothing

  • control_name::Union{Nothing, String}: Default: nothing

  • state_dimension::Union{Nothing, Integer}: Default: nothing

  • state_components_names::Union{Nothing, Vector{String}}: Default: nothing

  • state_name::Union{Nothing, String}: Default: nothing

  • variable_dimension::Union{Nothing, Integer}: Default: nothing

  • variable_components_names::Union{Nothing, Vector{String}}: Default: nothing

  • variable_name::Union{Nothing, String}: Default: nothing

  • lagrange::Union{Nothing, Lagrange}: Default: nothing

  • mayer::Union{Nothing, Mayer}: Default: nothing

  • criterion::Union{Nothing, Symbol}: Default: nothing

  • dynamics::Union{Nothing, Dynamics}: Default: nothing

  • constraints::Dict{Symbol, Tuple}: Default: Dict{Symbol, Tuple{Vararg{Any}}}()

  • dim_control_constraints::Union{Nothing, Integer}: Default: nothing

  • dim_state_constraints::Union{Nothing, Integer}: Default: nothing

  • dim_mixed_constraints::Union{Nothing, Integer}: Default: nothing

  • dim_boundary_constraints::Union{Nothing, Integer}: Default: nothing

  • dim_variable_constraints::Union{Nothing, Integer}: Default: nothing

  • dim_control_range::Union{Nothing, Integer}: Default: nothing

  • dim_state_range::Union{Nothing, Integer}: Default: nothing

  • dim_variable_range::Union{Nothing, Integer}: Default: nothing

source

Getters

CTBase.constraintMethod
constraint(
    ocp::OptimalControlModel{T<:TimeDependence, V<:VariableDependence},
    label::Symbol
) -> Union{Nothing, VariableConstraint, BoundaryConstraint, ControlConstraint, MixedConstraint, StateConstraint}

Retrieve a labeled constraint. The result is a function associated with the constraint computation (not taking into account provided value / bounds).

Example

julia> constraint!(ocp, :initial, 0, :c0)
julia> c = constraint(ocp, :c0)
julia> c(1)
1
source
CTBase.constraints_labelsMethod
constraints_labels(
    ocp::OptimalControlModel
) -> Base.KeySet{Symbol, Dict{Symbol, Tuple}}

Return the labels of the constraints as a Base.keys.

source
CTBase.control_components_namesMethod
control_components_names(
    ocp::OptimalControlModel
) -> Union{Nothing, Vector{String}}

Return the names of the components of the control of the optimal control problem or nothing.

source
CTBase.control_dimensionMethod
control_dimension(
    ocp::OptimalControlModel
) -> Union{Nothing, Integer}

Return the dimention of the control of the optimal control problem or nothing.

source
CTBase.control_nameMethod
control_name(
    ocp::OptimalControlModel
) -> Union{Nothing, String}

Return the name of the control of the optimal control problem or nothing.

source
CTBase.criterionMethod
criterion(
    ocp::OptimalControlModel
) -> Union{Nothing, Symbol}

Return the criterion (:min or :max) of the optimal control problem or nothing.

source
CTBase.dim_boundary_constraintsMethod
dim_boundary_constraints(
    ocp::OptimalControlModel
) -> Union{Nothing, Integer}

Return the dimension of the boundary constraints (nothing if not knonw). Information is updated after nlp_constraints! is called.

source
CTBase.dim_control_constraintsMethod
dim_control_constraints(
    ocp::OptimalControlModel
) -> Union{Nothing, Integer}

Return the dimension of nonlinear control constraints (nothing if not knonw). Information is updated after nlp_constraints! is called.

source
CTBase.dim_control_rangeMethod
dim_control_range(
    ocp::OptimalControlModel
) -> Union{Nothing, Integer}

Return the dimension of range constraints on control (nothing if not knonw). Information is updated after nlp_constraints! is called.

source
CTBase.dim_mixed_constraintsMethod
dim_mixed_constraints(
    ocp::OptimalControlModel
) -> Union{Nothing, Integer}

Return the dimension of nonlinear mixed constraints (nothing if not knonw). Information is updated after nlp_constraints! is called.

source
CTBase.dim_path_constraintsMethod
dim_path_constraints(ocp::OptimalControlModel) -> Any

Return the dimension of nonlinear path (state + control + mixed) constraints (nothing if one of them is not knonw). Information is updated after nlp_constraints! is called.

source
CTBase.dim_state_constraintsMethod
dim_state_constraints(
    ocp::OptimalControlModel
) -> Union{Nothing, Integer}

Return the dimension of nonlinear state constraints (nothing if not knonw). Information is updated after nlp_constraints! is called.

source
CTBase.dim_state_rangeMethod
dim_state_range(
    ocp::OptimalControlModel
) -> Union{Nothing, Integer}

Return the dimension of range constraints on state (nothing if not knonw). Information is updated after nlp_constraints! is called.

source
CTBase.dim_variable_constraintsMethod
dim_variable_constraints(
    ocp::OptimalControlModel
) -> Union{Nothing, Integer}

Return the dimension of nonlinear variable constraints (nothing if not knonw). Information is updated after nlp_constraints! is called.

source
CTBase.dim_variable_rangeMethod
dim_variable_range(
    ocp::OptimalControlModel
) -> Union{Nothing, Integer}

Return the dimension of range constraints on variable (nothing if not knonw). Information is updated after nlp_constraints! is called.

source
CTBase.dynamicsMethod
dynamics(
    ocp::OptimalControlModel
) -> Union{Nothing, Dynamics}

Return the dynamics of the optimal control problem or nothing.

source
CTBase.final_timeMethod
final_time(
    ocp::OptimalControlModel
) -> Union{Nothing, Index, Real}

Return the final time of the optimal control problem or nothing.

source
CTBase.final_time_nameMethod
final_time_name(
    ocp::OptimalControlModel
) -> Union{Nothing, String}

Return the name of the final time of the optimal control problem or nothing.

source
CTBase.has_free_final_timeMethod
has_free_final_time(ocp::OptimalControlModel) -> Bool

Return true if the model has been defined with free final time.

source
CTBase.has_lagrange_costMethod
has_lagrange_cost(ocp::OptimalControlModel) -> Bool

Return true if the model has been defined with lagrange cost.

source
CTBase.has_mayer_costMethod
has_mayer_cost(ocp::OptimalControlModel) -> Bool

Return true if the model has been defined with mayer cost.

source
CTBase.initial_timeMethod
initial_time(
    ocp::OptimalControlModel
) -> Union{Nothing, Index, Real}

Return the initial time of the optimal control problem or nothing.

source
CTBase.initial_time_nameMethod
initial_time_name(
    ocp::OptimalControlModel
) -> Union{Nothing, String}

Return the name of the initial time of the optimal control problem or nothing.

source
CTBase.is_autonomousMethod
is_autonomous(ocp::OptimalControlModel{Autonomous}) -> Bool

Return true if the model is autonomous.

source
CTBase.is_fixedMethod
is_fixed(
    ocp::OptimalControlModel{<:TimeDependence, Fixed}
) -> Bool

Return true if the model is fixed (= has no variable).

source
CTBase.is_maxMethod
is_max(ocp::OptimalControlModel) -> Bool

Return true if the criterion type of ocp is :max.

source
CTBase.is_minMethod
is_min(ocp::OptimalControlModel) -> Bool

Return true if the criterion type of ocp is :min.

source
CTBase.is_time_dependentMethod
is_time_dependent(ocp::OptimalControlModel) -> Bool

Return true if the model has been defined as time dependent.

source
CTBase.lagrangeMethod
lagrange(
    ocp::OptimalControlModel
) -> Union{Nothing, Lagrange}

Return the Lagrange part of the cost of the optimal control problem or nothing.

source
CTBase.mayerMethod
mayer(ocp::OptimalControlModel) -> Union{Nothing, Mayer}

Return the Mayer part of the cost of the optimal control problem or nothing.

source
CTBase.model_expressionMethod
model_expression(
    ocp::OptimalControlModel
) -> Union{Nothing, Expr}

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

source
CTBase.nlp_constraints!Method
nlp_constraints!(
    ocp::OptimalControlModel
) -> Tuple{Tuple{Any, CTBase.var"#ξ#41", Vector{Real}}, Tuple{Any, CTBase.var"#η#42", Vector{Real}}, Tuple{Any, CTBase.var"#ψ#43", Vector{Real}}, Tuple{Any, CTBase.var"#ϕ#44", Vector{Real}}, Tuple{Any, CTBase.var"#θ#45", Vector{Real}}, Tuple{Vector{Real}, Vector{Int64}, Vector{Real}}, Tuple{Vector{Real}, Vector{Int64}, Vector{Real}}, Tuple{Vector{Real}, Vector{Int64}, Vector{Real}}}

Return a 6-tuple of tuples:

  • (ξl, ξ, ξu) are control constraints
  • (ηl, η, ηu) are state constraints
  • (ψl, ψ, ψu) are mixed constraints
  • (ϕl, ϕ, ϕu) are boundary constraints
  • (θl, θ, θu) are variable constraints
  • (ul, uind, uu) are control linear constraints of a subset of indices
  • (xl, xind, xu) are state linear constraints of a subset of indices
  • (vl, vind, vu) are variable linear constraints of a subset of indices

and update information about constraints dimensions of ocp.

Note
  • The dimensions of the state and control must be set before calling nlp_constraints!.
  • For a Fixed problem, dimensions associated with constraints on the variable are set to zero.

Example

julia> (ξl, ξ, ξu), (ηl, η, ηu), (ψl, ψ, ψu), (ϕl, ϕ, ϕu), (θl, θ, θu),
    (ul, uind, uu), (xl, xind, xu), (vl, vind, vu) = nlp_constraints!(ocp)
source
CTBase.state_components_namesMethod
state_components_names(
    ocp::OptimalControlModel
) -> Union{Nothing, Vector{String}}

Return the names of the components of the state of the optimal control problem or nothing.

source
CTBase.state_dimensionMethod
state_dimension(
    ocp::OptimalControlModel
) -> Union{Nothing, Integer}

Return the dimension of the state of the optimal control problem or nothing.

source
CTBase.state_nameMethod
state_name(
    ocp::OptimalControlModel
) -> Union{Nothing, String}

Return the name of the state of the optimal control problem or nothing.

source
CTBase.time_nameMethod
time_name(
    ocp::OptimalControlModel
) -> Union{Nothing, String}

Return the name of the time component of the optimal control problem or nothing.

source
CTBase.variable_components_namesMethod
variable_components_names(
    ocp::OptimalControlModel
) -> Union{Nothing, Vector{String}}

Return the names of the components of the variable of the optimal control problem or nothing.

source
CTBase.variable_dimensionMethod
variable_dimension(
    ocp::OptimalControlModel
) -> Union{Nothing, Integer}

Return the dimension of the variable of the optimal control problem or nothing.

source
CTBase.variable_nameMethod
variable_name(
    ocp::OptimalControlModel
) -> Union{Nothing, String}

Return the name of the variable of the optimal control problem or nothing.

source

Constructors and setters

CTBase.ModelMethod
Model(
    dependencies::DataType...
) -> OptimalControlModel{Autonomous, Fixed}

Return a new OptimalControlModel instance, that is a model of an optimal control problem.

The model is defined by the following argument:

  • dependencies: either Autonomous or NonAutonomous. Default is Autonomous. And either NonFixed or Fixed. Default is Fixed.

Examples

julia> ocp = Model()
julia> ocp = Model(NonAutonomous)
julia> ocp = Model(Autonomous, NonFixed)
Note
  • If the time dependence of the model is defined as nonautonomous, then, the dynamics function, the lagrange cost and the path constraints must be defined as functions of time and state, and possibly control. If the model is defined as autonomous, then, the dynamics function, the lagrange cost and the path constraints must be defined as functions of state, and possibly control.
source
CTBase.ModelMethod
Model(
;
    autonomous,
    variable
) -> OptimalControlModel{Autonomous, Fixed}

Return a new OptimalControlModel instance, that is a model of an optimal control problem.

The model is defined by the following optional keyword argument:

  • autonomous: either true or false. Default is true.
  • variable: either true or false. Default is false.

Examples

julia> ocp = Model()
julia> ocp = Model(autonomous=false)
julia> ocp = Model(autonomous=false, variable=true)
Note
  • If the time dependence of the model is defined as nonautonomous, then, the dynamics function, the lagrange cost and the path constraints must be defined as functions of time and state, and possibly control. If the model is defined as autonomous, then, the dynamics function, the lagrange cost and the path constraints must be defined as functions of state, and possibly control.
source
CTBase.constraint!Method
constraint!(
    ocp::OptimalControlModel{T<:TimeDependence, V<:VariableDependence},
    type::Symbol;
    rg,
    f,
    lb,
    ub,
    val,
    label
)

Add a constraint to an optimal control problem, denoted ocp.

Note
  • The state, control and variable dimensions must be set before. Use state!, control! and variable!.
  • The initial and final times must be set before. Use time!.
  • When an element is of dimension 1, consider it as a scalar.

You can add an :initial, :final, :control, :state or :variable box constraint (whole range).

Range constraint on the state, control or variable

You can add an :initial, :final, :control, :state or :variable box constraint on a range of it, that is only on some components. If not range is specified, then the constraint is on the whole range. We denote by x, u and v respectively the state, control and variable. We denote by n, m and q respectively the dimension of the state, control and variable. The range of the constraint must be contained in 1:n if the constraint is on the state, or 1:m if the constraint is on the control, or 1:q if the constraint is on the variable.

Examples

julia> constraint!(ocp, :initial; rg=1:2:5, lb=[ 0, 0, 0 ], ub=[ 1, 2, 1 ])
julia> constraint!(ocp, :initial; rg=2:3, lb=[ 0, 0 ], ub=[ 1, 2 ])
julia> constraint!(ocp, :final; rg=1, lb=0, ub=2)
julia> constraint!(ocp, :control; rg=1, lb=0, ub=2)
julia> constraint!(ocp, :state; rg=2:3, lb=[ 0, 0 ], ub=[ 1, 2 ])
julia> constraint!(ocp, :variable; rg=1:2, lb=[ 0, 0 ], ub=[ 1, 2 ])
julia> constraint!(ocp, :initial; lb=[ 0, 0, 0 ])                 # [ 0, 0, 0 ] ≤ x(t0),                          dim(x) = 3
julia> constraint!(ocp, :initial; lb=[ 0, 0, 0 ], ub=[ 1, 2, 1 ]) # [ 0, 0, 0 ] ≤ x(t0) ≤ [ 1, 2, 1 ],            dim(x) = 3
julia> constraint!(ocp, :final; lb=-1, ub=1)                      #          -1 ≤ x(tf) ≤ 1,                      dim(x) = 1
julia> constraint!(ocp, :control; lb=0, ub=2)                     #           0 ≤ u(t)  ≤ 2,        t ∈ [t0, tf], dim(u) = 1
julia> constraint!(ocp, :state; lb=[ 0, 0 ], ub=[ 1, 2 ])         #    [ 0, 0 ] ≤ x(t)  ≤ [ 1, 2 ], t ∈ [t0, tf], dim(x) = 2
julia> constraint!(ocp, :variable; lb=[ 0, 0 ], ub=[ 1, 2 ])      #    [ 0, 0 ] ≤    v  ≤ [ 1, 2 ],               dim(v) = 2

Functional constraint

You can add a :boundary, :control, :state, :mixed or :variable box functional constraint.

Examples

# variable independent ocp
julia> constraint!(ocp, :boundary; f = (x0, xf) -> x0[3]+xf[2], lb=0, ub=1)

# variable dependent ocp
julia> constraint!(ocp, :boundary; f = (x0, xf, v) -> x0[3]+xf[2]*v[1], lb=0, ub=1)

# time independent and variable independent ocp
julia> constraint!(ocp, :control; f = u -> 2u, lb=0, ub=1)
julia> constraint!(ocp, :state; f = x -> x-1, lb=[ 0, 0, 0 ], ub=[ 1, 2, 1 ])
julia> constraint!(ocp, :mixed; f = (x, u) -> x[1]-u, lb=0, ub=1)

# time dependent and variable independent ocp
julia> constraint!(ocp, :control; f = (t, u) -> 2u, lb=0, ub=1)
julia> constraint!(ocp, :state; f = (t, x) -> t * x, lb=[ 0, 0, 0 ], ub=[ 1, 2, 1 ])
julia> constraint!(ocp, :mixed; f = (t, x, u) -> x[1]-u, lb=0, ub=1)

# time independent and variable dependent ocp
julia> constraint!(ocp, :control; f = (u, v) -> 2u * v[1], lb=0, ub=1)
julia> constraint!(ocp, :state; f = (x, v) -> x * v[1], lb=[ 0, 0, 0 ], ub=[ 1, 2, 1 ])
julia> constraint!(ocp, :mixed; f = (x, u, v) -> x[1]-v[2]*u, lb=0, ub=1)

# time dependent and variable dependent ocp
julia> constraint!(ocp, :control; f = (t, u, v) -> 2u+v[2], lb=0, ub=1)
julia> constraint!(ocp, :state; f = (t, x, v) -> x-t*v[1], lb=[ 0, 0, 0 ], ub=[ 1, 2, 1 ])
julia> constraint!(ocp, :mixed; f = (t, x, u, v) -> x[1]*v[2]-u, lb=0, ub=1)
source
CTBase.control!Function
control!(ocp::OptimalControlModel, m::Integer)
control!(ocp::OptimalControlModel, m::Integer, name::String)
control!(
    ocp::OptimalControlModel,
    m::Integer,
    name::String,
    components_names::Vector{String}
)

Define the control dimension and possibly the names of each coordinate.

Note

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

Examples

julia> control!(ocp, 1)
julia> ocp.control_dimension
1
julia> ocp.control_components_names
["u"]

julia> control!(ocp, 1, "v")
julia> ocp.control_dimension
1
julia> ocp.control_components_names
["v"]

julia> control!(ocp, 2)
julia> ocp.control_dimension
2
julia> ocp.control_components_names
["u₁", "u₂"]

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

julia> control!(ocp, 2, "v")
julia> ocp.control_dimension
2
julia> ocp.control_components_names
["v₁", "v₂"]
source
CTBase.dynamics!Method
dynamics!(
    ocp::OptimalControlModel{T<:TimeDependence, V<:VariableDependence},
    f::Function
)

Set the dynamics.

Note

You can use dynamics! only once to define the dynamics.

  • The state, control and variable dimensions must be set before. Use state!, control! and variable!.
  • The times must be set before. Use time!.
  • When an element is of dimension 1, consider it as a scalar.

Example

julia> dynamics!(ocp, f)
source
CTBase.objective!Method
objective!(
    ocp::OptimalControlModel{T<:TimeDependence, V<:VariableDependence},
    type::Symbol,
    g::Function,
    f⁰::Function
)
objective!(
    ocp::OptimalControlModel{T<:TimeDependence, V<:VariableDependence},
    type::Symbol,
    g::Function,
    f⁰::Function,
    criterion::Symbol
)

Set the criterion to the function g and f⁰. Type can be :bolza. Criterion is :min or :max.

Note

You can use objective! only once to define the objective.

  • The state, control and variable dimensions must be set before. Use state!, control! and variable!.
  • The times must be set before. Use time!.
  • When an element is of dimension 1, consider it as a scalar.

Example

julia> objective!(ocp, :bolza, (x0, xf) -> x0[1] + xf[2], (x, u) -> x[1]^2 + u^2) # the control is of dimension 1
source
CTBase.objective!Method
objective!(
    ocp::OptimalControlModel{T<:TimeDependence, V<:VariableDependence},
    type::Symbol,
    f::Function
)
objective!(
    ocp::OptimalControlModel{T<:TimeDependence, V<:VariableDependence},
    type::Symbol,
    f::Function,
    criterion::Symbol
)

Set the criterion to the function f. Type can be :mayer or :lagrange. Criterion is :min or :max.

Note

You can use objective! only once to define the objective.

  • The state, control and variable dimensions must be set before. Use state!, control! and variable!.
  • The times must be set before. Use time!.
  • When an element is of dimension 1, consider it as a scalar.

Examples

julia> objective!(ocp, :mayer, (x0, xf) -> x0[1] + xf[2])
julia> objective!(ocp, :lagrange, (x, u) -> x[1]^2 + u^2) # the control is of dimension 1
Warning

If you set twice the objective, only the last one will be taken into account.

source
CTBase.remove_constraint!Method
remove_constraint!(ocp::OptimalControlModel, label::Symbol)

Remove a labeled constraint.

Example

julia> remove_constraint!(ocp, :con)
source
CTBase.state!Function
state!(ocp::OptimalControlModel, n::Integer)
state!(ocp::OptimalControlModel, n::Integer, name::String)
state!(
    ocp::OptimalControlModel,
    n::Integer,
    name::String,
    components_names::Vector{String}
)

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> ocp.state_dimension
1
julia> ocp.state_components_names
["x"]

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

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

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

julia> state!(ocp, 2, "y")
julia> ocp.state_dimension
2
julia> ocp.state_components_names
["y₁", "y₂"]
source
CTBase.time!Method
time!(
    ocp::OptimalControlModel{<:TimeDependence, VT};
    t0,
    tf,
    ind0,
    indf,
    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, name="s") # name is a String
# or
julia> time!(ocp, t0=0, tf=1, name=:s ) # name is a Symbol  
source
CTBase.variable!Function
variable!(ocp::OptimalControlModel, q::Integer)
variable!(
    ocp::OptimalControlModel,
    q::Integer,
    name::String
)
variable!(
    ocp::OptimalControlModel,
    q::Integer,
    name::String,
    components_names::Vector{String}
)

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

Note

You can use variable! once to set the variable dimension when the model is NonFixed.

Examples

julia> variable!(ocp, 1, "v")
julia> variable!(ocp, 2, "v", [ "v₁", "v₂" ])
source