Skip to content

[Problems] ModelingToolkitBackend, extension ModelingToolkitModels, and continuous bryson_denham (#308) #437

Closed

Description

@ocots

Agents: read the guide for agents in #310 first (order, difficulty, how to claim an issue, working method). Read AGENTS.md and clone .ct-conventions (v0.11.0) and .ct-agents (v0.4.0). Specification: this description and #308.

Sub-issue of #308, part 1 of 3. Independent (all prerequisites in #310 are merged).


Goal

Introduce the ModelingToolkitBackend (:ModelingToolkit) modeling, the ModelingToolkitModels weak-dependency extension, the CTModels.Models component/dimension interface on ModelingToolkit.AbstractSystem, and our own continuous bryson_denham model in ext/ModelingToolkitModels/bryson_denham.jl (with status :draft until Part 2 adds the Corleone equivalence and reference tests), together with problem-retrieval unit tests in test/suite/ModelingToolkit/test_problem.jl.

At the end of this issue, bryson_denham(ModelingToolkitBackend()) is functional, returns a continuous Problem wrapping a ModelingToolkit.System with shared parameters and :init from metadata, and both suite/core and suite/ModelingToolkit pass.


Technical Findings (from #308 preliminary checks)

  1. Continuous path constraint (a) works in ModelingToolkit.System (v11):
    ModelingToolkit.System(eqs, t; costs, constraints, name) accepts a symbolic path inequality depending on the independent variable t = ModelingToolkit.t_nounits:
    cstr = [x₁(tf) ~ x1_tf, x₂(tf) ~ x2_tf, x₁(t) ≲ x1_max]
    The time horizon (t0, tf) is carried by the integral cost domain Symbolics.Integral(t in (t0, tf))(0.5 * u(t)^2).
    Therefore bryson_denham(ModelingToolkitBackend(); parameters=nothing, init=:default) takes no grid argument and returns a purely continuous ModelingToolkit.System. (Sampling x₁(t) ≲ x1_max onto constraint_grid is done at solve time in Part 2.)
  2. CTModels.Models interface on ModelingToolkit.AbstractSystem:
    Base.show(io, ::MIME"text/plain", pb::Problem) calls CTModels.Models.state_dimension(model), control_dimension(model), and variable_dimension(model).
    In ext/ModelingToolkitModels.jl, define on sys::ModelingToolkit.AbstractSystem:
    • States: unknowns(sys) where !ModelingToolkit.isinput(v)
    • Controls: unknowns(sys) where ModelingToolkit.isinput(v) (declared with [input = true])
    • Variables: parameters(sys) where ModelingToolkit.istunable(p, true) && !ModelingToolkit.isinput(p)
      Strip the (t) suffix from symbolic names so component names are "x₁", "x₂", "u" (matching OptimalControl and JuMP modelings).

Mandatory Rules & Conventions (ct-conventions v0.11.0, #310)

  • Worktree per issue (#310 rule 3a): Never edit or switch branches in the main checkout. Create a dedicated worktree (git worktree add ../OptimalControlProblems-<issue> -b feat/<issue>-mtk-backend main).
  • Plan before code (AGENTS.md): Propose a plan following .ct-conventions/plan-template.md and wait for confirmation if working locally with a human, or post the plan on the issue after claiming it (#310 rule 3b).
  • Qualified imports only (MOD3, MOD4): Always use using Pkg: Pkg, sym. Never use bare using Pkg or import.
  • No top-level exports (MOD8): Only ModelingToolkitBackend is exported from Modelings and re-exported by OptimalControlProblems (matching OptimalControlBackend, OptimalControlScalarizedBackend, JuMPBackend).
  • 1-D is a scalar (DIM1): A 1-D state, control, or variable is a Number, not a 1-element Vector.
  • No hand-written problem lists (test_no_handwritten_list in test/suite/core/test_consistency.jl): Never write :bryson_denham or "bryson_denham" literals outside docstrings in src/, test/, or ext/*.jl. In tests, always iterate with Main.TestStatus.for_each_problem(..., OptimalControlProblems.ModelingToolkitBackend()) or OptimalControlProblems.problems(OptimalControlProblems.ModelingToolkitBackend()).
  • Docstrings (DOC1–DOC8): English only (GEN1), written once implementation is stable.

Detailed Implementation Plan

###Files to Create or Modify

File Action Details
Project.toml Modify Add ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" to [weakdeps], ModelingToolkitModels = "ModelingToolkit" to [extensions], and ModelingToolkit = "11" to [compat].
src/Modelings/backends.jl Modify Define struct ModelingToolkitBackend <: AbstractModelBackend end. Add (type=ModelingToolkitBackend, name=:ModelingToolkit, ext=:ModelingToolkitModels, pkg="ModelingToolkit") to MODELING_SPECS. Update the docstring table.
src/Modelings/Modelings.jl Modify Export ModelingToolkitBackend.
src/OptimalControlProblems.jl Modify Import and re-export ModelingToolkitBackend.
ext/ModelingToolkitModels.jl Create Module ModelingToolkitModels: include all ext/ModelingToolkitModels/*.jl files; extend CTModels.Models.{state_components,control_components,variable_components,state_dimension,control_dimension,variable_dimension} on ModelingToolkit.AbstractSystem.
ext/ModelingToolkitModels/bryson_denham.jl Create Define OptimalControlProblems.bryson_denham(::OptimalControlProblems.ModelingToolkitBackend; parameters=nothing, init=:default): merge parameters with metadata(:bryson_denham)[:parameters], parse init via Problems.parse_init, build the continuous ModelingToolkit.System (@variables x₁(t) = x1_0 [tunable = false] x₂(t) = x2_0 [tunable = false] u(t) [input = true], D(x₁) ~ x₂, D(x₂) ~ u, costs = [Symbolics.Integral(t in (t0, tf))(0.5 * u^2)], constraints = [x₁(tf) ~ x1_tf, x₂(tf) ~ x2_tf, x₁(t) ≲ x1_max]), and return Problems.Problem(:bryson_denham, meta[:title], sys, init_parsed).
ext/MetaData/bryson_denham.jl Modify Add ModelingToolkit=:draft to :status.
docs/modelings.jl Modify Add :ModelingToolkit => ModelingAdapter(...) to MODELINGS ( required by consistency/doc checks whenever a backend is added to MODELING_SPECS).
test/suite/core/test_modelings.jl Modify Add OptimalControlProblems.ModelingToolkitBackend() to BACKENDS.
test/suite/ModelingToolkit/Project.toml Create Isolated test environment with ModelingToolkit (compat = "11"), CTBase, CTModels, Test.
test/suite/ModelingToolkit/test_problem.jl Create Test Problem retrieval, model(pb) isa ModelingToolkit.AbstractSystem, CTModels dimensions and component names, equations/costs/constraints presence, parameters override, and init variants (:default, :zero, custom NamedTuple, invalid init throwing CTBase.Exceptions.IncorrectArgument) using Main.TestStatus.for_each_problem.

Verification Commands

julia --project=. -e 'using Pkg; Pkg.test(test_args=["suite/core"])'
julia --project=. -e 'using Pkg; Pkg.test(test_args=["suite/ModelingToolkit"])'

Done when

  • ModelingToolkitBackend is registered in MODELING_SPECS and exported.
  • ext/ModelingToolkitModels.jl and ext/ModelingToolkitModels/bryson_denham.jl build a continuous ModelingToolkit.System with continuous path constraint x₁(t) ≲ x1_max, integral cost ∫ 0.5 u(t)², our parameters, and shared :init.
  • CTModels.Models dimension and component functions work on ModelingToolkit.AbstractSystem, and Base.show displays the Problem summary cleanly.
  • bryson_denham has ModelingToolkit=:draft in ext/MetaData/bryson_denham.jl.
  • suite/core (including test_no_handwritten_list and test_modelings) and suite/ModelingToolkit (test_problem.jl) pass with 0 failures/errors.

Refs #308, #310

Activity

  1. ocots commented on Nov 8, 2026

    @ocots
    MemberAuthor

    Claiming #437. Pinned versions: ct-conventions v0.11.0, ct-agents v0.4.0. Creating dedicated worktree.

  2. added a commit that references this issue on Nov 8, 2026
  3. ocots commented on Nov 9, 2026

    @ocots
    MemberAuthor

    Done in #440 (merged).

    What was done

    • Weakdep & Extension: Registered weak dependency ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" (compat "11") and extension ModelingToolkitModels = "ModelingToolkit" in Project.toml.
    • Backend: Defined and exported ModelingToolkitBackend in src/Modelings/backends.jl, src/Modelings/Modelings.jl, and src/OptimalControlProblems.jl. Added its entry to MODELING_SPECS.
    • Model Contract: Implemented CTModels.Models methods (state_components, state_dimension, control_components, control_dimension, variable_components, variable_dimension) for ModelingToolkit.AbstractSystem in ext/ModelingToolkitModels.jl.
    • Continuous Model: Built OptimalControlProblems.bryson_denham(::ModelingToolkitBackend) in ext/ModelingToolkitModels/bryson_denham.jl, wrapping a continuous ModelingToolkit.System with continuous path constraint x₁(t) ≲ x1_max, integral cost ∫ 0.5 u(t)², default parameters, and shared :init.
    • Metadata & Docs Adapter: Added ModelingToolkit=:draft status in ext/MetaData/bryson_denham.jl, and added the :ModelingToolkit documentation adapter in docs/modelings.jl.
    • Test Suite: Created isolated test environment test/suite/ModelingToolkit/Project.toml and test file test/suite/ModelingToolkit/test_problem.jl. Both suite/core and suite/ModelingToolkit pass cleanly. Documentation builds without warnings.

    Follow-up: #438 (Part 2 — Corleone solve helper and test suite).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

featureNew feature or request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions