# Is there a way to debug an Nlopt optimization

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-debug-an-nlopt-optimization/24289>\
**Category:** Juno\
**Tags:** optimization\
**Created:** [May 16, 2019, 5:52pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-debug-an-nlopt-optimization/24289 "2019-05-16T17:52:32Z")\
**Posts on this page:** 8\
**Page:** 2

<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 20, 2019, 3:36pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-debug-an-nlopt-optimization/24289/22 "2019-05-20T15:36:38Z")

</div>

Without a minimal working example, it’s not possible to provide you with much more help.

---

<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 20, 2019, 3:43pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-debug-an-nlopt-optimization/24289/23 "2019-05-20T15:43:24Z")

</div>

Okay that’s fair, I was just confused because my function should be deterministic and acts deterministic in every other way. So I’ll assume this discrepancy must mean there is something wrong with my function if you haven’t heard of this happening before. It just makes no sense to me where this `minf` is coming from if I can plug `minx` back in and get different values. I’ll work on getting a working example but I don’t think I’ll be able to come up with a minimum working example that exhibits this strange behavior without just giving you my whole function - which I cannot do.

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [May 20, 2019, 6:39pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-debug-an-nlopt-optimization/24289/24 "2019-05-20T18:39:35Z")

</div>

What value do you get if you call `Ef(minx)` (or any other value but always the same) several times in a row? Do you get the same result?

Also, at 127 degrees of freedom, your parameter space is pretty big. Can you reduce the number of free parameters to make it easier for yourself to check the intermediate steps? Can you tell what operations you are doing within your objective function?

---

<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 20, 2019, 8:17pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-debug-an-nlopt-optimization/24289/25 "2019-05-20T20:17:21Z")

</div>

When I call `Ef(minx)` I get the return value of my function every time, my function is very consistent when calling it with different vectors, even older vectors which I know the output for and they are consistent. I just don’t know where `minf` is coming from. The `Ef(minx)` values are reasonable, its just `minf` that I can’t trace.

This is actually the smallest number of dof that I will have, I’ll only increase it from here unfortunately. My function works with kronecker products of complex arrays and anti-hermitian arrays and their conjugates, and I am then always dealing with the traces of these arrays which are always real.

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [May 21, 2019, 5:07am UTC](https://discourse.julialang.org/t/is-there-a-way-to-debug-an-nlopt-optimization/24289/26 "2019-05-21T05:07:01Z")

</div>

As far as I understand from the docs, the function is not evaluated outside constraints in the case of lower and upper bounds only, as in the OP question.

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [May 24, 2019, 5:16am UTC](https://discourse.julialang.org/t/is-there-a-way-to-debug-an-nlopt-optimization/24289/27 "2019-05-24T05:16:25Z")

</div>

It looks like you [solved](https://discourse.julialang.org/t/equality-constraint-format-in-nlopt/24491) the problem in this post: what was it?

---

<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:** [June 4, 2019, 7:34pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-debug-an-nlopt-optimization/24289/28 "2019-06-04T19:34:58Z")

</div>

Essentially, I was passing an imaginary valued return value to the optimizer. My imaginary parts were something like 10^-15 but I needed to pass real() to the return value before NLopt could handle it, and same with the constraints. As far as the problem of getting a different `minf` than when I plugged `minx` back into the function, I rewrote my problem so that constraints were automatically implemented and the problem went away.

---

<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:** [June 6, 2019, 6:31pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-debug-an-nlopt-optimization/24289/29 "2019-06-06T18:31:08Z")

</div>

I will say, I am now still getting the `minf` discrepancy problem. But now the discrepancy is much much less, the reported `minf` by NLopt is very close to the minimum I get when I plug `minx` back into the function I optimize. I believe that the reported `minf` might be a previous iteration because it’s always a little larger than the minimum I get plugging `minx` back in.

[Previous page](https://discourse.julialang.org/t/is-there-a-way-to-debug-an-nlopt-optimization/24289.md?page=1)
