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
2 changes: 1 addition & 1 deletion ext/MetaData/mountain_car.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mountain_car_meta = OrderedDict(
b=0.0025,
c=3.0,
),
:status => (OptimalControl=:valid, OptimalControlScalarized=:valid, JuMP=:valid),
:status => (OptimalControl=:valid, OptimalControlScalarized=:valid, JuMP=:valid, ModelingToolkit=:valid),
:notes => "On the default grid, the scalarized model solved with MadNLP gives the objective 102.381 instead of 102.398 (JuMP and OptimalControl), a relative gap of 1.6e-4: a convergence gap, reported by the convergence tests (#360); the models are equivalent at the initial guess.",
# initial guess of every modeling (#377), from the parameters
:init => p -> (state=[p.pos_t0, p.vel_t0], control=0.0, variable=p.tf_start),
Expand Down
67 changes: 67 additions & 0 deletions ext/ModelingToolkitModels/mountain_car.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""The **Mountain Car problem** is a classical benchmark in optimal control: reach the top of a hill in minimal time, with a car that is too weak to climb it directly.

Builds the `mountain_car` problem with the ModelingToolkit backend.

```julia-repl
julia> using ModelingToolkit, OptimalControlProblems
julia> pb = mountain_car(ModelingToolkitBackend())
```

Default parameters and the modeling status are available through
`problem_metadata(:mountain_car)` and `OptimalControlProblems.status(:mountain_car)`.
"""
function OptimalControlProblems.mountain_car(
::ModelingToolkitBackend;
parameters::Union{Nothing,NamedTuple}=nothing,
init=nothing,
)
OptimalControlProblems.Catalogue._warn_if_draft(:mountain_car, :ModelingToolkit)
params = OptimalControlProblems.parameters_data(:mountain_car, parameters)
t0 = float(params[:t0])
tf_min = float(params[:tf_min])
pos_t0, vel_t0 = float(params[:pos_t0]), float(params[:vel_t0])
pos_tf, vel_tf_min = float(params[:pos_tf]), float(params[:vel_tf_min])
pos_min, pos_max = float(params[:pos_min]), float(params[:pos_max])
vel_min, vel_max = float(params[:vel_min]), float(params[:vel_max])
u_min, u_max = float(params[:u_min]), float(params[:u_max])
a, b, c = params[:a], params[:b], params[:c]

# The final time is free. Corleone integrates on a fixed horizon: the time is reduced to
# s = (t - t0) / (tf - t0) in [0, 1], and the final time `tf`, the variable of the problem,
# is a parameter to optimize. The derivatives are multiplied by the duration tf - t0.
s = ModelingToolkit.t_nounits
D = ModelingToolkit.Differential(s)

ModelingToolkit.@parameters tf [tunable = true, bounds = (tf_min, Inf)]
ModelingToolkit.@variables pos(..) = pos_t0 [tunable = false]
ModelingToolkit.@variables vel(..) = vel_t0 [tunable = false]
ModelingToolkit.@variables u(..) [input = true, bounds = (u_min, u_max)]

t = s
eqs = [
D(pos(t)) ~ (tf - t0) * vel(t),
D(vel(t)) ~ (tf - t0) * (a * u(t) - b * cos(c * pos(t))),
]
# a Mayer cost: the final time
costs = [tf]
# the final conditions are at s = 1
constraints = [
pos(1.0) ~ pos_tf,
ModelingToolkit.Symbolics.:≳(vel(1.0), vel_tf_min),
ModelingToolkit.Symbolics.:≳(pos(t), pos_min),
ModelingToolkit.Symbolics.:≲(pos(t), pos_max),
ModelingToolkit.Symbolics.:≳(vel(t), vel_min),
ModelingToolkit.Symbolics.:≲(vel(t), vel_max),
]

ModelingToolkit.@named sys = ModelingToolkit.System(
eqs, t; costs=costs, constraints=constraints
)

# initial guess: from the metadata (#377), unless given
init = OptimalControlProblems.Metadata._initial_guess(:mountain_car, params, init)

return OptimalControlProblems.Problems._problem(
:mountain_car, :ModelingToolkit, sys, init, params
)
end
62 changes: 43 additions & 19 deletions test/common/continuous_modelingtoolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ 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.
time `t`, `p.x0`, `p.xf` for the state at the first or last time, `p.v` for the variables. `sig`
holds the independent variable, the states, the controls, the variables, and the first and last
times of the horizon of the `System`.
"""
function evaluate(e, sig, p)
e = unwrap(e)
isequal(e, sig.iv) && return p.t
i = findfirst(s -> isequal(e, s), sig.variables)
isnothing(i) || return p.v[i]
if SU.iscall(e)
op, args = SU.operation(e), SU.arguments(e)
for (signals, now, at_t0, at_tf) in
Expand All @@ -69,7 +72,9 @@ function evaluate(e, sig, p)
return v
end

point(; t=nothing, x=nothing, u=nothing, x0=nothing, xf=nothing) = (; t, x, u, x0, xf)
function point(; t=nothing, x=nothing, u=nothing, x0=nothing, xf=nothing, v=nothing)
return (; t, x, u, x0, xf, v)
end

"""`h` as `(h′, c)` with `h = h′ + c` and `c` the constant term of a sum."""
function split_constant(h)
Expand Down Expand Up @@ -148,33 +153,48 @@ 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
`control_names` (those of the OptimalControl model), the physical times being `(t0, tf)`: a
`System` does not say them when its cost has no integral.

With a free final time (`tf` is `nothing`), the `System` is written in the reduced time
`s = (t - t0) / (tf - t0)` on `[0, 1]`, with the final time `tf` as a parameter to optimize, the
derivatives and the integrands multiplied by the duration `tf - t0`. It is read back in the
physical time: the variable is `tf`, and the functions are divided by the duration.
"""
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"))
function problem_data(sys, state_names, control_names, (t0, tf))
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)
vs = filter(p -> MTK.istunable(p, true) && !MTK.isinput(p), MTK.parameters(sys))
free = isnothing(tf)
name.(vs) == (free ? ["tf"] : String[]) ||
throw(ArgumentError("variables $(name.(vs)) not supported: only the final time `tf`"))
n, m = length(xs), length(us)
t0, tf = Float64.(tspan)
t0 = Float64(t0)

# costs: integrals over (t0, tf) and terms in the state at t0 or tf
# The horizon of the `System`, and the physical time of a time s of that horizon.
horizon = free ? (0.0, 1.0) : (t0, Float64(tf))
duration(v) = free ? v[1] - t0 : 1.0
reduced(t, v) = free ? (t - t0) / (v[1] - t0) : t

# costs: integrals over the horizon and terms in the state at its ends
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)"))
(constant(d.left), constant(d.right)) == horizon ||
throw(ArgumentError("an integral over another interval than the horizon"))
push!(integrands, only(SU.arguments(c)))
else
push!(terms, c)
end
end
sig = (; iv, states=unwrap.(xs), controls=unwrap.(us), t0, tf)
sig = (;
iv, states=unwrap.(xs), controls=unwrap.(us), variables=unwrap.(vs),
t0=horizon[1], tf=horizon[2],
)

# dynamics, in the order of the states
rhs = Vector{Any}(nothing, n)
Expand All @@ -195,6 +215,7 @@ function problem_data(sys, state_names, control_names, tspan)
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))
lower_v, upper_v = first.(metadata_bounds.(vs)), last.(metadata_bounds.(vs))
path_rows, boundary_rows = Tuple{Float64,Any,Float64}[], Tuple{Float64,Any,Float64}[]
for c in MTK.get_constraints(sys)
lo, h, hi = bounded(c)
Expand All @@ -213,11 +234,11 @@ function problem_data(sys, state_names, control_names, tspan)
end
end
path = Tuple{Any,Function,Any}[
(lo, (x, u, v, t) -> evaluate(h, sig, point(; t, x, u)), hi) for
(lo, (x, u, v, t) -> evaluate(h, sig, point(; t=reduced(t, v), x, u, v)), hi) for
(lo, h, hi) in merged(path_rows)
]
boundary = Tuple{Any,Function,Any}[
(lo, (x0, xf, v) -> evaluate(h, sig, point(; x0, xf)), hi) for
(lo, (x0, xf, v) -> evaluate(h, sig, point(; x0, xf, v)), hi) for
(lo, h, hi) in merged(boundary_rows)
]
for (i, x) in enumerate(xs)
Expand All @@ -229,17 +250,20 @@ function problem_data(sys, state_names, control_names, tspan)
lagrange = if isempty(integrands)
nothing
else
(x, u, v, t) -> sum(evaluate(ℓ, sig, point(; t, x, u)) for ℓ in integrands)
(x, u, v, t) ->
sum(evaluate(ℓ, sig, point(; t=reduced(t, v), x, u, v)) for ℓ in integrands) /
duration(v)
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],
variable=[component(vs[j], lower_v[j], upper_v[j]) for j in eachindex(vs)],
times=v -> free ? (t0, v[1]) : (t0, Float64(tf)),
dynamics=(x, u, v, t) ->
[evaluate(f, sig, point(; t=reduced(t, v), x, u, v)) / duration(v) 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),
I + sum(evaluate(g, sig, point(; x0, xf, v)) for g in terms; init=0.0),
path=path,
boundary=boundary,
)
Expand Down
75 changes: 63 additions & 12 deletions test/common/solve_modelingtoolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,32 @@ using OrdinaryDiffEqTsit5: Tsit5
using OptimalControlProblems: OptimalControlProblems as OCP

"""
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).
Whether the final time of the `System` `sys` is free: the problem has a variable `tf`, a
parameter to optimize. Corleone integrates on a fixed horizon, so such a problem is written
in the reduced time `s = (t - t0) / (tf - t0)` on `[0, 1]`, and its derivatives are multiplied
by the duration `tf - t0`.
"""
function tspan(pb)
free_final_time(sys) = "tf" in CTModels.Models.variable_components(sys)

"""
The physical times `(t0, tf)` of the problem `pb`, from its parameters (a `System` does not say
them when its cost has no integral, a Mayer cost); `tf` is `nothing` when the final time is
free, the variable `tf` being then optimized.
"""
function times(pb)
params = OCP.metadata(pb)[:parameters]
return (Float64(params.t0), Float64(params.tf))
t0 = Float64(params.t0)
free_final_time(OCP.model(pb)) && return (t0, nothing)
return (t0, Float64(params.tf))
end

"""
The time horizon the `System` of `pb` is integrated on: `(t0, tf)`, or `(0, 1)` when the final
time is free (reduced time, see `free_final_time`).
"""
function tspan(pb)
t0, tf = times(pb)
return isnothing(tf) ? (0.0, 1.0) : (t0, tf)
end

"""
Expand Down Expand Up @@ -129,6 +149,14 @@ function state_start(init, t0, n::Int)
return _start(init, :state, t0, n)
end

"""The parameters of the `System` `sys` that are variables of the problem, as `variable_components`."""
function variable_symbols(sys)
return filter(p -> MTK.istunable(p, true) && !MTK.isinput(p), MTK.parameters(sys))
end

"""The start value of each of the `n` variables, from the initial guess `init` of the problem."""
variable_start(init, n::Int) = _start(init, :variable, 0.0, n)

function _start(init, key::Symbol, t0, n::Int)
g = haskey(init, key) ? init[key] : 0.0
g isa Function && (g = g(t0))
Expand Down Expand Up @@ -170,6 +198,11 @@ function prepare(
states = filter(!MTK.isinput, MTK.unknowns(sys))
start_x = state_start(init, t0, length(states))
append!(inits, [x => start_x[i] for (i, x) in enumerate(states) if MTK.istunable(x, false)])

# The variables (the final time `tf`) are parameters to optimize, started from the guess.
vars = variable_symbols(sys)
start_v = variable_start(init, length(vars))
append!(inits, [v => start_v[i] for (i, v) in enumerate(vars)])
switches = [u => collect(grid[1:(end - 1)]) for u in ctrl_syms]

dynamic = Corleone.CorleoneDynamicOptProblem(
Expand Down Expand Up @@ -209,6 +242,13 @@ function control_values(dynamic, name)
return dynamic.ps.parameter.parameters[Symbol(name, "(t)")].parameter.ps
end

"""
The values of the variable `name` (`"tf"`) the problem `dynamic` of a `prepare` starts from.
"""
function variable_values(dynamic, name)
return dynamic.ps.parameter.parameters[Symbol(name)].parameter.ps
end

"""The objective of a solution `sol` of `solve`."""
objective(sol) = sol.result.objective

Expand Down Expand Up @@ -279,14 +319,18 @@ 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
(dynamic.layer.problem, dynamic.evaluation_grid), solved_parameters(sol), dynamic.st
)
return traj
end

"""The parameters of the problem solved by `sol`, at the solution."""
function solved_parameters(sol)
_, reconstruct = Corleone.pack(Val(:ComponentArrays), sol.dynamic.ps)
return reconstruct(sol.result.u)
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
Expand All @@ -308,6 +352,17 @@ function _trajectory(sol, pb)
t = collect(traj.t)
n_points = length(t)

# The variables at the solution; with a free final time, the time is reduced to [0, 1]
# and goes back to the physical time t0 + (tf - t0) s.
xs = CTModels.Models.state_components(sys)
us = CTModels.Models.control_components(sys)
vs = CTModels.Models.variable_components(sys)
solved = [only(solved_parameters(sol).parameter.parameters[Symbol(v)].parameter.ps) for v in vs]
if free_final_time(sys)
t0, _ = times(pb)
t = t0 .+ (only(solved[findall(==("tf"), vs)]) - t0) .* t
end

# At most TestStatus.TRAJECTORY_POINTS points, as for the other modelings
max_points = Main.TestStatus.TRAJECTORY_POINTS
keep = if n_points <= max_points
Expand All @@ -317,10 +372,6 @@ function _trajectory(sol, pb)
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)]
Expand All @@ -345,7 +396,7 @@ function _trajectory(sol, pb)
"state" => state,
"control" => control,
"costate" => Dict{String,Vector{Float64}}(),
"variable" => Float64[],
"variable" => r.(solved),
"names" => Dict("state" => xs, "control" => us, "variable" => vs),
)
end
Expand Down
19 changes: 19 additions & 0 deletions test/suite/ModelingToolkit/test_methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ function test_methods()
domain = MTK.SymbolicUtils.operation(MTK.get_costs(sys)[1]).domain.domain
Test.@test (Float64(domain.left), Float64(domain.right)) == tspan

# 1b. a free final time: the system is in the reduced time, tf is a variable to optimize
free = OCP.mountain_car(backend)
Test.@test Main.SolveModelingToolkit.free_final_time(OCP.model(free))
Test.@test !Main.SolveModelingToolkit.free_final_time(sys)
Test.@test OCP.variable_components(OCP.model(free)) == ["tf"]
Test.@test Main.SolveModelingToolkit.times(free) == (0.0, nothing)
Test.@test Main.SolveModelingToolkit.times(pb) == (0.0, 1.0)
Test.@test Main.SolveModelingToolkit.tspan(free) == (0.0, 1.0)
# started from the guess of the variable
started = Main.SolveModelingToolkit.prepare(free; grid_size=6).dynamic
Test.@test Main.SolveModelingToolkit.variable_values(started, "tf") ==
[OCP.initial_guess(free).variable]

# 2. _is_path_constraint
iv = MTK.independent_variable(sys)
cons = MTK.get_constraints(sys)
Expand Down Expand Up @@ -99,6 +112,12 @@ function test_methods()
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)
# with a free final time, the time axis is the physical one: from t0 to the solved tf
sol_free = S.solve(free, cfg; grid_size=10, max_iter=2)
tr_free = S.trajectory(sol_free, free)
Test.@test tr_free["time"][1] == 0.0
Test.@test tr_free["time"][end] ≈ tr_free["variable"][1]
Test.@test tr_free["names"]["variable"] == ["tf"]
params = OCP.metadata(pb)[:parameters]
Test.@test tr["state"]["x₁"][1] ≈ params.x1_t0
Test.@test tr["state"]["x₂"][1] ≈ params.x2_t0
Expand Down
4 changes: 2 additions & 2 deletions test/suite/ModelingToolkit/test_modelings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ function test_modelings()
Test.@test OCP.state_components(sys) == xs
Test.@test OCP.control_components(sys) == us
end
tspan = Main.SolveModelingToolkit.tspan(pb_mtk)
data = Main.ContinuousModelingToolkit.problem_data(sys, xs, us, tspan)
times = Main.SolveModelingToolkit.times(pb_mtk)
data = Main.ContinuousModelingToolkit.problem_data(sys, xs, us, times)
checks = Main.Continuous.compare(data, ocp, OCP.initial_guess(pb))
for (name, ok) in sort!(collect(checks); by=first)
Test.@testset "$name" begin
Expand Down
Loading