Commit 57bcc04
Fix NLopt crash with gradient-based algorithms when no AD backend specified
This fixes the issue reported in https://discourse.julialang.org/t/error-when-using-multistart-optimization/133174
## Problem
When using NLopt's gradient-based algorithms (like LD_LBFGS) without specifying
an AD backend in OptimizationFunction, the code would crash with:
`MethodError: objects of type Nothing are not callable`
This occurred because the NLopt wrapper tried to call `cache.f.grad(G, θ)` at
line 181, but `cache.f.grad` was `nothing` when no AD backend was specified.
## Solution
Added a check in the `__solve` method to verify that if the algorithm requires
gradients, `cache.f.grad` is not `nothing`. If it is `nothing`, we now throw
a helpful `IncompatibleOptimizerError` that guides users to:
1. Use `OptimizationFunction` with an AD backend (e.g., `AutoForwardDiff()`)
2. Or provide gradients manually via the `grad` kwarg
## Changes
1. **OptimizationNLopt.jl**: Added gradient availability check before attempting
to use gradients, providing a clear error message for users
2. **runtests.jl**: Added comprehensive tests to verify:
- Error is thrown when gradient-based algorithms are used without AD
- Error is thrown with both `NLopt.LD_LBFGS()` and `NLopt.Opt(:LD_LBFGS, 2)`
- Gradient-free algorithms still work without AD backend
- Gradient-based algorithms work correctly when AD is provided
3. **multistartoptimization.md**: Fixed documentation example to include AD backend
## Test Results
All tests pass, including the new test that reproduces the discourse issue.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>1 parent 6fe78dc commit 57bcc04
File tree
3 files changed
+45
-3
lines changed- docs/src/optimization_packages
- lib/OptimizationNLopt
- src
- test
3 files changed
+45
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
73 | | - | |
| 74 | + | |
| 75 | + | |
74 | 76 | | |
75 | 77 | | |
76 | 78 | | |
| |||
167 | 169 | | |
168 | 170 | | |
169 | 171 | | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
170 | 181 | | |
171 | 182 | | |
172 | 183 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
174 | 174 | | |
175 | 175 | | |
176 | 176 | | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
177 | 208 | | |
0 commit comments