jfowkes / pycutest

Python interface to CUTEst
https://jfowkes.github.io/pycutest/
GNU General Public License v3.0
27 stars 11 forks source link

Add access to gradient only #40

Closed jeffrey-hokanson closed 9 months ago

jeffrey-hokanson commented 1 year ago

Is your feature request related to a problem? Please describe Currently, the gradient for an unconstrained problem can only be accessed along with either the objective or Hessian:

_, g = prob.obj(x, gradient = True)
g, _ = prob.gradhess(x)

A similar issue is true with constrained problems.

Describe the solution you'd like Ideally, I'd like to have access to the gradient alone, i.e.,

g = prob.grad(x)

Describe alternatives you've considered This isn't a blocking change for my work since accessing the gradient through the objective is sufficiently cheap, but this approach is aesthetically unpleasing.

jfowkes commented 1 year ago

Thank you for the feature request, the lack of a direct function for accessing the gradient is not very user-friendly. We are just copying the CUTEst interfaces here, but I agree this would be desirable and relatively easy to do (essentially a wrapper around problem.obj).

@lindonroberts what are your thoughts on this?

lindonroberts commented 1 year ago

I agree this would be useful, and perhaps a similar interface to problem.cons to just extract constraint gradients.

The only wrinkle I can see is that there is the internal CUTEst counter of objective/gradient/Hessian evaluations in problem.report() that would need adjusting. Perhaps a tally of how many times problem.grad is called, and subtract that from the total objective evaluations (since that information was ignored)?

jeffrey-hokanson commented 1 year ago

That's an interesting design choice in CUTEst, but I can see why it was made. Many (most? all?) optimization algorithms evaluate objective & gradient (e.g., steepest descent) or gradient & Hessian (e.g., Newton) at the same time. In the code I've been developing, I already extract the gradient as @jfowkes describes so this doesn't really bother me.

jfowkes commented 9 months ago

I have realised there is a CUTEst_ugr function for accessing the objective gradient and a CUTEst_cigr function for accessing the constrained gradient. I will add a PyCUTEst interface to these in due course. Apologies for missing this.