I am trying to solve a linear program with CVXOPT. There are 2n variables, x_1,...,x_2n. The LP is in the form of
min x_{n+1}+...+x_{2n}
s.t. Ax \leq b
Here, A and b are fixed. It seems very straightforward from here. Since I am optimizing the sum of the second half of the variables, I create a vector of n zeroes and n ones: c = (0,...,0,1,...,1) and have the following code (assume A and b are already computed):
c = [1]*(2*k + 2)
for i in range(k + 1):
c[i] = 0
c = matrix(c)
sol=solvers.lp(c,A,b)
print(sol['x'])
This code is straight from CVXOPT documentation. However, I get an error saying:
TypeError: 'c' must be a dense column matrix
I looked this up but it seems to me that calling matrix() should have converted c to the appropriate type. Does anyone know how to fix this?
The problem is that the constructor of the matrix object interprets it as being of integer type, whereas it should be double. If you populate your list with explicitly double numbers it should work.
From the source code of cvxopt:
if type(c) is not matrix or c.typecode != 'd' or c.size[1] != 1:
raise TypeError("'c' must be a dense column matrix")
checking..
import cvxopt
k = 20
c = [1]*(2*k + 2)
for i in range(k + 1):
c[i] = 0
c = cvxopt.matrix(c)
print c.typecode # Prints 'i'
Solution:
c = [1.]*(2*k + 2)
print c.typecode # Prints 'd'