# Bypassing Forced Termination in Nlopt

**URL:** <https://discourse.julialang.org/t/bypassing-forced-termination-in-nlopt/24224>\
**Category:** General Usage\
**Tags:** optimization\
**Created:** [May 14, 2019, 11:33pm UTC](https://discourse.julialang.org/t/bypassing-forced-termination-in-nlopt/24224 "2019-05-14T23:33:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)\
**Post date:** [May 14, 2019, 11:33pm UTC](https://discourse.julialang.org/t/bypassing-forced-termination-in-nlopt/24224/1 "2019-05-14T23:33:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [May 15, 2019, 12:26am UTC](https://discourse.julialang.org/t/bypassing-forced-termination-in-nlopt/24224/2 "2019-05-15T00:26:36Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 15, 2019, 2:02am UTC](https://discourse.julialang.org/t/bypassing-forced-termination-in-nlopt/24224/3 "2019-05-15T02:02:56Z")

</div>

> [@CPPhysics](#):
>
> I just read that Nlopt has a built-in forced\_stop trigger when the optimization reaches zero.

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`:

```julia
function f(x, grad)
    if length(grad) > 0
         grad[1] = 2*(x[1]-1)
    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.)

---

<div class="post-metadata">

**Author:** ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)\
**Post date:** [May 15, 2019, 3:41pm UTC](https://discourse.julialang.org/t/bypassing-forced-termination-in-nlopt/24224/4 "2019-05-15T15:41:07Z")

</div>

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

[https://nlopt.readthedocs.io/en/latest/NLopt\_Reference/#forced-termination](https://nlopt.readthedocs.io/en/latest/NLopt_Reference/#forced-termination)

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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 16, 2019, 12:52am UTC](https://discourse.julialang.org/t/bypassing-forced-termination-in-nlopt/24224/5 "2019-05-16T00:52:21Z")

</div>

> [@CPPhysics](#):
>
> Passing `val=0` to `nlopt_set_force_stop`

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.)

---

<div class="post-metadata">

**Author:** ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)\
**Post date:** [May 16, 2019, 2:19pm UTC](https://discourse.julialang.org/t/bypassing-forced-termination-in-nlopt/24224/6 "2019-05-16T14:19:30Z")

</div>

Got it, thank you for clearing that up.
