如何使用scipy.optimize.linprog获得整数解?

2024-04-23 13:41:47 发布

您现在位置:Python中文网/ 问答频道 /正文

当我解决线性规划问题时,就像在下面的公式中一样,我希望x的结果都是int类型的

考虑以下问题:

最小化:f = -1*x[0] + 4*x[1]

服从:

-3*x[0] + 1*x[1] <= 6    
1*x[0] + 2*x[1] <= 4    
x[1] >= -3

其中:-inf <= x[0] <= inf

下一个是python编码器

>>> c = [-1, 4]
>>> A = [[-3, 1], [1, 2]]
>>> b = [6, 4]
>>> x0_bounds = (None, None)
>>> x1_bounds = (-3, None)
>>> res = linprog(c, A_ub=A, b_ub=b, bounds=(x0_bounds, x1_bounds),
...               options={"disp": True})
>>> print(res)
Optimization terminated successfully.
Current function value: -11.428571
Iterations: 2
status: 0
success: True
fun: -11.428571428571429
x: array([-1.14285714,  2.57142857])
message: 'Optimization terminated successfully.'
nit: 2

Tags: nonetrue类型res编码器公式intinf