# Bypassing Forced Termination in Nlopt

So, I am using Nlopt to minimize a nonlinear optimization problem, and for awhile I have been running the code and I will get zero as my return value after 2 iterations, with Nlopt showing “Forced_Stop”

I just read that Nlopt has a built-in forced_stop trigger when the optimization reaches zero.

However I am looking for a negative-valued minimum, so I’d like to bypass this trigger. How should I do this? I have seen in the documentation some explanation of how to do this but it’s not as clearly laid out in the github page and I’m having some trouble, any help would be appreciated.

I have been working on this code for awhile and I cannot find any other mistake that would cause the optimizer to stop at zero after 2 iterations. I can change a lot about my function and I’ll still get the same result, through every measure I take. I also haven’t been able to configure a good working example that’s short enough to test but also indicative of my problem, but I don’t want to post the whole code if I can avoid it.

what algorithm are you using? it happened to me a lot when using NLopt and optimizers requiring gradients (like SLSQP), but i didn´t found such restriction (similar as you, iterative optimizations until finding a negative value)

No it doesn’t, unless you set `opt.stopval = 0`. Where did you read that?

Here is an example minimizing the function f(x) = (x_1-1)^2 - 2, which has its minimum `f([1]) == -2`:

``````function f(x, grad)
end
return (x[1]-1)^2 - 2
end
opt = Opt(:LD_SLSQP, 1)
opt.min_objective = f
opt.xtol_rel = 1e-4
optimize(opt, [8.3])
``````

The output is `(-2.0, [1.0], :XTOL_REACHED)`, i.e. it correctly found the (negative) minimum of `-2.0`. It also works fine for algorithms that don’t use the derivative, like `:LN_BOBYQA`.

(The most common reason for an NLopt algorithm to “give up” prematurely is if you are using a gradient-based algorithm and your derivative is inaccurate.)

2 Likes

Thank you for the example, it works for me as well and minimizes to a negative number.

If you scroll down a little it says:

“The force-stop value is reset to zero at the beginning of `nlopt_optimize` . Passing `val=0` to `nlopt_set_force_stop` tells NLopt not to force a halt”

I have tried most of the offered algorithms and I either get zero or infinity after 2 iterations with Nlopt passing Forced_Stop. I don’t expect much help since I haven’t provided a working example, but thank you guys anyway.

That documentation is about the `nlopt_set_force_stop` function and has nothing to do with the return value of your objective.

(The forced-stop functionality was mainly implemented in NLopt to provide languages with exceptions a clean way to exit when exceptions are thrown. It is used internally in NLopt.jl for dealing with Julia exceptions. Most people shouldn’t be calling the forced-stop API directly.)

3 Likes

Got it, thank you for clearing that up.