NLopt Constraint

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?

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

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:

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

define a new function

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

is this function for constraint or objective function?

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.

1 Like

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

2 Likes