Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .github/workflows/CronModelingToolkit.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion .github/workflows/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -30,6 +31,7 @@ JuMP = "1"
LiveServer = "1"
MadNLP = "0.10"
Markdown = "1"
ModelingToolkit = "11"
NLPModelsIpopt = "0.11"
OptimalControl = "2.2"
Plots = "1"
Expand Down
1 change: 1 addition & 0 deletions docs/api_reference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ 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.
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
Expand Down
16 changes: 11 additions & 5 deletions docs/modelings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
)

Expand Down
2 changes: 1 addition & 1 deletion docs/src/getting-started/first-problem.md
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion docs/src/guide/get.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/src/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion ext/MetaData/jackson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
2 changes: 1 addition & 1 deletion ext/MetaData/robbins.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
2 changes: 1 addition & 1 deletion ext/MetaData/vanderpol.jl
Original file line number Diff line number Diff line change
@@ -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),
)
62 changes: 62 additions & 0 deletions ext/ModelingToolkitModels/jackson.jl
Original file line number Diff line number Diff line change
@@ -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
57 changes: 57 additions & 0 deletions ext/ModelingToolkitModels/robbins.jl
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions ext/ModelingToolkitModels/vanderpol.jl
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion ext/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ext/
├── Descriptions/<problem>.md # description of every problem: one file each
├── <Modeling>Models/<problem>.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
```

Expand Down Expand Up @@ -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=<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
Expand Down
Loading
Loading