# Equality Constraint format in NLopt

**URL:** https://discourse.julialang.org/t/equality-constraint-format-in-nlopt/24491
**Category:** New to Julia
**Tags:** optimization
**Created:** [May 22, 2019, 7:22pm UTC](https://discourse.julialang.org/t/equality-constraint-format-in-nlopt/24491 "2019-05-22T19:22:47Z")
**Posts on this page:** 6
**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 22, 2019, 7:22pm UTC](https://discourse.julialang.org/t/equality-constraint-format-in-nlopt/24491/1 "2019-05-22T19:22:48Z")

</div>

I have a Nonlinear optimization problem with equality constraints which I have been implementing as lagrange multiplier constraints, but I’d like to see if NLopt can handle them better.

One of my constraints is that the square of the complex array A needs to be zero. From the documentation, I have formulated the following code:

```julia
function a(x::Vector, grad::Vector)
    A*A
end

equality_constraint!(opt2, a, 1e-8)

```

However when I try to run the code with this constraint, I get a Force\_Stop error code. Is this possibly because A is complex and NLopt cannot handle complex constraints? Or do I simply have the formatting incorrect? My code has been working fine until I add this function and constraint call into it.

When I try

```julia
function a(x::Vector, grad::Vector)
    real(A)*real(A)
end

```

It still gives a Forced\_Stop. This may be a problem with my function, but I just wanted to make sure I wasn’t messing up the actual syntax/formatting of these constraints.

---

<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 22, 2019, 8:13pm UTC](https://discourse.julialang.org/t/equality-constraint-format-in-nlopt/24491/2 "2019-05-22T20:13:46Z")

</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 remember to include a minimal working example.

It’s likely because your function `a` takes arguments `x` and `grad`, but doesn’t use them. Instead, it uses `A` which isn’t defined.

---

<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 22, 2019, 8:24pm UTC](https://discourse.julialang.org/t/equality-constraint-format-in-nlopt/24491/3 "2019-05-22T20:24:18Z")

</div>

Thanks, unfortunately I cannot provide a working example that both illustrates my problem and also does not violate the privacy of my project.

But A is defined in my function by part of the vector x which I create in the function I optimize.

I don’t have a gradient so I’ve been ignoring it but I have to include the option of gradient in the argument otherwise NLopt won’t work and returns a force\_stop. How can I “use” the gradient in this constraint function without actually having a gradient? I’ve been confused by this because in the documentation, they have:

```julia
function myconstraint(x::Vector, grad::Vector, a, b)
    if length(grad) > 0
        grad[1] = 3a * (a*x[1] + b)^2
        grad[2] = -1
    end
    (a*x[1] + b)^3 - x[2]
end

```

So I thought I could just ignore the gradient part. But when I take it out of the arguments since I don’t use it, that doesn’t work either.

I don’t expect anyone to fully help me without a minimum working example, I’m just looking for any help or direction I can get.

---

<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 22, 2019, 8:34pm UTC](https://discourse.julialang.org/t/equality-constraint-format-in-nlopt/24491/4 "2019-05-22T20:34:03Z")

</div>

Note the line

```nohighlight
inequality_constraint!(opt, (x,g) -> myconstraint(x,g,-1,1), 1e-8)

```

in the readme.

This creates an anonymous function with two arguments (`x` and `g`). You _must_ pass a function that takes these two arguments, which is why it doesn’t work when you remove it.

In addition, this function must return a number like `Float64`, and _not_ a matrix. (It isn’t clear what `A` is in your example.)

---

<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 22, 2019, 10:10pm UTC](https://discourse.julialang.org/t/equality-constraint-format-in-nlopt/24491/5 "2019-05-22T22:10:09Z")

</div>

Okay that makes a lot of sense, thank you. I would then try something like:

```julia
function a(x::Vector, grad::Vector)
    real(norm(A,2))
end

```

Where A is defined in my function like so:

```julia
A = reshape(Complex.(x[1:2:32],x[2:2:32]),4,4)

```

and `x` is a `Float64` vector chosen by NLopt.

However this still returns a Forced\_Stop.

---

<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 24, 2019, 11:21am UTC](https://discourse.julialang.org/t/equality-constraint-format-in-nlopt/24491/6 "2019-05-24T11:21:58Z")

</div>

> [@CPPhysics](#):
>
> Is this possibly because A is complex and NLopt cannot handle complex constraints?

Every function in NLopt needs to be real-valued. If you have complex equality constraints you need to convert them to twice the number of real constraints.
