gpt4 book ai didi

python - 类型错误 : 'list' object is not callable in Python in mathematical programming

转载 作者:太空宇宙 更新时间:2023-11-03 15:58:28 24 4
gpt4 key购买 nike

我在类中定义了一个函数

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

其中 obj1、obj2 和 obj3 是一些要优化的目标函数。我正在运行这个方法来单独创建一个对象:

newObj = SomeClass()
newObj.objFunction(weights)

这工作正常并给出预期结果。但是当我在类中的另一个方法中使用相同的方法时,它会返回上述错误。这就是我正在做的:

a = someClass()
a.asf(a.objFunction(weights)

它抛出这个:

    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

我认为我在编码中犯了一些 OOP(面向对象编程)错误,因为我不擅长。对此有何建议?谢谢

最佳答案

a.objFunction(weights) 返回一个列表,从定义中可以清楚地看出。

a.asf 需要一个名为 f 的参数,该参数在定义中的用法如下:

f(x[0],x[1],x[2])

所以你给了 a.asf 一个列表并尝试像函数一样调用它。

关于python - 类型错误 : 'list' object is not callable in Python in mathematical programming,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40557977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com