Skip to content

Conversation

@ChrisRackauckas
Copy link
Member

Description

This PR adds validation to ensure that constrained optimization problems have both lcons and ucons bounds defined when constraints are present. This addresses issue SciML/Optimization.jl#959.

Problem

When users forget to provide lcons and ucons for a constrained optimization problem, they get unhelpful errors like:

MethodError: no method matching keys(::Nothing)

Solution

Added a check in the _check_opt_alg function that validates:

  • If the algorithm supports constraints (allowsconstraints(alg))
  • AND constraints are present (\!isnothing(prob.f.cons))
  • AND either lcons or ucons is missing
  • THEN throw a helpful ArgumentError with an example

Example Error Message

Constrained optimization problem requires both `lcons` and `ucons` to be provided to OptimizationProblem. Example: OptimizationProblem(optf, u0, p; lcons=[-Inf], ucons=[0.0])

Benefits

This validation at the SciMLBase level ensures all optimization solvers benefit from the improved error message, providing a uniform experience across the ecosystem.

Related

🤖 Generated with Claude Code

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]>
@ChrisRackauckas ChrisRackauckas merged commit 1381d10 into SciML:master Jul 28, 2025
55 of 63 checks passed
@ChrisRackauckas ChrisRackauckas deleted the fix-constrained-optimization-validation branch July 28, 2025 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

no method matching keys(::Nothing) when I forget to pass lcons and ucons to a constrained problem

2 participants