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
7 changes: 6 additions & 1 deletion ext/MetaData/mountain_car.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ mountain_car_meta = OrderedDict(
b=(unit=nothing, meaning="coefficient of the gravity term b cos(c pos)"),
c=(unit=nothing, meaning="frequency of the hill profile"),
),
:status => (OptimalControl=:valid, OptimalControlScalarized=:valid, JuMP=:valid, ModelingToolkit=:valid),
:status => (
OptimalControl=:valid,
OptimalControlScalarized=:valid,
JuMP=:valid,
ModelingToolkit=:valid,
),
:notes => "On the default grid, the configuration modeler=exa solver=madnlp reaches the objective 102.381 in both OptimalControl modelings, against 102.398 with Ipopt in JuMP and both OptimalControl modelings, a relative gap of 1.6e-4: a convergence gap of this configuration, 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
4 changes: 1 addition & 3 deletions ext/ModelingToolkitModels/beam.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ Default parameters and the modeling status are available through
`problem_metadata(:beam)` and `OptimalControlProblems.status(:beam)`.
"""
function OptimalControlProblems.beam(
::ModelingToolkitBackend;
parameters::Union{Nothing,NamedTuple}=nothing,
init=nothing,
::ModelingToolkitBackend; parameters::Union{Nothing,NamedTuple}=nothing, init=nothing
)
OptimalControlProblems.Catalogue._warn_if_draft(:beam, :ModelingToolkit)
params = OptimalControlProblems.parameters_data(:beam, parameters)
Expand Down
4 changes: 1 addition & 3 deletions ext/ModelingToolkitModels/bioreactor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ Default parameters and the modeling status are available through
`problem_metadata(:bioreactor)` and `OptimalControlProblems.status(:bioreactor)`.
"""
function OptimalControlProblems.bioreactor(
::ModelingToolkitBackend;
parameters::Union{Nothing,NamedTuple}=nothing,
init=nothing,
::ModelingToolkitBackend; parameters::Union{Nothing,NamedTuple}=nothing, init=nothing
)
OptimalControlProblems.Catalogue._warn_if_draft(:bioreactor, :ModelingToolkit)
params = OptimalControlProblems.parameters_data(:bioreactor, parameters)
Expand Down
4 changes: 1 addition & 3 deletions ext/ModelingToolkitModels/double_oscillator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ Default parameters and the modeling status are available through
`problem_metadata(:double_oscillator)` and `OptimalControlProblems.status(:double_oscillator)`.
"""
function OptimalControlProblems.double_oscillator(
::ModelingToolkitBackend;
parameters::Union{Nothing,NamedTuple}=nothing,
init=nothing,
::ModelingToolkitBackend; parameters::Union{Nothing,NamedTuple}=nothing, init=nothing
)
OptimalControlProblems.Catalogue._warn_if_draft(:double_oscillator, :ModelingToolkit)
params = OptimalControlProblems.parameters_data(:double_oscillator, parameters)
Expand Down
6 changes: 2 additions & 4 deletions ext/ModelingToolkitModels/electric_vehicle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ Default parameters and the modeling status are available through
`problem_metadata(:electric_vehicle)` and `OptimalControlProblems.status(:electric_vehicle)`.
"""
function OptimalControlProblems.electric_vehicle(
::ModelingToolkitBackend;
parameters::Union{Nothing,NamedTuple}=nothing,
init=nothing,
::ModelingToolkitBackend; parameters::Union{Nothing,NamedTuple}=nothing, init=nothing
)
OptimalControlProblems.Catalogue._warn_if_draft(:electric_vehicle, :ModelingToolkit)
params = OptimalControlProblems.parameters_data(:electric_vehicle, parameters)
Expand All @@ -36,7 +34,7 @@ function OptimalControlProblems.electric_vehicle(

eqs = [D(x(t)) ~ v(t), D(v(t)) ~ h1 * u(t) - h2 * v(t)^2 - h0 - road(x(t))]
costs = [
ModelingToolkit.Symbolics.Integral(t in (t0, tf))(b1 * u(t) * v(t) + b2 * u(t)^2),
ModelingToolkit.Symbolics.Integral(t in (t0, tf))(b1 * u(t) * v(t) + b2 * u(t)^2)
]
constraints = [x(tf) ~ x_tf, v(tf) ~ v_tf]

Expand Down
4 changes: 1 addition & 3 deletions ext/ModelingToolkitModels/mountain_car.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ 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,
::ModelingToolkitBackend; parameters::Union{Nothing,NamedTuple}=nothing, init=nothing
)
OptimalControlProblems.Catalogue._warn_if_draft(:mountain_car, :ModelingToolkit)
params = OptimalControlProblems.parameters_data(:mountain_car, parameters)
Expand Down
21 changes: 13 additions & 8 deletions test/common/continuous_modelingtoolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ function problem_data(sys, state_names, control_names, (t0, tf))
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`"))
name.(vs) == (free ? ["tf"] : String[]) || throw(
ArgumentError("variables $(name.(vs)) not supported: only the final time `tf`")
)
n, m = length(xs), length(us)
t0 = Float64(t0)

Expand All @@ -192,8 +193,12 @@ function problem_data(sys, state_names, control_names, (t0, tf))
end
end
sig = (;
iv, states=unwrap.(xs), controls=unwrap.(us), variables=unwrap.(vs),
t0=horizon[1], tf=horizon[2],
iv,
states=unwrap.(xs),
controls=unwrap.(us),
variables=unwrap.(vs),
t0=horizon[1],
tf=horizon[2],
)

# dynamics, in the order of the states
Expand Down Expand Up @@ -251,16 +256,16 @@ function problem_data(sys, state_names, control_names, (t0, tf))
nothing
else
(x, u, v, t) ->
sum(evaluate(ℓ, sig, point(; t=reduced(t, v), x, u, v)) for ℓ in integrands) /
duration(v)
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=[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],
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, v)) for g in terms; init=0.0),
Expand Down
12 changes: 9 additions & 3 deletions test/common/solve_modelingtoolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ function _start(init, key::Symbol, t0, n::Int)
g = haskey(init, key) ? init[key] : 0.0
g isa Function && (g = g(t0))
v = g isa Number ? fill(Float64(g), n) : Float64.(vec(collect(g)))
length(v) == n || throw(DimensionMismatch("$key guess of length $(length(v)), $n $(key)s"))
length(v) == n ||
throw(DimensionMismatch("$key guess of length $(length(v)), $n $(key)s"))
return v
end

Expand Down Expand Up @@ -200,7 +201,9 @@ function prepare(
# states is the one of `state_components`, that of the OptimalControl model.
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)])
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)
Expand Down Expand Up @@ -360,7 +363,10 @@ function _trajectory(sol, pb)
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]
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
Expand Down
25 changes: 13 additions & 12 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,19 @@ function run_environment(
return true
end
# the rotation (#435): one process per problem, in order, while the budget lasts
outcomes, skipped =
TestSelection.run_within_budget(problem_names, budget.seconds) do p
problem_report = "$report-$p"
rm(joinpath(TestStatus.REPORTS, "$problem_report.md"); force=true)
cmd = `$julia --project=$project $script $tests $name $problem_report problem=$p $patterns`
outcome = TestSelection.run_process(cmd; timeout=budget.timeout)
outcome in (:crashed, :timeout) && TestStatus.write_process_report(
problem_report, p, name, outcome; timeout=budget.timeout
)
println("Problem $p: $outcome")
return outcome
end
outcomes, skipped = TestSelection.run_within_budget(
problem_names, budget.seconds
) do p
problem_report = "$report-$p"
rm(joinpath(TestStatus.REPORTS, "$problem_report.md"); force=true)
cmd = `$julia --project=$project $script $tests $name $problem_report problem=$p $patterns`
outcome = TestSelection.run_process(cmd; timeout=budget.timeout)
outcome in (:crashed, :timeout) && TestStatus.write_process_report(
problem_report, p, name, outcome; timeout=budget.timeout
)
println("Problem $p: $outcome")
return outcome
end
isempty(skipped) ||
println("Budget spent, left for a later run: ", join(skipped, ", "))
return all(o -> last(o) === :success, outcomes)
Expand Down
9 changes: 5 additions & 4 deletions test/suite/core/test_new.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ include(joinpath(TOOLS, "lint", "ProblemLint.jl"))
const N = NewProblem
const L = ProblemLint

failures(problem, root) = [
r.message for r in L.checks(L.source(problem; root)) if r.status === :fail
]
function failures(problem, root)
return [r.message for r in L.checks(L.source(problem; root)) if r.status === :fail]
end

# what the person writes in place of each `TODO`
function fill_todos(root)
Expand Down Expand Up @@ -65,7 +65,8 @@ function test_new()
Test.@test statuses(:foo, root) ==
(OptimalControl=:draft, JuMP=:valid, ModelingToolkit=:draft)
failed = failures(:foo, root)
Test.@test failed == ["nothing left to fill (`TODO`): model ModelingToolkit"]
Test.@test failed ==
["nothing left to fill (`TODO`): model ModelingToolkit"]
end
end

Expand Down
9 changes: 6 additions & 3 deletions tools/new/NewProblem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ end
function check_modelings(ms)
isempty(ms) && throw(ArgumentError("give at least one modeling, among $(modelings())"))
unknown = setdiff(ms, modelings())
isempty(unknown) ||
throw(ArgumentError("unknown modelings $(unknown), expected some of $(modelings())"))
isempty(unknown) || throw(
ArgumentError("unknown modelings $(unknown), expected some of $(modelings())")
)
allunique(ms) || throw(ArgumentError("a modeling is given twice: $(ms)"))
return nothing
end
Expand Down Expand Up @@ -98,7 +99,9 @@ function add(problem::Symbol, ms::AbstractVector{Symbol}; root::AbstractString=p
)
present = filter(m -> haskey(statuses, m) || isfile(model_path(root, problem, m)), ms)
isempty(present) || throw(ArgumentError("$(problem) has already $(present)"))
paths = [write_new(model_path(root, problem, m), template("$(m).jl", problem)) for m in ms]
paths = [
write_new(model_path(root, problem, m), template("$(m).jl", problem)) for m in ms
]
merge!(statuses, Dict(m => :draft for m in ms))
write(path, replace(text, found.match => status_entry(statuses)))
return [path; paths]
Expand Down
2 changes: 1 addition & 1 deletion tools/new/templates/metadata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
x_t0=(unit=nothing, meaning="initial value of x"),
x_u=(unit=nothing, meaning="upper bound of x"),
),
{{status}}
{{status}}
# initial guess of every modeling (#377), from the parameters
:init => p -> (state=p.x_t0, control=0.0),
)