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
19 changes: 19 additions & 0 deletions BREAKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ This document describes breaking changes in OptimalControlProblems releases.

## [Unreleased]

#### `grid_size_data(problem)` replaced by `grid_size_data(problem, backend)`

- **The grid depends on the modeling**: `grid_size_data(problem, backend)` is the default
grid of `problem` in the modeling of `backend` (#473); the one-argument form is removed.
The field `:grid_size` of the metadata is still the grid of the problem, the default of the
modelings but for ModelingToolkit (coarser, solved by shooting).
- **Why**: the modelings are independent (#435), and a grid without a modeling was the wrong
one for ModelingToolkit and for any problem with `:grid_sizes`.

**Migration**:

```julia
# Before
N = grid_size_data(:beam)

# After
N = grid_size_data(:beam, OptimalControlBackend())
```

#### The references are by modeling

- **`reference_objective`, `solution_kind`, `references` and `best_reference` take the
Expand Down
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### 💥 Breaking Changes

#### `grid_size_data(problem)` replaced by `grid_size_data(problem, backend)`

- **The grid depends on the modeling**: `grid_size_data(problem, backend)` is the default
grid of `problem` in the modeling of `backend` (#473); the one-argument form is removed.
The field `:grid_size` of the metadata is still the grid of the problem, the default of the
modelings but for ModelingToolkit (coarser, solved by shooting).
- **Why**: the modelings are independent (#435), and a grid without a modeling was the wrong
one for ModelingToolkit and for any problem with `:grid_sizes`.

**Migration**:

```julia
# Before
N = grid_size_data(:beam)

# After
N = grid_size_data(:beam, OptimalControlBackend())
```

#### The references are by modeling

- **`reference_objective`, `solution_kind`, `references` and `best_reference` take the
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 @@ -29,7 +29,7 @@ problem, an ADNLPModels model and Ipopt. The reference objectives of the collect
computed with the trapezoidal rule, which is not the default scheme of `solve`: give it.

```@example first
N = grid_size_data(:beam)
N = grid_size_data(:beam, OptimalControlBackend())
sol = solve(
model(pb), :collocation, :adnlp, :ipopt;
init=initial_guess(pb),
Expand Down
2 changes: 1 addition & 1 deletion docs/src/guide/compare.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ To compare two modelings, build them the same way and compare the objectives to
using OptimalControl, JuMP, Ipopt, NLPModelsIpopt, OptimalControlProblems

problem = :beam
N = grid_size_data(problem)
N = grid_size_data(problem, OptimalControlBackend())
reference = OptimalControlProblems.reference_objective(
problem, OptimalControlBackend(); grid_size=N, scheme=:trapeze
)
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 @@ -56,7 +56,7 @@ The [problem page](@ref description-beam) of each problem lists its parameters.
| :--- | :--- |
| `problem_metadata(:p)` | every metadata field, without building the problem |
| `metadata(pb)` | the metadata of a built `Problem`, with the parameters it was built with |
| `grid_size_data(:p)` | the default number of steps |
| `grid_size_data(:p, backend)` | the default number of steps in the modeling of `backend` |
| `parameters_data(:p)` | the default parameters |
| `OptimalControlProblems.initial_guess_data(:p)` | the shared initial guess (see [Initial guess](@ref initial-guess)) |
| `OptimalControlProblems.reference_objective(:p, backend; grid_size, scheme)` | the reference objective of the discretized problem in the modeling of `backend`, or `nothing` |
Expand Down
2 changes: 1 addition & 1 deletion docs/src/guide/plot.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using OptimalControl, JuMP, Ipopt, NLPModelsIpopt, OptimalControlProblems
using Plots

pb = beam(OptimalControlBackend())
N = grid_size_data(:beam)
N = grid_size_data(:beam, OptimalControlBackend())
sol = solve(
model(pb), :collocation, :adnlp, :ipopt;
init=initial_guess(pb), grid_size=N, scheme=:trapeze, display=false,
Expand Down
2 changes: 1 addition & 1 deletion docs/src/guide/solve-jump.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using JuMP, Ipopt, OptimalControlProblems
## Build and solve

```@example jump
nlp = beam(JuMPBackend(); grid_size=grid_size_data(:beam), scheme=:trapeze)
nlp = beam(JuMPBackend(); grid_size=grid_size_data(:beam, JuMPBackend()), scheme=:trapeze)
set_optimizer(nlp, Ipopt.Optimizer)
set_silent(nlp)
optimize!(nlp)
Expand Down
2 changes: 1 addition & 1 deletion docs/src/guide/solve-optimalcontrol.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using NLPModelsIpopt
using MadNLP

pb = beam(OptimalControlBackend())
N = grid_size_data(:beam)
N = grid_size_data(:beam, OptimalControlBackend())
nothing # hide
```

Expand Down
15 changes: 15 additions & 0 deletions docs/src/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ problem_metadata(:beam)[:grid_size] # [!code ++]
metadata(beam(OptimalControlBackend()))[:grid_size] # [!code ++]
```

## The grid and the references depend on the modeling

The modelings are independent: each has its default grid (the grid of the problem, coarser
for ModelingToolkit, solved by shooting) and its reference objectives, computed from its own
solves. The functions take the backend.

```julia
grid_size_data(:beam) # [!code --]
grid_size_data(:beam, OptimalControlBackend()) # [!code ++]
OptimalControlProblems.reference_objective(:beam) # [!code --]
OptimalControlProblems.reference_objective(:beam, JuMPBackend()) # [!code ++]
```

`solution_kind`, `references` and `best_reference` take the backend the same way.

## Errors

Passing `parameters` to a problem that has none throws
Expand Down
39 changes: 11 additions & 28 deletions src/Metadata/access.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Return a dictionary containing the metadata of `problem`, without building it.
The metadata of a built problem is read with `metadata(pb)` on the [`Problem`](@ref OptimalControlProblems.Problems.Problem).
The following keys are valid:

- `grid_size::Int`: the default number of steps. For example:
- `grid_size::Int`: the default number of steps of the problem, the grid of the modelings
but for ModelingToolkit (see `grid_size_data`). For example:
```julia
:grid_size => 500,
```
Expand Down Expand Up @@ -76,50 +77,32 @@ function problem_metadata(problem::Symbol)
return METADATA_STORAGE[problem]
end

"""
$(TYPEDSIGNATURES)

Return the number of discretisation steps, from the metadata, for a given optimal control problem.

# Arguments

- `problem::Symbol`: The name of the problem.

# Returns

- `Int`: The number of discretisation steps (`N`) of the specified problem.

# Example

```julia-repl
julia> grid_size_data(:beam)
500
```
"""
function grid_size_data(problem::Symbol)
return problem_metadata(problem)[:grid_size]
end

"""
The default number of steps of a ModelingToolkit problem: it is solved by direct shooting
(Corleone), one integration per control interval, which needs a coarser grid than a
collocation (#435). At most the grid of the problem.
"""
const MODELING_TOOLKIT_GRID_SIZE = 50

_grid_size(problem::Symbol, ::Modelings.AbstractModelBackend) = grid_size_data(problem)
# the default of a modeling: the grid of the problem (`:grid_size`), coarser for shooting
function _grid_size(problem::Symbol, ::Modelings.AbstractModelBackend)
return problem_metadata(problem)[:grid_size]
end
function _grid_size(problem::Symbol, ::Modelings.ModelingToolkitBackend)
return min(grid_size_data(problem), MODELING_TOOLKIT_GRID_SIZE)
return min(problem_metadata(problem)[:grid_size], MODELING_TOOLKIT_GRID_SIZE)
end

"""
$(TYPEDSIGNATURES)

Return the default number of steps of `problem` in the modeling of `backend`: the entry of
the modeling in the field `:grid_sizes` of the metadata when there is one, otherwise the
default of the modeling, the grid of the problem (`grid_size_data(problem)`) but for
default of the modeling, the grid of the problem (the field `:grid_size`) but for
ModelingToolkit, solved by shooting on a coarser grid (`MODELING_TOOLKIT_GRID_SIZE`).

The grid depends on the modeling: the modelings are independent (#435), so there is no
grid of a problem without a modeling.

# Example

```julia-repl
Expand Down
31 changes: 24 additions & 7 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ test/
developed, so that each extension is shown to load alone.
- `cron/<group>/` holds the slow tests of a group; they run in the environment of
`suite/<group>/`, as `robustness/<group>/` does.
- `robustness/<group>/test_robustness.jl` solves every problem on its default grid under
- `robustness/<group>/test_robustness.jl` solves every problem on the default grid of the
modeling (`grid_size_data(problem, backend)`) under
other options of each solver (`TestConfigs.ROBUSTNESS`): does the initial guess still
lead to the best known solution? Reported, never tested. Run it with the word
`robustness` (`Pkg.test(; test_args=["OptimalControl", "robustness"])`); it runs only
Expand Down Expand Up @@ -171,7 +172,17 @@ not test OptimalControl or CTDirect.
`cron-failure` issue of the scope `comparison`.
- **Not compared**: the solutions found by two solvers. After a solve, two modelings are two
optimization paths, and a problem with several local solutions ends on one or the other
depending on the platform; convergence is reported, never tested. Not tested either: the
depending on the platform; convergence is reported, never tested. The one exception is
`beam`, solved by JuMP and by CTDirect on the same small NLP in `test_solution`, which
checks the solution built from a JuMP model, costate signs included (#326).
- **The modelings are independent** (#435) but for the definition of the problems
(parameters, initial guess, description): each has its configurations
(`CONFIGS.<modeling>`), its default grid (`grid_size_data(problem, backend)`: the grid of
the problem, coarser for ModelingToolkit, or the entry of the modeling in `:grid_sizes`)
and its reference objectives, computed from its own solves (`[<modeling>.<scheme>]` in
`ext/MetaData/references/`, #474). The gap of a solve in the reports is to the reference of
its modeling; the problem pages show how close the modelings are, computed when they are
built. Not tested either: the
Exa modeler against ADNLP and the NLP of the scalarized model, which are properties of
OptimalControl.

Expand Down Expand Up @@ -275,18 +286,24 @@ The environment `ModelingToolkit` (`test/suite/ModelingToolkit/`) solves the pro
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
and the path constraints are imposed at its nodes. As in the examples of Corleone, the grid
is coarse (`grid_size_data(problem, ModelingToolkitBackend())`, at most
`MODELING_TOOLKIT_GRID_SIZE` = 50 steps: one integration per control interval) and the
Hessian is a quasi-Newton one (`hessian_approximation = "limited-memory"`): the exact one,
by ForwardDiff through every integration, is dense and costly (#435). The convergence test
compiles on the smoke grid first (the `warmup` of the adapter), then times the solve. 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`, daily, a rotation with a budget, #435) 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.
stays free of ModelingToolkit. Its convergence report (`cron/ModelingToolkit`, daily, a rotation with a budget, #435) gives
the gap to the reference objective of ModelingToolkit for the single shooting
(`[ModelingToolkit.shooting_single]`, #474), else to the objective of the analytic solution,
when the problem has one; `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

Expand Down
2 changes: 1 addition & 1 deletion test/common/modeling_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function test_problem(backend; model_type::Type, check=(problem, pb) -> nothing)
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)
Test.@test data[:grid_size] == OCP.problem_metadata(problem)[:grid_size]

# The parameters a problem is built with are recorded in its metadata, and the
# metadata of the collection are left untouched.
Expand Down
4 changes: 3 additions & 1 deletion test/cron/JuMP/test_convergence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ function test_convergence()
OptimalControlProblems.JuMPBackend(),
Main.TestConfigs.label(config);
Main.SolveJuMP.settings(config)...,
grid_size=OptimalControlProblems.grid_size_data(problem),
grid_size=OptimalControlProblems.grid_size_data(
problem, OptimalControlProblems.JuMPBackend()
),
scheme=Main.TestConfigs.SCHEME,
Main.SolveJuMP.outcome(model)...,
time=time,
Expand Down
26 changes: 0 additions & 26 deletions test/extras/beam.jl

This file was deleted.

2 changes: 1 addition & 1 deletion test/robustness/JuMP/test_robustness.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function test_robustness()
backend,
label;
Main.SolveJuMP.settings(config; options=options)...,
grid_size=OptimalControlProblems.grid_size_data(problem),
grid_size=OptimalControlProblems.grid_size_data(problem, backend),
scheme=C.SCHEME,
Main.SolveJuMP.outcome(model)...,
time=time,
Expand Down
2 changes: 1 addition & 1 deletion test/robustness/OptimalControl/test_robustness.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function test_robustness()
test = "test_robustness $label"
Main.TestStatus.for_each_problem(test, backend) do problem
pb = getfield(OptimalControlProblems, problem)(backend)
N = OptimalControlProblems.grid_size_data(problem)
N = OptimalControlProblems.grid_size_data(problem, backend)
sol = nothing
time = @elapsed sol = S.solve(pb, config; grid_size=N, options=options)
Main.TestStatus.result!(
Expand Down
7 changes: 4 additions & 3 deletions test/suite/core/test_parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function test_parameters()
mtk = OCP.Metadata.MODELING_TOOLKIT_GRID_SIZE
for problem in OCP.problems(; status=:all)
haskey(OCP.problem_metadata(problem), :grid_sizes) && continue
N = OCP.grid_size_data(problem)
N = OCP.problem_metadata(problem)[:grid_size]
Test.@test OCP.grid_size_data(problem, OCP.JuMPBackend()) == N
Test.@test OCP.grid_size_data(problem, OCP.OptimalControlBackend()) == N
Test.@test OCP.grid_size_data(problem, OCP.ModelingToolkitBackend()) ==
Expand All @@ -50,19 +50,20 @@ function test_parameters()
:grid_size => 500, :grid_sizes => (ModelingToolkit=120, JuMP=200)
)
try
Test.@test OCP.grid_size_data(:_grid_sizes) == 500
Test.@test OCP.grid_size_data(:_grid_sizes, OCP.ModelingToolkitBackend()) == 120
Test.@test OCP.grid_size_data(:_grid_sizes, OCP.JuMPBackend()) == 200
Test.@test OCP.grid_size_data(:_grid_sizes, OCP.OptimalControlBackend()) == 500
finally
delete!(storage, :_grid_sizes)
end
# no grid without a modeling: the modelings are independent (#435)
Test.@test !hasmethod(OCP.grid_size_data, Tuple{Symbol})
end

Test.@testset "problem_metadata" verbose = VERBOSE showtiming = SHOWTIMING begin
problem = first(OptimalControlProblems.problems(; status=:all))
data = OptimalControlProblems.problem_metadata(problem)
Test.@test data[:grid_size] == OptimalControlProblems.grid_size_data(problem)
Test.@test data[:grid_size] isa Int
Test.@test data[:parameters] == OptimalControlProblems.parameters_data(problem)
# the initial guess comes from the parameters (#377)
guess = OptimalControlProblems.initial_guess_data(problem)
Expand Down
4 changes: 2 additions & 2 deletions test/suite/core/test_status.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function test_status()
modeler=nothing,
solver=:ipopt,
options=(tol=1e-8, mu_init=1e-3),
grid_size=OptimalControlProblems.grid_size_data(problem),
grid_size=OptimalControlProblems.grid_size_data(problem, BACKEND),
scheme=:trapeze,
converged=true,
reason="LOCALLY_SOLVED",
Expand Down Expand Up @@ -205,7 +205,7 @@ function test_status()
modeler=nothing,
solver=:ipopt,
options=(;),
grid_size=OptimalControlProblems.grid_size_data(problem),
grid_size=OptimalControlProblems.grid_size_data(problem, BACKEND),
scheme=:trapeze,
converged=true,
reason="LOCALLY_SOLVED",
Expand Down
Loading