本文介绍了如何使用 scipy.optimize.linprog 获得整数解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我解决线性规划问题时,就像下面的公式,我希望x的结果都是int类型
考虑以下问题:
最小化:f = -1*x[0] + 4*x[1]
受制于:
-3*x[0] + 1*x[1] = -3
其中:-inf
接下来是python编码器
>>>c = [-1, 4]>>>A = [[-3, 1], [1, 2]]>>>b = [6, 4]>>>x0_bounds =(无,无)>>>x1_bounds = (-3, 无)>>>res = linprog(c, A_ub=A, b_ub=b, bounds=(x0_bounds, x1_bounds),... options="disp": True})>>>打印(资源)优化已成功终止.当前函数值:-11.428571迭代次数:2状态:0成功:正确乐趣:-11.428571428571429x: 数组([-1.14285714, 2.57142857])消息:'优化成功终止.'尼特:2 解决方案
来自 文档:
method : str,可选求解器类型.此时只有单纯"是支持.
Simplex 无法处理完整性约束,因此您还无法使用 scipy.optimize.linprog 解决整数规划问题.您可以尝试其他库,例如 PuLP、Pyomo 或 CVXOPT.
When I solve the problem of Linear Programming, like in the following formula, I want the result of x all to be int type
Consider the following problem:
Minimize: f = -1*x[0] + 4*x[1]
Subject to:
-3*x[0] + 1*x[1] <= 6
1*x[0] + 2*x[1] <= 4
x[1] >= -3
where: -inf <= x[0] <= inf
next is the python coder
>>> 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
解决方案
From the docs:
Simplex cannot handle integrality constraints so you cannot solve integer programming problems with scipy.optimize.linprog yet. You can try other libraries like PuLP, Pyomo or CVXOPT.
这篇关于如何使用 scipy.optimize.linprog 获得整数解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!