Repository navigation
Add constraints_to_penalties: lower constraints to quadratic penalty costs - #5111
ChrisRackauckas merged 1 commit into
Conversation
|
Follow-up on the JET QA failure noted in the PR body: investigated on a clean checkout of master ( The ~234 reports with Investigation run by an AI agent (Devin CLI 3000.10.21, model: SWE-2 High). |
|
Status on the remaining red checks — all reproduce identically on master CI (verified against the latest master runs), so none are caused by this PR:
Verified by an AI agent (Devin CLI 3000.10.21, model: SWE-2 High; local session, no shareable URL). |
55c12b3 to
69e9a4a
Compare
…costs Implement a System -> System transformation that rewrites every constraint of a system (including subsystem constraints, recursively) as a weighted quadratic penalty term appended to costs, returning a system with empty constraints. This is the "classical PINN" formulation of a constrained problem: all constraint residuals are folded into the objective, producing an unconstrained system that can be solved by optimizers which do not accept explicit cons/lcons/ucons constraints (an explicit alternative to the constrained-solver lowering used for e.g. Ipopt/MadNLP). - Equation constraints l ~ r contribute weights[i] * (l - r)^2. - Inequality constraints contribute weights[i] * max(residual, 0)^2, where residual is the constraint in canonical residual <= 0 form via Symbolics.canonical_form, so only violations are penalized. - weights may be a scalar applied to every constraint, or a vector with one entry per element of constraints(sys) (own constraints first, then subsystems' recursively). Symbolic weights that are not already parameters or unknowns are automatically added to the system's parameters, rebuilding the IndexCache when present so the transformation also works on completed systems. Motivated by SciML/NeuralPDE.jl#1161 Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Agent-Harness: Devin CLI 3000.10.21 Agent-Model: SWE-2 High Agent-Session: local Devin CLI session (no shareable URL)
69e9a4a to
3317e5b
Compare
#5116) Three recently merged PRs widened ModelingToolkit's surface without updating the two version-controlled QA audit lists in `test/qa/qa.jl`, which is what those lists exist to catch: - #4774 (`limited` operator) exports `limited`, `limitnew`, `limitold` from ModelingToolkitBase and reaches for `MTKBase.LimitedCtx`, `MTKBase.apply_limited_lowering` and `MTKBase.attach_stage_limiters` from the root package. - #5111 exports `constraints_to_penalties` from ModelingToolkitBase. The four exported names are documented public API of ModelingToolkitBase with rendered `@docs` entries (`docs/src/basics/Limiting.md`, `docs/src/API/problems.md`), so they are added to `REEXPORTED_API`. The three qualified accesses are MTK/MTKBase shared implementation seams -- a metadata registry key and two lowering entry points populated by the compiler, not anything a user sets -- so they join `lower_homotopy` and `has_any_homotopy`, the directly analogous names from the `homotopy` operator, in `NONPUBLIC_QUALIFIED_ACCESSES`. Agent-Harness: Claude Code 2.0.14 Agent-Model: claude-opus-5[1m] Agent-Session: https://claude.ai/code/session_01FeJYXni9MkJhPFA9yxYvfn Claude-Session: https://claude.ai/code/session_01FeJYXni9MkJhPFA9yxYvfn Co-authored-by: ChrisRackauckas-Claude <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
- Add constraints_to_penalties: lower constraints to quadratic penalty costs (#5111) - Fix QA: approve the `limited`/`constraints_to_penalties` API additions (#5116) - Concretize initialization problems once at construction (#5062) - Make tovar public (#5120) - Fix SciCompDSL model macro hygiene (#5058) - Release ModelingToolkitBase 1.71.0 (#5126) - Forward ProblemTypeCtx metadata to OptimizationProblem (#5115) - Release ModelingToolkitBase 1.71.1 (#5127) Agent-Harness: Claude Code Agent-Model: claude-opus-5[1m] Claude-Session: https://claude.ai/code/session_014FEzNTLFutCmTEAZ3zBg5R Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Summary
Adds
constraints_to_penalties(sys::System; weights = 1.0)inlib/ModelingToolkitBase, aSystem -> Systemtransformation that removes every constraint ofsys(including subsystem constraints, recursively) and appends a weighted quadratic penalty term tocosts. This is the "classical PINN" formulation of a constrained problem — all constraint residuals folded into the objective — giving an unconstrained system for optimizers that do not accept explicitcons/lcons/uconsconstraints. It is deliberately available as an explicit alternative to the constrained-solver lowering used for e.g. Ipopt/MadNLP.Motivation: SciML/NeuralPDE.jl#1161
Semantics:
Equationconstraintsl ~ rcontributeweights[i] * (l - r)^2.Inequalityconstraints contributeweights[i] * max(residual, 0)^2, whereresidualis the constraint rewritten in canonicalresidual ≲ 0form viaSymbolics.canonical_form, so only violations are penalized.maxis a registered diadic symbolic function and codegens throughbuild_function(already used inconnectors.jl).weightsmay be a scalar applied to every constraint, or a vector with one entry per element ofconstraints(sys)(own constraints first, then subsystem constraints, recursively). Symbolic weights that are not already parameters or unknowns are automatically added to the system's parameters; when the system has anIndexCache(completed systems) it is rebuilt so the weights are tunable through the problem's parameter object.costs(sys)and are combined with the rest of the objective through the system'sconsolidatefunction.The transform is implemented with
@set!(the established system-rebuild pattern), so unknowns, parameters, consolidate, defaults and metadata are preserved, and a completed system stays completed.Test plan
New
constraints_to_penaltiestestset inlib/ModelingToolkitBase/test/optimization/optimizationsystem.jl(20 tests), run withcd lib/ModelingToolkitBase/test/optimization && julia --project=. optimizationsystem.jl:min (x-2)^2s.t.x ≲ 1lowered withweights = 1.0:constraintsemptied,costsgrew by 1,OptimizationProblemhaslcons === ucons === nothingandf.cons === nothing, and unconstrained Nelder-Mead findsx ≈ 1.5 = (2 + w)/(1 + w)(vsx ≈ 2without the constraint).weights = 1e4pulls the argmin to(2 + 1e4)/(1 + 1e4) ≈ 1.0001.x ~ 0.5gives the expectedargmin 1.25.ArgumentError.parametersand is tunable via the problem'sp.Observed output:
All other testsets in
optimizationsystem.jlpass unchanged. Runic formatting clean (Runic.main--check/--inplaceproduced no diff) andtyposclean over the changed files. The QA group (Aqua/ExplicitImports) passes; its JET subtest reports 234 pre-existing "possible errors" (all##And#/##Call#locals generated by Moshi@matchin untouched files — none inoptimizationproblem.jlor the new functions), which is a master pre-existing failure unrelated to this change. The docs build was not run locally; the change adds one@docsentry (ModelingToolkit.constraints_to_penalties, verified to resolve toModelingToolkitBase.constraints_to_penalties).🤖 Generated with Devin (harness: Devin CLI 3000.10.21, model: SWE-2 High, session: local Devin CLI session — no shareable URL)