# Stop optimization problem when the objective is negative?

**URL:** <https://discourse.julialang.org/t/stop-optimization-problem-when-the-objective-is-negative/23330>\
**Category:** Optimization (Mathematical)\
**Created:** [April 20, 2019, 2:39am UTC](https://discourse.julialang.org/t/stop-optimization-problem-when-the-objective-is-negative/23330 "2019-04-20T02:39:34Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [April 20, 2019, 2:39am UTC](https://discourse.julialang.org/t/stop-optimization-problem-when-the-objective-is-negative/23330/1 "2019-04-20T02:39:34Z")

</div>

i have a Helmholtz function, that needs to be optimized, but i need to stop the optimization algorithm if the value of the objective function is negative, any ideas?, im using Optim.jl

```julia
using Optim
N = 10
c = rand(N) .- 0.5
x0 = rand(N)
f(x) = sum(c.*x.^collect(1:length(x))) # a nth grade polinomial as example
#my objetive function is not a polynomial
lb = zeros(N)
ub = ones(N)
#this is straightly copied from the Optim.jl guide
inner_optimizer=GradientDescent(linesearch=LineSearches.BackTracking(order=3))
sol = optimize(f,lb,ub,x0,Fminbox(inner_optimizer)

```

---

<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:** [April 20, 2019, 3:15am UTC](https://discourse.julialang.org/t/stop-optimization-problem-when-the-objective-is-negative/23330/2 "2019-04-20T03:15:49Z")

</div>

> [@longemen3000](#):
>
> I need to stop the optimization algorithm if the value of the objective function is negative

With NLopt.jl you can specify a lower bound for the objective function as a stopping condition.

---

<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:** [April 20, 2019, 3:17am UTC](https://discourse.julialang.org/t/stop-optimization-problem-when-the-objective-is-negative/23330/3 "2019-04-20T03:17:16Z")

</div>

that would suffice!, thanks!

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [April 20, 2019, 9:42am UTC](https://discourse.julialang.org/t/stop-optimization-problem-when-the-objective-is-negative/23330/5 "2019-04-20T09:42:23Z")

</div>

You can minimize `max(0, f(x))` or `max(0, f(x))^2` if you want to make it differentiable.

---

<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:** [April 21, 2019, 11:10pm UTC](https://discourse.julialang.org/t/stop-optimization-problem-when-the-objective-is-negative/23330/6 "2019-04-21T23:10:55Z")

</div>

This is more of a theoretical question, but i’m implementing a tunneling algorithm for psudo-global minimization, where i can instead use some of the already written algorithms in NLopt, the idea is to find a negative value as soon as possible (the reason for the stop condition). instead of using a tunneling algorithm (finding local minima, using a tunneling function to carve away from that minima and keep searching), i can use a global optimization method that can achieve the same result. my functions are n-differentiable, and the precision in this step is not required, just the sign. what of the available algorithms in NLopt can you recommend me in this case?
