Skip to content

Commit 8dc6356

Browse files
committed
Add validation for constrained optimization problems missing bounds
When a constrained optimization problem has constraints defined but is missing lcons or ucons bounds, provide a helpful error message instead of allowing it to fail with cryptic errors downstream. This addresses SciML/Optimization.jl#959 by adding the validation at the SciMLBase level so all optimization solvers benefit from it. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 0baa71f commit 8dc6356

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

src/solve.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ function _check_opt_alg(prob::OptimizationProblem, alg; kwargs...)
151151
throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) does not support constraints. Either remove the `cons` function passed to `OptimizationFunction` or use a different algorithm."))
152152
requiresconstraints(alg) && isnothing(prob.f.cons) &&
153153
throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) requires constraints, pass them with the `cons` kwarg in `OptimizationFunction`."))
154+
# Check that if constraints are present and the algorithm supports constraints, both lcons and ucons are provided
155+
allowsconstraints(alg) && !isnothing(prob.f.cons) && (isnothing(prob.lcons) || isnothing(prob.ucons)) &&
156+
throw(ArgumentError("Constrained optimization problem requires both `lcons` and `ucons` to be provided to OptimizationProblem. " *
157+
"Example: OptimizationProblem(optf, u0, p; lcons=[-Inf], ucons=[0.0])"))
154158
!allowscallback(alg) && haskey(kwargs, :callback) &&
155159
throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) does not support callbacks, remove the `callback` keyword argument from the `solve` call."))
156160
requiresgradient(alg) && !(prob.f isa AbstractOptimizationFunction) &&

0 commit comments

Comments
 (0)