Skip to content

Conversation

@ChrisRackauckas
Copy link
Member

Summary

  • Added validation check for lcons and ucons when a constrained optimization problem is defined
  • Provides a clear error message with an example when these required parameters are missing
  • Fixes the confusing "no method matching keys(::Nothing)" error

Test plan

  • Added test cases that verify the new error message is thrown for both LBFGS and AugLag solvers
  • Verified that optimization still works when lcons and ucons are properly provided
  • Ran tests locally to ensure no regressions

Fixes #959

🤖 Generated with Claude Code

When a constrained optimization problem is defined with `cons` but without
`lcons` and `ucons`, the error message was "no method matching keys(::Nothing)"
which was confusing. Now it provides a clear error message that explains the
missing parameters and includes an example.

Fixes #959

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@ChrisRackauckas
Copy link
Member Author

Implementation Details

Problem Analysis

The issue occurred when users provided a constrained optimization problem (with cons function) but forgot to specify the constraint bounds (lcons and ucons). The code attempted to access these missing parameters, resulting in the unhelpful error: MethodError: no method matching keys(::Nothing).

Solution

Added validation checks in both lbfgsb.jl and auglag.jl that:

  1. Check if cache.f.cons is not nothing (i.e., constraints are defined)
  2. Then check if either cache.lcons or cache.ucons is nothing
  3. If so, throw a clear ArgumentError with:
    • Explanation of what's missing
    • Example of how to properly provide the parameters

Files Changed

  • src/lbfgsb.jl: Added validation at line 101-104
  • src/auglag.jl: Added validation at line 83-86
  • test/native.jl: Added comprehensive test cases to verify the fix

Test Results

The new tests verify that:

  • Both LBFGS and AugLag throw the helpful error message when constraint bounds are missing
  • The error message contains the expected keywords (lcons, ucons, OptimizationProblem, Example)
  • Optimization still works correctly when bounds are properly provided

The fix has been tested locally and all tests pass.

Process Notes

  1. Searched codebase to understand where the error occurred
  2. Found similar patterns in other solver files for reference
  3. Implemented consistent error checking in both affected files
  4. Added comprehensive tests following the existing test structure
  5. Formatted code with JuliaFormatter

Following user feedback, the constraint validation is now done uniformly
across all solvers through a common utility function `_check_constrained_problem`
in utils.jl. This ensures consistent error messages and behavior across all
constraint-supporting solvers.

Changes:
- Added _check_constrained_problem utility function in utils.jl
- Updated LBFGS and AugLag to use the utility function
- Added validation to all other constraint-supporting solvers:
  - OptimizationNLopt
  - OptimizationOptimJL (IPNewton)
  - OptimizationPRIMA (COBYLA, LINCOA)
  - OptimizationEvolutionary
  - OptimizationSciPy (ScipyMinimize, ScipyShgo)
  - OptimizationMOI (NLP)
  - OptimizationNOMAD

This provides a more maintainable solution that ensures all solvers
give the same helpful error message when constraint bounds are missing.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@ChrisRackauckas
Copy link
Member Author

@ChrisRackauckas I've updated the PR based on your feedback. The constraint validation is now done uniformly across all solvers using a common utility function.

Changes made:

  1. Created a _check_constrained_problem utility function in utils.jl that performs the validation
  2. Updated LBFGS and AugLag to use this utility function instead of duplicating the validation code
  3. Added the same validation to all other constraint-supporting solvers:
    • OptimizationNLopt
    • OptimizationOptimJL (IPNewton)
    • OptimizationPRIMA (COBYLA, LINCOA)
    • OptimizationEvolutionary
    • OptimizationSciPy (ScipyMinimize, ScipyShgo)
    • OptimizationMOI (NLP)
    • OptimizationNOMAD

This ensures that all constraint-supporting solvers will give the same helpful error message when users forget to provide lcons and ucons.

The tests still pass and the error message remains the same helpful one:

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

@ChrisRackauckas
Copy link
Member Author

no this should be on the higher level solve dispatch

@ChrisRackauckas
Copy link
Member Author

@ChrisRackauckas You're absolutely right - this should be in SciMLBase.jl for a truly uniform solution.

I've created a PR in SciMLBase.jl that adds the validation there: SciML/SciMLBase.jl#1080

The validation is now added to the _check_opt_alg function in SciMLBase, which is called by both solve and init for all optimization problems. This ensures that:

  1. All optimization solvers automatically get this validation
  2. The check happens early in the solve process
  3. No individual solver packages need to implement this validation

Once the SciMLBase PR is merged, we can close this PR in Optimization.jl since the fix will be at the appropriate level in the stack.

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

3 participants