Search code examples
pythonpython-3.xcvxpy

Looping Iteration over a PSD Constraint in CVXPY


I need to state PSD constraints element wise. The code is just a piece of SDP problem. It returns the following error though:

TypeError: 'PSD' object is not iterable

Isn't it allowed to construct more than PSD constraint in iteration loop?

import cvxpy as cp
import numpy as np
R=np.random.rand(2,1)
A=np.random.rand(2,2)


X=cp.Variable((2,1))

c=[1,4]

Z=R+A@X
W=R-A@X



const=[]

for i in range(2):
    const +=cp.bmat( [   
                            [c[i]**2,        Z[i],    0,        -W[i]],
                            [Z[i],          1,      W[i],          0],
                            [0,       W[i],    c[i]**2,        Z[i]],
                            [-W[i],         0,       Z[i],          1]      
                                                                                      ])>>0

Solution

  • CVXPY constraints is on the form of list so for matrix we should use .append the following works:

    import cvxpy as cp
    import numpy as np
    R=np.random.rand(2,1)
    A=np.random.rand(2,2)
    
    
    X=cp.Variable((2,1))
    
    c=[1,4]
    
    Z=R+A@X
    W=R-A@X
    
    
    
    const=[]
    
    for i in range(2):
        LMI =cp.bmat( [   
                                [c[i]**2,        Z[i],    0,        -W[i]],
                                [Z[i],          1,      W[i],          0],
                                [0,       W[i],    c[i]**2,        Z[i]],
                                [-W[i],         0,       Z[i],          1]      
                                                                                          ])
        const.append(LMI>>0)