CVXOPT:求解一个简单的整数线性规划程序

2024-05-13 22:17:53 发布

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

我使用CVXOPT来解决一个非常简单的问题:

min -7890424934354.171875*x1 -7890424934354.274414*x2 -7890424934354.246093*x3
s.t: 
  x1 + x2 + x3 = 1
  x1,x2,x3 are binary

我们可以看出,最优解显然应该是:

^{pr2}$

然而,使用CVXOPT的ILP时,我没有得到正确的答案(我知道上面的问题太简单了,无法使用ILP,但我只是好奇)。关于CVXOPT的ILP的详细描述是here。在

我的程序是这样的:

from cvxopt.glpk import ilp
from cvxopt import matrix
c = matrix([-7890424934354.171875,-7890424934354.274414,-7890424934354.246093],tc='d')
G = matrix(0.0, (1,3)) #since I do not have a constraint like G*x <= h, I make them zeros here
h = matrix(0.0, (1,1))
A = matrix([1,1,1],tc='d')
b = matrix(1,tc='d')
(status, x) = ilp(c,G,h,A.T,b,B=set([0,1,2]))

结果是:

GLPK Integer Optimizer, v4.61
2 rows, 3 columns, 3 non-zeros
3 integer variables, all of which are binary
Preprocessing...
1 row, 3 columns, 3 non-zeros
3 integer variables, all of which are binary
Scaling...
 A: min|aij| =  1.000e+00  max|aij| =  1.000e+00  ratio =  1.000e+00
Problem data seem to be well scaled
Constructing initial basis...
Size of triangular part is 1
Solving LP relaxation...
GLPK Simplex Optimizer, v4.61
1 row, 3 columns, 3 non-zeros
*     0: obj =  -7.890424934e+12 inf =   0.000e+00 (0)
OPTIMAL LP SOLUTION FOUND
Integer optimization begins...
+     0: mip =     not found yet >=              -inf        (1; 0)
+     0: >>>>>  -7.890424934e+12 >=  -7.890424934e+12   0.0% (1; 0)
+     0: mip =  -7.890424934e+12 >=     tree is empty   0.0% (0; 1)
INTEGER OPTIMAL SOLUTION FOUND
optimal
[ 0.00e+00]
[ 0.00e+00]
[ 1.00e+00]

不会返回最优解。在

但如果我把目标函数改为-171875*x1-274414*x2-246093*x3,我就能得到正确的答案:x1=0,x2=1,x3=0。在

我真的很困惑为什么会这样:

我首先猜测了像-7890424934354.171875这样的浮点值在传递给ILP时是否会失去精度,但似乎这不是原因所在。在

另一个猜测是,如果我没有G*x<;=h这样的约束,我就不应该生成G和h零。但是如果我想在G和h为空时使用ILP,因为它需要

  G: mxn dense or sparse 'd' matrix with m>=1

如果有什么建议,我将不胜感激。提前致谢。在


Tags: columnsof答案zerosminmatrixaretc