# Optimizing a function with constraints in NLopt (want a different expression returned than optimized)

**URL:** https://discourse.julialang.org/t/optimizing-a-function-with-constraints-in-nlopt-want-a-different-expression-returned-than-optimized/23916
**Category:** Optimization (Mathematical)
**Created:** [May 6, 2019, 3:14pm UTC](https://discourse.julialang.org/t/optimizing-a-function-with-constraints-in-nlopt-want-a-different-expression-returned-than-optimized/23916 "2019-05-06T15:14:24Z")
**Posts on this page:** 3
**Page:** 1

<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 6, 2019, 3:14pm UTC](https://discourse.julialang.org/t/optimizing-a-function-with-constraints-in-nlopt-want-a-different-expression-returned-than-optimized/23916/1 "2019-05-06T15:14:24Z")

</div>

Hello, I am optimizing a function utilizing Lagrange constraints, and I would like NLopt to optimize an expression with Lagrange constraints, but return the value of that expression without constraints. I added a flag to the code which calls different versions of the expression, however I want to make sure that NLopt is still optimizing the expression when it includes constraints. My flag if loop is placed at the bottom of my function being optimized and looks like this:

```julia
    if flag == 0
            return f 
        elseif flag == 1
            return H
        elseif flag == 2
            return f_with_constraints
        else
            return f_with_constraints # This is the output that I hope will be used in the optimization
    end
end # This is the end for the overall function

```

I am having a lot of bugs with this code and I just want to make sure this is not one of them.

Thank you!

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [May 6, 2019, 3:52pm UTC](https://discourse.julialang.org/t/optimizing-a-function-with-constraints-in-nlopt-want-a-different-expression-returned-than-optimized/23916/2 "2019-05-06T15:52:50Z")

</div>

Please read [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) and provide a minimum working example.

---

<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: [May 6, 2019, 8:11pm UTC](https://discourse.julialang.org/t/optimizing-a-function-with-constraints-in-nlopt-want-a-different-expression-returned-than-optimized/23916/3 "2019-05-06T20:11:58Z")

</div>

> [@CPPhysics](#):
>
> Hello, I am optimizing a function utilizing Lagrange constraints, and I would like NLopt to optimize an expression with Lagrange constraints, but return the value of that expression without constraints. I added a flag to the code which calls different versions of the expression, however I want to make sure that NLopt is still optimizing the expression when it includes constraints. My flag if loop is placed at the bottom of my function being optimized and looks like this:

what solvers are you using?
