0

For an optimization problem, I need to find argmin_u f(u, a) for many, many different parameters a. All my code is written in Python, so I figured I would use cython to optimize this particular task. I use scipy.optimize.minimize for the actual optimization.

I wrote a simple cython class to wrap the function, with separate methods to set the parameter and to evaluate the function with a single input. However, when I try to optimize it, compilation works but I receive a run-time error:

TypeError: wrap() takes exactly 2 positional arguments (1 given)

Here is a minimal example in which I managed to reproduce the error (in a .pyx file):

from scipy.optimize import minimize

cdef double testfun(double x):
  return (x-3)**2

cdef class TestClass(object):
  cdef double eval(self, double x):
    return (x-5)**2

cpdef optimize_testfun():

  # this works:
  res = minimize(testfun, [0])
  print('Result: {}'.format(res.x[0]))

  # this does not work:
  cdef TestClass test_object = TestClass()
  res = minimize(test_object.eval, [0])
  print('Result: {}'.format(res.x[0]))

Note that for a regular compiled function, the optimization works fine. Also, for a regular Python class this construction works. I suppose the problem lies in the way that cython handles the self argument.

Does anyone have an idea to get the code to work with the compiled class? Or does anyone have an alternative idea of how to wrap the multi-argument function to present it as a compiled single-argument function to minimize?

My experience with both optimization and cython is limited, so if this is a stupid way to go about things, please let me know.

[NB. My initial question was very vaguely formulated, I almost completely rewrote it to give a clear and reproducible problem outline]

Scipio
  • 313
  • 2
  • 15
  • I've downvoted this because it's lacking an [mcve]. A vague outline is there but it isn't complete, and the error message does not match your simplified example. – DavidW Mar 25 '18 at 16:10
  • @DavidW thanks for the feedback. a MCVE would involve at least three files, which seemed a bit over the top, given that the basic question (minimize a compiled functioneren) is pretty clear. But i will rephrase the question when I'm back home – Scipio Mar 25 '18 at 16:22
  • @DavidW, I rewrote the question. You're absolutely right that I should have put more thought in this before asking - hope this is better. – Scipio Mar 25 '18 at 20:29
  • Yes I realized this is way more to the point while working out the example, proving once again for myself why you should always do that :P It's definitely a duplicate, I have some questions but will ask there, then this can be closed. – Scipio Mar 25 '18 at 20:57

0 Answers0