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

Đang mở
#437 1 bình luận 0 reaction 1 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@ocots đang làm issue này rồi.

Từ ngày 8/11/2026.

Đánh giá

Issue này chưa được đánh giá.

Mô tả

feature

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

Ngôn ngữ chính
Julia
Star
13
Fork
1
Merge trung bình
3 giờ 33 phút
Pull request đã merge (30 ngày)
55

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của control-toolbox/OptimalControlProblems.jl

Tất cả issue của control-toolbox/OptimalControlProblems.jl

Issue tương tự

Thêm issue về Julia

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.