I would like to do nonlinear optimization with inequality constraint by
NLopt, in which my objective function is not defined when the constraint is not satisfied. I’m using
LN_COBYLA because I cannot compute the derivative.
However, the solver sometimes try to evaluate the objective function outside of the constraint and, as a consequence, the solver fails.
The minimal example below shows the problem.
I know I can impose a bounds constraint for this simple example but not in general.
Is there any way to search values only inside of the constraint? (What I can come up with is to let the objective function return huge value when undefined, although I don’t think it’s a beautiful solution.)
Example: \min x_1+x_2 subject to x_1\geq0 and x_2\geq0 but x_1+x_2 is undefined for x_1<0 or x_2<0
using NLopt function obj(x, grad) if any(x .<0) println("negative element") println(x) return error("undefined") else return x+x end end function myconstraint(result, x, grad) result .= -x end opt = Opt(:LN_COBYLA, 2) min_objective!(opt, obj) inequality_constraint!(opt, myconstraint, [0, 0]) optimize(opt, [0.001, 0.001])
undefined [-0.000320083, 0.000507602] (0.000585786437626905, [0.000292893, 0.000292893], :FORCED_STOP)