# NLopt :FORCED\_STOP when objective function has wrong signature (missing gradient arg)

**URL:** https://discourse.julialang.org/t/nlopt-forced-stop-when-objective-function-has-wrong-signature-missing-gradient-arg/82742
**Category:** Optimization (Mathematical)
**Tags:** nlopt
**Created:** [June 14, 2022, 12:03pm UTC](https://discourse.julialang.org/t/nlopt-forced-stop-when-objective-function-has-wrong-signature-missing-gradient-arg/82742 "2022-06-14T12:03:02Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)
#### Post date: [June 14, 2022, 12:03pm UTC](https://discourse.julialang.org/t/nlopt-forced-stop-when-objective-function-has-wrong-signature-missing-gradient-arg/82742/1 "2022-06-14T12:03:02Z")

</div>

Hello,

I just faced a small usability issue using [NLopt.jl](https://github.com/JuliaOpt/NLopt.jl/). I believe the issue is common enough (e.g. forum post from Oct 2020 [https://discourse.julialang.org/t/nlopt-forced-stop](https://discourse.julialang.org/t/nlopt-forced-stop)) that it may suggest a better error catching.

To give a bit of background, I had some prior experience with NLopt last year, then a long pause and then yesterday I was studying a fresh optimization problem from scratch (not starting from a working code template). So I was partly a NLopt newcomer. And I get stuck with a `:FORCED_STOP` return code.

I took me some hours of instrumenting my cost functions (with prints, logging of parameters values in a global array…) to find the silly cause of the error. Here is a simplified version of my problem which shows the issue: a seemingly innocent simple unconstrained **derivative-free** optimization problem:

```julia
using NLopt

function obj_badsig(x)
    J = sum(x.^2)
    return J
end

opt = Opt(:LN_NELDERMEAD, nx)

x0 = [1., 1., 1.]
xmax = [2., 2., 2.]

opt.lower_bounds = -xmax
opt.upper_bounds = xmax

opt.min_objective = obj_badsig
opt.xtol_rel = 1e-4

(minf,minx,ret) = optimize(opt, x0)

```

This yields `(0.0, [1.0, 1.0, 1.0], :FORCED_STOP)`. And in fact the objective function is never called.

And the solution is to reread the [NLopt.jl](https://github.com/JuliaOpt/NLopt.jl/) README with better eyes: a gradient parameter is required, even when working with gradient-free algorithms:

```julia
function obj(x::Vector, grad::Vector)
    J = sum(x.^2)
    return J
end

```

which works.

So my question is: is there a better way for NLopt.jl to catch wrong signatures of objective (and constraint) function to report a more informative error message than `:FORCED_STOP` for absent-minded users like me?

---

<div class="post-metadata">

### Author: ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)
#### Post date: [June 21, 2022, 11:41am UTC](https://discourse.julialang.org/t/nlopt-forced-stop-when-objective-function-has-wrong-signature-missing-gradient-arg/82742/2 "2022-06-21T11:41:10Z")

</div>

Hello @stevengj, do you believe I should open an issue on this topic of uncatched wrong signature? Or is it just a matter of requiring users to read `NLopt.jl`’s doc more carefully?

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [June 21, 2022, 9:00pm UTC](https://discourse.julialang.org/t/nlopt-forced-stop-when-objective-function-has-wrong-signature-missing-gradient-arg/82742/3 "2022-06-21T21:00:01Z")

</div>

Related:

- [Exception thrown by objective function is not rethrown by nlopt · Issue #156 · JuliaOpt/NLopt.jl · GitHub](https://github.com/JuliaOpt/NLopt.jl/issues/156)
- [Fix #156, exceptions from FORCED\_STOP not rethrown by ikirill · Pull Request #157 · JuliaOpt/NLopt.jl · GitHub](https://github.com/JuliaOpt/NLopt.jl/pull/157)

Nlopt catches all errors and throws FORCED\_STOP. The right fix would be to communicate what error caused the FORCED\_STOP.
