Repository navigation
Description
Agents: read the guide for agents in #310 first (order, difficulty, how to claim an issue, working method). Read
AGENTS.mdand clone.ct-conventions(v0.11.0) and.ct-agents(v0.4.0). Specification: this description and #308.
Sub-issue of #308, part 2 of 3. Depends on #437 (ModelingToolkitBackend and continuous bryson_denham model).
Goal
Implement the Corleone-based solve helper in test/common/solve_modelingtoolkit.jl (keeping Corleone strictly in the test environment, outside the package extension), complete the test/suite/ModelingToolkit/ test suite (test_methods.jl, test_problem.jl, test_smoke.jl, test_reference_solution.jl), and promote bryson_denham from :draft to :valid for ModelingToolkit.
At the end of this issue, bryson_denham(ModelingToolkitBackend()) is verified as a valid ModelingToolkit.System model (dynamics, costs, constraints, boundary conditions), solved with Corleone + Ipopt using the shared :init from metadata, verified against the analytic reference solution (:reference_solution), marked :valid, and all suite/ModelingToolkit and suite/core tests pass.
Technical Findings (from #308 preliminary checks)
-
Model verification vs. Solver algorithmics:
UnlikeJuMPBackendwhere we write our own transcription in this repository and must verify that our discrete NLP matches CTDirect (suite/comparison/test_equivalence.jl),ModelingToolkitBackendproduces a continuous symbolic model (ModelingToolkit.System), andCorleoneis an external solver using direct shooting with ODE integration (OrdinaryDiffEq).
Because the transcriptions are completely different (shooting vs. collocation) and the algorithmics are internal toCorleone, there is no algorithmic or NLP equivalence test with Corleone.
What is essential is verifying the model itself:- Structure and equations (
test_problem.jl): state and control variables, independent variable$t$ , dynamics$\dot{x} = f(x, u, t)$ , boundary conditions, path constraints, and cost functional. - Solvability (
test_smoke.jl): verifying that the model can be fed toCorleonewithout translation or dimensional mismatch and advance at least 1 iteration. - Solution accuracy (
test_reference_solution.jl): checking that solving withCorleoneconverges to the analytic reference solution ($J^* = 4.5$ for Bryson-Denham) within discretization tolerance.
- Structure and equations (
-
Discretizing continuous path constraints for
Corleone:
Corleone.CorleoneDynamicOptProblem(sys, ...)(Corleone/ext/MTKExtension/utils.jl:75) requires every signal callx(τ)inModelingToolkit.get_constraints(sys)to have a concrete finite real numberτ(time isa Real && isfinite(time)), throwingArgumentError("Signal times must be finite real numbers: x₁(t).")on symbolict = ModelingToolkit.get_iv(sys).
Intest/common/solve_modelingtoolkit.jl, before callingCorleoneDynamicOptProblem, inspectModelingToolkit.get_constraints(sys):- Keep point constraints (neither
lhsnorrhscontainsiv = ModelingToolkit.get_iv(sys)as a free variable, e.g.x₁(tf) ~ x1_tf) as-is. - Expand any path constraint (whose
lhsorrhscontainsiv, e.g.x₁(t) ≲ x1_max) bySymbolics.substitute(c, Dict(iv => τ))for eachτinconstraint_grid(filtering outt0if initial state is fixed), and rebuild aModelingToolkit.Systemwith the discretized constraints.
- Keep point constraints (neither
-
Extracting
tspan = (t0, tf)fromsys:
Extract(t0, tf)from theSymbolics.Integraldomain inModelingToolkit.get_costs(sys)(DomainSets.infimum/supremumorinterval.left/interval.right). -
Injecting shared
:init(#306) intomodel.ps:
Inmodel = Corleone.CorleoneDynamicOptProblem(sys_disc, tspan, ...; switches=control_grid[2:(end - 1)], shooting=shooting_grid, alg=OrdinaryDiffEqTsit5.Tsit5()):-
model.pshas fields(initialcondition = ..., parameter = ...). -
Controls (
model.ps.parameter.parameters): forN = length(control_grid) - 1piecewise-constant intervals[t_k, t_{k+1}], evaluateinit.controlat the interval midpoints (or left endpoints) and write into each control's.psvector of lengthN. -
States (
model.ps.initialcondition): in single shooting with fixed initial state (tunable = false),model.ps.initialconditionis empty; in multiple shooting, evaluateinit.stateat the shooting nodes and write intomodel.ps.initialcondition(or viaCorleone.custom_initialization). - Pass the updated
pstoOptimization.OptimizationProblem(model, Optimization.AutoForwardDiff(); ps=ps)soopt.u0uses our shared initial guess.
-
Mandatory Rules & Conventions (ct-conventions v0.11.0, #310)
- Worktree per issue (
#310rule 3a): Never edit or switch branches in the main checkout. Use a dedicated worktree (git worktree add ../OptimalControlProblems-<issue> -b feat/<issue>-mtk-corleone-tests main). - Separation of modeling and solving:
Corleonemust not be added to the rootProject.tomlorext/ModelingToolkitModels.jl; it belongs only intest/suite/ModelingToolkit/Project.tomlandtest/common/solve_modelingtoolkit.jl. - Qualified imports only (
MOD3,MOD4): Always useusing Pkg: Pkg, sym. Never use bareusing Pkgorimport. - Fake types at module top-level (
TST3): Any helper structs in tests must be defined at module top-level, never inside@testsetor functions. - No hand-written problem lists (
test_no_handwritten_listintest/suite/core/test_consistency.jl): Never write:bryson_denhamor"bryson_denham"literals intest/! Always loop withMain.TestStatus.for_each_problem(..., OptimalControlProblems.ModelingToolkitBackend()).
Detailed Implementation Plan
Files to Create or Modify
| File | Action | Details |
|---|---|---|
test/suite/ModelingToolkit/Project.toml |
Modify | Add Corleone (compat = "2"), OrdinaryDiffEqTsit5 ("2"), Optimization ("5"), OptimizationMOI ("1"), Ipopt ("1"), ComponentArrays ("0.15"), ForwardDiff ("1"), OptimalControl ("2.2"), Symbolics, SciMLBase. |
test/common/configs.jl |
Modify | Add ModelingToolkit = [(shooting=:single, solver=:ipopt)] to CONFIGS. |
test/common/status.jl |
Modify | Add "ModelingToolkit", "Corleone", "OrdinaryDiffEqTsit5" to KEY_DEPENDENCIES. |
test/common/solve_modelingtoolkit.jl |
Create | Module SolveModelingToolkit:- _extract_tspan(sys)- _build_grids(tspan; grid_size, shooting, control_grid, constraint_grid, shooting_grid)- _discretize_path_constraints(sys, constraint_grid)- _apply_initial_guess!(model, sys, init, control_grid, shooting_grid)- prepare(pb; shooting=:single, grid_size=nothing, ...)- solve(pb; shooting=:single, solver=:ipopt, grid_size=nothing, max_iter=1000, tol=1e-6, print_level=0, ...)- outcome(sol, model, reconstruct), settings(pb, cfg; grid_size=nothing), trajectory(sol, model, reconstruct, sys) |
test/suite/ModelingToolkit/test_methods.jl |
Create | Unit-test SolveModelingToolkit: _extract_tspan, _build_grids (single & multiple shooting), _discretize_path_constraints, _apply_initial_guess!, settings, outcome, trajectory. |
test/suite/ModelingToolkit/test_problem.jl |
Modify / Complete | Test the ModelingToolkit.System model directly: components, dimensions, symbolic dynamics evaluation against expected equations, cost expression, and constraints. |
test/suite/ModelingToolkit/test_smoke.jl |
Create | Smoke test every problem in ModelingToolkitBackend() across CONFIGS.ModelingToolkit with grid_size = TestConfigs.SMALL_GRID (20) and max_iter = TestConfigs.SMOKE_MAX_ITER (10): verify that Corleone runs without error and returns finite values. |
test/suite/ModelingToolkit/test_reference_solution.jl |
Create | For each problem in ModelingToolkitBackend() with has_reference_solution(problem), solve with Corleone + Ipopt (grid_size=50, tol=1e-6) and verify that the optimal objective matches reference_solution_data(problem).objective within discretization tolerance. |
ext/MetaData/bryson_denham.jl |
Modify | Switch ModelingToolkit status from :draft to :valid. |
Verification Commands
julia --project=. -e 'using Pkg; Pkg.test(test_args=["suite/ModelingToolkit"])'
julia --project=. -e 'using Pkg; Pkg.test(test_args=["suite/core"])'Done when
-
test/common/solve_modelingtoolkit.jldiscretizes continuous path constraints ontoconstraint_grid, injects shared:initintomodel.ps, and solves withCorleone+Ipopt. -
test_methods.jl,test_problem.jl,test_smoke.jl, andtest_reference_solution.jlall pass insuite/ModelingToolkit. -
bryson_denhamstatus forModelingToolkitis:validinext/MetaData/bryson_denham.jl. -
suite/corepasses (includingtest_consistency.jlandtest_no_handwritten_list).
Activity
[-][Tests] Corleone solve helper and ModelingToolkit test suite (smoke, equivalence, reference solution) (#308)[/-][+][Tests] Corleone solve helper and ModelingToolkit test suite (smoke, reference solution) (#308)[/+]Merge pull request #442 from control-toolbox/feat/438-mtk-corleone-tests