From c4a07eb27a55b8cbcca11261e706805295bec7cb Mon Sep 17 00:00:00 2001 From: ocots <66357348+ocots@users.noreply.github.com> Date: Mon, 23 Nov 2026 00:35:18 +0000 Subject: [PATCH] Format .jl files --- ext/MetaData/mountain_car.jl | 7 +++++- ext/ModelingToolkitModels/beam.jl | 4 +-- ext/ModelingToolkitModels/bioreactor.jl | 4 +-- .../double_oscillator.jl | 4 +-- ext/ModelingToolkitModels/electric_vehicle.jl | 6 ++--- ext/ModelingToolkitModels/mountain_car.jl | 4 +-- test/common/continuous_modelingtoolkit.jl | 21 ++++++++++------ test/common/solve_modelingtoolkit.jl | 12 ++++++--- test/runtests.jl | 25 ++++++++++--------- test/suite/core/test_new.jl | 9 ++++--- tools/new/NewProblem.jl | 9 ++++--- tools/new/templates/metadata.jl | 2 +- 12 files changed, 59 insertions(+), 48 deletions(-) diff --git a/ext/MetaData/mountain_car.jl b/ext/MetaData/mountain_car.jl index db712c035..a8e33ea3a 100644 --- a/ext/MetaData/mountain_car.jl +++ b/ext/MetaData/mountain_car.jl @@ -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), diff --git a/ext/ModelingToolkitModels/beam.jl b/ext/ModelingToolkitModels/beam.jl index 4ec234984..13329f125 100644 --- a/ext/ModelingToolkitModels/beam.jl +++ b/ext/ModelingToolkitModels/beam.jl @@ -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) diff --git a/ext/ModelingToolkitModels/bioreactor.jl b/ext/ModelingToolkitModels/bioreactor.jl index 1f4ce27c3..05a5d28e9 100644 --- a/ext/ModelingToolkitModels/bioreactor.jl +++ b/ext/ModelingToolkitModels/bioreactor.jl @@ -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) diff --git a/ext/ModelingToolkitModels/double_oscillator.jl b/ext/ModelingToolkitModels/double_oscillator.jl index 0fc4a0de6..ac5bad526 100644 --- a/ext/ModelingToolkitModels/double_oscillator.jl +++ b/ext/ModelingToolkitModels/double_oscillator.jl @@ -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) diff --git a/ext/ModelingToolkitModels/electric_vehicle.jl b/ext/ModelingToolkitModels/electric_vehicle.jl index 303c50413..580e14f97 100644 --- a/ext/ModelingToolkitModels/electric_vehicle.jl +++ b/ext/ModelingToolkitModels/electric_vehicle.jl @@ -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) @@ -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] diff --git a/ext/ModelingToolkitModels/mountain_car.jl b/ext/ModelingToolkitModels/mountain_car.jl index 7578b0f5c..cb3bba819 100644 --- a/ext/ModelingToolkitModels/mountain_car.jl +++ b/ext/ModelingToolkitModels/mountain_car.jl @@ -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) diff --git a/test/common/continuous_modelingtoolkit.jl b/test/common/continuous_modelingtoolkit.jl index 44ace656d..19651ff2c 100644 --- a/test/common/continuous_modelingtoolkit.jl +++ b/test/common/continuous_modelingtoolkit.jl @@ -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) @@ -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 @@ -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), diff --git a/test/common/solve_modelingtoolkit.jl b/test/common/solve_modelingtoolkit.jl index bc5ac97d3..25b60012a 100644 --- a/test/common/solve_modelingtoolkit.jl +++ b/test/common/solve_modelingtoolkit.jl @@ -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 @@ -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) @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 5c226b6ba..99f7e0319 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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) diff --git a/test/suite/core/test_new.jl b/test/suite/core/test_new.jl index b48ce6af1..e23cb37f7 100644 --- a/test/suite/core/test_new.jl +++ b/test/suite/core/test_new.jl @@ -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) @@ -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 diff --git a/tools/new/NewProblem.jl b/tools/new/NewProblem.jl index bf6616b19..190f9878e 100644 --- a/tools/new/NewProblem.jl +++ b/tools/new/NewProblem.jl @@ -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 @@ -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] diff --git a/tools/new/templates/metadata.jl b/tools/new/templates/metadata.jl index 7127df9de..62c9a07cd 100644 --- a/tools/new/templates/metadata.jl +++ b/tools/new/templates/metadata.jl @@ -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), )