JuMP User-Defined Function, Value and Gradient At Once

Hi -

I have a user-defined function for which I have an analytical gradient I’d like to use. Furthermore, most of the effort in calculating that gradient is already done in calculating the value of the function. Is there a way to pass JuMP a function that does both, in a way that is efficient? I recognize I could have a “wrapper” function that extracts only the part that JuMP wants (value or gradient), but surely that will be implemented in a very slow fashion. NLsolve, for instance, has a capability like this.

Thank you!


You can cache the evaluation point and last value/gradient. If the evaluation point doesn’t change, return the value that you’ve already computed. It sounds like your function has a nontrivial evaluation cost; in this case the extra time spent in this check should be negligible.

Brilliant - thanks!

I think that check is almost always negligible. We do it by default every time f(x), g(x) or h(x) is called in Optim for example.