Optimal Control Problem Utils

Index

Warning

In the examples in the documentation below, the methods are not prefixed by the module name even if they are private.

julia> using CTFlows
julia> x = 1
julia> private_fun(x) # throw an error

must be replaced by

julia> using CTFlows
julia> x = 1
julia> CTFlows.private_fun(x)

However, if the method is reexported by another package, then, there is no need of prefixing.

julia> module OptimalControl
           import CTFlows: private_fun
           export private_fun
       end
julia> using OptimalControl
julia> x = 1
julia> private_fun(x)

Documentation

CTFlows.__create_hamiltonianMethod
__create_hamiltonian(
    ocp::CTModels.OCP.Model,
    u::Function,
    g,
    μ;
    autonomous,
    variable
)

Overload for control law as a raw function with autonomous and variable flags.

source
CTFlows.__create_hamiltonianMethod
__create_hamiltonian(
    ocp::CTModels.OCP.Model,
    g::Function,
    μ::Function;
    autonomous,
    variable
)

Overload for control-free problem with raw constraint and multiplier functions.

Wraps the raw functions into StateConstraint and Multiplier objects.

Arguments

  • ocp::CTModels.Model: The optimal control problem (must have EmptyControlModel).
  • g::Function: State constraint function.
  • μ::Function: Multiplier function.
  • autonomous::Bool: Whether the system is autonomous.
  • variable::Bool: Whether the system parameters are variable.

Returns

  • (H, u_dummy): Tuple containing the Hamiltonian and a dummy control law.
source
CTFlows.__create_hamiltonianMethod
__create_hamiltonian(
    ocp::CTModels.OCP.Model,
    u::Function;
    autonomous,
    variable
)

Helper method to construct the Hamiltonian when control is given as a plain function.

The function is wrapped in a ControlLaw, and the flags autonomous and variable define its behavior type.

source
CTFlows.__create_hamiltonianMethod
__create_hamiltonian(
    ocp::CTModels.OCP.Model
) -> Tuple{Union{CTFlows.Hamiltonian{CTFlows.var"#H#makeH##4"{CTFlows.Dynamics{CTFlows.var"#__dynamics##0#__dynamics##1"{ocp, n}, CTFlows.NonAutonomous, CTFlows.NonFixed}, CTFlows.Lagrange{var"#s179", CTFlows.NonAutonomous, CTFlows.NonFixed}, Int64, Float64}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {ocp, n, var"#s179"<:Function}, CTFlows.Hamiltonian{CTFlows.var"#makeH##0#makeH##1"{CTFlows.Dynamics{CTFlows.var"#__dynamics##0#__dynamics##1"{ocp, n}, CTFlows.NonAutonomous, CTFlows.NonFixed}}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {ocp, n}}, CTFlows.ControlLaw{CTFlows.var"#__create_hamiltonian##0#__create_hamiltonian##1", CTFlows.NonAutonomous, CTFlows.NonFixed}}

Construct and return the Hamiltonian for a control-free optimal control problem.

The Hamiltonian is built using model dynamics (and possibly a running cost) without a control law. The control is passed as an empty array Float64[] to the dynamics.

Returns a tuple (H, u_dummy) where H is the Hamiltonian function and u_dummy is a dummy control law that returns Float64[].

Arguments

  • ocp::CTModels.Model: The optimal control problem (must have EmptyControlModel).

Returns

  • (H, u_dummy): Tuple containing the Hamiltonian and a dummy control law.

Notes

This method is used for parameter estimation and optimal design problems without control variables.

See also: makeH

source
CTFlows.__create_hamiltonianMethod
__create_hamiltonian(
    ocp::CTModels.OCP.Model,
    u::CTFlows.ControlLaw{<:Function, T, V},
    g::CTFlows.MixedConstraint{<:Function, T, V},
    μ::CTFlows.Multiplier{<:Function, T, V}
) -> Tuple{Union{CTFlows.Hamiltonian{CTFlows.var"#H#makeH##7"{f, u, g, μ}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, u, g, μ}, CTFlows.Hamiltonian{CTFlows.var"#H#makeH##9"{f, u, f⁰, p⁰, s, g, μ}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, u, f⁰, p⁰, s, g, μ}}, CTFlows.ControlLaw}

Construct the Hamiltonian for a constrained optimal control problem.

This function supports multiple input types for the control law (u), path constraints (g), and multipliers (μ), automatically adapting to autonomous/non-autonomous systems and fixed/non-fixed parameters.

Supported input types

  • u can be:

    • a raw function,
    • a ControlLaw object,
    • or a FeedbackControl object.
  • g can be:

    • a plain constraint function,
    • a MixedConstraint,
    • or a StateConstraint.
  • μ can be:

    • a function,
    • or a Multiplier object.

The function normalizes these inputs to the appropriate types internally using multiple dispatch and pattern matching.

Arguments

  • ocp::CTModels.Model: The continuous-time optimal control problem.
  • u: Control law, flexible input type as described.
  • g: Path constraint, flexible input type as described.
  • μ: Multiplier associated with the constraints.
  • autonomous::Bool (optional keyword): Specifies if the system is autonomous.
  • variable::Bool (optional keyword): Specifies if the system parameters are variable.

Returns

  • (H, u): Tuple containing the Hamiltonian object H and the processed control law u.

Examples

# Using a raw function control law with autonomous system and fixed parameters
H, u_processed = __create_hamiltonian(ocp, u_function, g_function, μ_function; autonomous=true, variable=false)

# Using a FeedbackControl control law
H, u_processed = __create_hamiltonian(ocp, feedback_control, g_constraint, μ_multiplier)
source
CTFlows.__create_hamiltonianMethod
__create_hamiltonian(
    ocp::CTModels.OCP.Model,
    u::CTFlows.ControlLaw{<:Function, T, V},
    g::CTFlows.MixedConstraint{<:Function, T, V},
    μ::Function
) -> Tuple{Union{CTFlows.Hamiltonian{CTFlows.var"#H#makeH##7"{f, u, g, μ}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, u, g, μ}, CTFlows.Hamiltonian{CTFlows.var"#H#makeH##9"{f, u, f⁰, p⁰, s, g, μ}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, u, f⁰, p⁰, s, g, μ}}, CTFlows.ControlLaw}

Overload that wraps multiplier functions into Multiplier objects.

source
CTFlows.__create_hamiltonianMethod
__create_hamiltonian(
    ocp::CTModels.OCP.Model,
    u::CTFlows.ControlLaw{<:Function, T, V},
    g_::CTFlows.StateConstraint{<:Function, T, V},
    μ
) -> Tuple{Union{CTFlows.Hamiltonian{CTFlows.var"#H#makeH##7"{f, u, g, μ}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, u, g, μ}, CTFlows.Hamiltonian{CTFlows.var"#H#makeH##9"{f, u, f⁰, p⁰, s, g, μ}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, u, f⁰, p⁰, s, g, μ}}, CTFlows.ControlLaw}

Overload that converts StateConstraint objects into MixedConstraint with appropriate signature adaptation.

source
CTFlows.__create_hamiltonianMethod
__create_hamiltonian(
    ocp::CTModels.OCP.Model,
    u::CTFlows.ControlLaw{<:Function, T, V},
    g::Function,
    μ
) -> Tuple{Union{CTFlows.Hamiltonian{CTFlows.var"#H#makeH##7"{f, u, g, μ}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, u, g, μ}, CTFlows.Hamiltonian{CTFlows.var"#H#makeH##9"{f, u, f⁰, p⁰, s, g, μ}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, u, f⁰, p⁰, s, g, μ}}, CTFlows.ControlLaw}

Overload that wraps plain constraint functions into MixedConstraint objects.

source
CTFlows.__create_hamiltonianMethod
__create_hamiltonian(
    ocp::CTModels.OCP.Model,
    u::CTFlows.ControlLaw{<:Function, T, V}
) -> Tuple{Union{CTFlows.Hamiltonian{CTFlows.var"#H#makeH##5"{f, u, f⁰, p⁰, s}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, u, f⁰, p⁰, s}, CTFlows.Hamiltonian{CTFlows.var"#makeH##2#makeH##3"{f, u}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, u}}, CTFlows.ControlLaw}

Construct and return the Hamiltonian for the given model and control law.

The Hamiltonian is built using model dynamics (and possibly a running cost) and returned as a callable function.

Returns a tuple (H, u) where H is the Hamiltonian function and u is the control law.

source
CTFlows.__create_hamiltonianMethod
__create_hamiltonian(
    ocp::CTModels.OCP.Model,
    u_::CTFlows.FeedbackControl{<:Function, T, V},
    g,
    μ
) -> Any

Overload for feedback control laws that adapts the signature based on autonomy and variability.

source
CTFlows.__create_hamiltonianMethod
__create_hamiltonian(
    ocp::CTModels.OCP.Model,
    g::CTFlows.StateConstraint{<:Function, T, V},
    μ::CTFlows.Multiplier{<:Function, T, V}
) -> Tuple{Union{CTFlows.Hamiltonian{CTFlows.var"#H#makeH##6"{f, g, μ}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, g, μ}, CTFlows.Hamiltonian{CTFlows.var"#H#makeH##8"{f, f⁰, p⁰, s, g, μ}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, f⁰, p⁰, s, g, μ}}, CTFlows.ControlLaw{CTFlows.var"#__create_hamiltonian##18#__create_hamiltonian##19", CTFlows.NonAutonomous, CTFlows.NonFixed}}

Construct the Hamiltonian for a control-free problem with state constraint and multiplier.

The Hamiltonian is built using model dynamics, state constraint, and associated multiplier. The control is passed as an empty array Float64[] to the dynamics.

Arguments

  • ocp::CTModels.Model: The optimal control problem (must have EmptyControlModel).
  • g::StateConstraint: State constraint function.
  • μ::Multiplier: Multiplier function associated with the constraint.

Returns

  • (H, u_dummy): Tuple containing the Hamiltonian and a dummy control law.

Notes

This method is used for control-free problems with state constraints.

See also: makeH

source
CTFlows.__create_hamiltonianMethod
__create_hamiltonian(
    ocp::CTModels.OCP.Model,
    g::Function,
    μ::CTFlows.Multiplier{<:Function, T, V};
    autonomous,
    variable
)

Overload for control-free problem with raw constraint function and typed multiplier.

Wraps the constraint function into a StateConstraint object.

source
CTFlows.__dynamicsMethod
__dynamics(
    ocp::CTModels.OCP.Model
) -> CTFlows.Dynamics{CTFlows.var"#__dynamics##0#__dynamics##1"{ocp, n}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {ocp, n}

Return a Dynamics object built from the model's right-hand side function.

The returned function computes the state derivative dx/dt = f(t, x, u, v), wrapped to return either a scalar or vector depending on the model's state dimension.

source
CTFlows.__get_data_for_ocp_flowMethod
__get_data_for_ocp_flow(
    ocp::CTModels.OCP.Model
) -> Tuple{CTFlows.Dynamics{CTFlows.var"#__dynamics##0#__dynamics##1"{ocp, n}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {ocp, n}, Union{Nothing, CTFlows.Lagrange{<:Function, CTFlows.NonAutonomous, CTFlows.NonFixed}}, Int64, Float64}

Return internal components needed to construct the OCP Hamiltonian.

Returns a tuple (f, f⁰, p⁰, s) where:

  • f : system dynamics (Dynamics)
  • f⁰ : optional Lagrange integrand (Lagrange or nothing)
  • p⁰ : constant multiplier for cost (typically -1)
  • s : sign for minimization/maximization (+1 or -1)
source
CTFlows.__is_minMethod
__is_min(ocp::CTModels.OCP.Model) -> Bool

Return true if the given model defines a minimization problem, false otherwise.

source
CTFlows.__lagrangeMethod
__lagrange(
    ocp::CTModels.OCP.Model
) -> Union{Nothing, CTFlows.Lagrange{<:Function, CTFlows.NonAutonomous, CTFlows.NonFixed}}

Return a Lagrange object if the model includes an integrand cost; otherwise, return nothing.

The resulting function can be used to compute the running cost of the optimal control problem.

source
CTFlows.__mayerMethod
__mayer(
    ocp::CTModels.OCP.Model
) -> Union{Nothing, CTFlows.Mayer{<:Function, CTFlows.NonFixed}}

Return a Mayer object if the model includes a terminal cost; otherwise, return nothing.

The resulting function can be used to compute the final cost in the objective.

source
CTFlows.makeHMethod
makeH(
    f::CTFlows.Dynamics,
    u::CTFlows.ControlLaw,
    f⁰::CTFlows.Lagrange,
    p⁰::Real,
    s::Real,
    g::CTFlows.MixedConstraint,
    μ::CTFlows.Multiplier
) -> CTFlows.var"#H#makeH##9"{CTFlows.Dynamics{TF, TD, VD}, CTFlows.ControlLaw{TF1, TD1, VD1}, CTFlows.Lagrange{TF2, TD2, VD2}, var"#s179", var"#s1791", CTFlows.MixedConstraint{TF3, TD3, VD3}, CTFlows.Multiplier{TF4, TD4, VD4}} where {TF<:Function, TD<:CTFlows.TimeDependence, VD<:CTFlows.VariableDependence, TF1<:Function, TD1<:CTFlows.TimeDependence, VD1<:CTFlows.VariableDependence, TF2<:Function, TD2<:CTFlows.TimeDependence, VD2<:CTFlows.VariableDependence, var"#s179"<:Real, var"#s1791"<:Real, TF3<:Function, TD3<:CTFlows.TimeDependence, VD3<:CTFlows.VariableDependence, TF4<:Function, TD4<:CTFlows.TimeDependence, VD4<:CTFlows.VariableDependence}

Construct the Hamiltonian:

H(t, x, p) = p ⋅ f(t, x, u(t, x, p)) + s p⁰ f⁰(t, x, u(t, x, p)) + μ(t, x, p) ⋅ g(t, x, u(t, x, p))

Combines integrand cost and path constraints.

source
CTFlows.makeHMethod
makeH(
    f::CTFlows.Dynamics,
    u::CTFlows.ControlLaw,
    f⁰::CTFlows.Lagrange,
    p⁰::Real,
    s::Real
) -> CTFlows.var"#H#makeH##5"{CTFlows.Dynamics{TF, TD, VD}, CTFlows.ControlLaw{TF1, TD1, VD1}, CTFlows.Lagrange{TF2, TD2, VD2}, <:Real, <:Real} where {TF<:Function, TD<:CTFlows.TimeDependence, VD<:CTFlows.VariableDependence, TF1<:Function, TD1<:CTFlows.TimeDependence, VD1<:CTFlows.VariableDependence, TF2<:Function, TD2<:CTFlows.TimeDependence, VD2<:CTFlows.VariableDependence}

Construct the Hamiltonian:

H(t, x, p) = p ⋅ f(t, x, u(t, x, p)) + s p⁰ f⁰(t, x, u(t, x, p))

Includes a Lagrange integrand scaled by p⁰ and sign s.

source
CTFlows.makeHMethod
makeH(
    f::CTFlows.Dynamics,
    u::CTFlows.ControlLaw,
    g::CTFlows.MixedConstraint,
    μ::CTFlows.Multiplier
) -> CTFlows.var"#H#makeH##7"{CTFlows.Dynamics{TF, TD, VD}, CTFlows.ControlLaw{TF1, TD1, VD1}, CTFlows.MixedConstraint{TF2, TD2, VD2}, CTFlows.Multiplier{TF3, TD3, VD3}} where {TF<:Function, TD<:CTFlows.TimeDependence, VD<:CTFlows.VariableDependence, TF1<:Function, TD1<:CTFlows.TimeDependence, VD1<:CTFlows.VariableDependence, TF2<:Function, TD2<:CTFlows.TimeDependence, VD2<:CTFlows.VariableDependence, TF3<:Function, TD3<:CTFlows.TimeDependence, VD3<:CTFlows.VariableDependence}

Construct the Hamiltonian:

H(t, x, p) = p ⋅ f(t, x, u(t, x, p)) + μ(t, x, p) ⋅ g(t, x, u(t, x, p))

Includes state-control constraints and associated multipliers.

source
CTFlows.makeHMethod
makeH(
    f::CTFlows.Dynamics,
    u::CTFlows.ControlLaw
) -> CTFlows.var"#makeH##2#makeH##3"{CTFlows.Dynamics{TF, TD, VD}, CTFlows.ControlLaw{TF1, TD1, VD1}} where {TF<:Function, TD<:CTFlows.TimeDependence, VD<:CTFlows.VariableDependence, TF1<:Function, TD1<:CTFlows.TimeDependence, VD1<:CTFlows.VariableDependence}

Construct the Hamiltonian:

H(t, x, p) = p ⋅ f(t, x, u(t, x, p))

The function returns a callable H(t, x, p, v) where v is an optional additional parameter.

source
CTFlows.makeHMethod
makeH(
    f::CTFlows.Dynamics,
    f⁰::CTFlows.Lagrange,
    p⁰::Real,
    s::Real,
    g::CTFlows.StateConstraint,
    μ::CTFlows.Multiplier
) -> CTFlows.var"#H#makeH##8"{CTFlows.Dynamics{TF, TD, VD}, CTFlows.Lagrange{TF1, TD1, VD1}, var"#s179", var"#s1791", CTFlows.StateConstraint{TF2, TD2, VD2}, CTFlows.Multiplier{TF3, TD3, VD3}} where {TF<:Function, TD<:CTFlows.TimeDependence, VD<:CTFlows.VariableDependence, TF1<:Function, TD1<:CTFlows.TimeDependence, VD1<:CTFlows.VariableDependence, var"#s179"<:Real, var"#s1791"<:Real, TF2<:Function, TD2<:CTFlows.TimeDependence, VD2<:CTFlows.VariableDependence, TF3<:Function, TD3<:CTFlows.TimeDependence, VD3<:CTFlows.VariableDependence}

Construct the Hamiltonian for a control-free problem with Lagrange cost and state constraint:

H(t, x, p) = p ⋅ f(t, x, Float64[], v) + s p⁰ f⁰(t, x, Float64[], v) + μ(t, x, p) ⋅ g(t, x, v)

Combines integrand cost and state constraints. The control is passed as an empty array Float64[].

Arguments

  • f::Dynamics: System dynamics function.
  • f⁰::Lagrange: Lagrange cost integrand.
  • p⁰::ctNumber: Constant multiplier for the cost (typically -1).
  • s::ctNumber: Sign for minimization (+1) or maximization (-1).
  • g::StateConstraint: State constraint function.
  • μ::Multiplier: Multiplier function associated with the constraint.

Returns

  • A callable Hamiltonian function H(t, x, p, v).
source
CTFlows.makeHMethod
makeH(
    f::CTFlows.Dynamics,
    f⁰::CTFlows.Lagrange,
    p⁰::Real,
    s::Real
) -> CTFlows.var"#H#makeH##4"{CTFlows.Dynamics{TF, TD, VD}, CTFlows.Lagrange{TF1, TD1, VD1}, <:Real, <:Real} where {TF<:Function, TD<:CTFlows.TimeDependence, VD<:CTFlows.VariableDependence, TF1<:Function, TD1<:CTFlows.TimeDependence, VD1<:CTFlows.VariableDependence}

Construct the Hamiltonian for a control-free problem with Lagrange cost:

H(t, x, p) = p ⋅ f(t, x, Float64[], v) + s p⁰ f⁰(t, x, Float64[], v)

Includes a Lagrange integrand scaled by p⁰ and sign s. The control is passed as an empty array Float64[].

Arguments

  • f::Dynamics: System dynamics function.
  • f⁰::Lagrange: Lagrange cost integrand.
  • p⁰::ctNumber: Constant multiplier for the cost (typically -1).
  • s::ctNumber: Sign for minimization (+1) or maximization (-1).

Returns

  • A callable Hamiltonian function H(t, x, p, v).
source
CTFlows.makeHMethod
makeH(
    f::CTFlows.Dynamics,
    g::CTFlows.StateConstraint,
    μ::CTFlows.Multiplier
) -> CTFlows.var"#H#makeH##6"{CTFlows.Dynamics{TF, TD, VD}, CTFlows.StateConstraint{TF1, TD1, VD1}, CTFlows.Multiplier{TF2, TD2, VD2}} where {TF<:Function, TD<:CTFlows.TimeDependence, VD<:CTFlows.VariableDependence, TF1<:Function, TD1<:CTFlows.TimeDependence, VD1<:CTFlows.VariableDependence, TF2<:Function, TD2<:CTFlows.TimeDependence, VD2<:CTFlows.VariableDependence}

Construct the Hamiltonian for a control-free problem with state constraint:

H(t, x, p) = p ⋅ f(t, x, Float64[], v) + μ(t, x, p) ⋅ g(t, x, v)

Includes state constraints and associated multipliers. The control is passed as an empty array Float64[].

Arguments

  • f::Dynamics: System dynamics function.
  • g::StateConstraint: State constraint function.
  • μ::Multiplier: Multiplier function associated with the constraint.

Returns

  • A callable Hamiltonian function H(t, x, p, v).
source
CTFlows.makeHMethod
makeH(
    f::CTFlows.Dynamics
) -> CTFlows.var"#makeH##0#makeH##1"{CTFlows.Dynamics{TF, TD, VD}} where {TF<:Function, TD<:CTFlows.TimeDependence, VD<:CTFlows.VariableDependence}

Construct the Hamiltonian for a control-free problem:

H(t, x, p) = p ⋅ f(t, x, Float64[], v)

The function returns a callable H(t, x, p, v) where v is an optional additional parameter. The control is passed as an empty array Float64[].

Arguments

  • f::Dynamics: System dynamics function.

Returns

  • A callable Hamiltonian function H(t, x, p, v).

Notes

This variant is used for optimal control problems without control variables (parameter estimation, optimal design problems).

source