Public API
This page lists exported symbols of CTModels.Models.
From CTModels.Models
CTModels.Models [Module]
CTModels.Models Module
ModelsImmutable optimal control problem model type and all its accessor methods.
Provides AbstractModel, struct Model (parametric over time dependence and component types), and all reader functions that operate on a built Model.
Organisation
hamiltonians.jl: the pseudo-Hamiltonian and Hamiltonian getters
constraint_terms.jl: the constraint terms adjoined to them, by label
model.jl: AbstractModel, Model struct, and all accessor functions (
CTModels.Components.state,CTModels.Components.control,CTModels.Components.variable,CTModels.Components.times,CTModels.Components.objective,CTModels.Models.constraints,CTModels.Models.dynamics,CTModels.Models.definition)
Public API
The following functions are exported and accessible as CTModels.function_name():
Time dependence predicates:
is_autonomous,is_nonautonomousVariable / control presence predicates:
is_variable,is_nonvariable,is_control_free,has_variable,has_control,has_abstract_definition,is_abstractly_definedComponent field accessors:
CTModels.Components.state,CTModels.Components.control,CTModels.Components.variable,CTModels.Components.times,CTModels.Components.objective,CTModels.Models.constraints,CTModels.Models.dynamics,CTModels.Models.definitionNamed accessors on state/control/variable:
CTModels.Models.state_name,CTModels.Models.state_components,CTModels.Models.state_dimension,CTModels.Models.control_name,CTModels.Models.control_components,CTModels.Models.control_dimension,CTModels.Models.variable_name,CTModels.Models.variable_components,CTModels.Models.variable_dimensionExaModels builder:
CTModels.Models.get_build_examodelConstraints helpers:
CTModels.Models.isempty_constraints,CTModels.Models.constraintHamiltonians:
pseudo_hamiltonian,hamiltonian(methods of theCTBase.Datageneric functions)
Dependencies
Depends on Components for foundational types and low-level accessor functions.
See also: CTModels.Components, CTModels.Building, CTModels.Solutions, CTModels.Init.
AbstractModel [Abstract Type]
CTModels.Models.AbstractModel Type
abstract type AbstractModelAbstract base type for optimal control problem models.
Subtypes represent either a fully built immutable model (CTModels.Models.Model) or a mutable model under construction (PreModel).
Model [Struct]
CTModels.Models.Model Type
struct Model{TD<:CTBase.Traits.TimeDependence, TimesModelType<:CTModels.Components.AbstractTimesModel, StateModelType<:CTModels.Components.AbstractStateModel, ControlModelType<:CTModels.Components.AbstractControlModel, VariableModelType<:CTModels.Components.AbstractVariableModel, DynamicsModelType<:Function, ObjectiveModelType<:CTModels.Components.AbstractObjectiveModel, ConstraintsModelType<:CTModels.Components.AbstractConstraintsModel, DefinitionType<:CTModels.Components.AbstractDefinition, BuildExaModelType<:Union{Nothing, Function}} <: CTModels.Models.AbstractModelImmutable optimal control problem model containing all problem components.
A Model is created from a PreModel once all required fields have been set. It is parameterised by the time dependence type (Autonomous or NonAutonomous) and the types of all its components.
Fields
times::TimesModelType: Initial and final time specification.state::StateModelType: State variable structure (name, components).control::ControlModelType: Control variable structure (name, components).variable::VariableModelType: Optimisation variable structure (may be empty).dynamics::DynamicsModelType: System dynamics function(t, x, u, v) -> ẋ.objective::ObjectiveModelType: Cost functional (Mayer, Lagrange, or Bolza).constraints::ConstraintsModelType: All problem constraints.definition::DefinitionType: Original symbolic definition of the problem.build_examodel::BuildExaModelType: Optional ExaModels builder function.
constraint [Function]
CTModels.Models.constraint Function
constraint(
model::CTModels.Models.Model,
label::Symbol
) -> Tuple{Symbol, Any, Any, Any}Get a labelled constraint from the model, as a tuple (type, f, lb, ub).
type is the kind of the constraint (:path, :boundary, :state, :control or :variable), f an out-of-place function evaluating the constrained quantity, and lb, ub its bounds. f is a named functor, so it prints as such:
type | f | Call |
|---|---|---|
:path | CTModels.Models.SubPathConstraint | f(t, x, u, v) |
:boundary | CTModels.Models.SubBoundaryConstraint | f(x0, xf, v) |
:state | CTModels.Models.BoxProjection{:state} | f(t, x, u, v) |
:control | CTModels.Models.BoxProjection{:control} | f(t, x, u, v) |
:variable | CTModels.Models.BoxProjection{:variable} | f(x0, xf, v) |
A constraint of dimension one gives a scalar value and scalar bounds (1-D is a scalar).
Arguments
model::Model: The optimal control problem.label::Symbol: The constraint label.
Returns
Tuple:(type, f, lb, ub).
Throws
CTBase.Exceptions.IncorrectArgument: if no constraint of the model has the labellabel.
See also: CTModels.Models.constraints, CTModels.Components.path_constraints_nl.
constraint_rows [Function]
CTModels.Models.constraint_rows Function
constraint_rows(
model::CTModels.Models.Model,
label::Symbol
) -> Tuple{Symbol, Vector{Int64}}Return the kind of the constraint labelled label and the rows it occupies, (kind, rows).
kindis:pathor:boundaryfor a nonlinear constraint, androwsits rows in the stacked constraint vector (path_constraints_nl(model),boundary_constraints_nl(model)), in increasing order. The rows of a label are contiguous, in the order of declaration.kindis:state,:controlor:variablefor a box, androwsthe components it bounds. A box label is any of the aliases of a deduplicated box declaration.
This is the layout of the duals of the solution: the dual of a path or boundary label is stored in the rows rows of the stacked dual, the dual of a box label in the components rows of the box duals.
Throws
CTBase.Exceptions.IncorrectArgument: iflabelis not a constraint of the model.
See also: CTModels.Models.constraint, CTModels.Solutions.dual.
constraints [Function]
CTModels.Models.constraints Function
constraints(
ocp::CTModels.Models.Model{<:CTBase.Traits.TimeDependence, <:CTModels.Components.AbstractTimesModel, <:CTModels.Components.AbstractStateModel, <:CTModels.Components.AbstractControlModel, <:CTModels.Components.AbstractVariableModel, <:Function, <:CTModels.Components.AbstractObjectiveModel, C}
) where C<:CTModels.Components.AbstractConstraintsModel -> CTModels.Components.AbstractConstraintsModelReturn the constraints struct.
Arguments
ocp::Model: The optimal control problem.
Returns
C: The constraints model.
See also: CTModels.Models.isempty_constraints, CTModels.Models.constraint.
control_components [Function]
CTModels.Models.control_components Function
control_components(
ocp::CTModels.Models.Model
) -> Vector{String}Return the names of the components of the control.
Arguments
ocp::Model: The optimal control problem.
Returns
Vector{String}: The control component names.
See also: CTModels.Components.control, CTModels.Models.control_name, CTModels.Models.control_dimension.
control_components(
sol::CTModels.Solutions.Solution
) -> Vector{String}Return the names of the components of the control.
Arguments
sol::Solution: The optimal control solution.
Returns
Vector{String}: The control component names.
See also: CTModels.Models.control_dimension, CTModels.Models.control_name.
control_dimension [Function]
CTModels.Models.control_dimension Function
control_dimension(ocp::CTModels.Models.Model) -> Int64Return the control dimension.
Arguments
ocp::Model: The optimal control problem.
Returns
Dimension: The control dimension.
See also: CTModels.Components.control, CTModels.Models.control_name, CTModels.Models.control_components.
control_dimension(ocp::CTModels.Building.PreModel) -> Int64Return the control dimension of the PreModel.
control defaults to CTModels.Components.EmptyControlModel (dimension 0), so — unlike CTModels.Models.state_dimension — no precondition on control having been set is required.
Arguments
ocp::PreModel: The pre-model to query.
control_dimension(sol::CTModels.Solutions.Solution) -> Int64Return the dimension of the control.
Arguments
sol::Solution: The optimal control solution.
Returns
Dimension: The control dimension.
See also: CTModels.Components.control, CTModels.Models.control_components.
control_name [Function]
CTModels.Models.control_name Function
control_name(ocp::CTModels.Models.Model) -> StringReturn the name of the control.
Arguments
ocp::Model: The optimal control problem.
Returns
String: The control name.
See also: CTModels.Components.control, CTModels.Models.control_components, CTModels.Models.control_dimension.
control_name(sol::CTModels.Solutions.Solution) -> StringReturn the name of the control.
Arguments
sol::Solution: The optimal control solution.
Returns
String: The control name.
See also: CTModels.Models.control_components, CTModels.Models.control_dimension.
definition [Function]
CTModels.Models.definition Function
definition(
ocp::CTModels.Models.Model{<:CTBase.Traits.TimeDependence, <:CTModels.Components.TimesModel, <:CTModels.Components.AbstractStateModel, <:CTModels.Components.AbstractControlModel, <:CTModels.Components.AbstractVariableModel, <:Function, <:CTModels.Components.AbstractObjectiveModel, <:CTModels.Components.AbstractConstraintsModel, D}
) where D<:CTModels.Components.AbstractDefinition -> CTModels.Components.AbstractDefinitionReturn the model definition.
Arguments
ocp::Model: The optimal control problem.
Returns
D: The model definition.
See also: CTModels.Components.expression.
dynamics [Function]
CTModels.Models.dynamics Function
dynamics(
ocp::CTModels.Models.Model{<:CTBase.Traits.TimeDependence, <:CTModels.Components.AbstractTimesModel, <:CTModels.Components.AbstractStateModel, <:CTModels.Components.AbstractControlModel, <:CTModels.Components.AbstractVariableModel, D}
) where D<:Function -> FunctionReturn the dynamics.
Arguments
ocp::Model: The optimal control problem.
Returns
D: The dynamics function.
See also: CTModels.Components.state, CTModels.Components.control.
get_build_examodel [Function]
CTModels.Models.get_build_examodel Function
get_build_examodel(
ocp::CTModels.Models.Model{<:CTBase.Traits.TimeDependence, <:CTModels.Components.AbstractTimesModel, <:CTModels.Components.AbstractStateModel, <:CTModels.Components.AbstractControlModel, <:CTModels.Components.AbstractVariableModel, <:Function, <:CTModels.Components.AbstractObjectiveModel, <:CTModels.Components.AbstractConstraintsModel, <:CTModels.Components.AbstractDefinition, BE}
) where BE<:Function -> FunctionReturn the build_examodel.
Arguments
ocp::Model: The optimal control problem with ExaModels builder.
Returns
BE: The ExaModels builder function.
See also: CTModels.Models.dynamics.
get_build_examodel(
::CTModels.Models.Model{<:CTBase.Traits.TimeDependence, <:CTModels.Components.AbstractTimesModel, <:CTModels.Components.AbstractStateModel, <:CTModels.Components.AbstractControlModel, <:CTModels.Components.AbstractVariableModel, <:Function, <:CTModels.Components.AbstractObjectiveModel, <:CTModels.Components.AbstractConstraintsModel, <:CTModels.Components.AbstractDefinition, <:Nothing}
)Fallback: throw when no Exa builder is present.
hamiltonian [Function]
CTBase.Data.hamiltonian Function
hamiltonian(object, args...; kwargs...)Return the Hamiltonian of object, an CTBase.Data.AbstractHamiltonian.
This generic function has no method in CTBase: it is the single owner of the name, and each package that builds a Hamiltonian adds its own methods to it (for instance the Hamiltonian of an optimal control problem, or the one a Hamiltonian flow integrates), so that every method is reached through one function. It is the counterpart of CTBase.Data.pseudo_hamiltonian.
See also: CTBase.Data.AbstractHamiltonian, CTBase.Data.pseudo_hamiltonian.
has_abstract_definition [Function]
CTModels.Models.has_abstract_definition Function
has_abstract_definition(ocp::CTModels.Models.Model) -> BoolCheck whether the problem has an abstract definition.
Arguments
ocp::Model: The optimal control problem.
Returns
Bool:trueif the problem has an abstract definition,falseotherwise.
See also: CTModels.Models.is_abstractly_defined, CTModels.Models.definition.
is_abstractly_defined [Function]
CTModels.Models.is_abstractly_defined Function
is_abstractly_defined(ocp::CTModels.Models.Model) -> BoolCheck whether the problem is abstractly defined.
Arguments
ocp::Model: The optimal control problem.
Returns
Bool:trueif the problem is abstractly defined,falseotherwise.
See also: CTModels.Models.has_abstract_definition.
isempty_constraints [Function]
CTModels.Models.isempty_constraints Function
isempty_constraints(ocp::CTModels.Models.Model) -> BoolReturn true if the model has no constraints.
Arguments
ocp::Model: The optimal control problem.
Returns
Bool:trueif the model has no constraints,falseotherwise.
See also: CTModels.Models.constraints, CTModels.Models.constraint.
pseudo_hamiltonian [Function]
CTBase.Data.pseudo_hamiltonian Function
pseudo_hamiltonian(
h::CTBase.Data.ComposedHamiltonian
) -> CTBase.Data.PseudoHamiltonianReturn the underlying pseudo-Hamiltonian H̃ of a CTBase.Data.ComposedHamiltonian.
See also: CTBase.Data.ComposedHamiltonian, CTBase.Data.control_law.
pseudo_hamiltonian(
h::CTBase.Data.ComposedConstrainedHamiltonian
) -> CTBase.Data.AbstractConstrainedPseudoHamiltonianReturn the constrained pseudo-Hamiltonian H(t, x, p, u, μ, v) of a CTBase.Data.ComposedConstrainedHamiltonian.
See also: CTBase.Data.control_law, CTBase.Data.multiplier.
pseudo_hamiltonian(
ocp::CTModels.Models.Model;
p0::Real,
constraints
) -> Union{CTBase.Data.ConstrainedPseudoHamiltonian, CTBase.Data.PseudoHamiltonian}Return the pseudo-Hamiltonian of the optimal control problem ocp,
where f is the dynamics and ℓ the Lagrange cost (absent for a Mayer problem). It does not depend on any control law. The pseudo-Hamiltonian is always maximised (ct-conventions SGN4): only the sign of p⁰ depends on the criterion.
With constraints, the labelled path constraints, state boxes or control boxes are adjoined with their multipliers μ, an explicit argument like the control:
where each constraint lb ≤ c ≤ ub enters on its active side, g ≥ 0, with its multiplier in the sign convention of the duals of a solution (ct-conventions SGN2, SGN3), component by component:
| Bounds of the component | Term | Multiplier y |
|---|---|---|
lower only, lb ≤ c | y (c − lb) | y ≥ 0 |
upper only, c ≤ ub | y (ub − c) | y ≥ 0 |
both, or c = lb = ub | −y (c − b), b = ub if y > 0, lb otherwise | signed |
For a constraint with two bounds the active side is read on the sign of y; the term is continuous at y = 0. Constraints not listed are inactive. The partial derivatives of H in x and p, at fixed u and μ (see CTBase.Data.freeze_multiplier), give the extremals of the maximum principle.
Arguments
ocp::Model: an optimal control problem with a control.p0::Real: the multiplier of the cost;-1for:minand+1for:maxby default (normal case),0for the abnormal case. Its sign must agree with the criterion.constraints: a label, or a tuple or vector of labels, of path constraints, state boxes or control boxes ofocp; none by default. The bounds of a box are the effective bounds of its components, intersected over all the labels that bound them: a label writtenx₁(t) ≥ 0gives a two-sided term, with a signed multiplier, if another label boundsx₁above.
Returns
Without
constraints, aCTBase.Data.PseudoHamiltonian, callable with its natural signature,H̃(x, p, u),H̃(t, x, p, u),H̃(x, p, u, v)orH̃(t, x, p, u, v), and with the uniform signatureH̃(t, x, p, u, v).With
constraints, aCTBase.Data.ConstrainedPseudoHamiltonian, callable asH([t, ]x, p, u, μ[, v])andH(t, x, p, u, μ, v), whereμstacks the multipliers of the labels in the order given, each with the components of its constraint.
A one-dimensional state, control, variable or multiplier is a number. A batched state (a Matrix, one column per point, as in CTFlows batched flows) comes with batched arguments, a quantity of dimension d being a d × batch matrix; the value is the sum over the points, and the functions of the model must be written on the columns.
Throws
CTBase.Exceptions.PreconditionError: ifocpis control-free (useCTBase.Data.hamiltonianinstead).CTBase.Exceptions.IncorrectArgument: if the sign ofp0contradicts the criterion; if a label is unknown, given twice, or names a boundary constraint or a variable box, which do not enter the Hamiltonian; if two labels bound the same component of the state or of the control; and, at the call, ifμdoes not have the dimension of the constraints.
Example
julia> H̃ = CTModels.Models.pseudo_hamiltonian(ocp) # ẋ = u, ∫ x² → min, x(t) ≥ 0.1, (x_con)
julia> H̃(1.0, 2.0, 0.5) # p·u − x²
0.0
julia> CTModels.Models.pseudo_hamiltonian(ocp; p0=0)(1.0, 2.0, 0.5) # abnormal: p·u
1.0
julia> H = CTModels.Models.pseudo_hamiltonian(ocp; constraints=:x_con);
julia> H(1.0, 2.0, 0.5, 3.0) # p·u − x² + μ (x − 0.1)
2.7See also: CTBase.Data.hamiltonian, CTModels.Models.PseudoHamiltonianFunction, CTModels.Models.ConstrainedPseudoHamiltonianFunction.
state_components [Function]
CTModels.Models.state_components Function
state_components(
ocp::CTModels.Models.Model
) -> Vector{String}Return the names of the components of the state.
Arguments
ocp::Model: The optimal control problem.
Returns
Vector{String}: The state component names.
See also: CTModels.Components.state, CTModels.Models.state_name, CTModels.Models.state_dimension.
state_components(
sol::CTModels.Solutions.Solution
) -> Vector{String}Return the names of the components of the state.
Arguments
sol::Solution: The optimal control solution.
Returns
Vector{String}: The state component names.
See also: CTModels.Models.state_dimension, CTModels.Models.state_name.
state_dimension [Function]
CTModels.Models.state_dimension Function
state_dimension(ocp::CTModels.Models.Model) -> Int64Return the state dimension.
Arguments
ocp::Model: The optimal control problem.
Returns
Dimension: The state dimension.
See also: CTModels.Components.state, CTModels.Models.state_name, CTModels.Models.state_components.
state_dimension(ocp::CTModels.Building.PreModel) -> Int64Return the state dimension of the PreModel.
Arguments
ocp::PreModel: The pre-model to query.
Throws
Exceptions.PreconditionError: if the state has not been set yet.
state_dimension(sol::CTModels.Solutions.Solution) -> Int64Return the dimension of the state.
Arguments
sol::Solution: The optimal control solution.
Returns
Dimension: The state dimension.
See also: CTModels.Components.state, CTModels.Models.state_components.
state_name [Function]
CTModels.Models.state_name Function
state_name(ocp::CTModels.Models.Model) -> StringReturn the name of the state.
Arguments
ocp::Model: The optimal control problem.
Returns
String: The state name.
See also: CTModels.Components.state, CTModels.Models.state_components, CTModels.Models.state_dimension.
state_name(sol::CTModels.Solutions.Solution) -> StringReturn the name of the state.
Arguments
sol::Solution: The optimal control solution.
Returns
String: The state name.
See also: CTModels.Models.state_components, CTModels.Models.state_dimension.
variable_components [Function]
CTModels.Models.variable_components Function
variable_components(
ocp::CTModels.Models.Model
) -> Vector{String}Return the names of the components of the variable.
Arguments
ocp::Model: The optimal control problem.
Returns
Vector{String}: The variable component names.
See also: CTModels.Components.variable, CTModels.Models.variable_name, CTModels.Models.variable_dimension.
variable_components(
sol::CTModels.Solutions.Solution
) -> Vector{String}Return the names of the components of the variable.
Arguments
sol::Solution: The optimal control solution.
Returns
Vector{String}: The variable component names.
See also: CTModels.Models.variable_dimension, CTModels.Models.variable_name.
variable_dimension [Function]
CTModels.Models.variable_dimension Function
variable_dimension(ocp::CTModels.Models.Model) -> Int64Return the variable dimension.
Arguments
ocp::Model: The optimal control problem.
Returns
Dimension: The variable dimension.
See also: CTModels.Components.variable, CTModels.Models.variable_name, CTModels.Models.variable_components.
variable_dimension(ocp::CTModels.Building.PreModel) -> Int64Return the variable dimension of the PreModel.
variable defaults to CTModels.Components.EmptyVariableModel (dimension 0), so — unlike CTModels.Models.state_dimension — no precondition on variable having been set is required.
Arguments
ocp::PreModel: The pre-model to query.
variable_dimension(
sol::CTModels.Solutions.Solution
) -> Int64Return the dimension of the variable.
Arguments
sol::Solution: The optimal control solution.
Returns
Dimension: The variable dimension.
See also: CTModels.Components.variable, CTModels.Models.variable_components.
variable_name [Function]
CTModels.Models.variable_name Function
variable_name(ocp::CTModels.Models.Model) -> StringReturn the name of the variable.
Arguments
ocp::Model: The optimal control problem.
Returns
String: The variable name.
See also: CTModels.Components.variable, CTModels.Models.variable_components, CTModels.Models.variable_dimension.
variable_name(sol::CTModels.Solutions.Solution) -> StringReturn the name of the variable.
Arguments
sol::Solution: The optimal control solution.
Returns
String: The variable name.
See also: CTModels.Models.variable_components, CTModels.Models.variable_dimension.