CVXOPT QP SOLVER:TypeError:'A'必须是10列的'd'矩阵

时间:2019-09-04 06:23:26

标签: python optimization quadratic cvxopt

我最近在使用CVXOPT QP解算器时遇到问题:我一直收到 TypeError:'A'必须是10列的'd'矩阵,而实际上我有一个矩阵尺寸(10,10)和类型d的A。

代码如下:

import numpy as np
from cvxopt import matrix
from cvxopt import solvers

var_all_numpy= np.array([[4,2,4,6,8,6,12,10,8,6],[8,4,6,6,8,10,6,6,2,2]])
var_1_numpy = np.array([4,2,4,6,8,6,12,10,8,6])
var_2_numpy = np.array([8,4,6,6,8,10,6,6,2,2])
y_numpy = np.array([-1,-1,-1,-1,-1,-1,1,1,1,1])
var = matrix(np.concatenate((var_1_numpy.reshape(-1,1),var_2_numpy.reshape(-1,1)),axis=1),tc='d')

Gram = np.dot(var,np.transpose(var))
print(Gram)

H = matrix(np.multiply(np.dot(y_numpy,np.transpose(y_numpy)),Gram),tc='d')
A = -np.array([[1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],[0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],[0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],[0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0],[0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0],[0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0],[0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0]])
A = A.astype(np.double)
A = matrix(A,tc='d')

B = matrix(np.zeros((10,1)),tc='d')
Aeq = matrix(np.transpose(y_numpy),tc='d')
Beq = matrix(0,tc='d')
ones = -matrix(np.ones((10,1)),tc='d')

sol = solvers.qp(H,ones,A,B,Aeq,Beq)

这是错误:

-----------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
     18 Beq = matrix(0,tc='d')
     19 ones = -matrix(np.ones((10,1)),tc='d')
---> 20 sol = solvers.qp(H,ones,A,B,Aeq,Beq)

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cvxopt/coneprog.py in qp(P, q, G, h, A, b, solver, kktsolver, initvals, **kwargs)
   4483             'residual as dual infeasibility certificate': dinfres}
   4484 
-> 4485     return coneqp(P, q, G, h, None, A,  b, initvals, kktsolver = kktsolver, options = options)

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cvxopt/coneprog.py in coneqp(P, q, G, h, dims, A, b, initvals, kktsolver, xnewcopy, xdot, xaxpy, xscal, ynewcopy, ydot, yaxpy, yscal, **kwargs)
   1912         if A.typecode != 'd' or A.size[1] != q.size[0]:
   1913             raise TypeError("'A' must be a 'd' matrix with %d columns" \
-> 1914                 %q.size[0])
   1915         def fA(x, y, trans = 'N', alpha = 1.0, beta = 0.0):
   1916             base.gemv(A, x, y, trans = trans, alpha = alpha, beta = beta)

TypeError: 'A' must be a 'd' matrix with 10 columns

对于许多用户来说,这显然是经常性的错误。我尝试了我在stackoverflow上看到的所有内容(如我在此处手动定义矩阵,使用astype将其设置为float ...),但仍然无法正常工作。

我x检查了一下:我确实有一个(10,10)矩阵,即'd'。

是什么问题?为什么在这种情况下CVXOPT不起作用?

0 个答案:

没有答案