# NLopt Constraint

**URL:** <https://discourse.julialang.org/t/nlopt-constraint/114856>\
**Category:** Optimization (Mathematical)\
**Tags:** optimization, nlopt, gradient\
**Created:** [May 28, 2024, 4:19pm UTC](https://discourse.julialang.org/t/nlopt-constraint/114856 "2024-05-28T16:19:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mary/32/53139_2.png) [@mary](https://discourse.julialang.org/u/mary)\
**Post date:** [May 28, 2024, 4:19pm UTC](https://discourse.julialang.org/t/nlopt-constraint/114856/1 "2024-05-28T16:19:38Z")

</div>

Hi, I am using Nlopt for optimization. my objective function depends on three variables like x1,x2,x3 also I have a constraint which depends on all three variable. I want to do the optimization just for x3 variable. should i include the gradients of the constraint wrt x1 and x2 in my constraint function or just wrt x3?

```julia
function wf_p(p0::Vector, gradw::Vector; r, β, η, fem_params)
    if length(gradw) > 0
        gradw[:] = #grad wrt x3
    end
    return wf
end

```

---

<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 28, 2024, 9:43pm UTC](https://discourse.julialang.org/t/nlopt-constraint/114856/2 "2024-05-28T21:43:55Z")

</div>

If you want to optimize only with respect to `x3`, then you should create a new function that accepts only `x3` as input.

For example, if your original function is:

```julia
foo(x::Vector) = 1.0 * x[1] + 2.0 * x[2] + 3.0 * x[3]

```

define a new function

```julia
foo_fixed(y::Vector; x1 = 1.0, x2 = 2.0) = foo([x1, x2, y[1]])

```

---

<div class="post-metadata">

**Author:** ![mary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mary/32/53139_2.png) [@mary](https://discourse.julialang.org/u/mary)\
**Post date:** [May 28, 2024, 9:46pm UTC](https://discourse.julialang.org/t/nlopt-constraint/114856/3 "2024-05-28T21:46:27Z")

</div>

is this function for constraint or objective function?

---

<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 28, 2024, 9:48pm UTC](https://discourse.julialang.org/t/nlopt-constraint/114856/4 "2024-05-28T21:48:47Z")

</div>

Both. Don’t think in terms of “can I optimize with a subset of variables.” Define a new objective and constraint functions that contain only x3 as a decision variable and hard-code your choices for x1 and x2.

---

<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 28, 2024, 10:27pm UTC](https://discourse.julialang.org/t/nlopt-constraint/114856/5 "2024-05-28T22:27:28Z")

</div>

> [@odow](#):
>
> If you want to optimize only with respect to `x3`, then you should create a new function that accepts only `x3` as input.

(You can alternatively just set the upper bounds equal to the lower bounds for the other variables, which constrains them to be constant.)
