Howto: CVXPY Matrix Inequality Constraints

5k views Asked by At

I am trying to formulate an optimization problem in the following way:

  1. My optimization variable x is a n*n matrix.
  2. x should be PSD.
  3. It should be in the range 0<=x<=I. Meaning, it would be in the range from the all zeros square matrix to n dimensional identity matrix.

Here is what I have come up with so far:

import cvxpy as cp
import numpy as np
import cvxopt

x = cp.Variable((2, 2), PSD=True)
a = cvxopt.matrix([[1, 0], [0, 0]])
b = cvxopt.matrix([[.5, .5], [.5, .5]])
identity = cvxopt.matrix([[1, 0], [0, 1]])
zeros = cvxopt.matrix([[0, 0], [0, 0]])

constraints = [x >= zeros, x <= identity]
objective = cp.Maximize(cp.trace(x*a - x * b))
prob = cp.Problem(objective, constraints)
prob.solve()

This gives me a result of [[1, 0], [0, 0]] as the optimal x, with a maximum trace of .5. But that should not be the case. Because I have done this same program in CVX in matlab and I got the answer matrix as [[.85, -.35], [-.35, .14]] with an optimal value of .707. Which is correct.

I think my constraint formulation is not correct or not following cvxpy standards. How do I enforce the constraints in my program correctly?

(Here is my matlab version of the code:)

a = [1, 0; 0, 0];
b = [.5, .5; .5, .5];

cvx_begin sdp

variable x(2, 2) hermitian;

maximize(trace(x*a  - x*b))
subject to
x >= 0;
x <= eye(2);

cvx_end

TIA

1

There are 1 answers

0
Jacques Kvam On BEST ANSWER

You need to use the PSD constraint. If you compare a matrix against a scalar, cvxpy does elementwise inequalities unless you use >> or <<. You already have constrained x to be PSD when you created it so all you need to change is:

constraints = [x << np.eye(2)]

Then I get your solution:

array([[ 0.85355339, -0.35355339],
       [-0.35355339,  0.14644661]])