diff --git a/.github/workflows/CronModelingToolkit.yml b/.github/workflows/CronModelingToolkit.yml index 05c902b6e..37d930767 100644 --- a/.github/workflows/CronModelingToolkit.yml +++ b/.github/workflows/CronModelingToolkit.yml @@ -1,7 +1,15 @@ name: Cron ModelingToolkit on: + schedule: + - cron: "0 4 * * 6" workflow_dispatch: + inputs: + problems: + description: "Only these problems, comma-separated (e.g. beam): publishes their results, leaves the reports, the state and the issues" + required: false + default: '' + type: string permissions: contents: read @@ -17,6 +25,6 @@ jobs: uses: ./.github/workflows/cron-reusable.yml with: environment: ModelingToolkit - enabled: false - note: "`CronModelingToolkit.yml` · manual only until the scope exists" + problems: ${{ inputs.problems }} + note: "`CronModelingToolkit.yml` · weekly (Saturday 04:00 UTC) · Julia 1.13, Ubuntu + Windows, CPU" secrets: inherit diff --git a/.github/workflows/README.md b/.github/workflows/README.md index 77a91c0b8..baf4ce864 100644 --- a/.github/workflows/README.md +++ b/.github/workflows/README.md @@ -9,7 +9,7 @@ scope labels. They are staggered at 04:00 UTC: | `CronOptimalControl.yml` | `OptimalControl` | Wednesday | Yes | | `CronOptimalControlScalarized.yml` | `OptimalControlScalarized` | Thursday | Yes | | `CronComparison.yml` | `comparison` | Friday | No | -| `CronModelingToolkit.yml` | `ModelingToolkit` | manual only until the scope exists | No | +| `CronModelingToolkit.yml` | `ModelingToolkit` | Saturday | No | | `CronRobustness.yml` | `JuMP-robustness`, `OptimalControl-robustness` | 1st of the month | No | Each thin workflow calls `cron-reusable.yml` (inputs `mode`: `cron`, or `robustness` for diff --git a/CHANGELOG.md b/CHANGELOG.md index 63c5ab495..8d6664fe0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -87,6 +87,12 @@ See [BREAKING.md](BREAKING.md). (tested once on fake problems, and on a few representative problems against CTDirect): see `test/README.md` (#441). A weekly cron, `comparison`, compares the NLP of every problem with CTDirect's. +- The ModelingToolkit modeling follows the same tests (#439): its model is compared with the + OptimalControl one without discretizing, it shares the API, smoke and convergence tests of + the other modelings, and a weekly cron reports its solves by direct shooting with Corleone, + started from the initial guess of the problem, with their trajectories for the documentation. +- `vanderpol`, `robbins` and `jackson` are written in the ModelingToolkit modeling too, with a + Lagrange cost, a Mayer cost, state and control bounds (#439). ### 📦 Dependencies diff --git a/docs/Project.toml b/docs/Project.toml index 6ed84e7f8..9e33f93fe 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -10,6 +10,7 @@ JuMP = "4076af6c-e467-56ae-b986-b466b2749572" LiveServer = "16fef848-5104-11e9-1b77-fb7a48bbb589" MadNLP = "2621e9c9-9eb4-46b1-8089-e8c72242dfb6" Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a" +ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" NLPModelsIpopt = "f4238b75-b362-5c4c-b852-0801c9a21d71" OptimalControl = "5f98b655-cc9a-415a-b60e-744165666948" Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" @@ -30,6 +31,7 @@ JuMP = "1" LiveServer = "1" MadNLP = "0.10" Markdown = "1" +ModelingToolkit = "11" NLPModelsIpopt = "0.11" OptimalControl = "2.2" Plots = "1" diff --git a/docs/api_reference.jl b/docs/api_reference.jl index fa135e00e..32d42efee 100644 --- a/docs/api_reference.jl +++ b/docs/api_reference.jl @@ -58,6 +58,7 @@ function generate_api_reference(src_dir::String, ext_dir::String) ), ), (sym=:OptimalControlModels, files=ext("OptimalControlModels.jl")), + (sym=:ModelingToolkitModels, files=ext("ModelingToolkitModels.jl")), ] loaded_extensions = [ (cfg=cfg, mod=Base.get_extension(OptimalControlProblems, cfg.sym)) for diff --git a/docs/make.jl b/docs/make.jl index 0da100356..7dc1f620a 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -14,6 +14,7 @@ using CTBase # automatic API reference using OptimalControlProblems using OptimalControl # loads the OptimalControl extension using JuMP # loads the JuMP extension +using ModelingToolkit: ModelingToolkit # loads the ModelingToolkit extension using Plots # The extensions, by name, for the `@docs` blocks of the API Reference. @@ -21,6 +22,9 @@ const JuMPModels = Base.get_extension(OptimalControlProblems, :JuMPModels) const OptimalControlModels = Base.get_extension( OptimalControlProblems, :OptimalControlModels ) +const ModelingToolkitModels = Base.get_extension( + OptimalControlProblems, :ModelingToolkitModels +) # Plots are saved as SVG (SITE12): DocumenterVitepress prefers PNG when both are offered. Base.showable(::MIME"image/png", ::Plots.Plot) = false diff --git a/docs/modelings.jl b/docs/modelings.jl index 6eb1ac51e..1538c4fe9 100644 --- a/docs/modelings.jl +++ b/docs/modelings.jl @@ -75,16 +75,22 @@ const ADAPTERS = Dict{Symbol,NamedTuple}( ), :ModelingToolkit => ( title="ModelingToolkit", - summary="The continuous problem, written with ModelingToolkit.jl, solved with direct \ - shooting via Corleone.jl.", - packages=["ModelingToolkit", "Corleone", "Ipopt"], + summary="The continuous problem, written as a ModelingToolkit.jl `System` with its \ + costs and constraints. The tests solve it by direct single shooting with Corleone.jl: \ + the controls are piecewise constant and the state path constraints are imposed at the \ + nodes of the grid; these choices are not part of the model, so the snippet stops at \ + the model.", + packages=["ModelingToolkit"], snippet=(problem, grid_size) -> """ using ModelingToolkit, OptimalControlProblems pb = $(problem)(ModelingToolkitBackend()) - sys = model(pb)""", + sys = model(pb) # a ModelingToolkit.System + init = initial_guess(pb) # the initial guess of the problem""", model_start=r"^\s*ModelingToolkit\.@named sys = ModelingToolkit\.System\(", - checked_against=:OptimalControl, + # compared with OptimalControl at the continuous level, in the opt-in ModelingToolkit + # environment only (`suite/ModelingToolkit/test_modelings`): not on every pull request + checked_against=nothing, ), ) diff --git a/docs/src/getting-started/first-problem.md b/docs/src/getting-started/first-problem.md index b7a539b36..fda7f4575 100644 --- a/docs/src/getting-started/first-problem.md +++ b/docs/src/getting-started/first-problem.md @@ -12,7 +12,7 @@ using NLPModelsIpopt ## Get the problem A problem is a function, called with the modeling as argument. With OptimalControl it returns a -[`Problem`](@ref OptimalControlProblems.Problem): the continuous problem and its initial guess. +[`Problem`](@ref OptimalControlProblems.Problems.Problem): the continuous problem and its initial guess. ```@example first pb = beam(OptimalControlBackend()) diff --git a/docs/src/guide/get.md b/docs/src/guide/get.md index 842bb5a2e..cd623be76 100644 --- a/docs/src/guide/get.md +++ b/docs/src/guide/get.md @@ -22,7 +22,7 @@ it can represent, and takes at retrieval only what it needs to build it: | Modeling | Returns | Keywords | | :--- | :--- | :--- | -| OptimalControl, scalarized OptimalControl | a [`Problem`](@ref OptimalControlProblems.Problem): the continuous problem and its initial guess | `parameters`, `init` | +| OptimalControl, scalarized OptimalControl | a [`Problem`](@ref OptimalControlProblems.Problems.Problem): the continuous problem and its initial guess | `parameters`, `init` | | JuMP | a `JuMP.Model`, already discretized | `grid_size`, `scheme`, `ordering`, `parameters`, `init`, and the arguments of `JuMP.Model` | ```@example get diff --git a/docs/src/migration.md b/docs/src/migration.md index 447f11ba1..695c4382a 100644 --- a/docs/src/migration.md +++ b/docs/src/migration.md @@ -5,7 +5,7 @@ The breaking changes since 0.5, with how to update your code. The full list is i ## An OptimalControl problem is the continuous problem -A problem of the OptimalControl modelings returns a [`Problem`](@ref OptimalControlProblems.Problem): +A problem of the OptimalControl modelings returns a [`Problem`](@ref OptimalControlProblems.Problems.Problem): the continuous model and its initial guess. It no longer discretizes, and no longer takes a description or `grid_size`: the discretization, the modeler and the solver are chosen in `solve`. diff --git a/ext/MetaData/jackson.jl b/ext/MetaData/jackson.jl index aa16e1416..bd3f1e8bc 100644 --- a/ext/MetaData/jackson.jl +++ b/ext/MetaData/jackson.jl @@ -18,7 +18,7 @@ jackson_meta = OrderedDict( b_t0=0, x₃_t0=0, ), - :status => (OptimalControl=:valid, OptimalControlScalarized=:valid, JuMP=:valid), + :status => (OptimalControl=:valid, OptimalControlScalarized=:valid, JuMP=:valid, ModelingToolkit=:valid), # initial guess of every modeling (#377), from the parameters :init => p -> (state=[0.1, 0.1, 0.1], control=0.1), ) diff --git a/ext/MetaData/robbins.jl b/ext/MetaData/robbins.jl index f359658da..0524af68f 100644 --- a/ext/MetaData/robbins.jl +++ b/ext/MetaData/robbins.jl @@ -14,7 +14,7 @@ robbins_meta = OrderedDict( x₂_tf=0, x₃_tf=0, ), - :status => (OptimalControl=:valid, OptimalControlScalarized=:valid, JuMP=:valid), + :status => (OptimalControl=:valid, OptimalControlScalarized=:valid, JuMP=:valid, ModelingToolkit=:valid), # initial guess of every modeling (#377), from the parameters :init => p -> (state=[0.1, 0.1, 0.1], control=0.1), ) diff --git a/ext/MetaData/vanderpol.jl b/ext/MetaData/vanderpol.jl index 61d2b8df1..ca63f37b3 100644 --- a/ext/MetaData/vanderpol.jl +++ b/ext/MetaData/vanderpol.jl @@ -1,7 +1,7 @@ vanderpol_meta = OrderedDict( :grid_size => 500, :parameters => (t0=0, tf=2, ω=1, ε=1, x₁_t0=1, x₂_t0=0), - :status => (OptimalControl=:valid, OptimalControlScalarized=:valid, JuMP=:valid), + :status => (OptimalControl=:valid, OptimalControlScalarized=:valid, JuMP=:valid, ModelingToolkit=:valid), # initial guess of every modeling (#377), from the parameters :init => p -> (state=[0.1, 0.1], control=0.1), ) diff --git a/ext/ModelingToolkitModels/jackson.jl b/ext/ModelingToolkitModels/jackson.jl new file mode 100644 index 000000000..aeb6a5c78 --- /dev/null +++ b/ext/ModelingToolkitModels/jackson.jl @@ -0,0 +1,62 @@ +"""The **Jackson problem** is a classical benchmark in optimal control. + +Builds the `jackson` problem with the ModelingToolkit backend. + +```julia-repl +julia> using ModelingToolkit, OptimalControlProblems +julia> pb = jackson(ModelingToolkitBackend()) +``` + +Default parameters and the modeling status are available through +`problem_metadata(:jackson)` and `OptimalControlProblems.status(:jackson)`. +""" +function OptimalControlProblems.jackson( + ::ModelingToolkitBackend; + parameters::Union{Nothing,NamedTuple}=nothing, + init=nothing, +) + OptimalControlProblems.Catalogue._warn_if_draft(:jackson, :ModelingToolkit) + params = OptimalControlProblems.parameters_data(:jackson, parameters) + t0, tf = float(params[:t0]), float(params[:tf]) + k1, k2, k3 = params[:k1], params[:k2], params[:k3] + a_l, a_u = float(params[:a_l]), float(params[:a_u]) + b_l, b_u = float(params[:b_l]), float(params[:b_u]) + x₃_l, x₃_u = float(params[:x₃_l]), float(params[:x₃_u]) + u_l, u_u = float(params[:u_l]), float(params[:u_u]) + a_t0, b_t0, x₃_t0 = float(params[:a_t0]), float(params[:b_t0]), float(params[:x₃_t0]) + + t = ModelingToolkit.t_nounits + D = ModelingToolkit.Differential(t) + + ModelingToolkit.@variables a(..) = a_t0 [tunable = false] + ModelingToolkit.@variables b(..) = b_t0 [tunable = false] + ModelingToolkit.@variables x₃(..) = x₃_t0 [tunable = false] + ModelingToolkit.@variables u(..) [input = true, bounds = (u_l, u_u)] + + eqs = [ + D(a(t)) ~ -u(t) * (k1 * a(t) - k2 * b(t)), + D(b(t)) ~ u(t) * (k1 * a(t) - k2 * b(t)) - (1 - u(t)) * k3 * b(t), + D(x₃(t)) ~ (1 - u(t)) * k3 * b(t), + ] + # a Mayer cost: the final value of x₃ + costs = [-x₃(tf)] + constraints = [ + ModelingToolkit.Symbolics.:≳(a(t), a_l), + ModelingToolkit.Symbolics.:≲(a(t), a_u), + ModelingToolkit.Symbolics.:≳(b(t), b_l), + ModelingToolkit.Symbolics.:≲(b(t), b_u), + ModelingToolkit.Symbolics.:≳(x₃(t), x₃_l), + ModelingToolkit.Symbolics.:≲(x₃(t), x₃_u), + ] + + ModelingToolkit.@named sys = ModelingToolkit.System( + eqs, t; costs=costs, constraints=constraints + ) + + # initial guess: from the metadata (#377), unless given + init = OptimalControlProblems.Metadata._initial_guess(:jackson, params, init) + + return OptimalControlProblems.Problems._problem( + :jackson, :ModelingToolkit, sys, init, params + ) +end diff --git a/ext/ModelingToolkitModels/robbins.jl b/ext/ModelingToolkitModels/robbins.jl new file mode 100644 index 000000000..8c9c5ce0b --- /dev/null +++ b/ext/ModelingToolkitModels/robbins.jl @@ -0,0 +1,57 @@ +"""The **Robbins benchmark problem** is a classical optimal control problem introduced by Robbins (1980), involving a **third-order state constraint**. + +Builds the `robbins` problem with the ModelingToolkit backend. + +```julia-repl +julia> using ModelingToolkit, OptimalControlProblems +julia> pb = robbins(ModelingToolkitBackend()) +``` + +Default parameters and the modeling status are available through +`problem_metadata(:robbins)` and `OptimalControlProblems.status(:robbins)`. +""" +function OptimalControlProblems.robbins( + ::ModelingToolkitBackend; + parameters::Union{Nothing,NamedTuple}=nothing, + init=nothing, +) + OptimalControlProblems.Catalogue._warn_if_draft(:robbins, :ModelingToolkit) + params = OptimalControlProblems.parameters_data(:robbins, parameters) + t0, tf = float(params[:t0]), float(params[:tf]) + α, β, γ = params[:α], params[:β], params[:γ] + x₁_l = float(params[:x₁_l]) + x₁_t0, x₂_t0, x₃_t0 = float(params[:x₁_t0]), float(params[:x₂_t0]), float(params[:x₃_t0]) + x₁_tf, x₂_tf, x₃_tf = float(params[:x₁_tf]), float(params[:x₂_tf]), float(params[:x₃_tf]) + + t = ModelingToolkit.t_nounits + D = ModelingToolkit.Differential(t) + + ModelingToolkit.@variables x₁(..) = x₁_t0 [tunable = false] + ModelingToolkit.@variables x₂(..) = x₂_t0 [tunable = false] + ModelingToolkit.@variables x₃(..) = x₃_t0 [tunable = false] + ModelingToolkit.@variables u(..) [input = true] + + eqs = [D(x₁(t)) ~ x₂(t), D(x₂(t)) ~ x₃(t), D(x₃(t)) ~ u(t)] + costs = [ + ModelingToolkit.Symbolics.Integral(t in (t0, tf))( + α * x₁(t) + β * x₁(t)^2 + γ * u(t)^2 + ), + ] + constraints = [ + x₁(tf) ~ x₁_tf, + x₂(tf) ~ x₂_tf, + x₃(tf) ~ x₃_tf, + ModelingToolkit.Symbolics.:≳(x₁(t), x₁_l), + ] + + ModelingToolkit.@named sys = ModelingToolkit.System( + eqs, t; costs=costs, constraints=constraints + ) + + # initial guess: from the metadata (#377), unless given + init = OptimalControlProblems.Metadata._initial_guess(:robbins, params, init) + + return OptimalControlProblems.Problems._problem( + :robbins, :ModelingToolkit, sys, init, params + ) +end diff --git a/ext/ModelingToolkitModels/vanderpol.jl b/ext/ModelingToolkitModels/vanderpol.jl new file mode 100644 index 000000000..581125422 --- /dev/null +++ b/ext/ModelingToolkitModels/vanderpol.jl @@ -0,0 +1,52 @@ +"""The **Van der Pol oscillator control problem** is a benchmark in nonlinear optimal control. + +Builds the `vanderpol` problem with the ModelingToolkit backend. + +```julia-repl +julia> using ModelingToolkit, OptimalControlProblems +julia> pb = vanderpol(ModelingToolkitBackend()) +``` + +Default parameters and the modeling status are available through +`problem_metadata(:vanderpol)` and `OptimalControlProblems.status(:vanderpol)`. +""" +function OptimalControlProblems.vanderpol( + ::ModelingToolkitBackend; + parameters::Union{Nothing,NamedTuple}=nothing, + init=nothing, +) + OptimalControlProblems.Catalogue._warn_if_draft(:vanderpol, :ModelingToolkit) + params = OptimalControlProblems.parameters_data(:vanderpol, parameters) + t0, tf = float(params[:t0]), float(params[:tf]) + ω, ε = params[:ω], params[:ε] + x₁_t0, x₂_t0 = float(params[:x₁_t0]), float(params[:x₂_t0]) + + t = ModelingToolkit.t_nounits + D = ModelingToolkit.Differential(t) + + ModelingToolkit.@variables x₁(..) = x₁_t0 [tunable = false] + ModelingToolkit.@variables x₂(..) = x₂_t0 [tunable = false] + ModelingToolkit.@variables u(..) [input = true] + + eqs = [ + D(x₁(t)) ~ x₂(t), + D(x₂(t)) ~ ε * ω * (1 - x₁(t)^2) * x₂(t) - ω^2 * x₁(t) + u(t), + ] + costs = [ + ModelingToolkit.Symbolics.Integral(t in (t0, tf))( + 0.5 * (x₁(t)^2 + x₂(t)^2 + u(t)^2) + ), + ] + constraints = [] + + ModelingToolkit.@named sys = ModelingToolkit.System( + eqs, t; costs=costs, constraints=constraints + ) + + # initial guess: from the metadata (#377), unless given + init = OptimalControlProblems.Metadata._initial_guess(:vanderpol, params, init) + + return OptimalControlProblems.Problems._problem( + :vanderpol, :ModelingToolkit, sys, init, params + ) +end diff --git a/ext/README.md b/ext/README.md index fbec53992..fcccf96d9 100644 --- a/ext/README.md +++ b/ext/README.md @@ -12,6 +12,7 @@ ext/ ├── Descriptions/.md # description of every problem: one file each ├── Models/.jl # the problem in a modeling, one directory per modeling ├── JuMPModels.jl # extension of the JuMP modeling +├── ModelingToolkitModels.jl # extension of the ModelingToolkit modeling └── OptimalControlModels.jl # extension of the OptimalControl modelings ``` @@ -46,7 +47,9 @@ ext/ - scalarized OptimalControl: the same model with scalar components only; - JuMP: a method that declares the problem as `ProblemData` (continuous functions, no discretization) and calls `build`; the schemes are written once, in - `JuMPModels/schemes/`. + `JuMPModels/schemes/`; + - ModelingToolkit: a method that writes the continuous model with ModelingToolkit.jl + (`@variables`, `Differential`, `Symbolics.Integral`, `System`) and returns `_problem(…)`. 4. **Test it**: `Pkg.test(; test_args=["problem="])` runs every test on this problem only (see `test/README.md`). The consistency tests check the files and the metadata; the smoke tests that it builds and runs; the equivalence tests that two modelings give the same diff --git a/test/README.md b/test/README.md index 7caf48a20..317a0c46d 100644 --- a/test/README.md +++ b/test/README.md @@ -175,10 +175,12 @@ transcription; a failure in the cron is a difference between the JuMP NLP and CT `test_tableaux`, `test_equivalence`, `common/equivalence.jl`, `common/representative.jl` and the cron `comparison`. They test code that this package writes; their files say so. -The modelings of OptimalControl (`OptimalControl`, `OptimalControlScalarized`) share their -tests: `common/optimalcontrol_tests.jl` holds `test_problem`, `test_smoke` and -`test_convergence`, parameterized by the backend, and each test file is a wrapper that gives -its backend. +The modelings that return a `Problem` (`OptimalControl`, `OptimalControlScalarized`, +`ModelingToolkit`) share their tests: `common/modeling_tests.jl` holds `test_problem`, +`test_smoke` and `test_convergence`, parameterized by the backend and by an adapter, the +solves of the modeling (`common/optimalcontrol_tests.jl` for the two OptimalControl modelings, +`common/modelingtoolkit_tests.jl` for ModelingToolkit), and each test file is a wrapper that +gives its backend. ## Configurations @@ -259,6 +261,25 @@ asked to CUDA in a test file (`TST6`): without a functional device the solves ar as skipped (`Broken` in the summary); on a runner named `kkt` or `occidata`, a missing device fails `test_contract`. +## ModelingToolkit tests + +The environment `ModelingToolkit` (`test/suite/ModelingToolkit/`) solves the problems written +with ModelingToolkit.jl by direct single shooting with Corleone.jl and Ipopt +(`CONFIGS.ModelingToolkit` in `common/configs.jl`, `common/solve_modelingtoolkit.jl`): the +controls are piecewise constant on the grid, started from the initial guess of the problem, +and the path constraints are imposed at its nodes. It is opt-in, for its heavy dependencies +(`Pkg.test(; test_args=["ModelingToolkit"])`, or the label `test ModelingToolkit backend`). + +It follows the three kinds of tests. Its model equivalence is `suite/ModelingToolkit/test_modelings`: +the `System` of every problem is read as the data of a JuMP problem +(`common/continuous_modelingtoolkit.jl`) and compared with the OptimalControl model by +`Continuous.compare`, as `suite/comparison/test_modelings` does for JuMP. It runs in this +environment, which therefore loads OptimalControl too, so that the `comparison` environment +stays free of ModelingToolkit. Its convergence report (`cron/ModelingToolkit`, weekly) has no +reference objective for a shooting discretization: the gap is to the objective of the analytic +solution, when the problem has one, and `violation` is the largest violation of the +constraints of the NLP solved. The costates are not computed by Corleone and are left empty. + ## Adding a test 1. Pick the group: `core` for what needs no extension, the modeling environment for one diff --git a/test/common/continuous_modelingtoolkit.jl b/test/common/continuous_modelingtoolkit.jl new file mode 100644 index 000000000..6b2216961 --- /dev/null +++ b/test/common/continuous_modelingtoolkit.jl @@ -0,0 +1,223 @@ +# The ModelingToolkit model of a problem as the continuous data `Continuous.compare` reads +# (#439): the fields of the JuMP `ProblemData` (times, dynamics, Lagrange integrand, cost, path +# and boundary constraints, bounds), evaluated from the `System` at numerical points, without +# compiling it. Used by `suite/ModelingToolkit/test_modelings`, which compares the +# ModelingToolkit modeling of every problem with the OptimalControl one, as +# `suite/comparison/test_modelings` does for JuMP (#441). +# +# What a ModelingToolkit problem writes, and how it is read: +# - the dynamics `D(xᵢ(t)) ~ fᵢ`, one equation per state; +# - the costs (minimized, summed): integrals `Integral(t in (t0, tf))(ℓ)` and terms in the state at +# `t0` or `tf`; the times are those of the parameters of the problem; +# - the initial state, as the default value of a state that is not tunable; +# - the constraints `a ~ b`, `a ≲ b`, `a ≳ b`: a constraint in `t` is a path constraint, or a +# bound when it constrains one state or one control; one in the state at `t0` or `tf` is a +# boundary constraint; +# - the bounds of the metadata (`[bounds = (l, u)]`). +module ContinuousModelingToolkit + +using CTModels: CTModels +using ModelingToolkit: ModelingToolkit as MTK + +const SU = MTK.SymbolicUtils +const SYM = MTK.Symbolics + +unwrap(e) = SYM.unwrap(e) + +"""The value of `e` when it is a number, `nothing` otherwise.""" +function constant(e) + v = SU.unwrap_const(unwrap(e)) + return v isa Real ? Float64(v) : nothing +end + +"""Name of a state or a control, without its argument: `x₁` for `x₁(t)`.""" +name(s) = replace(string(s), r"\(.*\)$" => "") + +"""Whether `e` depends on the independent variable `iv`.""" +function depends_on_time(e, iv) + e = unwrap(e) + isequal(e, iv) && return true + SU.iscall(e) || return false + return any(a -> depends_on_time(a, iv), SU.arguments(e)) +end + +""" +The value of the expression `e` at the point `p`: `p.t`, `p.x`, `p.u` for a signal at the +time `t`, `p.x0`, `p.xf` for the state at `t0` or `tf`. `sig` holds the independent variable, +the states and the controls, and the times. +""" +function evaluate(e, sig, p) + e = unwrap(e) + isequal(e, sig.iv) && return p.t + if SU.iscall(e) + op, args = SU.operation(e), SU.arguments(e) + for (signals, now, at_t0, at_tf) in + ((sig.states, p.x, p.x0, p.xf), (sig.controls, p.u, nothing, nothing)) + i = findfirst(s -> isequal(op, SU.operation(s)), signals) + isnothing(i) && continue + arg = unwrap(only(args)) + isequal(arg, sig.iv) && return now[i] + τ = constant(arg) + !isnothing(at_t0) && τ == sig.t0 && return at_t0[i] + !isnothing(at_tf) && τ == sig.tf && return at_tf[i] + throw(ArgumentError("cannot evaluate $e: a signal at $τ, not at t0 or tf")) + end + return op(map(a -> evaluate(a, sig, p), args)...) + end + v = constant(e) + isnothing(v) && throw(ArgumentError("cannot evaluate $e")) + return v +end + +point(; t=nothing, x=nothing, u=nothing, x0=nothing, xf=nothing) = (; t, x, u, x0, xf) + +"""`h` as `(h′, c)` with `h = h′ + c` and `c` the constant term of a sum.""" +function split_constant(h) + h = unwrap(h) + (SU.iscall(h) && SU.operation(h) === (+)) || return h, 0.0 + args = SU.arguments(h) + values = map(constant, args) + rest = [a for (a, c) in zip(args, values) if isnothing(c)] + c = sum(c for c in values if !isnothing(c); init=0.0) + return (length(rest) == 1 ? only(rest) : +(rest...)), c +end + +""" +A constraint as `(lower, h, upper)`: `lower ≤ h ≤ upper`, the bounds being numbers. A +constraint between an expression and a number keeps the expression, as the other modelings +write it (`x(tf) == x_tf`). +""" +function bounded(c) + if c isa SYM.Equation + l, r = c.lhs, c.rhs + cr, cl = constant(r), constant(l) + !isnothing(cr) && return (cr, unwrap(l), cr) + !isnothing(cl) && return (cl, unwrap(r), cl) + h, k = split_constant(l - r) + return (-k, h, -k) + elseif c isa SYM.Inequality + # l ≤ r + l, r = c.relational_op === SYM.leq ? (c.lhs, c.rhs) : (c.rhs, c.lhs) + cr, cl = constant(r), constant(l) + !isnothing(cr) && return (-Inf, unwrap(l), cr) + !isnothing(cl) && return (cl, unwrap(r), Inf) + h, k = split_constant(l - r) + return (-Inf, h, -k) + end + throw(ArgumentError("unsupported constraint $c")) +end + +"""Initial value of the state `x` when it is fixed (not tunable), `nothing` otherwise.""" +function initial_value(sys, x) + MTK.istunable(x, false) && return nothing + v = nothing + if isdefined(MTK, :initial_conditions) + ics = MTK.initial_conditions(sys) + v = get(ics, unwrap(x), nothing) + end + isnothing(v) && (v = SYM.getdefaultval(x, nothing)) + return isnothing(v) ? nothing : constant(v) +end + +"""`syms` in the order of `names`; an error when a name is missing.""" +function ordered(syms, names) + return map(names) do n + i = findfirst(s -> name(s) == n, syms) + isnothing(i) && throw(ArgumentError("no component $n among $(name.(syms))")) + syms[i] + end +end + +""" +The data of the ModelingToolkit `System` `sys` in the fields of the JuMP `ProblemData`, read by +`Continuous.compare`; the states and the controls in the order of `state_names` and +`control_names` (those of the OptimalControl model), the times `(t0, tf)` being `tspan`: a +`System` does not say them when its cost has no integral. +""" +function problem_data(sys, state_names, control_names, tspan) + CTModels.Models.variable_dimension(sys) == 0 || + throw(ArgumentError("a problem with a variable is not supported yet")) + iv = unwrap(MTK.get_iv(sys)) + unknowns = MTK.unknowns(sys) + xs = ordered(filter(!MTK.isinput, unknowns), state_names) + us = ordered(filter(MTK.isinput, unknowns), control_names) + n, m = length(xs), length(us) + t0, tf = Float64.(tspan) + + # costs: integrals over (t0, tf) and terms in the state at t0 or tf + integrands, terms = Any[], Any[] + for c in MTK.get_costs(sys) + c = unwrap(c) + if SU.iscall(c) && SU.operation(c) isa SYM.Integral + d = SU.operation(c).domain.domain + (constant(d.left), constant(d.right)) == (t0, tf) || + throw(ArgumentError("an integral over another interval than (t0, tf)")) + push!(integrands, only(SU.arguments(c))) + else + push!(terms, c) + end + end + sig = (; iv, states=unwrap.(xs), controls=unwrap.(us), t0, tf) + + # dynamics, in the order of the states + rhs = Vector{Any}(nothing, n) + for eq in MTK.equations(sys) + lhs = unwrap(eq.lhs) + (SU.iscall(lhs) && SU.operation(lhs) isa SYM.Differential) || + throw(ArgumentError("not a dynamics: $eq")) + i = findfirst(s -> isequal(s, unwrap(only(SU.arguments(lhs)))), sig.states) + isnothing(i) && throw(ArgumentError("not the derivative of a state: $eq")) + rhs[i] = eq.rhs + end + any(isnothing, rhs) && throw(ArgumentError("a state has no dynamics")) + + # bounds of the metadata, then the constraints + function metadata_bounds(s) + l, u = MTK.getbounds(s) + return something(constant(l), -Inf), something(constant(u), Inf) + end + lower_x, upper_x = first.(metadata_bounds.(xs)), last.(metadata_bounds.(xs)) + lower_u, upper_u = first.(metadata_bounds.(us)), last.(metadata_bounds.(us)) + path, boundary = Tuple{Any,Function,Any}[], Tuple{Any,Function,Any}[] + for c in MTK.get_constraints(sys) + lo, h, hi = bounded(c) + if depends_on_time(h, iv) + i = findfirst(s -> isequal(s, h), sig.states) + k = findfirst(s -> isequal(s, h), sig.controls) + if !isnothing(i) + lower_x[i], upper_x[i] = max(lower_x[i], lo), min(upper_x[i], hi) + elseif !isnothing(k) + lower_u[k], upper_u[k] = max(lower_u[k], lo), min(upper_u[k], hi) + else + f = (x, u, v, t) -> evaluate(h, sig, point(; t, x, u)) + push!(path, (lo, f, hi)) + end + else + push!(boundary, (lo, (x0, xf, v) -> evaluate(h, sig, point(; x0, xf)), hi)) + end + end + for (i, x) in enumerate(xs) + a = initial_value(sys, x) + isnothing(a) || push!(boundary, (a, (x0, xf, v) -> x0[i], a)) + end + + component(s, l, u) = (; name=name(s), lower=l, upper=u) + lagrange = if isempty(integrands) + nothing + else + (x, u, v, t) -> sum(evaluate(ℓ, sig, point(; t, x, u)) for ℓ in integrands) + end + return (; + state=[component(xs[i], lower_x[i], upper_x[i]) for i in 1:n], + control=[component(us[k], lower_u[k], upper_u[k]) for k in 1:m], + variable=NamedTuple[], + times=v -> (t0, tf), + dynamics=(x, u, v, t) -> [evaluate(f, sig, point(; t, x, u)) for f in rhs], + lagrange=lagrange, + cost=(I, x0, xf, v) -> I + sum(evaluate(g, sig, point(; x0, xf)) for g in terms; init=0.0), + path=path, + boundary=boundary, + ) +end + +end # module ContinuousModelingToolkit diff --git a/test/common/modeling_tests.jl b/test/common/modeling_tests.jl new file mode 100644 index 000000000..c4fe5ad71 --- /dev/null +++ b/test/common/modeling_tests.jl @@ -0,0 +1,148 @@ +# The tests every modeling that returns a `Problem` shares (#441, #439): the API of a problem, +# the smoke solves, the convergence report. Written once and parameterized by the backend and +# by an adapter, a named tuple of what differs between the modelings: +# +# - `solve(pb, config; grid_size[, max_iter])`: a solution of `pb` with `config`; +# - `objective(sol)`: its objective; +# - `settings(config)`: `(modeler, solver, options)`, as `TestStatus.result!` records them; +# - `scheme(config)`: the discretization, for the report; +# - `outcome(sol)`: `(converged, reason, objective, iterations, violation)`; +# - `trajectory(sol, pb)`: the trajectory for the documentation, or `nothing`; +# - `reference(problem, grid_size, scheme)`: the objective the gap of the report is computed +# against; `nothing` for the reference objective of the problem for this discretization. +# +# The configurations come from `Main.TestConfigs.CONFIGS.`; the names of the test +# sets and of the lines of the report are those of the modeling. +module ModelingTests + +using Test: Test +using OptimalControlProblems: OptimalControlProblems + +const OCP = OptimalControlProblems +const VERBOSE = isdefined(Main, :TestOptions) ? Main.TestOptions.VERBOSE : true +const SHOWTIMING = isdefined(Main, :TestOptions) ? Main.TestOptions.SHOWTIMING : true + +"""Name of the modeling of `backend` (`:OptimalControl`, `:ModelingToolkit`, ...).""" +modeling(backend) = Main.TestStatus.modeling(backend) + +"""Configurations to solve with for `backend`, see `Main.TestConfigs.CONFIGS`.""" +configs(backend) = getfield(Main.TestConfigs.CONFIGS, modeling(backend)) + +""" +`params` with its last nonzero numerical value changed by a relative 1e-6, small enough to +keep every bound of the problem ordered; `nothing` if there is no such value. +""" +function changed(params::NamedTuple) + k = findlast(v -> v isa AbstractFloat || (v isa Integer && v != 0), values(params)) + isnothing(k) && return nothing + key = keys(params)[k] + return merge(params, NamedTuple{(key,)}((params[key] * (1 + 1e-6),))) +end + +""" +The API of a problem of the modeling of `backend`: the `Problem`, whose model is a +`model_type`, its metadata, its parameters, its initial guess, its display. `check(problem, pb)` +adds the checks of the modeling. +""" +function test_problem(backend; model_type::Type, check=(problem, pb) -> nothing) + name = modeling(backend) + Test.@testset "$name Problem API" verbose = VERBOSE showtiming = SHOWTIMING begin + Main.TestStatus.for_each_problem("problem", backend) do problem + pb = getfield(OCP, problem)(backend) + Test.@test pb isa OCP.Problem + Test.@test OCP.model(pb) isa model_type + Test.@test OCP.initial_guess(pb) isa NamedTuple + Test.@test issubset(keys(OCP.initial_guess(pb)), (:state, :control, :variable)) + data = OCP.metadata(pb) + Test.@test data[:parameters] == OCP.parameters_data(problem) + Test.@test data[:status] == OCP.status(problem) + Test.@test data[:grid_size] == OCP.grid_size_data(problem) + + # The parameters a problem is built with are recorded in its metadata, and the + # metadata of the collection are left untouched. + params = OCP.parameters_data(problem) + new = isnothing(params) ? nothing : changed(params) + if !isnothing(new) + Test.@test new != params + Test.@test OCP.metadata(getfield(OCP, problem)(backend; parameters=new))[:parameters] == + new + Test.@test OCP.parameters_data(problem) == params + end + + # The initial guess is the one of the metadata (#377), unless another one is given. + Test.@test OCP.initial_guess(pb) == OCP.initial_guess_data(problem) + other = (state=OCP.initial_guess_data(problem).state, control=0) + Test.@test OCP.initial_guess(getfield(OCP, problem)(backend; init=other)) === other + + # Shown with its name, modeling, status and dimensions. + shown = sprint(show, MIME("text/plain"), pb) + Test.@test occursin(string(problem), shown) + Test.@test occursin("$name modeling", shown) + Test.@test occursin(string(OCP.status(problem)[name]), shown) + Test.@test occursin( + "state dimension: $(OCP.state_dimension(OCP.model(pb)))", shown + ) + Test.@test sprint(show, pb) == "Problem(:$(problem))" + + check(problem, pb) + end + end +end + +""" +Every problem builds, evaluates and runs a few iterations in each configuration; the +convergence is reported by the convergence tests (`convergence` argument, cron), #360. +""" +function test_smoke(backend, adapter) + name = modeling(backend) + Test.@testset "$name smoke (small grid, few iterations)" verbose = VERBOSE showtiming = + SHOWTIMING begin + for config in configs(backend) + Test.@testset "$(Main.TestConfigs.label(config))" begin + test = "test_smoke $(Main.TestConfigs.label(config))" + Main.TestStatus.for_each_problem(test, backend) do problem + pb = getfield(OCP, problem)(backend) + o = Main.TestConfigs + sol = adapter.solve( + pb, config; grid_size=o.SMOKE_GRID, max_iter=o.SMOKE_MAX_ITER + ) + Test.@test isfinite(adapter.objective(sol)) + end + end + end + end +end + +""" +Every problem solved on its default grid in each configuration, and reported, not tested +(#360); an exception still fails. The second solve is timed, once compiled. +""" +function test_convergence(backend, adapter) + name = modeling(backend) + Test.@testset "$name convergence report (default grid)" verbose = VERBOSE showtiming = + SHOWTIMING begin + for config in configs(backend) + Test.@testset "$(Main.TestConfigs.label(config))" begin + test = "test_convergence $(Main.TestConfigs.label(config))" + Main.TestStatus.for_each_problem(test, backend) do problem + pb = getfield(OCP, problem)(backend) + N = OCP.grid_size_data(problem) + scheme = adapter.scheme(config) + sol = adapter.solve(pb, config; grid_size=N) + time = @elapsed sol = adapter.solve(pb, config; grid_size=N) # compiled + # convergence is reported, not tested (#360); an exception still fails + Main.TestStatus.result!( + test, problem, backend, Main.TestConfigs.label(config); + adapter.settings(config)..., + grid_size=N, scheme=scheme, adapter.outcome(sol)..., + time=time, trajectory=adapter.trajectory(sol, pb), + reference=adapter.reference(problem, N, scheme), + ) + Test.@test true # the problem was solved and reported: a line of the report + end + end + end + end +end + +end # module ModelingTests diff --git a/test/common/modelingtoolkit_tests.jl b/test/common/modelingtoolkit_tests.jl new file mode 100644 index 000000000..ee0710167 --- /dev/null +++ b/test/common/modelingtoolkit_tests.jl @@ -0,0 +1,59 @@ +# The tests of the ModelingToolkit modeling: the tests every modeling that returns a `Problem` +# shares (`common/modeling_tests.jl`, #441), given the ModelingToolkit solves +# (`Main.SolveModelingToolkit`, Corleone and Ipopt), and the checks of a `System` (#439). +isdefined(Main, :SolveModelingToolkit) || + include(joinpath(@__DIR__, "solve_modelingtoolkit.jl")) +isdefined(Main, :ModelingTests) || include(joinpath(@__DIR__, "modeling_tests.jl")) + +module ModelingToolkitTests + +using Test: Test +using ModelingToolkit: ModelingToolkit as MTK # loads the ModelingToolkitModels extension +using OptimalControlProblems: OptimalControlProblems + +const OCP = OptimalControlProblems + +""" +The objective of the analytic solution of `problem`, the reference of the gap in the report: +a shooting solve has no reference objective of its own discretization; `nothing` without one. +""" +function analytic_objective(problem, grid_size, scheme) + ref = OCP.analytic_reference(problem) + return isnothing(ref) ? nothing : ref.objective +end + +"""What the shared tests need of the ModelingToolkit solves (see `common/modeling_tests.jl`).""" +function adapter() + S = Main.SolveModelingToolkit + return ( + solve=(pb, config; kwargs...) -> S.solve(pb, config; kwargs...), + objective=S.objective, + settings=S.settings, + scheme=S.scheme, + outcome=S.outcome, + trajectory=S.trajectory, + reference=analytic_objective, + ) +end + +"""The checks of a `System`: its dimensions, one dynamics per state, a cost.""" +function check(problem, pb) + sys = OCP.model(pb) + Test.@test OCP.state_dimension(sys) == length(OCP.state_components(sys)) > 0 + Test.@test OCP.control_dimension(sys) == length(OCP.control_components(sys)) > 0 + Test.@test OCP.variable_dimension(sys) == length(OCP.variable_components(sys)) + Test.@test length(MTK.equations(sys)) == OCP.state_dimension(sys) + Test.@test !isempty(MTK.get_costs(sys)) +end + +"""The API of a problem of the modeling: the `Problem`, its metadata, its display.""" +test_problem(backend) = + Main.ModelingTests.test_problem(backend; model_type=MTK.AbstractSystem, check=check) + +"""Every problem builds and runs a few iterations; the convergence is in the cron (#360).""" +test_smoke(backend) = Main.ModelingTests.test_smoke(backend, adapter()) + +"""Every problem solved on its default grid, and reported, not tested (#360).""" +test_convergence(backend) = Main.ModelingTests.test_convergence(backend, adapter()) + +end # module ModelingToolkitTests diff --git a/test/common/optimalcontrol_tests.jl b/test/common/optimalcontrol_tests.jl index 93b0e4d8b..c33a5168c 100644 --- a/test/common/optimalcontrol_tests.jl +++ b/test/common/optimalcontrol_tests.jl @@ -1,135 +1,44 @@ # The tests of the OptimalControl modelings (OptimalControl and OptimalControlScalarized), written # once and parameterized by the backend (#441): the API of a problem, the smoke solves, the # convergence report. A test file of a modeling is a wrapper that gives its backend (TST1). -# The configurations come from `Main.TestConfigs.CONFIGS.`, the solves from -# `Main.SolveOptimalControl`; the names of the test sets and of the lines of the report are -# those of the modeling, as before the tests were shared. +# The tests themselves are shared with every modeling that returns a `Problem` +# (`common/modeling_tests.jl`, #439); this module gives them the OptimalControl solves +# (`Main.SolveOptimalControl`). isdefined(Main, :SolveOptimalControl) || include(joinpath(@__DIR__, "solve_optimalcontrol.jl")) +isdefined(Main, :ModelingTests) || include(joinpath(@__DIR__, "modeling_tests.jl")) module OptimalControlTests -using Test: Test using OptimalControl: OptimalControl # loads the OptimalControlModels extension -using OptimalControlProblems: OptimalControlProblems -const OCP = OptimalControlProblems -const VERBOSE = isdefined(Main, :TestOptions) ? Main.TestOptions.VERBOSE : true -const SHOWTIMING = isdefined(Main, :TestOptions) ? Main.TestOptions.SHOWTIMING : true - -"""Name of the modeling of `backend` (`:OptimalControl`, `:OptimalControlScalarized`).""" -modeling(backend) = Main.TestStatus.modeling(backend) - -"""Configurations to solve with for `backend`, see `Main.TestConfigs.CONFIGS`.""" -configs(backend) = getfield(Main.TestConfigs.CONFIGS, modeling(backend)) - -""" -`params` with its last nonzero numerical value changed by a relative 1e-6, small enough to -keep every bound of the problem ordered; `nothing` if there is no such value. -""" -function changed(params::NamedTuple) - k = findlast(v -> v isa AbstractFloat || (v isa Integer && v != 0), values(params)) - isnothing(k) && return nothing - key = keys(params)[k] - return merge(params, NamedTuple{(key,)}((params[key] * (1 + 1e-6),))) +"""What the shared tests need of the OptimalControl solves (see `common/modeling_tests.jl`).""" +function adapter() + S = Main.SolveOptimalControl + return ( + solve=(pb, config; kwargs...) -> S.solve(pb, config; kwargs...), + objective=OptimalControl.objective, + settings=S.settings, + scheme=config -> Main.TestConfigs.SCHEME, + outcome=S.outcome, + trajectory=(sol, pb) -> Main.TestStatus.trajectory(sol), + reference=(problem, grid_size, scheme) -> nothing, + ) end """The API of a problem of the modeling of `backend`: the `Problem`, its metadata, its display.""" -function test_problem(backend) - name = modeling(backend) - Test.@testset "$name Problem API" verbose = VERBOSE showtiming = SHOWTIMING begin - Main.TestStatus.for_each_problem("problem", backend) do problem - pb = getfield(OCP, problem)(backend) - Test.@test pb isa OCP.Problem - Test.@test OCP.model(pb) isa OptimalControl.Model - Test.@test OCP.initial_guess(pb) isa NamedTuple - Test.@test issubset(keys(OCP.initial_guess(pb)), (:state, :control, :variable)) - data = OCP.metadata(pb) - Test.@test data[:parameters] == OCP.parameters_data(problem) - Test.@test data[:status] == OCP.status(problem) - Test.@test data[:grid_size] == OCP.grid_size_data(problem) - - # The parameters a problem is built with are recorded in its metadata, and the - # metadata of the collection are left untouched. - params = OCP.parameters_data(problem) - new = isnothing(params) ? nothing : changed(params) - if !isnothing(new) - Test.@test new != params - Test.@test OCP.metadata(getfield(OCP, problem)(backend; parameters=new))[:parameters] == - new - Test.@test OCP.parameters_data(problem) == params - end - - # The initial guess is the one of the metadata (#377), unless another one is given. - Test.@test OCP.initial_guess(pb) == OCP.initial_guess_data(problem) - other = (state=OCP.initial_guess_data(problem).state, control=0) - Test.@test OCP.initial_guess(getfield(OCP, problem)(backend; init=other)) === other - - # Shown with its name, modeling, status and dimensions. - shown = sprint(show, MIME("text/plain"), pb) - Test.@test occursin(string(problem), shown) - Test.@test occursin("$name modeling", shown) - Test.@test occursin(string(OCP.status(problem)[name]), shown) - Test.@test occursin( - "state dimension: $(OCP.state_dimension(OCP.model(pb)))", shown - ) - Test.@test sprint(show, pb) == "Problem(:$(problem))" - end - end -end +test_problem(backend) = Main.ModelingTests.test_problem(backend; model_type=OptimalControl.Model) """ Every problem builds, evaluates and runs a few iterations in each configuration; the convergence is reported by the convergence tests (`convergence` argument, cron), #360. """ -function test_smoke(backend) - S = Main.SolveOptimalControl - name = modeling(backend) - Test.@testset "$name smoke (small grid, few iterations)" verbose = VERBOSE showtiming = - SHOWTIMING begin - for config in configs(backend) - Test.@testset "$(Main.TestConfigs.label(config))" begin - test = "test_smoke $(Main.TestConfigs.label(config))" - Main.TestStatus.for_each_problem(test, backend) do problem - pb = getfield(OCP, problem)(backend) - o = Main.TestConfigs - sol = S.solve(pb, config; grid_size=o.SMOKE_GRID, max_iter=o.SMOKE_MAX_ITER) - Test.@test isfinite(OptimalControl.objective(sol)) - end - end - end - end -end +test_smoke(backend) = Main.ModelingTests.test_smoke(backend, adapter()) """ Every problem solved on its default grid in each configuration, and reported, not tested (#360); an exception still fails. """ -function test_convergence(backend) - S = Main.SolveOptimalControl - name = modeling(backend) - Test.@testset "$name convergence report (default grid)" verbose = VERBOSE showtiming = - SHOWTIMING begin - for config in configs(backend) - Test.@testset "$(Main.TestConfigs.label(config))" begin - test = "test_convergence $(Main.TestConfigs.label(config))" - Main.TestStatus.for_each_problem(test, backend) do problem - pb = getfield(OCP, problem)(backend) - N = OCP.grid_size_data(problem) - sol = S.solve(pb, config; grid_size=N) - time = @elapsed sol = S.solve(pb, config; grid_size=N) # second, compiled - # convergence is reported, not tested (#360); an exception still fails - Main.TestStatus.result!( - test, problem, backend, Main.TestConfigs.label(config); - S.settings(config)..., - grid_size=N, scheme=Main.TestConfigs.SCHEME, S.outcome(sol)..., - time=time, trajectory=Main.TestStatus.trajectory(sol), - ) - Test.@test true - end - end - end - end -end +test_convergence(backend) = Main.ModelingTests.test_convergence(backend, adapter()) end # module OptimalControlTests diff --git a/test/common/solve_modelingtoolkit.jl b/test/common/solve_modelingtoolkit.jl index 57d3d9105..04fb929e5 100644 --- a/test/common/solve_modelingtoolkit.jl +++ b/test/common/solve_modelingtoolkit.jl @@ -12,19 +12,12 @@ using OrdinaryDiffEqTsit5: Tsit5 using OptimalControlProblems: OptimalControlProblems as OCP """ -Extract the time horizon `(t0, tf)` from the integral cost domain in `sys`. -""" -function _extract_tspan(sys::MTK.AbstractSystem) - costs = MTK.get_costs(sys) - isempty(costs) && throw(ArgumentError("system has no cost terms")) - cost = costs[1] - op = MTK.SymbolicUtils.operation(cost) - if hasproperty(op, :domain) && hasproperty(op.domain, :domain) - d = op.domain.domain - return (Float64(d.left), Float64(d.right)) - else - throw(ArgumentError("cannot extract time horizon from cost $cost")) - end +The time horizon `(t0, tf)` of the problem `pb`, from its parameters: a `System` does not say +it when the cost has no integral (a Mayer cost). +""" +function tspan(pb) + params = OCP.metadata(pb)[:parameters] + return (Float64(params.t0), Float64(params.tf)) end """ @@ -116,7 +109,25 @@ function solver_options( end """ -Prepare an `Optimization.OptimizationProblem` from a ModelingToolkit problem `pb` using Corleone. +The start value of each of the `m` controls, from the initial guess `init` of the problem +(#377): a number, a vector, or a function of the time taken at `t0`; `0.0` without a control +guess. Corleone starts every interval of a control from one value. +""" +function control_start(init, t0, m::Int) + g = haskey(init, :control) ? init.control : 0.0 + g isa Function && (g = g(t0)) + v = g isa Number ? fill(Float64(g), m) : Float64.(vec(collect(g))) + length(v) == m || + throw(DimensionMismatch("control guess of length $(length(v)), $m controls")) + return v +end + +""" +Prepare the solve of the ModelingToolkit problem `pb` with Corleone, built once: the path +constraints imposed at the `grid_size` nodes of the grid, the controls piecewise constant +between them and started from the initial guess of the problem. Returns the +`Optimization.OptimizationProblem` (`problem`) and the `Corleone.CorleoneDynamicOptProblem` +it was built from (`dynamic`), from which the trajectory of a solution is recovered. """ function prepare( pb; @@ -126,32 +137,32 @@ function prepare( ) shooting === :single || throw(ArgumentError("unsupported shooting method $shooting")) sys = OCP.model(pb) - iv = MTK.independent_variable(sys) - t0, tf = _extract_tspan(sys) + t0, tf = tspan(pb) grid = range(t0, tf, length=grid_size) # Discretize path constraints over the grid sys_disc = _discretize_path_constraints(sys, grid) - # Controls and initial values + # Controls: started from the initial guess, switching at the nodes of the grid ctrl_syms = filter(MTK.isinput, MTK.unknowns(sys)) - inits = [u => 0.0 for u in ctrl_syms] - # Control switches: interior switches preceded by tspan[1] - switches = [u => collect(grid[1:end-1]) for u in ctrl_syms] + start = control_start(OCP.initial_guess(pb), t0, length(ctrl_syms)) + inits = [u => start[i] for (i, u) in enumerate(ctrl_syms)] + switches = [u => collect(grid[1:(end - 1)]) for u in ctrl_syms] - opt_prob = Corleone.CorleoneDynamicOptProblem( + dynamic = Corleone.CorleoneDynamicOptProblem( sys_disc, inits, switches...; algorithm=Tsit5(), tspan=(t0, tf), ) - - return Optimization.OptimizationProblem(opt_prob, Optimization.AutoForwardDiff()) + problem = Optimization.OptimizationProblem(dynamic, Optimization.AutoForwardDiff()) + return (; problem, dynamic) end """ -Solve the `Problem` `pb` with `config`. +Solve the `Problem` `pb` with `config`: a named tuple with the Optimization solution +(`result`) and the problems it solves (`problem`, `dynamic`, see `prepare`). """ function solve( pb, @@ -160,48 +171,163 @@ function solve( max_iter::Int=Main.TestConfigs.MAX_ITER, options::NamedTuple=(;), ) - prob = prepare( + prepared = prepare( pb; grid_size=grid_size, shooting=get(config, :shooting, :single), solver=get(config, :solver, :ipopt), ) opts = solver_options(get(config, :solver, :ipopt); max_iter=max_iter, options=options) - res = Optimization.solve(prob, Ipopt.Optimizer(); opts...) - return res + result = Optimization.solve(prepared.problem, Ipopt.Optimizer(); opts...) + return (; result, prepared...) +end + +""" +The values of the control `name` (`"u"`) the problem `dynamic` of a `prepare` starts from: +one per interval. +""" +function control_values(dynamic, name) + return dynamic.ps.parameter.parameters[Symbol(name, "(t)")].parameter.ps end +"""The objective of a solution `sol` of `solve`.""" +objective(sol) = sol.result.objective + """ -How a solve with `config` and `options` was configured. +How a solve with `config` and `options` was configured, as `TestStatus.result!` records it. +The shooting is in the scheme, see `scheme`. """ function settings(config; max_iter::Int=Main.TestConfigs.MAX_ITER, options::NamedTuple=(;)) return ( modeler=:corleone, - shooting=get(config, :shooting, :single), solver=config.solver, options=solver_options(config.solver; max_iter=max_iter, options=options), ) end +"""The discretization of a solve with `config`, for the report: `:shooting_single`.""" +scheme(config) = Symbol(:shooting_, get(config, :shooting, :single)) + """ Whether the optimization succeeded. """ -function succeeded(res) +function succeeded(sol) + res = sol.result return Optimization.SciMLBase.successful_retcode(res) || res.retcode == Optimization.ReturnCode.Success end +""" +The largest violation of the constraints of the NLP solved (the path constraints at the nodes +of the grid, the boundary constraints) and of the bounds of its unknowns, at the solution. +""" +function violation(sol) + prob, u = sol.problem, sol.result.u + v = 0.0 + if !isnothing(prob.lb) + v = max(v, maximum(max.(prob.lb .- u, u .- prob.ub, 0.0); init=0.0)) + end + if !isnothing(prob.f.cons) && !isnothing(prob.lcons) + c = zeros(length(prob.lcons)) + prob.f.cons(c, u, prob.p) + v = max(v, maximum(max.(prob.lcons .- c, c .- prob.ucons, 0.0); init=0.0)) + end + return v +end + """ What `TestStatus.result!` records of a solution. """ -function outcome(res) - iters = hasproperty(res, :stats) && hasproperty(res.stats, :iterations) ? res.stats.iterations : 0 +function outcome(sol) + res = sol.result + iters = if hasproperty(res, :stats) && hasproperty(res.stats, :iterations) + res.stats.iterations + else + 0 + end return ( - converged=succeeded(res), + converged=succeeded(sol), reason=res.retcode, objective=res.objective, iterations=iters, - violation=0.0, + violation=violation(sol), + ) +end + +""" +The `Corleone.Trajectory` of a solution `sol` of `solve`, from the problem it solved: the +states at the nodes of the grid and the controls. +""" +function dynamic_trajectory(sol) + dynamic = sol.dynamic + _, reconstruct = Corleone.pack(Val(:ComponentArrays), dynamic.ps) + current = reconstruct(sol.result.u) + traj, _ = dynamic.layer( + (dynamic.layer.problem, dynamic.evaluation_grid), current, dynamic.st + ) + return traj +end + +""" +The trajectory of a solution `sol` of `solve` for the plots of the documentation (#314), in +the schema of `TestStatus.trajectory`. The costates are empty: Corleone, a direct shooting +method, computes none. +""" +function trajectory(sol, pb) + (hasproperty(sol.result, :u) && !isnothing(sol.result.u)) || return nothing + try + return _trajectory(sol, pb) + catch err + @warn "No trajectory for this ModelingToolkit solve" exception = err + return nothing + end +end + +function _trajectory(sol, pb) + traj = dynamic_trajectory(sol) + sys = OCP.model(pb) + t = collect(traj.t) + n_points = length(t) + + # At most TestStatus.TRAJECTORY_POINTS points, as for the other modelings + max_points = Main.TestStatus.TRAJECTORY_POINTS + keep = if n_points <= max_points + eachindex(t) + else + unique(round.(Int, range(1, n_points; length=max_points))) + end + r(v) = round(Float64(v); sigdigits=6) + + xs = CTModels.Models.state_components(sys) + us = CTModels.Models.control_components(sys) + vs = CTModels.Models.variable_components(sys) + + state = Dict{String,Vector{Float64}}() + for name in xs + vals = traj[Symbol(name)] + length(vals) == n_points || + throw(DimensionMismatch("$(length(vals)) values of $name, $n_points times")) + state[name] = [r(vals[i]) for i in keep] + end + + # A control is constant on each interval: one value per interval (n_points - 1), the last + # one extended to the final time, or one value per time. + control = Dict{String,Vector{Float64}}() + for name in us + vals = traj[Symbol(name)] + length(vals) in (n_points - 1, n_points) || + throw(DimensionMismatch("$(length(vals)) values of $name, $n_points times")) + full = [vals[min(i, length(vals))] for i in 1:n_points] + control[name] = [r(full[i]) for i in keep] + end + + return Dict{String,Any}( + "time" => r.(t[keep]), + "state" => state, + "control" => control, + "costate" => Dict{String,Vector{Float64}}(), + "variable" => Float64[], + "names" => Dict("state" => xs, "control" => us, "variable" => vs), ) end diff --git a/test/common/status.jl b/test/common/status.jl index 9be4f7581..e0265e3c9 100644 --- a/test/common/status.jl +++ b/test/common/status.jl @@ -139,12 +139,14 @@ gap(objective, reference) = """ result!(test, problem, backend, configuration; modeler, solver, options, grid_size, scheme, converged, reason, objective, iterations, violation, time, - trajectory=nothing) + trajectory=nothing, reference=nothing) Record the result of a solve of `problem` in the modeling of `backend`, discretized on `grid_size` steps with `scheme`. `modeler`, `solver` and `options` are the `settings` of the solve modules, the solver status the `outcome` of a solution, `trajectory` the -`trajectory` of the solution. +`trajectory` of the solution. The gap is to `reference`, by default the reference objective +of the problem for this discretization; a modeling without one (ModelingToolkit, solved by +shooting) gives the objective of the analytic solution, when there is one (#439). """ function result!( test, @@ -163,8 +165,11 @@ function result!( violation, time, trajectory=nothing, + reference=nothing, ) - reference = OptimalControlProblems.reference_objective(problem; grid_size, scheme) + if isnothing(reference) + reference = OptimalControlProblems.reference_objective(problem; grid_size, scheme) + end push!( RESULTS, Result( diff --git a/test/cron/ModelingToolkit/test_convergence.jl b/test/cron/ModelingToolkit/test_convergence.jl new file mode 100644 index 000000000..e7a1601f6 --- /dev/null +++ b/test/cron/ModelingToolkit/test_convergence.jl @@ -0,0 +1,16 @@ +isdefined(Main, :ModelingToolkitTests) || + include(joinpath(@__DIR__, "..", "..", "common", "modelingtoolkit_tests.jl")) + +module TestConvergenceModelingToolkit + +using OptimalControlProblems: OptimalControlProblems + +# The convergence report on the default grid; written once in `common/modeling_tests.jl` +# (#441, #439). The gap is to the objective of the analytic solution, when there is one. +test_convergence() = Main.ModelingToolkitTests.test_convergence( + OptimalControlProblems.ModelingToolkitBackend() +) + +end # module TestConvergenceModelingToolkit + +test_convergence() = TestConvergenceModelingToolkit.test_convergence() diff --git a/test/suite/ModelingToolkit/Project.toml b/test/suite/ModelingToolkit/Project.toml index 0655f64e7..3736ecf59 100644 --- a/test/suite/ModelingToolkit/Project.toml +++ b/test/suite/ModelingToolkit/Project.toml @@ -6,6 +6,8 @@ Corleone = "2751e0db-33e9-4374-b36d-b7219e1e6b40" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" +OptimalControl = "5f98b655-cc9a-415a-b60e-744165666948" +OptimalControlProblems = "59046045-fb9c-4c23-964f-ff0a25704f96" Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba" OptimizationMOI = "fd9f6733-72f4-499f-8506-86b2bdd0dea1" OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a" @@ -19,6 +21,7 @@ Corleone = "2" ForwardDiff = "1" Ipopt = "1" ModelingToolkit = "11" +OptimalControl = "2.2" Optimization = "5" OptimizationMOI = "1" OrdinaryDiffEqTsit5 = "2" diff --git a/test/suite/ModelingToolkit/test_methods.jl b/test/suite/ModelingToolkit/test_methods.jl index bc8b784e5..1f9b04b61 100644 --- a/test/suite/ModelingToolkit/test_methods.jl +++ b/test/suite/ModelingToolkit/test_methods.jl @@ -16,9 +16,11 @@ function test_methods() pb = OCP.bryson_denham(backend) sys = OCP.model(pb) - # 1. _extract_tspan - tspan = Main.SolveModelingToolkit._extract_tspan(sys) + # 1. tspan: from the parameters of the problem, the same as the domain of the integral + tspan = Main.SolveModelingToolkit.tspan(pb) Test.@test tspan == (0.0, 1.0) + domain = MTK.SymbolicUtils.operation(MTK.get_costs(sys)[1]).domain.domain + Test.@test (Float64(domain.left), Float64(domain.right)) == tspan # 2. _is_path_constraint iv = MTK.independent_variable(sys) @@ -40,25 +42,63 @@ function test_methods() Test.@test opts.tol == Main.TestConfigs.TOL Test.@test_throws ArgumentError Main.SolveModelingToolkit.solver_options(:madnlp) - # 5. settings + # 5. settings and scheme cfg = (shooting=:single, solver=:ipopt) stg = Main.SolveModelingToolkit.settings(cfg; max_iter=5) + Test.@test stg.modeler === :corleone Test.@test stg.solver === :ipopt - Test.@test stg.shooting === :single Test.@test stg.options.max_iter == 5 + Test.@test Main.SolveModelingToolkit.scheme(cfg) === :shooting_single - # 6. prepare - opt_prob = Main.SolveModelingToolkit.prepare(pb; grid_size=10) - Test.@test opt_prob isa Main.SolveModelingToolkit.Optimization.OptimizationProblem - - # 7. solve (smoke: 2 iterations) - res = Main.SolveModelingToolkit.solve(pb, cfg; grid_size=10, max_iter=2) - Test.@test isfinite(res.objective) - Test.@test hasproperty(res, :retcode) - out = Main.SolveModelingToolkit.outcome(res) - Test.@test haskey(out, :converged) - Test.@test haskey(out, :objective) - Test.@test isfinite(out.objective) + # 6. control_start: the initial guess of the problem (#377) + S = Main.SolveModelingToolkit + Test.@test S.control_start((control=0.5,), 0.0, 2) == [0.5, 0.5] + Test.@test S.control_start((control=[1, 2],), 0.0, 2) == [1.0, 2.0] + Test.@test S.control_start((control=t -> 3t,), 1.0, 1) == [3.0] + Test.@test S.control_start((state=[0.0, 0.0],), 0.0, 1) == [0.0] + Test.@test_throws DimensionMismatch S.control_start((control=[1, 2],), 0.0, 1) + # and it is the value the controls of the problem solved start from + other = OCP.bryson_denham(backend; init=(state=[0.0, 0.0], control=0.7)) + started = S.prepare(other; grid_size=6).dynamic + Test.@test S.control_values(started, "u") == fill(0.7, 5) + Test.@test S.control_values(S.prepare(pb; grid_size=6).dynamic, "u") == zeros(5) + + # 7. prepare: built once, the Optimization problem and the Corleone one + prepared = S.prepare(pb; grid_size=10) + Test.@test prepared.problem isa S.Optimization.OptimizationProblem + Test.@test prepared.dynamic isa S.Corleone.CorleoneDynamicOptProblem + + # 8. solve (smoke: 2 iterations) and outcome + sol = S.solve(pb, cfg; grid_size=10, max_iter=2) + Test.@test isfinite(S.objective(sol)) + out = S.outcome(sol) + Test.@test keys(out) == (:converged, :reason, :objective, :iterations, :violation) + Test.@test out.objective == S.objective(sol) + Test.@test isfinite(out.violation) && out.violation >= 0 + + # 9. trajectory, from the problem the solve built: the states start at their + # initial conditions whatever the iterate, and the controls are the solved ones + traj = S.dynamic_trajectory(sol) + Test.@test traj isa S.Corleone.Trajectory + Test.@test length(traj.t) == 10 + Test.@test first(traj.t) == tspan[1] && last(traj.t) == tspan[2] + + tr = S.trajectory(sol, pb) + Test.@test tr isa Dict{String,Any} + Test.@test issetequal( + keys(tr), ("time", "state", "control", "costate", "variable", "names") + ) + xs = OCP.state_components(sys) + us = OCP.control_components(sys) + Test.@test tr["names"]["state"] == xs && tr["names"]["control"] == us + Test.@test length(tr["time"]) == 10 + Test.@test isempty(tr["costate"]) && isempty(tr["variable"]) + Test.@test all(length(tr["state"][x]) == 10 for x in xs) + Test.@test all(length(tr["control"][u]) == 10 for u in us) + params = OCP.metadata(pb)[:parameters] + Test.@test tr["state"]["x₁"][1] ≈ params.x1_t0 + Test.@test tr["state"]["x₂"][1] ≈ params.x2_t0 + Test.@test all(isfinite, tr["control"]["u"]) end end diff --git a/test/suite/ModelingToolkit/test_modelings.jl b/test/suite/ModelingToolkit/test_modelings.jl new file mode 100644 index 000000000..ef24832b2 --- /dev/null +++ b/test/suite/ModelingToolkit/test_modelings.jl @@ -0,0 +1,56 @@ +isdefined(Main, :Continuous) || + include(joinpath(@__DIR__, "..", "..", "common", "continuous.jl")) +isdefined(Main, :SolveModelingToolkit) || + include(joinpath(@__DIR__, "..", "..", "common", "solve_modelingtoolkit.jl")) +isdefined(Main, :ContinuousModelingToolkit) || + include(joinpath(@__DIR__, "..", "..", "common", "continuous_modelingtoolkit.jl")) + +module TestModelingsModelingToolkit + +using Test: Test +using ModelingToolkit: ModelingToolkit # loads the ModelingToolkitModels extension +using OptimalControl: OptimalControl # loads the OptimalControlModels extension +using OptimalControlProblems: OptimalControlProblems + +const OCP = OptimalControlProblems +const VERBOSE = isdefined(Main, :TestOptions) ? Main.TestOptions.VERBOSE : true +const SHOWTIMING = isdefined(Main, :TestOptions) ? Main.TestOptions.SHOWTIMING : true + +# Do the ModelingToolkit and the OptimalControl modelings of a problem describe the same +# problem? As `suite/comparison/test_modelings` for JuMP (#441), at the continuous level and +# without a solve: the `System` is read as the data of a JuMP problem +# (`common/continuous_modelingtoolkit.jl`) and compared by `Continuous.compare` on the times, +# the dynamics, the objective, the path and boundary constraints and the bounds, at points +# taken on the initial guess. Here and not in `comparison`: ModelingToolkit is opt-in (#439). +function test_modelings() + mtk = OCP.ModelingToolkitBackend() + Test.@testset "modelings: the same continuous problem" verbose = VERBOSE showtiming = + SHOWTIMING begin + for other in (OCP.OptimalControlBackend(), OCP.OptimalControlScalarizedBackend()) + Test.@testset "ModelingToolkit vs $(Main.TestStatus.modeling(other))" begin + Main.TestStatus.for_each_problem("test_modelings", mtk, other) do problem + build = getfield(OCP, problem) + pb_mtk, pb = build(mtk), build(other) + sys, ocp = OCP.model(pb_mtk), OCP.model(pb) + xs, us = OCP.state_components(ocp), OCP.control_components(ocp) + Test.@testset "components" begin + Test.@test sort(OCP.state_components(sys)) == sort(xs) + Test.@test sort(OCP.control_components(sys)) == sort(us) + end + tspan = Main.SolveModelingToolkit.tspan(pb_mtk) + data = Main.ContinuousModelingToolkit.problem_data(sys, xs, us, tspan) + checks = Main.Continuous.compare(data, ocp, OCP.initial_guess(pb)) + for (name, ok) in sort!(collect(checks); by=first) + Test.@testset "$name" begin + Test.@test ok + end + end + end + end + end + end +end + +end # module TestModelingsModelingToolkit + +test_modelings() = TestModelingsModelingToolkit.test_modelings() diff --git a/test/suite/ModelingToolkit/test_problem.jl b/test/suite/ModelingToolkit/test_problem.jl index 41377d1de..80cf12757 100644 --- a/test/suite/ModelingToolkit/test_problem.jl +++ b/test/suite/ModelingToolkit/test_problem.jl @@ -1,96 +1,12 @@ +isdefined(Main, :ModelingToolkitTests) || + include(joinpath(@__DIR__, "..", "..", "common", "modelingtoolkit_tests.jl")) + module TestProblemModelingToolkit -using Test: Test -using ModelingToolkit: ModelingToolkit as MTK using OptimalControlProblems: OptimalControlProblems -using CTModels: CTModels -using CTBase: CTBase - -const OCP = OptimalControlProblems -const VERBOSE = isdefined(Main, :TestOptions) ? Main.TestOptions.VERBOSE : true -const SHOWTIMING = isdefined(Main, :TestOptions) ? Main.TestOptions.SHOWTIMING : true - -""" -`params` with its last nonzero numerical value changed by a relative 1e-6, small enough to -keep every bound of the problem ordered; `nothing` if there is no such value. -""" -function changed(params::NamedTuple) - k = findlast(v -> v isa AbstractFloat || (v isa Integer && v != 0), values(params)) - isnothing(k) && return nothing - key = keys(params)[k] - return merge(params, NamedTuple{(key,)}((params[key] * (1 + 1e-6),))) -end - -function test_problem() - backend = OCP.ModelingToolkitBackend() - Test.@testset "ModelingToolkit Problem API" verbose = VERBOSE showtiming = SHOWTIMING begin - Main.TestStatus.for_each_problem("problem", backend) do problem - pb = getfield(OCP, problem)(backend) - Test.@test pb isa OCP.Problem - Test.@test OCP.model(pb) isa MTK.AbstractSystem - Test.@test OCP.initial_guess(pb) isa NamedTuple - Test.@test issubset(keys(OCP.initial_guess(pb)), (:state, :control, :variable)) - data = OCP.metadata(pb) - Test.@test data[:parameters] == OCP.parameters_data(problem) - Test.@test data[:status] == OCP.status(problem) - Test.@test data[:grid_size] == OCP.grid_size_data(problem) - - # Check CTModels dimension and component methods - sys = OCP.model(pb) - Test.@test CTModels.Models.state_dimension(sys) == length(CTModels.Models.state_components(sys)) - Test.@test CTModels.Models.control_dimension(sys) == length(CTModels.Models.control_components(sys)) - Test.@test CTModels.Models.variable_dimension(sys) == length(CTModels.Models.variable_components(sys)) - Test.@test CTModels.Models.state_dimension(sys) > 0 - Test.@test CTModels.Models.control_dimension(sys) > 0 - - # Verify MTK model equations and structure - eqs = MTK.equations(sys) - Test.@test length(eqs) == CTModels.Models.state_dimension(sys) - iv = MTK.independent_variable(sys) - Test.@test iv !== nothing - - # Verify costs - costs = MTK.get_costs(sys) - Test.@test !isempty(costs) - cost = costs[1] - op = MTK.SymbolicUtils.operation(cost) - Test.@test hasproperty(op, :domain) - d = op.domain.domain - params = OCP.parameters_data(problem) - Test.@test Float64(d.left) == Float64(params.t0) - Test.@test Float64(d.right) == Float64(params.tf) - - # Verify constraints - cons = MTK.get_constraints(sys) - Test.@test !isempty(cons) - - # The parameters a problem is built with are recorded in its metadata, and the - # metadata of the collection are left untouched. - new = isnothing(params) ? nothing : changed(params) - if !isnothing(new) - Test.@test new != params - Test.@test OCP.metadata(getfield(OCP, problem)(backend; parameters=new))[:parameters] == - new - Test.@test OCP.parameters_data(problem) == params - end - - # The initial guess is the one of the metadata (#377), unless another one is given. - Test.@test OCP.initial_guess(pb) == OCP.initial_guess_data(problem) - other = (state=OCP.initial_guess_data(problem).state, control=0) - Test.@test OCP.initial_guess(getfield(OCP, problem)(backend; init=other)) === other - # Shown with its name, modeling, status and dimensions. - shown = sprint(show, MIME("text/plain"), pb) - Test.@test occursin(string(problem), shown) - Test.@test occursin("ModelingToolkit modeling", shown) - Test.@test occursin(string(OCP.status(problem)[:ModelingToolkit]), shown) - Test.@test occursin( - "state dimension: $(CTModels.Models.state_dimension(sys))", shown - ) - Test.@test sprint(show, pb) == "Problem(:$(problem))" - end - end -end +# Written once in `common/modeling_tests.jl` (#441, #439). +test_problem() = Main.ModelingToolkitTests.test_problem(OptimalControlProblems.ModelingToolkitBackend()) end # module TestProblemModelingToolkit diff --git a/test/suite/ModelingToolkit/test_reference_solution.jl b/test/suite/ModelingToolkit/test_reference_solution.jl deleted file mode 100644 index 090c12911..000000000 --- a/test/suite/ModelingToolkit/test_reference_solution.jl +++ /dev/null @@ -1,33 +0,0 @@ -isdefined(Main, :SolveModelingToolkit) || - include(joinpath(@__DIR__, "..", "..", "common", "solve_modelingtoolkit.jl")) - -module TestReferenceSolutionModelingToolkit - -using Test: Test -using ModelingToolkit: ModelingToolkit as MTK -using OptimalControlProblems: OptimalControlProblems as OCP - -const VERBOSE = isdefined(Main, :TestOptions) ? Main.TestOptions.VERBOSE : true -const SHOWTIMING = isdefined(Main, :TestOptions) ? Main.TestOptions.SHOWTIMING : true - -function test_reference_solution() - backend = OCP.ModelingToolkitBackend() - Test.@testset "ModelingToolkit reference solution" verbose = VERBOSE showtiming = SHOWTIMING begin - Main.TestStatus.for_each_problem("reference_solution", backend) do problem - ref = OCP.reference_solution_data(problem) - isnothing(ref) && return nothing - pb = getfield(OCP, problem)(backend) - - # Solve with Corleone + Ipopt - res = Main.SolveModelingToolkit.solve(pb; grid_size=30, max_iter=100) - Test.@test Main.SolveModelingToolkit.succeeded(res) - # The numerical objective converges towards the reference objective (4.5 for bryson_denham) - rel_err = abs(res.objective - ref.objective) / abs(ref.objective) - Test.@test rel_err < 0.05 - end - end -end - -end # module TestReferenceSolutionModelingToolkit - -test_reference_solution() = TestReferenceSolutionModelingToolkit.test_reference_solution() diff --git a/test/suite/ModelingToolkit/test_smoke.jl b/test/suite/ModelingToolkit/test_smoke.jl index d66dfd5d6..1205ffa55 100644 --- a/test/suite/ModelingToolkit/test_smoke.jl +++ b/test/suite/ModelingToolkit/test_smoke.jl @@ -1,35 +1,12 @@ -isdefined(Main, :SolveModelingToolkit) || - include(joinpath(@__DIR__, "..", "..", "common", "solve_modelingtoolkit.jl")) +isdefined(Main, :ModelingToolkitTests) || + include(joinpath(@__DIR__, "..", "..", "common", "modelingtoolkit_tests.jl")) module TestSmokeModelingToolkit -using Test: Test -using ModelingToolkit: ModelingToolkit as MTK using OptimalControlProblems: OptimalControlProblems -const VERBOSE = isdefined(Main, :TestOptions) ? Main.TestOptions.VERBOSE : true -const SHOWTIMING = isdefined(Main, :TestOptions) ? Main.TestOptions.SHOWTIMING : true - -# Every problem builds, evaluates and runs a few iterations in each configuration; the -# convergence is reported by the convergence tests. -function test_smoke() - S = Main.SolveModelingToolkit - backend = OptimalControlProblems.ModelingToolkitBackend() - Test.@testset "ModelingToolkit smoke (small grid, few iterations)" verbose = VERBOSE showtiming = - SHOWTIMING begin - for config in Main.TestConfigs.CONFIGS.ModelingToolkit - Test.@testset "$(Main.TestConfigs.label(config))" begin - test = "test_smoke $(Main.TestConfigs.label(config))" - Main.TestStatus.for_each_problem(test, backend) do problem - pb = getfield(OptimalControlProblems, problem)(backend) - o = Main.TestConfigs - sol = S.solve(pb, config; grid_size=o.SMOKE_GRID, max_iter=o.SMOKE_MAX_ITER) - Test.@test isfinite(sol.objective) - end - end - end - end -end +# Written once in `common/modeling_tests.jl` (#441, #439). +test_smoke() = Main.ModelingToolkitTests.test_smoke(OptimalControlProblems.ModelingToolkitBackend()) end # module TestSmokeModelingToolkit diff --git a/tools/ci/scope.py b/tools/ci/scope.py index 3ca22ab48..9b333c2f7 100755 --- a/tools/ci/scope.py +++ b/tools/ci/scope.py @@ -217,7 +217,13 @@ def test_args(event, labels, rows, expected, repo): problems = ALL reason += "; every problem (label)" order = repo.environments + sorted(repo.opt_in) - if envs == ALL: + # ALL leaves out the opt-in environments: one asked for by a label is named, with every + # other environment, or the label would be lost when a shared file runs everything + opt_in = sorted(set(environments_of_labels(labels)) & repo.opt_in) + if envs == ALL and opt_in: + args = repo.environments + opt_in + reason += "; opt-in environments by label: " + ", ".join(opt_in) + elif envs == ALL: args = [] elif not envs: args = ["core"] diff --git a/tools/ci/test/test_scope.py b/tools/ci/test/test_scope.py index 930059c54..aaf6f9f8e 100644 --- a/tools/ci/test/test_scope.py +++ b/tools/ci/test/test_scope.py @@ -270,6 +270,14 @@ def test_opt_in_environment_by_label(self): out, _ = run_scope(changed(["docs/src/index.md"]), labels=["test ModelingToolkit backend"]) self.assertEqual("ModelingToolkit", out) + def test_opt_in_environment_by_label_with_everything(self): + # a shared file runs every environment: the opt-in one of the label is kept + out, err = run_scope(changed(["src/a.jl"]), labels=["test ModelingToolkit backend"]) + self.assertEqual("JuMP OptimalControl OptimalControlScalarized comparison ModelingToolkit", out) + self.assertIn("opt-in environments by label: ModelingToolkit", err) + out, _ = run_scope(changed(["docs/src/index.md"]), labels=["test everything", "test GPU backend"]) + self.assertEqual("JuMP OptimalControl OptimalControlScalarized comparison GPU", out) + def test_null_labels(self): out, _ = run_scope(changed(["ext/MetaData/beam.jl"]), labels=None) self.assertEqual("problem=beam", out) diff --git a/tools/cron/report.py b/tools/cron/report.py index 5a7b6f24c..bb66a8e63 100644 --- a/tools/cron/report.py +++ b/tools/cron/report.py @@ -81,7 +81,7 @@ "ModelingToolkit": { "title": "ModelingToolkit — Nominal Convergence", "focus": "Symbolic modeling backend with ModelingToolkit.jl", - "cadence": "Manual dispatch (disabled until the environment exists)", + "cadence": "Weekly (Saturday 04:00 UTC) · Julia 1.13, Ubuntu + Windows (CPU)", }, } @@ -860,9 +860,16 @@ def labels(issue: dict) -> set[str]: "OptimalControl", "OptimalControl-robustness", "OptimalControlScalarized", + "ModelingToolkit", ], - "failure": ["JuMP", "OptimalControl", "OptimalControlScalarized", "comparison"], - "draft": ["JuMP", "OptimalControl", "OptimalControlScalarized"], + "failure": [ + "JuMP", + "OptimalControl", + "OptimalControlScalarized", + "comparison", + "ModelingToolkit", + ], + "draft": ["JuMP", "OptimalControl", "OptimalControlScalarized", "ModelingToolkit"], "capability": ["JuMP", "OptimalControl", "OptimalControlScalarized"], }