2019-09-14 21:42

# 如何解决numpy.ndarray' object is not callable

``````from scipy.optimize import minimize
import numpy as np
def fun():
v=lambda x:abs((x(1)+0*x(2)+0*x(3)+0*x(4)+0*x(5)+0*x(6)+0*x(7)+0*x(8)+0*x(9)+0*x(10)+0*x(11)+0*x(12)+0*x(13)+0*x(14)))
return v
def cons():

# d,e=args
con=({'type':'eq','fun':lambda x: x(1)-(2171.4/0.85*x(2))},
{'type': 'eq', 'fun': lambda x: (x(5)*x(6)*89.496/6.28)-x(7)-x(8)},
{'type': 'eq', 'fun': lambda x: x(9)*x(10)-x(10)*x(11)-x(8)*x(11)},
{'type': 'eq', 'fun': lambda x: x(12) - x(13)+x(11)},
{'type': 'eq', 'fun': lambda x: x(10) - 12500*3.1415},
{'type': 'ineq', 'fun': lambda x: x(6) - x(14)},
{'type': 'ineq', 'fun': lambda x: x(3) - x(4)-2.45},
{'type': 'eq', 'fun': lambda x: x(14) - 12.14*3.1415},
{'type': 'ineq', 'fun': lambda x: x(4) - 2.45},
{'type': 'ineq', 'fun': lambda x: 97.55 - x(4)},
{'type': 'ineq', 'fun': lambda x: x(3) },
{'type': 'ineq', 'fun': lambda x: 95.1 - x(3)},
{'type': 'ineq', 'fun': lambda x: x(5)-2.45-x(4)},
{'type': 'ineq', 'fun': lambda x: 100-x(5)}
)
return con
a=fun()
b=cons()
# print(b)
x0=np.asarray([3,4,3,2,3,4,3,2,3,4,3,2,3,2])
bounds=((0,None),(0,None),(0,None),(0,None),(0,None),(0,None),(0,None),(0,None),(0,None),(0,None),(0,None),(0,None),(0,None),(0,None))
res=minimize(a,x0,method='SLSQP',constraints=b,bounds=bounds)
print(res)
print(b)

``````
• 写回答
• 好问题 提建议
• 追加酬金
• 关注问题
• 邀请回答