Sue*_*Sue 5 python optimization integer-programming cvxopt
我正在使用 CVXOPT 来解决一个非常简单的问题:
min -7890424934354.171875*x1 -7890424934354.274414*x2 -7890424934354.246093*x3
s.t:
x1 + x2 + x3 = 1
x1,x2,x3 are binary
Run Code Online (Sandbox Code Playgroud)
我们可以看到最优解显然应该是:
x1 =0; x2 = 1; x3 = 0
Run Code Online (Sandbox Code Playgroud)
但是我没有从 CVXOPT 使用 ILP 得到正确答案(我知道上面的问题太简单了,无法使用 ILP,但我只是好奇)。关于 CVXOPT 的 ILP 的详细描述在这里。
我的程序是这样的:
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]))
Run Code Online (Sandbox Code Playgroud)
结果是:
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]
Run Code Online (Sandbox Code Playgroud)
这不会返回最佳解决方案。
但是如果我将目标函数更改为-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
Run Code Online (Sandbox Code Playgroud)
我很感激任何建议。提前谢谢了。