Scipy最小化-TypeError:“ int”对象不可调用

时间:2018-12-11 15:56:10

标签: python python-3.x scipy

我对Python编程非常陌生,我正在尝试编写代码来优化当净库存低于某个特定值时必须订购的数量。我收到一个错误,我想这与终端告诉我的第42行有关。我不知道是否有人可以帮助我,但我将在下面附上我的代码以及出现的错误,不确定是否在做任何明显的错误。预先感谢。

编辑:当我注释使用最小化函数的行时,没有任何错误。不知道我在做什么错。

代码:

import random
import numpy as np
from scipy.optimize import minimize

def objective (h, b, Ip, Im, NIt, s, Qt):
    return h*Ip+b*Im

def constraint1 (h, b, Ip, Im, NIt, s, Qt):
    return NIt-s

def constraint2 (h, b, Ip, Im, NIt, s, Qt):
    return Qt

def test(T, d, RT, LT, h, b, I0, s):
    i = 1
    It = [0] * T
    Qt = [0] * T
    NIt = [0] * T
    It[0] = I0 - d[0]
    Qt[0] = d[0]
    NIt[0] = I0
    while i < T:
        if (i - LT) >= 0:
            It[i] = It[i-1] - d[i] + Qt[i-LT]
        else:
            It[i] = It[i-1] - d[i]
        NIt[i] = NIt[i-1] - d[i-1] + Qt[i-1]
        if It[i-1] > 0:
            Ip = It[i-1]
            Im = 0
        if It[i-1] < 0:
            Ip = 0
            Im = It[i-1]
        if It[i-1] == 0:
            Ip = 0
            Im = 0

        x0 = [h, b, Ip, Im, NIt[i], s, Qt[i]]
        con1 = {'type': 'ineq', 'fun': constraint1(h, b, Ip, Im, NIt[i], s, 
Qt[i])}
        con2 = {'type': 'ineq', 'fun': constraint2(h, b, Ip, Im, NIt[i], s, 
Qt[i])}
        cons = [con1, con2]
        sol = minimize(objective, x0, constraints=cons)

        Qt[i] = sol.Qt
        i += 1
    return It, NIt, Qt

d = []
for j in range(30):
    d.append(random.randint(0, 2)) 

[It, NIt, Qt] = test(30, d, 1, 1, 1, 1, 1, 2)

错误:

Traceback (most recent call last):
  File "c:/Users/Fernandes/Desktop/HelloWorld/app1.py", line 52, in <module>
    [It, NIt, Qt] = test(30, d, 1, 1, 1, 1, 1, 2)
  File "c:/Users/Fernandes/Desktop/HelloWorld/app1.py", line 42, in test
    sol = minimize(objective, x0, constraints=cons)
  File "C:\Users\Fernandes\AppData\Local\Programs\Python\Python37-32\lib\site-packages\scipy\optimize\_minimize.py", line 611, in minimize
    constraints, callback=callback, **options)
  File "C:\Users\Fernandes\AppData\Local\Programs\Python\Python37-32\lib\site-packages\scipy\optimize\slsqp.py", line 315, in _minimize_slsqp
    for c in cons['ineq']]))
  File "C:\Users\Fernandes\AppData\Local\Programs\Python\Python37-32\lib\site-packages\scipy\optimize\slsqp.py", line 315, in <listcomp>
    for c in cons['ineq']]))
TypeError: 'int' object is not callable

1 个答案:

答案 0 :(得分:0)

minimize期望一个ndarray,并且您正在将其传递给数组。 尝试:

x0 = np.array([h, b, Ip, Im, NIt[i], s, Qt[i]])