# NLopt optimization with inequality constraint where objective function is undefined outside of the constraint

**URL:** https://discourse.julialang.org/t/nlopt-optimization-with-inequality-constraint-where-objective-function-is-undefined-outside-of-the-constraint/22924
**Category:** General Usage
**Tags:** optimization
**Created:** [April 8, 2019, 3:23pm UTC](https://discourse.julialang.org/t/nlopt-optimization-with-inequality-constraint-where-objective-function-is-undefined-outside-of-the-constraint/22924 "2019-04-08T15:23:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Shunsuke-Hori](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shunsuke-hori/32/3732_2.png) [@Shunsuke-Hori](https://discourse.julialang.org/u/Shunsuke-Hori)
#### Post date: [April 8, 2019, 3:23pm UTC](https://discourse.julialang.org/t/nlopt-optimization-with-inequality-constraint-where-objective-function-is-undefined-outside-of-the-constraint/22924/1 "2019-04-08T15:23:57Z")

</div>

Hello,

I would like to do nonlinear optimization with inequality constraint by `NLopt`, in which my objective function is not defined when the constraint is not satisfied. I’m using `LN_COBYLA` because I cannot compute the derivative.  
However, the solver sometimes try to evaluate the objective function outside of the constraint and, as a consequence, the solver fails.

The minimal example below shows the problem.  
I know I can impose a bounds constraint for this simple example but not in general.

Is there any way to search values only inside of the constraint? (What I can come up with is to let the objective function return huge value when undefined, although I don’t think it’s a beautiful solution.)

Thank you.

=======================  
Example: \min x\_1+x\_2 subject to x\_1\geq0 and x\_2\geq0 but x\_1+x\_2 is undefined for x\_1\<0 or x\_2\<0

```julia
using NLopt
function obj(x, grad)
    if any(x .<0)
        println("negative element")
        println(x)
        return error("undefined")
    else
        return x[1]+x[2]
    end
end
function myconstraint(result, x, grad)
    result .= -x
end
opt = Opt(:LN_COBYLA, 2)
min_objective!(opt, obj)
inequality_constraint!(opt, myconstraint, [0, 0])

optimize(opt, [0.001, 0.001])

```

return

```julia
undefined
[-0.000320083, 0.000507602]
(0.000585786437626905, [0.000292893, 0.000292893], :FORCED_STOP)

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [April 8, 2019, 4:05pm UTC](https://discourse.julialang.org/t/nlopt-optimization-with-inequality-constraint-where-objective-function-is-undefined-outside-of-the-constraint/22924/2 "2019-04-08T16:05:31Z")

</div>

Do a domain transformation, such as using `x^2` or `exp(x)` as the input, so that way it is defined on the full real lined.

---

<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 9, 2019, 1:33am UTC](https://discourse.julialang.org/t/nlopt-optimization-with-inequality-constraint-where-objective-function-is-undefined-outside-of-the-constraint/22924/3 "2019-04-09T01:33:27Z")

</div>

> [@Shunsuke-Hori](#):
>
> I would like to do nonlinear optimization with inequality constraint by `NLopt` , in which my objective function is not defined when the constraint is not satisfied. I’m using `LN_COBYLA` because I cannot compute the derivative.  
> However, the solver sometimes try to evaluate the objective function outside of the constraint and, as a consequence, the solver fails.

For _general nonlinear constraints_, NLopt makes no guarantee that it will only evaluate points inside the feasible set (i.e. satisfying the constraints).

However, for a constraint like x₁ ≥ 0, you should instead specify the constraint as a bound constraint via `opt.lower_bounds = ...` Not only is this much more efficient, but also NLopt guarantees that it will never evaluate a point outside of the bound constraints.

---

<div class="post-metadata">

### Author: ![Shunsuke-Hori](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shunsuke-hori/32/3732_2.png) [@Shunsuke-Hori](https://discourse.julialang.org/u/Shunsuke-Hori)
#### Post date: [April 10, 2019, 1:43am UTC](https://discourse.julialang.org/t/nlopt-optimization-with-inequality-constraint-where-objective-function-is-undefined-outside-of-the-constraint/22924/4 "2019-04-10T01:43:23Z")

</div>

Thank you both,

It’s good to know there is no way to force the solver to use a value satisfying the constraint.

I’ll think about change of variables so that I can use box constraints, although I’m not sure if I can.
