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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ See [BREAKING.md](BREAKING.md).
`objective(sol)` and `iterations(sol)` work on it. The JuMP costate uses the sign
convention of `rules/signs.md` (`-JuMP.dual`, the same as OptimalControl, #326) and is
stamped at the step midpoints.
- `grid_size_data(problem, backend)`: the default grid of a problem in a modeling, the grid
of the problem but for ModelingToolkit, solved by shooting on a coarser one (at most 50
steps); a problem can set it per modeling with the optional metadata field `:grid_sizes`
(#435).

### 🛠 Enhancements

Expand Down
4 changes: 2 additions & 2 deletions docs/problems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function parameters_section(problem::Symbol)
isnothing(params) &&
return "### [Parameters](@id $(anchor(problem, :parameters)))\n\nThe problem has no parameters.\n"
info = get(OCP.problem_metadata(problem), :parameter_info, nothing)
unit(k) = (u = info[k].unit; isnothing(u) ? "–" : u)
unit(k) = (u=info[k].unit; isnothing(u) ? "–" : u)
rows = if isnothing(info)
join(("| `$(k)` | `$(v)` | | |" for (k, v) in pairs(params)), "\n")
else
Expand Down Expand Up @@ -250,7 +250,7 @@ function modeling_panel(problem::Symbol, spec, adapter, refs)
status = OCP.status(problem)[spec.name]
file = problem_file(problem, spec)
text = read(joinpath(ROOT, file), String)
grid_size = OCP.grid_size_data(problem)
grid_size = OCP.grid_size_data(problem, spec.backend())
notes = get(OCP.problem_metadata(problem), :notes, nothing)
draft = if status === :draft
"""
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/balanced_field.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.balanced_field(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:balanced_field),
grid_size::Int=grid_size_data(:balanced_field, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/beam.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.beam(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:beam),
grid_size::Int=grid_size_data(:beam, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/bioreactor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.bioreactor(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:bioreactor),
grid_size::Int=grid_size_data(:bioreactor, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/brachistochrone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.brachistochrone(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:brachistochrone),
grid_size::Int=grid_size_data(:brachistochrone, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/bryson_denham.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.bryson_denham(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:bryson_denham),
grid_size::Int=grid_size_data(:bryson_denham, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/cart_pendulum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.cart_pendulum(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:cart_pendulum),
grid_size::Int=grid_size_data(:cart_pendulum, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/chain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.chain(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:chain),
grid_size::Int=grid_size_data(:chain, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/dielectrophoretic_particle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.dielectrophoretic_particle(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:dielectrophoretic_particle),
grid_size::Int=grid_size_data(:dielectrophoretic_particle, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/double_oscillator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.double_oscillator(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:double_oscillator),
grid_size::Int=grid_size_data(:double_oscillator, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/ducted_fan.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.ducted_fan(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:ducted_fan),
grid_size::Int=grid_size_data(:ducted_fan, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/electric_vehicle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.electric_vehicle(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:electric_vehicle),
grid_size::Int=grid_size_data(:electric_vehicle, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/glider.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.glider(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:glider),
grid_size::Int=grid_size_data(:glider, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/insurance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.insurance(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:insurance),
grid_size::Int=grid_size_data(:insurance, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/jackson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.jackson(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:jackson),
grid_size::Int=grid_size_data(:jackson, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/moonlander.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.moonlander(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:moonlander),
grid_size::Int=grid_size_data(:moonlander, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/mountain_car.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.mountain_car(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:mountain_car),
grid_size::Int=grid_size_data(:mountain_car, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/robbins.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.robbins(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:robbins),
grid_size::Int=grid_size_data(:robbins, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/robot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.robot(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:robot),
grid_size::Int=grid_size_data(:robot, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/rocket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.rocket(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:rocket),
grid_size::Int=grid_size_data(:rocket, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/space_shuttle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.space_shuttle(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:space_shuttle),
grid_size::Int=grid_size_data(:space_shuttle, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/steering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.steering(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:steering),
grid_size::Int=grid_size_data(:steering, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
2 changes: 1 addition & 1 deletion ext/JuMPModels/vanderpol.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through
function OptimalControlProblems.vanderpol(
::JuMPBackend,
args...;
grid_size::Int=grid_size_data(:vanderpol),
grid_size::Int=grid_size_data(:vanderpol, JuMPBackend()),
scheme::Symbol=:trapeze,
ordering::Symbol=:interleaved,
parameters::Union{Nothing,NamedTuple}=nothing,
Expand Down
7 changes: 5 additions & 2 deletions ext/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ ext/
- `:status`, one entry per modeling where the problem has a file, `:draft` to start with;
- `:init`, the initial guess shared by every modeling, a function of the parameters
returning `(state=…, control=…, variable=…)`;
- optionally `:notes` (why a modeling is a draft) and `:reference_solution` (the analytic
solution of the continuous problem, see `src/Metadata/solutions.jl`).
- optionally `:notes` (why a modeling is a draft), `:reference_solution` (the analytic
solution of the continuous problem, see `src/Metadata/solutions.jl`) and `:grid_sizes`
(the grid in a modeling where the default of the modeling does not suit the problem:
`grid_size_data(problem, backend)` reads `:grid_size`, but ModelingToolkit, solved by
shooting, takes a coarser one).

The schema is `METADATA_SCHEMA` in `src/Metadata/loading.jl`, checked when the
package loads.
Expand Down
3 changes: 2 additions & 1 deletion src/Metadata/Metadata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ The metadata of the problems: the list of the problems (one file per problem in
them: `problem_metadata`, `grid_size_data`, `parameters_data`, `initial_guess_data`. The
analytic solutions used by some metadata files (`reference_solution`) are defined here too.

Depends on no other submodule.
Depends on `Modelings`: the grid of a problem depends on the modeling (`grid_size_data`).
"""
module Metadata

using CTBase: CTBase
using DocStringExtensions: TYPEDSIGNATURES
using OrderedCollections: OrderedDict
using ..Modelings: Modelings

include(joinpath(@__DIR__, "discovery.jl"))
include(joinpath(@__DIR__, "solutions.jl"))
Expand Down
38 changes: 38 additions & 0 deletions src/Metadata/access.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ The following keys are valid:
```julia
:grid_size => 500,
```
- `grid_sizes::NamedTuple` (optional): the number of steps of the problem in some modelings,
where it differs from the default of the modeling (see `grid_size_data`). For example:
```julia
:grid_sizes => (ModelingToolkit=100,),
```
- `parameters::Union{Nothing,NamedTuple}`: the list of parameters. For example:
```julia
:parameters => (
Expand Down Expand Up @@ -95,6 +100,39 @@ 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)
function _grid_size(problem::Symbol, ::Modelings.ModelingToolkitBackend)
return min(grid_size_data(problem), 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
ModelingToolkit, solved by shooting on a coarser grid (`MODELING_TOOLKIT_GRID_SIZE`).

# Example

```julia-repl
julia> grid_size_data(:beam, JuMPBackend())
500
```
"""
function grid_size_data(problem::Symbol, backend::Modelings.AbstractModelBackend)
grid_sizes = get(problem_metadata(problem), :grid_sizes, (;))
name = Modelings._modeling_spec(backend).name
return haskey(grid_sizes, name) ? grid_sizes[name] : _grid_size(problem, backend)
end

"""
$(TYPEDSIGNATURES)

Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const METADATA_SCHEMA = (
status=NamedTuple,
init=Function,
),
optional=(notes=AbstractString, reference_solution=Function),
optional=(notes=AbstractString, reference_solution=Function, grid_sizes=NamedTuple),
generated=(reference_objective=NamedTuple,),
)
const METADATA_STORAGE = OrderedDict{Symbol,OrderedDict}()
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 @@ -128,7 +128,7 @@ function test_convergence(backend, adapter)
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)
N = OCP.grid_size_data(problem, backend)
scheme = adapter.scheme(config)
warmup = get(adapter, :warmup, nothing)
if isnothing(warmup)
Expand Down
13 changes: 12 additions & 1 deletion test/suite/core/test_lint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,16 @@ end

info(names...) = NamedTuple{names}(Tuple((unit=nothing, meaning="m") for _ in names))

function metadata(; status=(OptimalControl=:valid,), parameter_info=:default)
function metadata(;
status=(OptimalControl=:valid,), parameter_info=:default, grid_sizes=nothing
)
parameters = (t0=0, tf=1, x_t0=0, x_u=1, guess=0.5)
data = Dict{Symbol,Any}(:grid_size => 100, :parameters => parameters, :status => status)
parameter_info === nothing || (
data[:parameter_info] =
parameter_info === :default ? info(keys(parameters)...) : parameter_info
)
isnothing(grid_sizes) || (data[:grid_sizes] = grid_sizes)
return data
end

Expand Down Expand Up @@ -149,6 +152,14 @@ function test_lint()
Test.@test fails(example(; parameter_info=bad), "not guess")
end

Test.@testset "grid per modeling" begin
Test.@test !fails(example(; grid_sizes=(OptimalControl=50,)), "`:grid_sizes`")
Test.@test fails(example(; grid_sizes=(JuMP=50,)), "`:grid_sizes`: a positive")
Test.@test fails(
example(; grid_sizes=(OptimalControl=0,)), "not OptimalControl"
)
end

Test.@testset "template of a model" begin
for (needle, what) in (
("\"\"\"The foo problem.\"\"\"\n" => "", "a docstring"),
Expand Down
27 changes: 27 additions & 0 deletions test/suite/core/test_parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,33 @@ function test_parameters()
delete!(storage, :_no_parameters)
end
end
Test.@testset "grid_size_data by modeling" verbose = VERBOSE showtiming = SHOWTIMING begin
OCP = OptimalControlProblems
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)
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()) ==
min(N, mtk)
end
# the grid of a problem in a modeling, given in its metadata: no problem of the
# collection has one, so one is added for the test
storage = OCP.Metadata.METADATA_STORAGE
storage[:_grid_sizes] = OCP.Metadata.OrderedDict{Symbol,Any}(
: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
end

Test.@testset "problem_metadata" verbose = VERBOSE showtiming = SHOWTIMING begin
problem = first(OptimalControlProblems.problems(; status=:all))
data = OptimalControlProblems.problem_metadata(problem)
Expand Down
Loading
Loading