What does the piece of code do?

I have a little piece of code of CVX but I don’t understand it. Can any body help me here? The piece of code is as follows:

cvx_begin sdp quiet    
    variable x(N*M,1) complex 
    variable F(M*N,M*N) hermitian 
    minimize(norm(y-x)) 
    subject to
        [F, x; x', lambda^2]>=0 
        for idxN1 = 0 : 1 : N-1
            for idxN2 = 0 : 1 : N-1
                Ftmp = F(idxN1*M+1:idxN1*M+M, idxN2*M+1:idxN2*M+M);
                for delta = -(M-1) : 1 : M-1
                    if ((idxN1==idxN2) && (delta==0))
                        sum(diag(Ftmp, delta)) <= 1/N;
                    else
                        sum(diag(Ftmp, delta)) == 0;
                    end
                end
            end
        end
cvx_end

The code solves the optimization problem specified by the code, namely, minimizing the norm of a difference of a complex argument subject to a semidefinite constraint and linear (affine) inequality and equality constraints.

This forum is not a game show “What does this random CVX code I found or someone gave me do?”

The forum is for providing help in using CVX to solve convex optimization problems specified by the questioner.

Thank you very much for your kind response dear Mark_L_Stone. Actually I am new and I have downloaded a MATLAB code in which this piece of code is used which I didn’t understand. Pardon me, but I am stuck because of this code. So I want to understand it well. When I run the total code in MATLAB, I don’t understand how the variable x is getting values. The values of variable y are visible how it get calculated, but I don’t understand from where the variable x is getting values? Can you guide me in this regard please?

y is input data to CVX. It gets its value however it was assigned in MATLAB prior to the CVX code being executed.

x is a declared (optimization) variable in this CVX program. If CVX /solver solves the problem to reported optimality (Status: Solved), after CVX execution (cvx_end),x becomes a regular MATLAB variable, containing the optimal value obtained in the optimization.

You should carefully read the CVX Users’ Guide http://cvxr.com/cvx/doc/ , and try to understand and try out the simpler problems there before trying to deal with more complicated problems. And that presumes you have sufficient understanding of convex optimization, which you can obtain, given a strong enough math background and enough time and effort, from https://web.stanford.edu/~boyd/cvxbook/ .

Note: If you remove quiet' from the statement cvx_begin sdp quiet , when the code is executed, it will show the CVX and solver output (but will not display the optimal value of x)/.

Thank you very much for your prompt response. I studied that document as well. The 1st command means its the beginning of cvx but in such a way that output will not be displayed on the screen during solution. The 2nd and 3rd lines mean these are variables of given sizes. The problem starts in 4th line i.e. in minimize(norm(y-x)). In this values of “y” are coming from MATLAB from which values of “x” are getting subtracted but “x” has got no values yet. So there starts my confusion. Further after subject to, What does [F, x; x’, lambda^2]>=0 do? Likewise what does
Ftmp = F(idxN1M+1:idxN1M+M, idxN2M+1:idxN2M+M); do?
These statements have increased my confusion and I didn’t find any help for these in that document there. Then I posted it here to understand it well as I am stuck because of this. Let me share the whole code:

close all; clear all; clc;
vec = @(MAT) MAT(:);
vecH = @(MAT) MAT(:).’;
% M = 5;
M = 10;
N = 4;
% N = 10;
d = 0.5;
K = 3;
SNR_dB = 20;

steerVecT = @(ang) exp(1j2pid[0:M-1].‘sin(vecH(ang)));
steerVecR = @(ang) exp(1j
2pid*[0:N-1].’*sin(vecH(ang)));
angleGrid = [-80:1e-2:80].’;
steerGrid = steerMatTR(deg2rad(angleGrid), steerVecT, steerVecR);

rng(222);
targetAngle = [-30, 0, 40].’+rand(K, 1);

A = ones(K, 1);

steerM = steerMatTR(deg2rad(targetAngle), steerVecT, steerVecR);
r = steerM*A;

noiseVar = r’ * r / length® / db2pow(SNR_dB);
w = sqrt(noiseVar / 2) * (randn(size®) + 1j * randn(size®));

y = r+w;

lambda = 2sqrt(MNlog(MN))sqrt(noiseVar);
cvx_begin sdp quiet
variable x(N
M,1) complex
variable F(MN,MN) hermitian
minimize(norm(y-x))
subject to
[F, x; x’, lambda^2]>=0
for idxN1 = 0 : 1 : N-1
for idxN2 = 0 : 1 : N-1
Ftmp = F(idxN1M+1:idxN1M+M, idxN2M+1:idxN2M+M);
for delta = -(M-1) : 1 : M-1
if ((idxN1==idxN2) && (delta==0))
sum(diag(Ftmp, delta)) <= 1/N;
else
sum(diag(Ftmp, delta)) == 0;
end
end
end
end
cvx_end
spectrumANM = abs(x’*steerGrid).^2;
spectrumANM = spectrumANM/max(spectrumANM);
figure; plot(angleGrid, spectrumANM);

% User defined function
function steerM = steerMatTR(targetAngle, steerVecT, steerVecR)

steerA = steerVecT(targetAngle);
steerB = steerVecR(targetAngle);
steerM = zeros(size(steerA, 1)*size(steerB, 1), length(targetAngle));
for idxK = 1 : 1 : length(targetAngle)
steerM(:, idxK) = kron(steerB(:, idxK), steerA(:, idxK));
end

1 Like

CVX finds the values of x and F which minimize norm(x-y), subject to the various constraints specified. The value of x is determined by the solver, which is called by CVX after cvx_end. the value of x is not determined until after cvx_end.

if you really want to understand the code, including why this optimization problem is being solved and its meaning, I suggest you consult the program documentation or the code author.

Thank you very much dear Mark_L_Stone for your help.

Hi dear Mark_L_Stone!
Hope you are doing well. According to your instructions, I studied the paper again to find the email of the author to ask him about this problem, but there is no email address there. It means I cannot contact him. Can you just guide me how the CVX finds the value of the variable x as I don’t understand it due to which I cannot proceed further?
Rgards,

To be productive , can you help to share the paper in this forum for the audience review and understanding.

Supra

CVX finds the value of the variable x by starting a solver once the whole optimization process has been defined (that is, when you reach cvx_end). For example if you write the optimization problem

\textrm{minimize}\quad x-5

\mathrm{subject\ to}\ 2x+3\geq 7

In CVX it is something like

cvx_begin
  variable x
  minimize x-5
  subject to
     2x+3>=7
cvx_end

At the beginning, like you say, x has got no values yet, but then you solve the problem and get x=2 and optimal objective value 2-5=-3.

Now I solved this problem in my head, and a semidefinite solver solving your problem is a bit more complicated than that, but the principle is the same. You (probably, like most users) can treat it as a black box, and I think it is more important for you to understand 1) basics of what optimization problems actually are and 2) what the given problem means for the paper you are reading, but that is out of the scope of this forum. The code you posted probably has a mathematical representation somewhere in the paper, similarly as it is in my example.

Thanks a lot dear Supra and Michal_Adamaszek for your responses. Yes, I can share the paper over here.

1 Like

So now you see your cvx code corresponds to equation (25) in the paper or maybe some small modification of it.

1 Like

Hi Sadiqakbar

You could addressed to one of the author Lan Tang; at tanglan@nju.edu.cn if you need
further clarifiation

Supra

Thanks a lot dear Michal_Adamaszek and Supra for your kind responses. Dear Supra where was this email address and is it correct?
Regards,

Dear Michal_Adamaszek! Can you convert it in MATLAB code? I mean if I want to write a piece of MATLAB code for whole given cvx code, what will be that? If you can convert it in MATLAB, then I will try to use MATLAB optimtool instead of this as I understand that well.
Regards,

1 Like

Hi sadiqakbar, the email address is clearly shown on the first page of your research article under correspondence

Supra

Thank you dear Supra for your help. Yes, you may be right but its not showing in my system. I don’t know why? May be there is some technical reason that I don’t understand. However, thanks for your help.

1 Like

HI sadiqakbar,

I had run on both SDPT3 and SeDuMI solvers and the results are the same as optimised

Supra

Here’s the results


Using SDPT3 Solver

Calling SDPT3: 1766 variables, 385 equality constraints

num. of constraints = 385
dim. of sdp var = 82, num. of sdp blk = 1
dim. of socp var = 81, num. of socp blk = 1
dim. of linear var = 4


SDPT3: Infeasible path-following algorithms


version predcorr gam expon scale_data
HKM 1 0.000 1 0
it pstep dstep pinfeas dinfeas gap prim-obj dual-obj cputime

0|0.000|0.000|8.3e+02|4.7e+01|7.5e+05| 1.716126e+02 0.000000e+00| 0:0:00| chol 1 1
1|0.921|1.000|6.6e+01|5.0e-01|7.6e+04| 3.655561e+02 -4.538790e+02| 0:0:00| chol 1 1
2|0.973|1.000|1.8e+00|1.5e-01|2.3e+03| 1.496204e+01 -2.976209e+02| 0:0:01| chol 1 1
3|0.929|0.984|1.3e-01|1.7e-02|1.8e+02| 1.212906e+01 -2.493119e+01| 0:0:01| chol 1 1
4|0.988|0.987|1.5e-03|1.7e-03|2.0e+01| 1.066935e+01 -8.300404e+00| 0:0:01| chol 1 1
5|0.810|1.000|2.8e-04|4.4e-04|3.4e+00| 1.015243e+01 6.802744e+00| 0:0:01| chol 1 1
6|0.984|0.860|4.3e-06|1.3e-04|6.7e-01| 9.955654e+00 9.286639e+00| 0:0:01| chol 1 1
7|0.870|0.639|5.5e-07|4.9e-05|3.4e-01| 9.888071e+00 9.550291e+00| 0:0:01| chol 1 1
8|1.000|0.889|3.0e-09|5.7e-06|1.3e-01| 9.866067e+00 9.731251e+00| 0:0:01| chol 1 1
9|0.633|1.000|1.7e-09|1.6e-08|4.0e-02| 9.858523e+00 9.819005e+00| 0:0:01| chol 1 1
10|1.000|0.929|1.1e-15|2.8e-09|7.5e-03| 9.852428e+00 9.844936e+00| 0:0:01| chol 1 1
11|0.915|1.000|4.6e-15|1.5e-10|8.5e-04| 9.851826e+00 9.850979e+00| 0:0:01| chol 1 1
12|0.979|0.978|1.1e-14|4.3e-12|2.4e-05| 9.851691e+00 9.851667e+00| 0:0:01| chol 1 1
13|0.993|0.993|4.3e-15|1.0e-12|3.2e-07| 9.851687e+00 9.851687e+00| 0:0:01| chol 1 1
14|0.993|0.993|2.7e-12|1.0e-12|4.4e-09| 9.851687e+00 9.851687e+00| 0:0:01|
stop: max(relative gap, infeasibilities) < 1.49e-08

number of iterations = 14
primal objective value = 9.85168696e+00
dual objective value = 9.85168696e+00
gap := trace(XZ) = 4.38e-09
relative gap = 2.11e-10
actual relative gap = 2.12e-10
rel. primal infeas = 2.72e-12
rel. dual infeas = 1.01e-12
norm(X), norm(y), norm(Z) = 2.9e+01, 8.2e+00, 9.6e+00
norm(A), norm(b), norm© = 2.4e+01, 2.2e+01, 2.0e+00
Total CPU time (secs) = 1.13
CPU time per iteration = 0.08
termination code = 0
DIMACS: 3.2e-12 0.0e+00 1.0e-12 0.0e+00 2.1e-10 2.1e-10

Status: Solved
Optimal value (cvx_optval): +9.85169

SEDUmI solver Result

Calling SeDuMi: 1766 variables, 385 equality constraints

SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 385, order n = 48, dim = 3448, blocks = 3
nnz(A) = 1765 + 0, nnz(ADA) = 148225, nnz(L) = 74305
it : by gap delta rate t/tP t/tD* feas cg cg prec
0 : 4.33E+00 0.000
1 : 6.31E+00 3.49E-01 0.000 0.0806 0.9900 0.9900 1.28 1 1 1.9E+00
2 : 7.46E+00 7.67E-02 0.000 0.2196 0.9000 0.9000 1.00 1 1 4.5E-01
3 : 8.43E+00 3.68E-02 0.000 0.4796 0.9000 0.9000 1.47 1 1 1.6E-01
4 : 8.93E+00 2.44E-02 0.000 0.6632 0.9000 0.9000 1.79 1 1 9.0E-02
5 : 9.39E+00 1.38E-02 0.000 0.5651 0.9000 0.9000 2.10 1 1 3.6E-02
6 : 9.54E+00 9.26E-03 0.000 0.6722 0.9000 0.9000 1.51 1 1 2.4E-02
7 : 9.74E+00 3.52E-03 0.000 0.3797 0.9000 0.9000 1.59 1 1 7.4E-03
8 : 9.80E+00 1.71E-03 0.000 0.4857 0.9000 0.9000 1.13 1 1 3.6E-03
9 : 9.84E+00 3.31E-04 0.000 0.1935 0.9000 0.9000 1.12 1 1 6.6E-04
10 : 9.85E+00 3.13E-05 0.084 0.0948 0.9900 0.9900 1.01 1 1 6.2E-05
11 : 9.85E+00 7.10E-07 0.000 0.0226 0.7494 0.9000 1.00 1 1 1.6E-05
12 : 9.85E+00 4.77E-09 0.000 0.0067 0.9972 0.9990 1.00 1 1 1.1E-07
13 : 9.85E+00 3.66E-10 0.268 0.0768 0.9900 0.9900 1.00 1 1 8.3E-09

iter seconds digits cx by
13 1.0 Inf 9.8516868312e+00 9.8516868354e+00
|Ax-b| = 1.5e-07, [Ay-c]_+ = 4.4E-10, |x|= 2.3e+01, |y|= 8.2e+00

Detailed timing (sec)
Pre IPM Post
1.563E-01 9.531E-01 4.688E-02
Max-norms: ||b||=1.806807e+01, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 446.094.

Status: Solved
Optimal value (cvx_optval): +9.85169

Supra

Dear Supra thanks a lot for your results by running the code, but I am sorry to say I don’t understand it. I will be relax only if the whole piece of code i.e.,

cvx_begin sdp quiet
variable x(NM,1) complex
variable F(M
N,MN) hermitian
minimize(norm(y-x))
subject to
[F, x; x’, lambda^2]>=0
for idxN1 = 0 : 1 : N-1
for idxN2 = 0 : 1 : N-1
Ftmp = F(idxN1
M+1:idxN1M+M, idxN2M+1:idxN2*M+M);
for delta = -(M-1) : 1 : M-1
if ((idxN1==idxN2) && (delta==0))
sum(diag(Ftmp, delta)) <= 1/N;
else
sum(diag(Ftmp, delta)) == 0;
end
end
end
end
cvx_end

is converted into MATLAB code. Then I will be able to understand it very well. So kindly if you can convert it into a MATLAB code, then I will be able to proceed further in it as then I will make a fitness function from this and then I can use MATLAB optimtool.
Regards,

1 Like