Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
emanuelegdepaoli opened this issue Nov 17, 2021 · 1 comment

Comments

@emanuelegdepaoli
Copy link

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?

@bnaras
Copy link
Collaborator

bnaras commented Nov 18, 2021

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

@bnaras bnaras closed this as completed Oct 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants