diff --git a/BREAKING.md b/BREAKING.md index eab2722c7..1c246fb40 100644 --- a/BREAKING.md +++ b/BREAKING.md @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 42c074b68..d230013cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/src/getting-started/first-problem.md b/docs/src/getting-started/first-problem.md index 674885902..8e366abda 100644 --- a/docs/src/getting-started/first-problem.md +++ b/docs/src/getting-started/first-problem.md @@ -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), diff --git a/docs/src/guide/compare.md b/docs/src/guide/compare.md index 7604c4b24..8531de4cc 100644 --- a/docs/src/guide/compare.md +++ b/docs/src/guide/compare.md @@ -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 ) diff --git a/docs/src/guide/get.md b/docs/src/guide/get.md index 725e541aa..ec324804c 100644 --- a/docs/src/guide/get.md +++ b/docs/src/guide/get.md @@ -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` | diff --git a/docs/src/guide/plot.md b/docs/src/guide/plot.md index 6c0d44b90..96d0c98c2 100644 --- a/docs/src/guide/plot.md +++ b/docs/src/guide/plot.md @@ -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, diff --git a/docs/src/guide/solve-jump.md b/docs/src/guide/solve-jump.md index 14cc32451..13b5b7248 100644 --- a/docs/src/guide/solve-jump.md +++ b/docs/src/guide/solve-jump.md @@ -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) diff --git a/docs/src/guide/solve-optimalcontrol.md b/docs/src/guide/solve-optimalcontrol.md index abb3ad514..fbc477073 100644 --- a/docs/src/guide/solve-optimalcontrol.md +++ b/docs/src/guide/solve-optimalcontrol.md @@ -12,7 +12,7 @@ using NLPModelsIpopt using MadNLP pb = beam(OptimalControlBackend()) -N = grid_size_data(:beam) +N = grid_size_data(:beam, OptimalControlBackend()) nothing # hide ``` diff --git a/docs/src/migration.md b/docs/src/migration.md index 695c4382a..e583ff8bf 100644 --- a/docs/src/migration.md +++ b/docs/src/migration.md @@ -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 diff --git a/src/Metadata/access.jl b/src/Metadata/access.jl index f02d14ec6..d859f807a 100644 --- a/src/Metadata/access.jl +++ b/src/Metadata/access.jl @@ -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, ``` @@ -76,30 +77,6 @@ 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 @@ -107,9 +84,12 @@ 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 """ @@ -117,9 +97,12 @@ $(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 diff --git a/test/README.md b/test/README.md index 45ed60dc6..f56632831 100644 --- a/test/README.md +++ b/test/README.md @@ -22,7 +22,8 @@ test/ developed, so that each extension is shown to load alone. - `cron//` holds the slow tests of a group; they run in the environment of `suite//`, as `robustness//` does. -- `robustness//test_robustness.jl` solves every problem on its default grid under +- `robustness//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 @@ -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.`), 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 (`[.]` 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. @@ -275,7 +286,12 @@ 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`: @@ -283,10 +299,11 @@ 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 diff --git a/test/common/modeling_tests.jl b/test/common/modeling_tests.jl index 0be9196b3..85d244ff3 100644 --- a/test/common/modeling_tests.jl +++ b/test/common/modeling_tests.jl @@ -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. diff --git a/test/cron/JuMP/test_convergence.jl b/test/cron/JuMP/test_convergence.jl index 8b55b5a23..5287f4b08 100644 --- a/test/cron/JuMP/test_convergence.jl +++ b/test/cron/JuMP/test_convergence.jl @@ -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, diff --git a/test/extras/beam.jl b/test/extras/beam.jl deleted file mode 100644 index c37206fee..000000000 --- a/test/extras/beam.jl +++ /dev/null @@ -1,26 +0,0 @@ -using Pkg -Pkg.activate("test") -using OptimalControl -using NLPModelsIpopt -using Plots - -function Beam(a) - tf = 1 - - ocp = @def begin - t ∈ [0, tf], time - x ∈ R², state - u ∈ R, control - x(0) == [0, 1] - x(tf) == [0, -1] - ẋ(t) == [x₂(t), u(t)] - 0 ≤ x₁(t) ≤ a - ∫(u(t)^2) → min - end - - return ocp -end - -plot(solve(Beam(1 / 2))) # state constraint inactive -plot(solve(Beam(1 / 5))) # state constraint active: touch point -plot(solve(Beam(1 / 8))) # state constraint active: arc diff --git a/test/robustness/JuMP/test_robustness.jl b/test/robustness/JuMP/test_robustness.jl index 7d7085d3b..db96315d3 100644 --- a/test/robustness/JuMP/test_robustness.jl +++ b/test/robustness/JuMP/test_robustness.jl @@ -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, diff --git a/test/robustness/OptimalControl/test_robustness.jl b/test/robustness/OptimalControl/test_robustness.jl index 28903d55f..c228a9d26 100644 --- a/test/robustness/OptimalControl/test_robustness.jl +++ b/test/robustness/OptimalControl/test_robustness.jl @@ -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!( diff --git a/test/suite/core/test_parameters.jl b/test/suite/core/test_parameters.jl index b7ef2ae23..4716d66de 100644 --- a/test/suite/core/test_parameters.jl +++ b/test/suite/core/test_parameters.jl @@ -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()) == @@ -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) diff --git a/test/suite/core/test_status.jl b/test/suite/core/test_status.jl index 17deaf6e9..76bdc6d63 100644 --- a/test/suite/core/test_status.jl +++ b/test/suite/core/test_status.jl @@ -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", @@ -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",