Skip to content

Commit

Permalink
Merge pull request #3140 from AayushSabharwal/as/optsys-fixes
Browse files Browse the repository at this point in the history
fix: respect bounds in `modelingtoolkitize`, make bounded variable irreducible
  • Loading branch information
ChrisRackauckas authored Oct 23, 2024
2 parents b7c9316 + 2a3e023 commit 57dcc7e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/systems/optimization/modelingtoolkitize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ function modelingtoolkitize(prob::DiffEqBase.OptimizationProblem;
end
_vars = reshape(_vars, size(prob.u0))
vars = ArrayInterface.restructure(prob.u0, _vars)
if prob.ub !== nothing # lb is also !== nothing
vars = map(vars, prob.lb, prob.ub) do sym, lb, ub
if iszero(lb) && iszero(ub) || isinf(lb) && lb < 0 && isinf(ub) && ub > 0
sym
else
Symbolics.setmetadata(sym, VariableBounds, (lb, ub))
end
end
end
params = if has_p
if p_names === nothing && SciMLBase.has_sys(prob.f)
p_names = Dict(parameter_index(prob.f.sys, sym) => sym
Expand Down
14 changes: 13 additions & 1 deletion src/systems/optimization/optimizationsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ function OptimizationSystem(op, unknowns, ps;
ps′ = value.(ps)
op′ = value(scalarize(op))

irreducible_subs = Dict()
for i in eachindex(unknowns′)
var = unknowns′[i]
if hasbounds(var)
irreducible_subs[var] = irrvar = setirreducible(var, true)
unknowns′[i] = irrvar
end
end
op′ = substitute(op′, irreducible_subs)
constraints = substitute.(constraints, (irreducible_subs,))

if !(isempty(default_u0) && isempty(default_p))
Base.depwarn(
"`default_u0` and `default_p` are deprecated. Use `defaults` instead.",
Expand All @@ -113,7 +124,8 @@ function OptimizationSystem(op, unknowns, ps;
throw(ArgumentError("System names must be unique."))
end
defaults = todict(defaults)
defaults = Dict(value(k) => value(v)
defaults = Dict(substitute(value(k), irreducible_subs) => substitute(
value(v), irreducible_subs)
for (k, v) in pairs(defaults) if value(v) !== nothing)

var_to_name = Dict()
Expand Down
1 change: 1 addition & 0 deletions src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ isoutput(x) = isvarkind(VariableOutput, x)
# Before the solvability check, we already have handled IO variables, so
# irreducibility is independent from IO.
isirreducible(x) = isvarkind(VariableIrreducible, x)
setirreducible(x, v) = setmetadata(x, VariableIrreducible, v)
state_priority(x) = convert(Float64, getmetadata(x, VariableStatePriority, 0.0))::Float64

function default_toterm(x)
Expand Down
10 changes: 10 additions & 0 deletions test/modelingtoolkitize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ sol = solve(prob, BFGS())
sol = solve(prob, Newton())
@test sol.objective < 1e-8

prob = OptimizationProblem(ones(3); lb = [-Inf, 0.0, 1.0], ub = [Inf, 0.0, 2.0]) do u, p
sum(abs2, u)
end

sys = complete(modelingtoolkitize(prob))
@test !ModelingToolkit.hasbounds(unknowns(sys)[1])
@test !ModelingToolkit.hasbounds(unknowns(sys)[2])
@test ModelingToolkit.hasbounds(unknowns(sys)[3])
@test ModelingToolkit.getbounds(unknowns(sys)[3]) == (1.0, 2.0)

## SIR System Regression Test

β = 0.01# infection rate
Expand Down
14 changes: 13 additions & 1 deletion test/optimizationsystem.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using ModelingToolkit, SparseArrays, Test, Optimization, OptimizationOptimJL,
OptimizationMOI, Ipopt, AmplNLWriter, Ipopt_jll
OptimizationMOI, Ipopt, AmplNLWriter, Ipopt_jll, SymbolicIndexingInterface
using ModelingToolkit: get_metadata

@testset "basic" begin
Expand Down Expand Up @@ -347,3 +347,15 @@ end
prob = @test_nowarn OptimizationProblem(sys, nothing)
@test_nowarn solve(prob, NelderMead())
end

@testset "Bounded unknowns are irreducible" begin
@variables x
@variables y [bounds = (-Inf, Inf)]
@variables z [bounds = (1.0, 2.0)]
obj = x^2 + y^2 + z^2
cons = [y ~ 2x
z ~ 2y]
@mtkbuild sys = OptimizationSystem(obj, [x, y, z], []; constraints = cons)
@test is_variable(sys, z)
@test !is_variable(sys, y)
end

0 comments on commit 57dcc7e

Please sign in to comment.