Skip to content

Commit 0c0a1df

Browse files
avoid breaking change
1 parent 682f74e commit 0c0a1df

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

src/systems/optimization/optimizationsystem.jl

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ function rep_pars_vals!(e, p) end
188188
```julia
189189
function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem,u0map,
190190
parammap=DiffEqBase.NullParameters();
191-
lb=nothing, ub=nothing,
192191
grad = false,
193192
hess = false, sparse = false,
194193
checkbounds = false,
@@ -211,11 +210,6 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map,
211210
linenumbers = true, parallel = SerialForm(),
212211
use_union = false,
213212
kwargs...) where {iip}
214-
if !(isnothing(lb) && isnothing(ub))
215-
Base.depwarn("`lb` and `ub` are deprecated. Use the `bounds` metadata for the variables instead.",
216-
:OptimizationProblem, force = true)
217-
end
218-
219213
if haskey(kwargs, :lcons) || haskey(kwargs, :ucons)
220214
Base.depwarn("`lcons` and `ucons` are deprecated. Specify constraints directly instead.",
221215
:OptimizationProblem, force = true)
@@ -225,11 +219,16 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map,
225219
ps = parameters(sys)
226220
cstr = constraints(sys)
227221

228-
lb = first.(getbounds.(dvs))
229-
ub = last.(getbounds.(dvs))
222+
if isnothing(lb) && isnothing(ub) # use the symbolically specified bounds
223+
lb = first.(getbounds.(dvs))
224+
ub = last.(getbounds.(dvs))
225+
lb[isbinaryvar.(dvs)] .= 0
226+
ub[isbinaryvar.(dvs)] .= 1
227+
else # use the user supplied variable bounds
228+
xor(isnothing(lb), isnothing(ub)) && throw(ArgumentError("Expected both `lb` and `ub` to be supplied"))
229+
end
230+
230231
int = isintegervar.(dvs) .| isbinaryvar.(dvs)
231-
lb[isbinaryvar.(dvs)] .= 0
232-
ub[isbinaryvar.(dvs)] .= 1
233232

234233
defs = defaults(sys)
235234
defs = mergedefaults(defs, parammap, ps)
@@ -240,7 +239,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map,
240239
lb = varmap_to_vars(dvs .=> lb, dvs; defaults = defs, tofloat = false, use_union)
241240
ub = varmap_to_vars(dvs .=> ub, dvs; defaults = defs, tofloat = false, use_union)
242241

243-
if all(lb .== -Inf) && all(ub .== Inf)
242+
if !isnothing(lb) && all(lb .== -Inf) && !isnothing(ub) && all(ub .== Inf)
244243
lb = nothing
245244
ub = nothing
246245
end
@@ -337,7 +336,7 @@ end
337336
```julia
338337
function DiffEqBase.OptimizationProblemExpr{iip}(sys::OptimizationSystem,
339338
parammap=DiffEqBase.NullParameters();
340-
u0=nothing, lb=nothing, ub=nothing,
339+
u0=nothing,
341340
grad = false,
342341
hes = false, sparse = false,
343342
checkbounds = false,
@@ -364,11 +363,6 @@ function OptimizationProblemExpr{iip}(sys::OptimizationSystem, u0,
364363
linenumbers = false, parallel = SerialForm(),
365364
use_union = false,
366365
kwargs...) where {iip}
367-
if !(isnothing(lb) && isnothing(ub))
368-
Base.depwarn("`lb` and `ub` are deprecated. Use the `bounds` metadata for the variables instead.",
369-
:OptimizationProblem, force = true)
370-
end
371-
372366
if haskey(kwargs, :lcons) || haskey(kwargs, :ucons)
373367
Base.depwarn("`lcons` and `ucons` are deprecated. Specify constraints directly instead.",
374368
:OptimizationProblem, force = true)
@@ -378,6 +372,31 @@ function OptimizationProblemExpr{iip}(sys::OptimizationSystem, u0,
378372
ps = parameters(sys)
379373
cstr = constraints(sys)
380374

375+
if isnothing(lb) && isnothing(ub) # use the symbolically specified bounds
376+
lb = first.(getbounds.(dvs))
377+
ub = last.(getbounds.(dvs))
378+
lb[isbinaryvar.(dvs)] .= 0
379+
ub[isbinaryvar.(dvs)] .= 1
380+
else # use the user supplied variable bounds
381+
xor(isnothing(lb), isnothing(ub)) && throw(ArgumentError("Expected both `lb` and `ub` to be supplied"))
382+
end
383+
384+
int = isintegervar.(dvs) .| isbinaryvar.(dvs)
385+
386+
defs = defaults(sys)
387+
defs = mergedefaults(defs, parammap, ps)
388+
defs = mergedefaults(defs, u0map, dvs)
389+
390+
u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = false)
391+
p = varmap_to_vars(parammap, ps; defaults = defs, tofloat = false, use_union)
392+
lb = varmap_to_vars(dvs .=> lb, dvs; defaults = defs, tofloat = false, use_union)
393+
ub = varmap_to_vars(dvs .=> ub, dvs; defaults = defs, tofloat = false, use_union)
394+
395+
if !isnothing(lb) && all(lb .== -Inf) && !isnothing(ub) && all(ub .== Inf)
396+
lb = nothing
397+
ub = nothing
398+
end
399+
381400
idx = iip ? 2 : 1
382401
f = generate_function(sys, checkbounds = checkbounds, linenumbers = linenumbers,
383402
expression = Val{true})
@@ -402,26 +421,6 @@ function OptimizationProblemExpr{iip}(sys::OptimizationSystem, u0,
402421
hess_prototype = nothing
403422
end
404423

405-
lb = first.(getbounds.(dvs))
406-
ub = last.(getbounds.(dvs))
407-
int = isintegervar.(dvs) .| isbinaryvar.(dvs)
408-
lb[isbinaryvar.(dvs)] .= 0
409-
ub[isbinaryvar.(dvs)] .= 1
410-
411-
defs = defaults(sys)
412-
defs = mergedefaults(defs, parammap, ps)
413-
defs = mergedefaults(defs, u0map, dvs)
414-
415-
u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = false)
416-
p = varmap_to_vars(parammap, ps; defaults = defs, tofloat = false, use_union)
417-
lb = varmap_to_vars(dvs .=> lb, dvs; defaults = defs, tofloat = false, use_union)
418-
ub = varmap_to_vars(dvs .=> ub, dvs; defaults = defs, tofloat = false, use_union)
419-
420-
if all(lb .== -Inf) && all(ub .== Inf)
421-
lb = nothing
422-
ub = nothing
423-
end
424-
425424
obj_expr = toexpr(subs_constants(objective(sys)))
426425
pairs_arr = if p isa SciMLBase.NullParameters
427426
[Symbol(_s) => Expr(:ref, :x, i) for (i, _s) in enumerate(dvs)]

0 commit comments

Comments
 (0)