Reputation: 5021
I have a funtion defined in a class
class someClass:
def objFunction(self, weights):
return [self.obj1(weights), self.obj2(weights), self.obj3(weights)]
def asf(self, f):
def obj(x):
return np.max(np.array(f(x[0],x[1],x[2])))+0.00001*np.sum(f(x[0],x[1],x[2]))
res=minimize(obj,
[0.3,0.3,0.4], method='SLSQP'
,jac=ad.gh(obj)[0],options = {'disp':True, 'ftol': 1e-20,
'maxiter': 1000})
return res
where obj1, obj2 and obj3 are some objective functions to optimized. I am running this method making an object separately:
newObj = SomeClass()
newObj.objFunction(weights)
This works fine and give expected results. But when I used the same method inside another method in the class it returns the above mentioned error. This is how I am doing:
a = someClass()
a.asf(a.objFunction(weights)
It throws this:
Traceback (most recent call last):
File "D:/*******.py", line 332, in <module>
print(investment.asf(obj1(w),ref,ideal,nadir, rho))
File "*******.py", line 313, in asf
,options = {'disp':True, 'ftol': 1e-20, 'maxiter': 1000})
File "C:\Users\*****\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\scipy\optimize\_minimize.py", line 455, in minimize
constraints, callback=callback, **options)
File "C:\Users\*****\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\scipy\optimize\slsqp.py", line 363, in _minimize_slsqp
fx = func(x)
File "C:\Users\*******\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\scipy\optimize\optimize.py", line 289, in function_wrapper
return function(*(wrapper_args + args))
File "D:********.py", line 305, in obj
return np.max(np.array(f(x[0], x[1], x[2], x[3])))+rho*np.sum(f(x[0], x[1], x[2], x[3]))
TypeError: 'list' object is not callable
I think I am doing some OOP (object oriented programming) error in coding because I am not good at it. Any suggestions for this ? Thanks
Upvotes: 1
Views: 592
Reputation: 36013
a.objFunction(weights)
returns a list
, that's clear from the definition.
a.asf
expects one argument called f
, which in the definition gets used like:
f(x[0],x[1],x[2])
So you are giving a.asf
a list and trying to call it like a function.
Upvotes: 1