From 8dc6356c75ee03968341931494723629ac6b1523 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 28 Jul 2025 18:04:52 -0400 Subject: [PATCH] Add validation for constrained optimization problems missing bounds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/solve.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/solve.jl b/src/solve.jl index d2ee37674..1c02f4a28 100644 --- a/src/solve.jl +++ b/src/solve.jl @@ -151,6 +151,10 @@ function _check_opt_alg(prob::OptimizationProblem, alg; kwargs...) throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) does not support constraints. Either remove the `cons` function passed to `OptimizationFunction` or use a different algorithm.")) requiresconstraints(alg) && isnothing(prob.f.cons) && throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) requires constraints, pass them with the `cons` kwarg in `OptimizationFunction`.")) + # Check that if constraints are present and the algorithm supports constraints, both lcons and ucons are provided + allowsconstraints(alg) && !isnothing(prob.f.cons) && (isnothing(prob.lcons) || isnothing(prob.ucons)) && + throw(ArgumentError("Constrained optimization problem requires both `lcons` and `ucons` to be provided to OptimizationProblem. " * + "Example: OptimizationProblem(optf, u0, p; lcons=[-Inf], ucons=[0.0])")) !allowscallback(alg) && haskey(kwargs, :callback) && throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) does not support callbacks, remove the `callback` keyword argument from the `solve` call.")) requiresgradient(alg) && !(prob.f isa AbstractOptimizationFunction) &&