0

Edit: It now runs fine - I had pasted a correction of a typo that I had not yet run... Thanks!

Original question:

With python / numpy / scipy, I would like to minimize a function f, which depends on several additional parameters c_1, c_2..., c_N. As these are many: is there a possibility, as is with matlab's fminsearch, to pass not only a tuple as additional arguments, but an arbitrary object, such as a class? I imagine something like:

from scipy import optimize as opt

def banana(x,s):
    return 100*(x[1]-s.c)**2+(1-x[0])**2 

class s:
    pass
s.c = 55

xopt, fopt, iter, funcalls, warnflag  \
    = opt.fmin(func=banana, x0=np.array([-1.2,1]),args=(s,), xtol=0.0001,ftol=0.00001, full_output = True)

The code above yields "AttributeError: 'int' object has no attribute 'c'", which I understand from the spec of the arguments being a tuple. Thanks!

  • 2
    The code above runs fine. – Ivan Dec 27 '20 at 10:07
  • Does this answer your question? [numpy/scipy analog of matlab's fminsearch](https://stackoverflow.com/questions/19070943/numpy-scipy-analog-of-matlabs-fminsearch) – David Dec 27 '20 at 10:13
  • @Ivan: Oh yes, it actually now runs fine - I had pasted a correction of a typo that I had not yet run... Thanks! – Filteruser Dec 30 '20 at 14:29

0 Answers0