Equality Constraint format in NLopt

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:

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

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.

Please read PSA: make it easier to help you, 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.

1 Like

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:

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.

Note the line

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.)

1 Like

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

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

Where A is defined in my function like so:

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.

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.

1 Like