我正在使用CVXOPT解决此简单的优化问题:
maximize X1 + X2
s.t:
X2 + X6 = 2
X1 + X2 + X5 = 2
X1 + X4 = 2
X1 >=0
X2 >=0
显然,这是一个非常简单的解决方案
X1 = 1
X2 = 1
(其余均为0)
但是,cvxopt将其完全错误。
这是我的工作:
>>> print A
[ 0.00e+00 1.00e+00 0.00e+00 0.00e+00 0.00e+00 1.00e+00]
[ 1.00e+00 1.00e+00 0.00e+00 0.00e+00 1.00e+00 0.00e+00]
[ 1.00e+00 0.00e+00 0.00e+00 1.00e+00 0.00e+00 0.00e+00]
>>> print b
[ 2.00e+00]
[ 2.00e+00]
[ 2.00e+00]
>>> print G
[-1.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00]
[ 0.00e+00 -1.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00]
>>> print h
[ 0.00e+00]
[ 0.00e+00]
>>> print c
[-1.00e+00]
[-1.00e+00]
[ 0.00e+00]
[ 0.00e+00]
[ 0.00e+00]
[ 0.00e+00]
(以上所有都是cvxopt的“矩阵”类型)
打印glpk.ilp(c,G,h,A,b,I = set([0,1,2,3,4,5]))[1]
GLPK Integer Optimizer, v4.43
5 rows, 6 columns, 9 non-zeros
6 integer variables, none of which are binary
Preprocessing...
3 rows, 5 columns, 7 non-zeros
5 integer variables, none 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 = 3
Solving LP relaxation...
GLPK Simplex Optimizer, v4.43
3 rows, 5 columns, 7 non-zeros
* 0: obj = 0.000000000e+00 infeas = 0.000e+00 (0)
PROBLEM HAS UNBOUNDED SOLUTION
None
最佳答案
这是实现以上内容的代码:
import cvxopt.glpk
import cvxopt
c=cvxopt.matrix([1,1,0,0,0,0])
G=cvxopt.matrix([[1.0,0,0,0,0,0], [0,1,0,0,0,0]])
h=cvxopt.matrix([0.0,0.0])
A=cvxopt.matrix([[0.0,1,0,0,0,6], [1,1,0,0,1,0], [1,0,0,1,0,0]])
b=cvxopt.matrix([2.0, 2, 2])
(status, c)=cvxopt.glpk.ilp(-c,-(G.T),-h,A.T,b,I=set([0,1,2,3,4,5]))
print(status, c)
结果是:
optimal [ 0.00e+00]
[ 2.00e+00]
[ 0.00e+00]
[ 2.00e+00]
[ 0.00e+00]
[ 0.00e+00]
我不确定如何获得所有解决方案...
关于python - cvxopt中的简单优化CVXOPT glpk MILP。没有优化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12554100/