以上内容仍然报错 Invalid input for linprog: b_ub must not contain values inf, nan, or None
时间: 2024-04-17 20:24:47 浏览: 177
linux,Ubuntu系统报错
非常抱歉,我之前的回答依然有误。在调用linprog函数时,确实不允许b_ub参数包含无穷大(inf)、NaN或None的值。
这里是修正后的代码:
```python
import numpy as np
from scipy.optimize import linprog
def integer_cutting_plane(c, A, b, bounds):
relaxed_A = A
relaxed_b = b
while True:
# 通过筛选出满足要求的约束来更新约束矩阵和约束向量
valid_constraints = relaxed_A @ relaxed_b < np.inf
relaxed_A = np.vstack((relaxed_A[valid_constraints], relaxed_A[-1]))
relaxed_b = np.append(relaxed_b[valid_constraints], np.inf)
res = linprog(c=c, A_ub=relaxed_A, b_ub=relaxed_b, bounds=bounds)
x = res.x
if all(int(val) == val for val in x):
return x.astype(int)
new_constraint = (relaxed_A @ x <= relaxed_b)
relaxed_A = np.vstack((relaxed_A, new_constraint))
def get_bounds():
return [(0, None), (0, None)]
def get_c():
return np.array([40, 90])
def get_A():
return np.array([[-9, -7], [-7, -20]])
def get_b():
return np.array([-56, -70])
if __name__ == '__main__':
bounds = get_bounds()
relaxed_A = get_A()
relaxed_b = get_b()
integer_cutting_plane(get_c(), relaxed_A, relaxed_b, bounds)
```
这次,在调用linprog函数之前,我们通过筛选出满足条件的约束来更新约束矩阵和约束向量。即,我们通过判断`relaxed_A @ relaxed_b < np.inf`来选择满足要求的约束,并将它们重新组合成新的约束矩阵和约束向量。
这样,我们可以避免将无穷大(inf)传递给linprog函数,从而解决报错问题。
再次对之前的错误表示歉意,并感谢你的耐心指正。希望这次修改能够解决问题。
阅读全文