Hamiltonians
The pseudo-Hamiltonian of an optimal control problem depends only on the model: its dynamics CTBase.Data.pseudo_hamiltonian and CTBase.Data.hamiltonian, re-exported by CTModels.Models. CTFlows uses them to build the flows of the indirect method.
| Call | Problem | Returns |
|---|---|---|
pseudo_hamiltonian(ocp; p0) | with a control | |
pseudo_hamiltonian(ocp; p0, constraints) | with a control | |
hamiltonian(ocp, u; p0) | with a control | |
hamiltonian(ocp, u; p0, multipliers) | with a control | |
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
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)x, p, u = 1.0, 2.0, 0.5
H̃(x, p, u) # natural call: p·u − x²0.0H̃(0.0, x, p, u, nothing) # uniform call H̃(t, x, p, u, v)0.0The sign of
The pseudo-Hamiltonian is always maximised, for a minimisation and for a maximisation; only SGN4):
| Criterion | Normal case (default) | Abnormal case | Allowed |
|---|---|---|---|
:min | |||
:max |
So
Models.pseudo_hamiltonian(ocp; p0=0)(x, p, u) # abnormal case: p·u1.0try
Models.pseudo_hamiltonian(ocp; p0=1)
catch e
println(e.msg, ": ", e.got)
endp0 does not have the sign of the criterion: p0 = 1 for a :min problemThe Hamiltonian for a control law
Given a control law in feedback form
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,
h = Models.hamiltonian(ocp, (x, p) -> -1.0)
h(x, p) # −p − x²-3.0A 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
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)μ = 3.0
H(x, p, u, μ) # p·u − x² + μ (x − lb)2.7Several labels are given as a tuple;
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 component | Term adjoined | Multiplier |
|---|---|---|
| lower only, | ||
| upper only, | ||
| both, or | signed |
For a constraint with two bounds, the active side is read on the sign of the multiplier, so it never has to be given:
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
| Kind | Enters the Hamiltonian | |
|---|---|---|
| path constraint (state, control or mixed) | yes | |
state box, e.g. x₁(t) ≤ 1 | yes | the same term as the path constraint x₁(t) - 1 ≤ 0 |
| control box | yes | inert in the flow, see below |
| boundary constraint | no | gives the transversality conditions |
| variable box | no | gives 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
A control box is inert in the flow: on an arc where the control is saturated,
try
CTModels.Models.pseudo_hamiltonian(ocp; constraints=:unknown)
catch e
println(e.msg)
endConstraint label not foundMultipliers 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,
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)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
CTBase.Data.freeze_multiplier(H, μ) returns the pseudo-Hamiltonian at a fixed value of
Differentiating
The two agree on an optimal arc, where the constraint is active (hamiltonian_type=:partial or :total).
Control-free problems
A problem without control has a Hamiltonian, and no pseudo-Hamiltonian:
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.0Constraints 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
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
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
| Situation | Exception |
|---|---|
pseudo_hamiltonian or hamiltonian(ocp, u) of a control-free problem | PreconditionError |
hamiltonian(ocp) of a problem with a control | PreconditionError |
| multipliers for a control-free problem | PreconditionError |
IncorrectArgument | |
| open-loop or closed-loop control law; wrong arity of a plain function | IncorrectArgument |
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 box | IncorrectArgument |
| multiplier without the dimension of its constraint (at the call) | IncorrectArgument |
| batched multiplier without one column per point (at the call) | IncorrectArgument |