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.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.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.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#101"{f, u, g, μ}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, u, g, μ}, CTFlows.Hamiltonian{CTFlows.var"#H#102"{f, u, f⁰, p⁰, s, g, μ}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, u, f⁰, p⁰, s, g, μ}}, CTFlows.ControlLaw{<:Function, T, V} where {T, V}}

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.Model,
    u::CTFlows.ControlLaw{<:Function, T, V},
    g::CTFlows.MixedConstraint{<:Function, T, V},
    μ::Function
) -> Tuple{Union{CTFlows.Hamiltonian{CTFlows.var"#H#101"{f, u, g, μ}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, u, g, μ}, CTFlows.Hamiltonian{CTFlows.var"#H#102"{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.Model,
    u::CTFlows.ControlLaw{<:Function, T, V},
    g_::CTFlows.StateConstraint{<:Function, T, V},
    μ
) -> Tuple{Union{CTFlows.Hamiltonian{CTFlows.var"#H#101"{f, u, g, μ}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, u, g, μ}, CTFlows.Hamiltonian{CTFlows.var"#H#102"{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.Model,
    u::CTFlows.ControlLaw{<:Function, T, V},
    g::Function,
    μ
) -> Tuple{Union{CTFlows.Hamiltonian{CTFlows.var"#H#101"{f, u, g, μ}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, u, g, μ}, CTFlows.Hamiltonian{CTFlows.var"#H#102"{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.Model,
    u::CTFlows.ControlLaw{<:Function, T, V}
) -> Tuple{Union{CTFlows.Hamiltonian{CTFlows.var"#98#99"{f, u}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, u}, CTFlows.Hamiltonian{CTFlows.var"#H#100"{f, u, f⁰, p⁰, s}, CTFlows.NonAutonomous, CTFlows.NonFixed} where {f, u, f⁰, p⁰, s}}, CTFlows.ControlLaw{<:Function, T, V} where {T, V}}

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.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.__dynamicsMethod
__dynamics(
    ocp::CTModels.Model
) -> CTFlows.Dynamics{CTFlows.var"#78#79"{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.Model
) -> Tuple{CTFlows.Dynamics{CTFlows.var"#78#79"{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.Model) -> Bool

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

source
CTFlows.__lagrangeMethod
__lagrange(
    ocp::CTModels.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.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#102"{CTFlows.Dynamics{TF, TD, VD}, CTFlows.ControlLaw{TF1, TD1, VD1}, CTFlows.Lagrange{TF2, TD2, VD2}, var"#s182", var"#s1821", 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"#s182"<:Real, var"#s1821"<: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#100"{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#101"{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"#98#99"{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