gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:13:41 48 4
gpt4 key购买 nike

当我解决线性规划问题时,如下式,我希望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

最佳答案

来自docs :

method : str, optional Type of solver. At this time only ‘simplex’ is supported.

Simplex 无法处理完整性约束,因此您还无法使用 scipy.optimize.linprog 解决整数规划问题。您可以尝试其他库,例如 PuLP , PyomoCVXOPT .

关于python - 如何使用 scipy.optimize.linprog 获得整数解?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36326246/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com