# NLopt not optimising

**URL:** <https://discourse.julialang.org/t/nlopt-not-optimising/51796>\
**Category:** General Usage\
**Tags:** optimization\
**Created:** [December 14, 2020, 9:44am UTC](https://discourse.julialang.org/t/nlopt-not-optimising/51796 "2020-12-14T09:44:27Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Massimiliano\_Vasile](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/massimiliano_vasile/32/20268_2.png) [@Massimiliano\_Vasile](https://discourse.julialang.org/u/Massimiliano_Vasile)\
**Post date:** [December 14, 2020, 9:44am UTC](https://discourse.julialang.org/t/nlopt-not-optimising/51796/1 "2020-12-14T09:44:28Z")

</div>

Hi  
I am rather new to Julia and I am experimenting with NLopt. I ran the tests on github and they work fine but then I tried my own objective and constraints.  
function ps(x,grad)  
return x[1]  
end  
function ps\_con(x,grad,w)  
f=zeros(2)  
f[1]=x[2]^2-1+x[3]  
f[2]=-10_x[2]^2+0.1_x[3]  
z=-f+w\*x[1]  
return z  
end

I then followed the same procedure followed in the test examples but with a derivative free optimiser.  
opt = Opt(:LN\_COBYLA, 3)  
opt.lower\_bounds = [0, -Inf.-Inf]  
opt.upper\_bounds = [1,Inf,Inf]  
opt.xtol\_rel = 1e-4  
opt.min\_objective = ps  
opt.inequality\_constraint = (x,g) → ps\_con(x,g,[1,1])  
(minf,minx,ret) = optimize(opt, [1,1,1])

No error occurs but the optimiser does not do anything and exits with either FORCED\_STOP or XTOL\_REACHED at the first iteration.

Note that calling the objective and constraint functions individually with random inputs does not produce any error.

What am I doing wrong?  
Mx

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [December 14, 2020, 10:13am UTC](https://discourse.julialang.org/t/nlopt-not-optimising/51796/2 "2020-12-14T10:13:43Z")

</div>

Try to call the function and constraint with the values you supplied to see if there is an error in them.

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [December 14, 2020, 10:51am UTC](https://discourse.julialang.org/t/nlopt-not-optimising/51796/3 "2020-12-14T10:51:03Z")

</div>

See [the vector valued constraints section](https://github.com/JuliaOpt/NLopt.jl#vector-valued-constraints). These (in)equality constraints expect first argument to be returned vector.

---

<div class="post-metadata">

**Author:** ![Massimiliano\_Vasile](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/massimiliano_vasile/32/20268_2.png) [@Massimiliano\_Vasile](https://discourse.julialang.org/u/Massimiliano_Vasile)\
**Post date:** [December 14, 2020, 10:59am UTC](https://discourse.julialang.org/t/nlopt-not-optimising/51796/4 "2020-12-14T10:59:06Z")

</div>

already done that and the function outputs the expected value with no error

---

<div class="post-metadata">

**Author:** ![Massimiliano\_Vasile](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/massimiliano_vasile/32/20268_2.png) [@Massimiliano\_Vasile](https://discourse.julialang.org/u/Massimiliano_Vasile)\
**Post date:** [December 14, 2020, 11:01am UTC](https://discourse.julialang.org/t/nlopt-not-optimising/51796/5 "2020-12-14T11:01:51Z")

</div>

ok I can try, this is the part I was not sure about, in fact  
I thought the the syntax I was using use equivalent to declaring a vector constraint but I will try to follow the example you suggested

---

<div class="post-metadata">

**Author:** ![Massimiliano\_Vasile](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/massimiliano_vasile/32/20268_2.png) [@Massimiliano\_Vasile](https://discourse.julialang.org/u/Massimiliano_Vasile)\
**Post date:** [December 14, 2020, 4:26pm UTC](https://discourse.julialang.org/t/nlopt-not-optimising/51796/6 "2020-12-14T16:26:22Z")

</div>

I tested these new functions  
function ps(x::Vector,grad::Vector)  
if length(grad)\>0  
grad[1]=1  
grad[2]=0  
grad[3]=0  
end  
return x[1]  
end  
function ps\_con(z::Vector,x::Vector,grad::Matrix,w::Vector)  
if length(grad)\>0  
grad[1,1]=w[1]  
grad[2,2]=20_x[2]  
grad[1,2]=-2_x[2]  
grad[1,3]=-1  
grad[2,1]=w[2]  
grad[2,3]=-0.1  
end  
z[1]=-(x[2]^2-1+x[3])+w[1]_x[1]  
z[2]=-(-10_x[2]^2+0.1\*x[3])+w[2]\*x[1]  
end  
and inserted the string  
inequality\_constraint!(opt, (z,x,g) → ps\_con(z,x,g,[1,1]), [1e-8,1e-8]::AbstractVector)  
(minf,minx,ret) = optimize(opt, [1,1, 1])  
but now I get:  
(1.0, [1.0, 1.0, 1.0], :FORCED\_STOP)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 14, 2020, 4:45pm UTC](https://discourse.julialang.org/t/nlopt-not-optimising/51796/7 "2020-12-14T16:45:46Z")

</div>

Please quote your code usingt triple backticks, it’s quite hard to read your posts (see [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757))

---

<div class="post-metadata">

**Author:** ![Massimiliano\_Vasile](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/massimiliano_vasile/32/20268_2.png) [@Massimiliano\_Vasile](https://discourse.julialang.org/u/Massimiliano_Vasile)\
**Post date:** [December 14, 2020, 4:48pm UTC](https://discourse.julialang.org/t/nlopt-not-optimising/51796/8 "2020-12-14T16:48:48Z")

</div>

> [@Massimiliano\_Vasile](#):
>
> I tested these new functions
> 
> ```julia
> function ps(x::Vector,grad::Vector)
> if length(grad)>0
> grad[1]=1
> grad[2]=0
> grad[3]=0
> end
> return x[1]
> end
> 
> ```
> 
> ```julia
> function ps_con(z::Vector,x::Vector,grad::Matrix,w::Vector)
> if length(grad)>0
> grad[1,1]=w[1]
> grad[2,2]=20*x[2]
> grad[1,2]=-2*x[2]
> grad[1,3]=-1
> grad[2,1]=w[2]
> grad[2,3]=-0.1
> end
> z[1]=-(x[2]^2-1+x[3])+w[1] <em>x[1]
> z[2]=-(-10</em> x[2]^2+0.1*x[3])+w[2]*x[1]
> end
> 
> ```
> 
> and inserted the string
> 
> ```julia
> inequality_constraint!(opt, (z,x,g) -> ps_con(z,x,g,[1,1]), [1e-8,1e-8]::AbstractVector)
> (minf,minx,ret) = optimize(opt, [1,1, 1])
> 
> ```
> 
> but now I get:
> 
> (1.0, [1.0, 1.0, 1.0], :FORCED\_STOP)

can you read it better?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 14, 2020, 5:30pm UTC](https://discourse.julialang.org/t/nlopt-not-optimising/51796/9 "2020-12-14T17:30:57Z")

</div>

Yes much better, thanks!

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [December 14, 2020, 5:32pm UTC](https://discourse.julialang.org/t/nlopt-not-optimising/51796/10 "2020-12-14T17:32:12Z")

</div>

NLopt is pretty aggressive about swallowing errors in my experience. The fact that you have `:FORCED_STOP` and just get your inits back leads me to believe that there is an error happening somewhere. I haven’t looked at your code in detail here and am not familiar with the constraint interface in NLopt, but just in the interest of a quick response, you’re almost certainly experiencing an error that NLopt is trying to elegantly handle without making everything crash.

---

<div class="post-metadata">

**Author:** ![Massimiliano\_Vasile](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/massimiliano_vasile/32/20268_2.png) [@Massimiliano\_Vasile](https://discourse.julialang.org/u/Massimiliano_Vasile)\
**Post date:** [December 14, 2020, 5:58pm UTC](https://discourse.julialang.org/t/nlopt-not-optimising/51796/11 "2020-12-14T17:58:32Z")

</div>

yeh I suspected that but I tested each function individually with no error and given that I started programming in Julia on Saturday I am not that skilled yet  
any advice on where to look for the bug would be precious

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [December 14, 2020, 6:06pm UTC](https://discourse.julialang.org/t/nlopt-not-optimising/51796/12 "2020-12-14T18:06:33Z")

</div>

It seems `grad` should be 3x2 as documentation says instead of 2x3 if it ever asked by optimization.

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [December 14, 2020, 6:14pm UTC](https://discourse.julialang.org/t/nlopt-not-optimising/51796/13 "2020-12-14T18:14:33Z")

</div>

```julia
julia> function ps(x::Vector,grad::Vector)
       if length(grad)>0
       grad[1]=1
       grad[2]=0
       grad[3]=0
       end
       return x[1]
       end
ps (generic function with 1 method)

julia> using NLopt

julia> function ps_con(z::Vector,x::Vector,grad::Matrix,w::Vector)
       if length(grad)>0
       grad[1,1]=w[1]
       grad[2,2]=20x[2]
       grad[2,1]=-2x[2]
       grad[3,1]=-1
       grad[1,2]=w[2]
       grad[3,2]=-0.1
       end
       z[1]=-(x[2]^2-1+x[3])+w[1]x[1]
       z[2]=-(-10x[2]^2+0.1*x[3])+w[2]*x[1]
       end
ps_con (generic function with 1 method)

julia> opt=Opt(:LD_MMA,3)
Opt(LD_MMA, 3)

julia> opt.min_objective=ps
ps (generic function with 1 method)

julia> opt.lower_bounds=[0,-Inf,-Inf];

julia> opt.upper_bounds=[1,Inf,Inf];

julia> inequality_constraint!(opt, (z,x,g) -> ps_con(z,x,g,[1,1]), [1e-8,1e-8])

julia> (minf,minx,ret) = optimize(opt, Float64[1,1, 1])

^C(0.0, [0.0, -0.01634360284914689, 2.11468681873786], :FORCED_STOP)

julia> opt.maxeval = 1000
1000

julia> (minf,minx,ret) = optimize(opt, Float64[1,1, 1])
(0.0, [0.0, -0.01634360284914689, 2.11468681873786], :MAXEVAL_REACHED)

```

It seems without a stopping criteria it never stops that’s why stopped first try.

---

<div class="post-metadata">

**Author:** ![Massimiliano\_Vasile](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/massimiliano_vasile/32/20268_2.png) [@Massimiliano\_Vasile](https://discourse.julialang.org/u/Massimiliano_Vasile)\
**Post date:** [December 14, 2020, 8:53pm UTC](https://discourse.julialang.org/t/nlopt-not-optimising/51796/14 "2020-12-14T20:53:19Z")

</div>

> [@tomaklutfu](#):
>
> `(minf,minx,ret) = optimize(opt, Float64[1,1, 1])`

great, thank you, I included a tolerance on the x at convergence and now it returns a solution, so just to be clear you did the following:  
1- transposed the constraint gradient  
2- you used ```inequality\_constraint!(opt, (z,x,g) → ps\_con(z,x,g,[1,1]), [1e-8,1e-8])

```julia
question: if the solver does not need gradients does it matter how I define the gradient? I guess no
```
