Repository navigation
Hamiltonians of a model: pseudo_hamiltonian(ocp), hamiltonian(ocp[, u]) - #538
Conversation
…rs (B1) - Data.pseudo_hamiltonian(ocp; p0): H̃ = p·f + p0·ℓ as a CTBase PseudoHamiltonian (requires a control). - Data.hamiltonian(ocp; p0): Hamiltonian of a control-free problem. - Data.hamiltonian(ocp, u; p0): ComposedHamiltonian with a DynClosedLoop law or a raw function (arity checked). - p0 defaults to -1 (:min) / +1 (:max), 0 is the abnormal case, a sign that contradicts the criterion is rejected (SGN4). - Functors moved from CTFlows (same behaviour, zero-filled buffer); the getters are methods of the CTBase.Data generic functions, re-exported by Models. - Tests (unit, spies, AD, type stability, golden values, integration, errors), JET hot path; ForwardDiff added to the test deps. - [sources] points CTBase to control-toolbox/CTBase.jl#663 until its release (to be removed before merge). Refs #532, #536 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Phase B1 — getters without constraints (7b41422)API
ImplementationThe functors
The one change: the buffer is now zero-filled, so a dynamics that does not write every component never reads uninitialised memory. Cross-check against CTFlowsThe old builders Tests
|
- pseudo_hamiltonian(ocp; constraints=labels): ConstrainedPseudoHamiltonian H(t,x,p,u,μ,v) = H̃ + Σ terms, μ an explicit argument stacked in label order. - hamiltonian(ocp, u; multipliers=label => μ): ComposedConstrainedHamiltonian with the multipliers in feedback form (Pair, tuple, NamedTuple or Dict; raw function with arity check, or Data.Multiplier). - Terms in the dual convention of the solution (SGN2/SGN3): y(c − lb), y(ub − c), −y(c − b) with b = ub if y > 0 else lb; continuous at y = 0. - Path constraints, state boxes and control boxes by label; boundary constraints and variable boxes rejected; dimension checks at call time. - Control-free hamiltonian(ocp; multipliers) rejected (PreconditionError). - Arity check skips variadic functions. - Fix: re-export the CTBase functions through the CTBase top level (MOD3). - Tests: kinds of terms, SGN5 path vs box, label order, multiplier forms, spies, AD total vs partial, KKT on a saturated control box, JET, golden values, errors. Refs #532, #536 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Phase B2 — constraints by label (804108a), with its testsAPIH = pseudo_hamiltonian(ocp; constraints=(:vmax, :mixed)) # μ explicit, stacked in label order
H(x, p, u, μ[, v]) # Data.ConstrainedPseudoHamiltonian
h = hamiltonian(ocp, u; multipliers=(:vmax => μ1, :mixed => μ2)) # μ in feedback form
h(x, p[, v]) # Data.ComposedConstrainedHamiltonian
Implementation notes
Tests added to
|
- New page docs/src/model/hamiltonians.md (OCP Model section): pseudo-Hamiltonian, sign of p0 (SGN4), Hamiltonian for a control law, constraints by label and the dual convention, which constraints enter H, multipliers in feedback form, partial vs total derivatives, control-free problems, errors; runnable examples on the order-one state-constraint problem. - Docstrings for every functor call group and helper (DOC1, DOC6); tables with stated alignment (SITE19). - API reference lists hamiltonians.jl and constraint_terms.jl. - CHANGELOG entries. - docs/Project.toml: temporary [sources] for CTBase (CTBase.jl#663), to remove with the one of Project.toml once CTBase 0.30.15 is released. Refs #532, #536 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Phase B4 — documentation (add95d2), and merge of
|
CTFlows calls the Hamiltonians with a Matrix state, one column per point (CTFlows#358). The derivative buffer is now n × batch for such a state, as in CTFlows; the value sums the columns, so AD gives one gradient per column. Found by running the CTFlows suite on the getters. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Validation in the CTFlows worktree, and a fix for batched states (71ed659)What was runCTFlows was plugged onto the getters:
Fix
Checks
The constrained cross-check uses admissible multipliers. With μ < 0 on a two-sided constraint given as |
_stacked_multiplier joins the traits of the multipliers with CTBase.Traits.join_time_dependence and join_variable_dependence (control-toolbox/CTBase.jl#667) instead of private copies. [sources] points to CTBase main, where #663 and #667 are merged; the branch of #663 is deleted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Public CTBase trait joins (7714221)Follow-up of the review in #536 (point 5) and of control-toolbox/CTBase.jl#667, now merged. Changes
Checks
Still open from the review in #536
|
CTBase 0.30.15 is registered (control-toolbox/CTBase.jl#669): drop the [sources] entry and require it in Project.toml and docs/Project.toml. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CTBase 0.30.15 is registered (control-toolbox/CTBase.jl#669): drop its [sources] entry and require it. CTModels stays on the branch of control-toolbox/CTModels.jl#538 until CTModels 0.21.3 is released. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The bounds of a box component declared under several labels are intersected, so each label gives the same effective box: adjoining two of them added its term twice. _constraint_terms now rejects them with an IncorrectArgument. The docs say that a box label gives the effective bounds, so a one-sided label can give a two-sided term with a signed multiplier. Review point 4 of #536. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review of #536: phases 0 and 1 of the plan for points 4, 6 and 7Plan: phase 0 for CTBase 0.30.15, phase 1 for point 4 (a box component adjoined twice), phase 2 for point 6 (batched states, option A: extend the broadcast contract), phase 3 for point 7 (measure, then guard allocations). Conventions: ct-conventions v0.15.0, ct-agents v0.5.0. Phase 0: CTBase 0.30.15 (606a425). CTBase 0.30.15 is registered (control-toolbox/CTBase.jl#669), so the Phase 1: two labels of the same box component (e45b886).
Next: phase 2 (batched states with constraints). |
A batched state (a Matrix, one column per point, as in CTFlows batched
flows) is accepted with constraints, as it already was without:
- a box takes the rows of the batched state or control;
- a path constraint fills an n x batch buffer (SubPathConstraint and
CompositeConstraint{:path} get a matrix method);
- a multiplier is a d x batch matrix, or a number or vector shared by
all the points; a batched multiplier without one column per point is
an IncorrectArgument;
- the value sums the points, so the gradient is taken column by column.
The functions of the model are written on the columns, as the dynamics
already was. Review point 6 of #536.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A constrained flow (a path constraint and a state box, by label) with a batched Matrix state gives, column by column, the flow of each point, in :total and :partial modes. Relies on the batched constrained Hamiltonians of control-toolbox/CTModels.jl#538. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Phase 2: batched states with constraints (review point 6 of #536, option A)0ba230e. Option A extends the batched contract already used for the dynamics: a quantity of dimension
Tests, compared with the sum of the per-point evaluations:
Docs: a new section Batched states on the Hamiltonians page, and a sentence in the docstrings. CTFlows (control-toolbox/CTFlows.jl#534, e51920d4): a constrained flow by label, with a path constraint and a state box, on a Results: the full CTModels suite passes, 5688/5688. Next: phase 3, cost on the hot path. |
The constrained pseudo-Hamiltonian built the empty variable of a problem without variable twice, and summed the terms of a constraint through a temporary array. The variable is now built once and shared with the pseudo-Hamiltonian, and the terms are summed lazily. Allocation contract in test/suite/meta/test_performance.jl: the CTBase wrappers allocate what the CTModels functor does, and the Hamiltonian with multipliers in feedback form what the composition written by hand does. The duplicate sections of [Unreleased] are merged in the order of CHG2. Review point 7 of #536. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Phase 3: cost on the hot path (review point 7 of #536)Benchmark. One point call of a constrained flow by label, CTFlows
The allocation counts are deterministic. The times vary by up to about ±25 % between runs on this machine. In an earlier run, the 2-D Optimisation (304bd97). The benchmark did not show a regression, but two avoidable allocations per evaluation were found while writing the allocation guard:
After this change, a constrained evaluation on the 1-D model allocates exactly what the pseudo-Hamiltonian and the raw constraint allocate: 13 = 8 + 5 allocations, against 15 before. Allocation contract (
A guard of the form "≤ H̃ + raw constraint" was tried and dropped. Measured alone, Results:
Review points 4, 6 and 7 of #536 are done. |
- The buffer of the dynamics takes the type of t when t is not a plain number, so ForwardDiff in t of a non-autonomous problem no longer writes a Dual into a Float64 buffer. A Float32 state keeps a Float32 buffer. - pseudo_hamiltonian(ocp; constraints) raises an IncorrectArgument, not a MethodError, for constraints not given by labels (a String, nothing, a tuple holding a String, ...). Minor points of the review of #536. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Closes #532. PR B of the plan tracked in #536.
Public getters for the Hamiltonians of an optimal control problem, as methods of the
CTBase.Datageneric functions. They replace the private builders of CTFlows, which will use them in a follow-up.Conventions: ct-conventions v0.14.0, ct-agents v0.5.0.
Phases
pseudo_hamiltonian(ocp; p0)hamiltonian(ocp; p0)for a control-free problemhamiltonian(ocp, u; p0)pseudo_hamiltonian(ocp; constraints=…)hamiltonian(ocp, u; multipliers=…)Each phase is one commit with an explanatory comment.
Depends on control-toolbox/CTBase.jl#663 (not released yet)
Project.tomlhas a temporary[sources]entry that points CTBase to the branch of control-toolbox/CTBase.jl#663. It serves for local testing and for the CI of this PR.CTBase = "0.30.15"once that version is released.[sources]. A CI job on 1.10 will fail until then.Release
No release from this PR alone. CTBase 0.30.15 and CTModels 0.21.3 come after the ecosystem validation described in #536.
🤖 Generated with Claude Code