cvxgrp / CVXR

An R modeling language for convex optimization problems.

Home Page:https://cvxr.rbind.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Solving SDP problem with cvx - difference between MATLAB and R solution

emanuelegdepaoli opened this issue · comments

I solved the following Linear Matrix Inequality (LMI) problem using cvx in Matlab:

Lhs = [19.467593196, 1.82394007, 0.1625838, 0.01685267, 0.002495194;
1.823940068, 1.78664305, 0.9845668, 0.32951706, 0.010431878;
0.162583843, 0.98456679, 1.2333818, 0.92276329, 0.132643463;
0.016852668, 0.32951706, 0.9227633, 1.55698000, 0.848190932;
0.002495194, 0.01043188, 0.1326435, 0.84819093, 0.638889503];

S = [0.001, -0.001, 0, 0, 0;
-0.001, 0.001, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0.001 -0.001;
0, 0, 0, -0.001,  0.001];


cvx_begin sdp
   variable t;
   minimize t;
   Lhs+t*S >= 0;
cvx_end

The result is correct and it is 28.312.

I need to solve the same problem in R. As far as I understood, it can't be expressed as a LMI with CVXR. Thus, I wrote the dual problem and first checked that the solution is the same in Matlab:

cvx_begin sdp
    variable X(5,5) symmetric;
    maximize -trace(Lhs*X);
    trace(S*X) == 1;
    X >= 0;
cvx_end

and the results coincide.

Unfortunately, when I write the problem in R with CVXR the solution is totally wrong:

Lhs = matrix(c(19.467593196, 1.82394007, 0.1625838, 0.01685267, 0.002495194,
               1.823940068, 1.78664305, 0.9845668, 0.32951706, 0.010431878,
               0.162583843, 0.98456679, 1.2333818, 0.92276329, 0.132643463,
               0.016852668, 0.32951706, 0.9227633, 1.55698000, 0.848190932,
               0.002495194, 0.01043188, 0.1326435, 0.84819093, 0.638889503), ncol = 5, byrow = T)

S = matrix(c(0.001, -0.001, 0, 0, 0,
             -0.001, 0.001, 0, 0, 0,
             0, 0, 0, 0, 0,
             0, 0, 0, 0.001, -0.001,
             0, 0, 0, -0.001,  0.001), ncol = 5, byrow = T)

X = Variable(k, k, PSD = T)
constr = list(matrix_trace(S%*%X) == 1,
              X >= 0)
prob = Problem(Maximize(-matrix_trace(Lhs%*%X)), constr)
result = solve(prob, solver = "SCS")
-sum(diag(Lhs%*%result$getValue(X)))
[1] -640.1262

Am I doing something wrong?

You're solving a different problem, I'd say.