Skip to content

Hamiltonians ​

The pseudo-Hamiltonian of an optimal control problem depends only on the model: its dynamics , its Lagrange cost and its criterion. CTModels builds it, and the Hamiltonians derived from it, as methods of the generic functions CTBase.Data.pseudo_hamiltonian and CTBase.Data.hamiltonian, re-exported by CTModels.Models. CTFlows uses them to build the flows of the indirect method.

CallProblemReturns
pseudo_hamiltonian(ocp; p0)with a control
pseudo_hamiltonian(ocp; p0, constraints)with a control
hamiltonian(ocp, u; p0)with a control, control in feedback form
hamiltonian(ocp, u; p0, multipliers)with a control, control and multipliers in feedback form
hamiltonian(ocp; p0)control-free

Each object is callable with its natural signature, which follows the time and variable dependence of the problem (H̃(x, p, u) for an autonomous problem without variable, H̃(t, x, p, u, v) for a non-autonomous one with a variable), and with a uniform signature that takes every argument. A one-dimensional state, control, variable or multiplier is a number.

The pseudo-Hamiltonian ​

The example of this page is the problem with a state constraint of order one of Bonnard, Faubourg, Launay and Trélat: minimise for  ,  , under the constraint   .

julia
using CTModels
const Models = CTModels.Models

lb = 0.1
pre = CTModels.PreModel()
CTModels.time!(pre; t0=0.0, tf=2.0)
CTModels.state!(pre, 1)
CTModels.control!(pre, 1)
CTModels.dynamics!(pre, (r, t, x, u, v) -> (r[1] = u; nothing))
CTModels.objective!(pre, :min; lagrange=(t, x, u, v) -> x^2)
CTModels.constraint!(pre, :path;
    f=(r, t, x, u, v) -> (r[1] = x - lb; nothing), lb=[0.0], ub=[Inf], label=:x_con)
CTModels.constraint!(pre, :control; rg=1:1, lb=[-1.0], ub=[1.0], label=:u_box)
CTModels.time_dependence!(pre; autonomous=true)
CTModels.definition!(pre, quote end)
ocp = CTModels.build(pre)

H̃ = Models.pseudo_hamiltonian(ocp)
PseudoHamiltonian: autonomous, fixed (no variable)
  natural call: h̃(x, p, u)
  uniform call: h̃(t, x, p, u, v)
julia
x, p, u = 1.0, 2.0, 0.5
H̃(x, p, u)                     # natural call: p·u − x²
0.0
julia
H̃(0.0, x, p, u, nothing)       # uniform call H̃(t, x, p, u, v)
0.0

The sign of ​

The pseudo-Hamiltonian is always maximised, for a minimisation and for a maximisation; only depends on the criterion (ct-conventions SGN4):

CriterionNormal case (default)Abnormal caseAllowed
:min    
:max    

So and   have the same pseudo-Hamiltonian, hence the same control, costate and multipliers. A whose sign contradicts the criterion is rejected.

julia
Models.pseudo_hamiltonian(ocp; p0=0)(x, p, u)        # abnormal case: p·u
1.0
julia
try
    Models.pseudo_hamiltonian(ocp; p0=1)
catch e
    println(e.msg, ": ", e.got)
end
p0 does not have the sign of the criterion: p0 = 1 for a :min problem

The Hamiltonian for a control law ​

Given a control law in feedback form , the Hamiltonian is

The law is a CTBase.Data.DynClosedLoop law, or a plain function with the natural arity of the problem: u(x, p), u(t, x, p), u(x, p, v) or u(t, x, p, v). On the first arc of the example,   :

julia
h = Models.hamiltonian(ocp, (x, p) -> -1.0)
h(x, p)                        # −p − x²
-3.0

A plain function with a single method is checked against the arity of the problem; a function with several methods, or with a variable number of arguments, is not.

Constraints ​

Constraints by label ​

The constraints active on an arc are adjoined by label, with their multipliers :

where each constraint    enters on its active side,  , and the multiplier is an explicit argument, like the control. This is the pseudo-Hamiltonian of the maximum principle with constraints.

julia
H = Models.pseudo_hamiltonian(ocp; constraints=:x_con)
ConstrainedPseudoHamiltonian: autonomous, fixed (no variable)
  natural call: H(x, p, u, μ)
  uniform call: H(t, x, p, u, μ, v)
julia
μ = 3.0
H(x, p, u, μ)                  # p·u − x² + μ (x − lb)
2.7

Several labels are given as a tuple; then stacks their multipliers in that order, each with the components of its constraint. Labels not given are inactive.

The sign convention of the multipliers ​

The multipliers are given in the sign convention of the duals of a solution (ct-conventions SGN2, SGN3), so that the dual of a label read in a solution of a direct method, dual(sol, :label), can be used as is. Component by component:

Bounds of the componentTerm adjoinedMultiplier
lower only,     
upper only,     
both, or     ,   if  , otherwisesigned

For a constraint with two bounds, the active side is read on the sign of the multiplier, so it never has to be given:   on the upper side,   on the lower side. The term is continuous at  . With the control box of the example,   :

julia
Hu = Models.pseudo_hamiltonian(ocp; constraints=:u_box)
Hu(x, p, 1.0, 0.5) - H̃(x, p, 1.0), Hu(x, p, -1.0, -0.5) - H̃(x, p, -1.0)   # both vanish on the bound
(0.0, 0.0)

Which constraints ​

KindEnters the Hamiltonian
path constraint (state, control or mixed)yes
state box, e.g. x₁(t) ≤ 1yesthe same term as the path constraint x₁(t) - 1 ≤ 0
control boxyesinert in the flow, see below
boundary constraintnogives the transversality conditions
variable boxnogives the transversality condition on

A constraint written as a box or as a path constraint has the same term, and the same multiplier (ct-conventions SGN5).

The bounds of a box are the effective bounds of its components: when a component is bounded under several labels, its bounds are intersected, and each of these labels gives the same box, and the same dual in the solution. With x₁(t) ≥ 0, (a) and x₁(t) ≤ 1, (b), the label :a gives the two-sided term of   , whose multiplier is signed:   when the lower bound is active. Adjoin one of the two labels; giving both would add the term twice, and is rejected.

A control box is inert in the flow: on an arc where the control is saturated,  , the term    vanishes, and so do its derivatives in and (with fixed, or with constant along the arc). Its multiplier is given by the stationarity   ; this is why it is usually left out, but adjoining it gives the dual of the control box, to compare with a direct method.

julia
try
    CTModels.Models.pseudo_hamiltonian(ocp; constraints=:unknown)
catch e
    println(e.msg)
end
Constraint label not found

Multipliers in feedback form ​

With a control law, the multipliers of the active constraints are given in feedback form, as label => μ pairs (a Pair, a tuple of pairs, a NamedTuple or a Dict):

On the boundary arc of the example,   so  ; the maximisation condition gives   and    , so  :

julia
hb = Models.hamiltonian(ocp, (x, p) -> 0.0; multipliers=(:x_con => (x, p) -> 2x))
ComposedConstrainedHamiltonian: autonomous, fixed (no variable)
  natural call: h(x, p)
  uniform call: h(t, x, p, v)
julia
hb(x, p), hb(lb, 0.0)          # on the arc, g = 0: h = −lb²
(0.8, -0.010000000000000002)

Each multiplier is a CTBase.Data.Multiplier, or a function with the natural arity of the problem: μ(x, p), μ(t, x, p), μ(x, p, v) or μ(t, x, p, v). Its value must have the dimension of its constraint (a number for a one-dimensional constraint).

Partial and total derivatives ​

The extremals of the maximum principle are given by the partial derivatives of with respect to and , at fixed and :

CTBase.Data.freeze_multiplier(H, μ) returns the pseudo-Hamiltonian at a fixed value of , whose gradients are these partial derivatives.

Differentiating instead gives the total derivative, through the control law and the multipliers:

The two agree on an optimal arc, where the constraint is active ( ) and the law is stationary or constant, but not elsewhere, for instance during the iterations of a shooting method. In the example, the total derivative of in is  , while the partial one is   . The flows of CTFlows offer both (hamiltonian_type=:partial or :total).

Control-free problems ​

A problem without control has a Hamiltonian, and no pseudo-Hamiltonian:

julia
pre = CTModels.PreModel()
CTModels.time!(pre; t0=0.0, tf=1.0)
CTModels.state!(pre, 2)
CTModels.dynamics!(pre, (r, t, x, u, v) -> (r[1] = x[2]; r[2] = -x[1]; nothing))
CTModels.objective!(pre, :min; lagrange=(t, x, u, v) -> x[1]^2)
CTModels.time_dependence!(pre; autonomous=true)
CTModels.definition!(pre, quote end)
ocp_cf = CTModels.build(pre)

H_cf = Models.hamiltonian(ocp_cf)
H_cf([1.0, 2.0], [3.0, 4.0])   # p₁x₂ − p₂x₁ − x₁²
1.0

Constraints are not adjoined to the Hamiltonian of a control-free problem: such a problem is easily ill-posed, and these conditions are rather boundary conditions. pseudo_hamiltonian(ocp_cf) and hamiltonian(ocp_cf; multipliers=…) raise a CTBase.Exceptions.PreconditionError, as does hamiltonian(ocp) for a problem with a control, which needs a control law.

Batched states ​

The flows of CTFlows can integrate several points at once: the state is then a Matrix, one column per point, and so are the costate, the control, the variable and the multipliers, a quantity of dimension being a   batch matrix. The Hamiltonians accept such arguments, and their value is the sum over the points, , so that its gradient in the batched state is the gradient at each point, column by column.

The functions of the model then receive whole matrices: the dynamics, the Lagrange cost and the path constraints must be written on the columns. The dynamics and the path constraints fill one column per point of an   batch buffer, and the Lagrange cost returns the sum of its values over the points:

julia
dynamics!(r, t, x, u, v) = (r .= A * x .+ B .* u; nothing)
lagrange(t, x, u, v) = 0.5 * sum(abs2, u)                 # Σ over the points
path!(r, t, x, u, v) = (r[1:1, :] .= x[1:1, :] .+ u; nothing)

A box needs nothing more: its rows are taken from the batched state or control. A multiplier that is not batched (a number, or a vector of the dimension of the constraints) is shared by all the points. A batched multiplier must have one column per point; otherwise the call raises a CTBase.Exceptions.IncorrectArgument.

Errors ​

SituationException
pseudo_hamiltonian or hamiltonian(ocp, u) of a control-free problemPreconditionError
hamiltonian(ocp) of a problem with a controlPreconditionError
multipliers for a control-free problemPreconditionError
with the wrong sign, or not finiteIncorrectArgument
open-loop or closed-loop control law; wrong arity of a plain functionIncorrectArgument
constraints not given by labels (a String, nothing, …)IncorrectArgument
unknown label, label given twice, two labels of the same box component, boundary constraint or variable boxIncorrectArgument
multiplier without the dimension of its constraint (at the call)IncorrectArgument
batched multiplier without one column per point (at the call)IncorrectArgument