Cvxopt公司

Cvxopt提供了许多解决凸优化问题的例程,例如线性和二次规划包。它还有一个非常好的稀疏矩阵库,它提供了一个到umfpack(与matlab使用的相同的稀疏矩阵求解器)的接口,它也有一个很好的接口到lapack。有关cvxopt的更多详细信息,请参阅其文档: http://cvxopt.org/userguide/index.html

稀疏矩阵用三元组表示法表示,即非零值、行索引和列索引的列表。这将在内部转换为压缩的稀疏列格式。例如我们要输入矩阵

\[左(begin{array}{ccccc}2&3&0&0&0\3&0&4&0&6\0&-1&-3&2&0\0&0&1&0&0\0&4&2&0&1end{array}right)\]

通过

sage: import numpy
sage: from cvxopt.base import spmatrix
sage: from cvxopt.base import matrix as m
sage: from cvxopt import umfpack
sage: Integer=int
sage: V = [2,3, 3,-1,4, 4,-3,1,2, 2, 6,1]
sage: I = [0,1, 0, 2,4, 1, 2,3,4, 2, 1,4]
sage: J = [0,0, 1, 1,1, 2, 2,2,2, 3, 4,4]
sage: A = spmatrix(V,I,J)

解方程 \(AX=B\)\(B=[1,1,1,1,1]\) ,我们可以做以下事情。

sage: B = numpy.array([1.0]*5)
sage: B.shape=(5,1)
sage: print(B)
[[1.]
 [1.]
 [1.]
 [1.]
 [1.]]


sage: print(A)
[ 2.00e+00  3.00e+00     0         0         0    ]
[ 3.00e+00     0      4.00e+00     0      6.00e+00]
[    0     -1.00e+00 -3.00e+00  2.00e+00     0    ]
[    0         0      1.00e+00     0         0    ]
[    0      4.00e+00  2.00e+00     0      1.00e+00]
sage: C=m(B)
sage: umfpack.linsolve(A,C)
sage: print(C)
[ 5.79e-01]
[-5.26e-02]
[ 1.00e+00]
[ 1.97e+00]
[-7.89e-01]

注意溶液存储在 \(B\) 后来。还要注意m(B),这会将numpy数组转换成cvxopt能够理解的格式。您可以使用cvxopt自己的matrix命令直接创建cvxopt矩阵,但我个人认为numpy数组更好。还请注意,我们显式地设置了numpy数组的形状,以明确它是一个列向量。

我们可以通过

sage: RealNumber=float
sage: Integer=int
sage: from cvxopt.base import spmatrix
sage: from cvxopt import amd
sage: A=spmatrix([10,3,5,-2,5,2],[0,2,1,2,2,3],[0,0,1,1,2,3])
sage: P=amd.order(A)
sage: print(P)
[ 1]
[ 0]
[ 2]
[ 3]

对于一个简单的线性规划例子,如果我们想解决

\[begin{array}{cc}text{minimize}&-4x1-5x2\text{subject to}&2x_1+xu2le3\&xu1+2xu2le3\&xu1ge0\&xu2ge0\结束{array}\]
sage: RealNumber=float
sage: Integer=int
sage: from cvxopt.base import matrix as m
sage: from cvxopt import solvers
sage: c = m([-4., -5.])
sage: G = m([[2., 1., -1., 0.], [1., 2., 0., -1.]])
sage: h = m([3., 3., 0., 0.])
sage: sol = solvers.lp(c,G,h) #random
     pcost       dcost       gap    pres   dres   k/t
 0: -8.1000e+00 -1.8300e+01  4e+00  0e+00  8e-01  1e+00
 1: -8.8055e+00 -9.4357e+00  2e-01  1e-16  4e-02  3e-02
 2: -8.9981e+00 -9.0049e+00  2e-03  1e-16  5e-04  4e-04
 3: -9.0000e+00 -9.0000e+00  2e-05  3e-16  5e-06  4e-06
 4: -9.0000e+00 -9.0000e+00  2e-07  1e-16  5e-08  4e-08
sage: print(sol['x'])    # ... below since can get -00 or +00 depending on architecture
[ 1.00e...00]
[ 1.00e+00]