Skip to content

Commit 21ec5ed

Browse files
handle user supplied lcons and ucons
1 parent dd58650 commit 21ec5ed

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

src/systems/optimization/optimizationsystem.jl

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map,
228228
else # use the user supplied variable bounds
229229
xor(isnothing(lb), isnothing(ub)) &&
230230
throw(ArgumentError("Expected both `lb` and `ub` to be supplied"))
231+
!isnothing(lb) && length(lb) != length(dvs) &&
232+
throw(ArgumentError("Expected both `lb` to be of the same length as the vector of optimization variables"))
233+
!isnothing(ub) && length(ub) != length(dvs) &&
234+
throw(ArgumentError("Expected both `ub` to be of the same length as the vector of optimization variables"))
231235
end
232236

233237
int = isintegervar.(dvs) .| isbinaryvar.(dvs)
@@ -241,10 +245,10 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map,
241245
lb = varmap_to_vars(dvs .=> lb, dvs; defaults = defs, tofloat = false, use_union)
242246
ub = varmap_to_vars(dvs .=> ub, dvs; defaults = defs, tofloat = false, use_union)
243247

244-
if !isnothing(lb) && all(lb .== -Inf)
248+
if !isnothing(lb) && all(lb .== -Inf)
245249
lb = nothing
246250
end
247-
if !isnothing(ub) && all(ub .== Inf)
251+
if !isnothing(ub) && all(ub .== Inf)
248252
ub = nothing
249253
end
250254

@@ -288,15 +292,25 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map,
288292

289293
if length(cstr) > 0
290294
@named cons_sys = ConstraintsSystem(cstr, dvs, ps)
291-
cons, lcons, ucons = generate_function(cons_sys, checkbounds = checkbounds,
292-
linenumbers = linenumbers,
293-
expression = Val{false})
295+
cons, lcons_, ucons_ = generate_function(cons_sys, checkbounds = checkbounds,
296+
linenumbers = linenumbers,
297+
expression = Val{false})
294298
cons_j = generate_jacobian(cons_sys; expression = Val{false}, sparse = sparse)[2]
295299
cons_h = generate_hessian(cons_sys; expression = Val{false}, sparse = sparse)[2]
296300

297301
cons_expr = toexpr.(subs_constants(constraints(cons_sys)))
298302
rep_pars_vals!.(cons_expr, Ref(pairs_arr))
299303

304+
if isnothing(lcons) && isnothing(ucons) # use the symbolically specified bounds
305+
lcons = lcons_
306+
ucons = ucons_
307+
else # use the user supplied variable bounds
308+
!isnothing(lcons) && length(lcons) != length(cstr) &&
309+
throw(ArgumentError("Expected both `lcons` to be of the same length as the vector of constraints"))
310+
!isnothing(ucons) && length(ucons) != length(cstr) &&
311+
throw(ArgumentError("Expected both `ucons` to be of the same length as the vector of constraints"))
312+
end
313+
300314
if sparse
301315
cons_jac_prototype = jacobian_sparsity(cons_sys)
302316
cons_hess_prototype = hessian_sparsity(cons_sys)
@@ -384,6 +398,10 @@ function OptimizationProblemExpr{iip}(sys::OptimizationSystem, u0,
384398
else # use the user supplied variable bounds
385399
xor(isnothing(lb), isnothing(ub)) &&
386400
throw(ArgumentError("Expected both `lb` and `ub` to be supplied"))
401+
!isnothing(lb) && length(lb) != length(dvs) &&
402+
throw(ArgumentError("Expected both `lb` to be of the same length as the vector of optimization variables"))
403+
!isnothing(ub) && length(ub) != length(dvs) &&
404+
throw(ArgumentError("Expected both `ub` to be of the same length as the vector of optimization variables"))
387405
end
388406

389407
int = isintegervar.(dvs) .| isbinaryvar.(dvs)
@@ -397,10 +415,10 @@ function OptimizationProblemExpr{iip}(sys::OptimizationSystem, u0,
397415
lb = varmap_to_vars(dvs .=> lb, dvs; defaults = defs, tofloat = false, use_union)
398416
ub = varmap_to_vars(dvs .=> ub, dvs; defaults = defs, tofloat = false, use_union)
399417

400-
if !isnothing(lb) && all(lb .== -Inf)
418+
if !isnothing(lb) && all(lb .== -Inf)
401419
lb = nothing
402420
end
403-
if !isnothing(ub) && all(ub .== Inf)
421+
if !isnothing(ub) && all(ub .== Inf)
404422
ub = nothing
405423
end
406424

@@ -439,15 +457,25 @@ function OptimizationProblemExpr{iip}(sys::OptimizationSystem, u0,
439457

440458
if length(cstr) > 0
441459
@named cons_sys = ConstraintsSystem(cstr, dvs, ps)
442-
cons, lcons, ucons = generate_function(cons_sys, checkbounds = checkbounds,
443-
linenumbers = linenumbers,
444-
expression = Val{false})
460+
cons, lcons_, ucons_ = generate_function(cons_sys, checkbounds = checkbounds,
461+
linenumbers = linenumbers,
462+
expression = Val{false})
445463
cons_j = generate_jacobian(cons_sys; expression = Val{false}, sparse = sparse)[2]
446464
cons_h = generate_hessian(cons_sys; expression = Val{false}, sparse = sparse)[2]
447465

448466
cons_expr = toexpr.(subs_constants(constraints(cons_sys)))
449467
rep_pars_vals!.(cons_expr, Ref(pairs_arr))
450468

469+
if isnothing(lcons) && isnothing(ucons) # use the symbolically specified bounds
470+
lcons = lcons_
471+
ucons = ucons_
472+
else # use the user supplied variable bounds
473+
!isnothing(lcons) && length(lcons) != length(cstr) &&
474+
throw(ArgumentError("Expected both `lcons` to be of the same length as the vector of constraints"))
475+
!isnothing(ucons) && length(ucons) != length(cstr) &&
476+
throw(ArgumentError("Expected both `ucons` to be of the same length as the vector of constraints"))
477+
end
478+
451479
if sparse
452480
cons_jac_prototype = jacobian_sparsity(cons_sys)
453481
cons_hess_prototype = hessian_sparsity(cons_sys)

0 commit comments

Comments
 (0)