# Extra code in objective function affects the result

(reza) #1

I have an objective function representing a weighted sum as follows:

``````function J = objfcn_cvx(X, L, Mu_d, Sigma_d, Mu_x, Sigma_x)
A1 = (L*X - Mu_d.').';
B1 = reshape(A1,size(A1,1)*size(A1,2),1);
D1 = Sigma_d * B1;
C1 = sum_square_abs(D1);   % instead of C = (D'*D) as specified in DCP;

A2 = (X - Mu_x.').';
B2 = reshape(A2, size(X,1)*size(X,2),1);
D2 = Sigma_x * B2;
C2 = sum_square_abs(D2);

J  = C1;
end
``````

Here, I am not using C2, however, commenting the second part of the code out will effect the results. Why is that? In other words, the following function has a different output

``````function J = objfcn_cvx(X, L, Mu_d, Sigma_d, Mu_x, Sigma_x)
A1 = (L*X - Mu_d.').';
B1 = reshape(A1,size(A1,1)*size(A1,2),1);
D1 = Sigma_d * B1;
C1 = sum_square_abs(D1);   % instead of C = (D'*D) as specified in DCP;

% A2 = (X - Mu_x.').';
% B2 = reshape(A2, size(X,1)*size(X,2),1);
% D2 = Sigma_x * B2;
% C2 = sum_square_abs(D2);

J  = C1;
end
``````

Eventually, I want to use the second objective, but this does not make any sense to me. Am I missing something?

This is how the main code looks like:

``````f = @(X)objfcn_cvx(X, L, Mu_d, Sigma_d, Mu_x, Sigma_x);
% CVX
cvx_begin
variable sol_x(n);
variable sol_y(n);
minimize(f([sol_x, sol_y]));
subject to
P_*[sol_x, sol_y] == G;
cvx_end
``````

the only difference is the part of the code in left-side objective function that has been commented out, the part that defined C2. Notice that the output of the function is independent from C2 but somehow it is affecting the results. For instance, It can be seen that the pick on the left side has been flattened. Why?

(Mark L. Stone) #2

You’re not showing a complete reproducible problem.

(reza) #3

Just added the main part of the code into the post.

(Mark L. Stone) #4

Did you re-execute the calls to `rand` when you got the different results? I.e., were you solving different problem instances because the inputs to CVX were different?

(reza) #5

No, there is no rand in the main code. I just added them here to have a functional code and providing the variable size to test.

(Mark L. Stone) #6

Please show exactly what you ran, with results, which are the results which don;t make sense to you.

(Mark L. Stone) #7

You now show some graphs of I don’t know what, and have removed the code you had. That is not reproducible.

Perhaps you should start with a clean MATLAB session, and create a reproducible example illustrating your discrepancy. In the course of doing so, perhaps you will discover the reason for the discrepancy, or the discrepancy will cease to exist when things are done cleanly and consistently.

(reza) #8

I have been trying to debug the code for the past two days. To make sure in both cases I am passing the exact data I even saved all the data before passing it to the optimization function. Now I am being convinced that this can be a CVX bug, when only commenting out some stuff inside the objective function changes the results significantly.

(Mark L. Stone) #9

Are you using CVX 3.0beta? If so, try using CVX 2.1, as CVX 3.0beta is known to have numerous bugs.

So far you haven’t even shown the results of a single run to CVX. Can you show results for the with and without commented out code? If you run them again in the same session, do you get the same results as before?

Forum readers don’t even know what you are plotting, so that doesn’t count as showing results.

(reza) #10

No, I’m using Version 2.1, Build 1123 (cff5298)

By the way, from the four commented lines of code, the last one is that making the problem. I.e, the following function produces similar results to the one with all four lines commented out

``````function J = objfcn_cvx(X, L, Mu_d, Sigma_d, Mu_x, Sigma_x)
A1 = (L*X - Mu_d.').';
B1 = reshape(A1,size(A1,1)*size(A1,2),1);
D1 = Sigma_d * B1;
C1 = sum_square_abs(D1);   % instead of C = (D'*D) as specified in DCP;

A2 = (X - Mu_x.').';
B2 = reshape(A2, size(X,1)*size(X,2),1);
D2 = Sigma_x * B2;
% C2 = sum_square_abs(D2);

J  = C1;
end``````

(Mark L. Stone) #11

What happens when you insert the code to create the objective directly into the main code, rather than via calling a function?

(reza) #12

Bringing everything in the main code, does not replicate the faulty result. The code:

``````w1 = 1;
w2 = 0.0;

cvx_begin
variable sol_x(n);
variable sol_y(n);
minimize( w1 * (Sigma_d * reshape((L*[sol_x sol_y] - Mu_d.').', numel(Mu_d),1)).' * (Sigma_d * reshape((L*     [sol_x sol_y] - Mu_d.').', numel(Mu_d),1)) + ...
w2 * (Sigma_x * reshape(([sol_x sol_y] - Mu_x.').', numel(Mu_x),1)).' * (Sigma_x * reshape(([sol_x, sol_y] - Mu_x.').', numel(Mu_x),1)))
subject to
P_*[sol_x, sol_y] == G;
cvx_end
``````

But if I run that for
w1 = 0.8;
w2 = 0.2;

I will get the following error:

``````Error using cvx/quad_form (line 230)
The second argument must be positive or negative semidefinite.
``````

Trying to solve it by using square_pos(norm(Q*x)).
Could that be related to the main issue?

(Mark L. Stone) #13

I don’t think there’s more help I can provide on this. Perhaps someone else will come along (it might be a while though) who can further investigate.

(reza) #14

Thanks for the help!