Skip to content

Commit ab2c452

Browse files
Merge pull request #2323 from SciML/optsysfix
Keep symbolic expressions as is and minor bugfixes optimization system
2 parents c10d206 + 9699028 commit ab2c452

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

src/systems/optimization/modelingtoolkitize.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function modelingtoolkitize(prob::DiffEqBase.OptimizationProblem; kwargs...)
2626
if !isnothing(prob.lcons)
2727
for i in 1:num_cons
2828
if !isinf(prob.lcons[i])
29-
if prob.lcons[i] != prob.ucons[i] &&
29+
if prob.lcons[i] != prob.ucons[i]
3030
push!(cons, prob.lcons[i] lhs[i])
3131
else
3232
push!(cons, lhs[i] ~ prob.ucons[i])

src/systems/optimization/optimizationsystem.jl

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,6 @@ end
196196

197197
hessian_sparsity(sys::OptimizationSystem) = hessian_sparsity(get_op(sys), states(sys))
198198

199-
function rep_pars_vals!(e::Expr, p)
200-
rep_pars_vals!.(e.args, Ref(p))
201-
replace!(e.args, p...)
202-
end
203-
204-
function rep_pars_vals!(e, p) end
205-
206199
"""
207200
```julia
208201
DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map,
@@ -275,14 +268,8 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map,
275268
f = generate_function(sys, checkbounds = checkbounds, linenumbers = linenumbers,
276269
expression = Val{false})
277270

278-
obj_expr = toexpr(subs_constants(objective(sys)))
279-
pairs_arr = if p isa SciMLBase.NullParameters
280-
[Symbol(_s) => Expr(:ref, :x, i) for (i, _s) in enumerate(dvs)]
281-
else
282-
vcat([Symbol(_s) => Expr(:ref, :x, i) for (i, _s) in enumerate(dvs)],
283-
[Symbol(_p) => p[i] for (i, _p) in enumerate(ps)])
284-
end
285-
rep_pars_vals!(obj_expr, pairs_arr)
271+
obj_expr = subs_constants(objective(sys))
272+
286273
if grad
287274
grad_oop, grad_iip = generate_gradient(sys, checkbounds = checkbounds,
288275
linenumbers = linenumbers,
@@ -342,14 +329,13 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map,
342329
else
343330
_cons_h = nothing
344331
end
345-
cons_expr = toexpr.(subs_constants(constraints(cons_sys)))
346-
rep_pars_vals!.(cons_expr, Ref(pairs_arr))
332+
cons_expr = subs_constants(constraints(cons_sys))
347333

348334
if !haskey(kwargs, :lcons) && !haskey(kwargs, :ucons) # use the symbolically specified bounds
349335
lcons = lcons_
350336
ucons = ucons_
351337
else # use the user supplied constraints bounds
352-
haskey(kwargs, :lcons) && haskey(kwargs, :ucons) &&
338+
(haskey(kwargs, :lcons) haskey(kwargs, :ucons)) &&
353339
throw(ArgumentError("Expected both `ucons` and `lcons` to be supplied"))
354340
haskey(kwargs, :lcons) && length(kwargs[:lcons]) != length(cstr) &&
355341
throw(ArgumentError("Expected `lcons` to be of the same length as the vector of constraints"))
@@ -527,7 +513,7 @@ function OptimizationProblemExpr{iip}(sys::OptimizationSystem, u0map,
527513
lcons = lcons_
528514
ucons = ucons_
529515
else # use the user supplied constraints bounds
530-
!haskey(kwargs, :lcons) && !haskey(kwargs, :ucons) &&
516+
(haskey(kwargs, :lcons) haskey(kwargs, :ucons)) &&
531517
throw(ArgumentError("Expected both `ucons` and `lcons` to be supplied"))
532518
haskey(kwargs, :lcons) && length(kwargs[:lcons]) != length(cstr) &&
533519
throw(ArgumentError("Expected `lcons` to be of the same length as the vector of constraints"))

test/optimizationsystem.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,16 @@ end
283283

284284
@test sol1.u sol2.u
285285
end
286+
287+
@testset "#2323 keep symbolic expressions and xor condition on constraint bounds" begin
288+
@variables x y
289+
@parameters a b
290+
loss = (a - x)^2 + b * (y - x^2)^2
291+
@named sys = OptimizationSystem(loss, [x, y], [a, b], constraints = [x^2 + y^2 0.0])
292+
@test_throws ArgumentError OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0], lcons = [0.0])
293+
@test_throws ArgumentError OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0], ucons = [0.0])
294+
295+
prob = OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0])
296+
@test prob.f.expr isa Symbolics.Symbolic
297+
@test all(prob.f.cons_expr[i].lhs isa Symbolics.Symbolic for i in 1:length(prob.f.cons_expr))
298+
end

0 commit comments

Comments
 (0)