以上代码报错 Invalid input for linprog: b_ub must not contain values inf, nan, or None
时间: 2024-04-21 21:22:39 浏览: 180
非常抱歉,我之前的回答仍然有误。在调用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:
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))
relaxed_b = np.append(relaxed_b, [np.inf])
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()
relaxed_b[relaxed_b == np.inf] = 1e8 # 将无穷大替换为一个大数
integer_cutting_plane(get_c(), relaxed_A, relaxed_b, bounds)
```
这次我在主函数中对`relaxed_b`进行了修改,将其中的无穷大值(inf)替换为一个大数(例如1e8),以避免报错。
再次对之前的错误表示歉意,并感谢你的耐心指正。希望这次修改能够解决问题。
阅读全文