Skip to content
Advertisement

cvxpy not allowing item assignment for MulExpression

I have a sparse matrix operation as part of the optimization constraint. I can implement the program in Matlab cvx, now I am trying to implement a cvxpy version. The problem is following constraint:

JavaScript

M is a sparse matrix, with only a few entries are declared variables. I don’t know a decent way to construct this constraint.

For example,

JavaScript

M is a 800* 800 matrix, with M[i, i]= a[i] for 0<=i<10, and the rest of M are all 0’s.

What I have done now is to declare M as M = cp.Variable((800,800), symmetric=True), and then add constraint like

constraints.append(M[i,i]==a[i]) for 0<=i<10; and constraints.append(M[i,j]==0) for the rest of M. But this way, it take lots of time and also the constraint list is large. I am wondering what is the best way to do so.

I also tried to do things like N[i,i] -= a[i] for 0<=i<10, but the item assignment is not allowed.

Advertisement

Answer

You can use this function to create a variable with a given sparsity pattern:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement