Python ValueError:linprog的输入无效
这是我正在执行的代码:Python ValueError:linprog的输入无效,python,numpy,Python,Numpy,这是我正在执行的代码: import numpy as np z = np.array([170, 160, 175, 180, 195]) a = np.array([[1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 1, 0, 0, 0], [1, 1, 1, 0, 0], [0, 1, 1, 0, 0], [0, 0, 1, 1, 0], [0, 0, 1, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]]) b = np.a
import numpy as np
z = np.array([170, 160, 175, 180, 195])
a = np.array([[1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 1, 0, 0, 0], [1, 1, 1, 0, 0], [0, 1, 1, 0, 0], [0, 0, 1, 1, 0], [0, 0, 1, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]])
b = np.array([48, 79, 65, 87, 64, 73, 82, 43, 52, 15])
x1_bound = x2_bound = x3_bound = x4_bound = x5_bound =(0, None)
from scipy import optimize
res = optimize.linprog(z, A_ub=-a, b_ub=-b,bounds=(x1_bound, x2_bound, x3_bound, x4_bound, x5_bound))
print(res)
这是我得到的
>ValueError: Invalid input for linprog: b_ub must be a 1-D array; b_ub must not have more than one non-singleton dimension and the number of rows in A_ub must equal the number of values in b_ub
如何修复它?您需要在
a
数组中添加另一行,或者从b
数组中删除一个值
a = np.array([[1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 1, 0, 0, 0], [1, 1, 1, 0, 0], [0, 1, 1, 0, 0], [0, 0, 1, 1, 0], [0, 0, 1, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]])
b = np.array([48, 79, 65, 87, 64, 73, 82, 43, 52])
输出:
con: array([], dtype=float64)
fun: 37824.99999983183
message: 'Optimization terminated successfully.'
nit: 6
slack: array([ 1.29979583e-10, -4.48082460e-10, 1.40000000e+01, 3.10000000e+01,
6.00000000e+00, 9.00000000e+00, -5.65137270e-10, 6.44249099e-11,
4.09272616e-12])
status: 0
success: True
x: array([48., 31., 39., 43., 52.])