我正在使用CVXOPT来解决一个非常简单的问题:
min -7890424934354.171875*x1 -7890424934354.274414*x2 -7890424934354.246093*x3
s.t:
x1 + x2 + x3 = 1
x1,x2,x3 are binary
我们可以看到最佳解决方案显然应该是:
x1 =0; x2 = 1; x3 = 0
但是我没有使用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&lt; = h这样的约束,我不应该使G和h为零。但是如果我想在G和h为空时使用ILP,我需要做什么,因为它需要
G: mxn dense or sparse 'd' matrix with m>=1
我很感激任何建议。非常感谢提前。