Open charelstoncrabb opened 10 months ago
This is a little more painful to add than you might expect.
Of course, we could add "double-sided" API, but under the hood it would need to transform it to the $f_c(x) ≤ 0$ form since that's what the algorithms all use. In principle, this is no problem — you create two inequality constraints internally. However, that is suboptimal because it will end up evaluating your $f_c$ function twice. Whereas if the user implements it as two constraints then they can memo-ize the $f_c$ function so that the second call just re-uses the results of the first.
The current interface requires (according to the documentation):
A very common pattern in constrained optimization is the more general setup of $l_c \leq f_c(x) \leq u_c$. Providing this type of interface seems much more aligned with what a user actually wants to do, and would take a lot of burden off the user to specialize implementations for each type of (upper/lower) constraint.
If I have an existing constraint function $f_c$ (as a part of some external shared runtime e.g.), this requirement significantly complicates wiring into nlopt.
It seems like the logic could be wrapped in an interface something like:
Somewhat related to why it makes it an unnecessary pain for the user is the lack of support for lambdas (though a separate issue itself).
If lambdas were supported, this would be much simpler, e.g.
Either the overloaded
add_inequality_constraint
method above, or overloaded methodswould be fantastic 😁