# NLopt stopval and termination criteria

**URL:** <https://discourse.julialang.org/t/nlopt-stopval-and-termination-criteria/7333>\
**Category:** Optimization (Mathematical)\
**Tags:** optim\
**Created:** [November 27, 2017, 1:39pm UTC](https://discourse.julialang.org/t/nlopt-stopval-and-termination-criteria/7333 "2017-11-27T13:39:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivborissov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivborissov/32/2841_2.png) [@ivborissov](https://discourse.julialang.org/u/ivborissov)\
**Post date:** [November 27, 2017, 1:39pm UTC](https://discourse.julialang.org/t/nlopt-stopval-and-termination-criteria/7333/1 "2017-11-27T13:39:32Z")

</div>

Hi,  
I am solving optimization problems like `find max x1 in [a1,b1] : f(x1,x2,x3...)<=0` plus I know that at initial point `f(a1,a2,a3...)<=0`.  
Sometimes my model `f` (based on experimental data) isn’t sensitive to `x1` (like in the example below), so I want to stop the optimizer when it reaches `b1` but it seems `stopval` doesn’t work this way:

```julia
f_ineq_constr(x::Vector, g::Vector) = 0.0*x[1] + (x[2]-1.0)^2 -2.0

global count = 0

function f_optim(x::Vector, g::Vector)
    global count
    count::Int += 1
    println("f_$count($x)")
    x[1]
end

using NLopt
opt = Opt(:LN_AUGLAG, 2)
max_objective!(opt, f_optim)
local_opt = Opt(:LN_SBPLX, 2)
lower_bounds!(opt, [0.0, -Inf])
upper_bounds!(opt, [9.0, +Inf])
xtol_abs!(opt, 1e-3)
stopval!(opt, 9.0)
inequality_constraint!(opt, f_ineq_constr, 1e-5)

(minf, minx, ret) = optimize(opt, zeros(2))

f_23035([8.9965, 0.0])
f_23036([9.0, 0.0])
f_23037([9.0, 0.0])
...

```

Finally it reaches `MAXEVAL_REACHED`.  
Could you please help me understand how `stopval` should work and what is a good choice of a termination criteria here?

---

<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:** [November 27, 2017, 2:07pm UTC](https://discourse.julialang.org/t/nlopt-stopval-and-termination-criteria/7333/2 "2017-11-27T14:07:17Z")

</div>

IIRC will ignore stopval if the point is infeasible (violates the constraint). The AUGLAG algorithm is a bit funny in that intermediate steps are almost always infeasible, so stopval won’t have much effect. (The CCSA algorithm, in contrast, is designed to produce feasible iterates, but it requires you to supply gradients.)

---

<div class="post-metadata">

**Author:** ![ivborissov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivborissov/32/2841_2.png) [@ivborissov](https://discourse.julialang.org/u/ivborissov)\
**Post date:** [November 28, 2017, 11:48am UTC](https://discourse.julialang.org/t/nlopt-stopval-and-termination-criteria/7333/3 "2017-11-28T11:48:59Z")

</div>

Thanks for clarification!  
Currently I use a “workaround” to stop the optimization when boundary is reached with `AUGLAG`. Instead of searching `max` of `f_optim = (x,g)->x[1]` I use `f_optim = (x,g)->-(x[1]-9)^2` and remove the boundary constraints.

---

<div class="post-metadata">

**Author:** ![ivborissov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivborissov/32/2841_2.png) [@ivborissov](https://discourse.julialang.org/u/ivborissov)\
**Post date:** [November 30, 2017, 3:24pm UTC](https://discourse.julialang.org/t/nlopt-stopval-and-termination-criteria/7333/4 "2017-11-30T15:24:39Z")

</div>

@stevengj Do you think there is a way to efficiently stop `AUGLAG` optimization for this problem when `x[1]` reaches the boundary `x[1]=9.0`? I have read about `ForcedStop` but not sure how to use it in Julia
