# Incorrect (?) cvx_optval

Hi,

I am writing the below optimization function. I am optimizing the trajectory (c_q) while minimizing the energy (subq_E). After I get the optimized c_q values, I copy it into another variable q, and calculate the energy using the same formula that I gave in optimization code. (I also verified that the copying is proper c_q and q are exactly same).
Now, when I calculated the energy using the copied variable, the energy calculated is 3313.71. When I print the cvx_optval, it is 31.7816. How is it possible, Shouldn’t they both be same?
I am pasting my code and output below.

cvx_begin quiet
variable c_q(M,N, 1,2);

``````expression subq_E;
subq_E = 0;

for m = M
for n = N-1
subq_E = subq_E + (w_2*0.5*Q*pow_pos(norm(reshape(c_q(m,n+1,:,:),[1 2])-reshape(c_q(m,n,:,:),[1 2])),2))/Delta;
end
end

minimize subq_E

for m = 1:M
c_q(m,1,:,:) == c_q(m,N,:,:); %C6
end
for m = 1:M
for n = 1:N-1
pow_pos(norm(reshape(c_q(m,n+1,:,:),[1 2]) - reshape(c_q(m,n,:,:),[1 2])),2) <= S_max^2; %C7
end
end

H_cottheta = H *cottheta;
for k = 1:K
for m = 1:M
for n = 1:N
a(k,m,n) * norm(reshape(c_q(m,n,:,:),[1 2]) -W_k(k,:),2) <= H_cottheta;
end
end
end

for n = 1:N
for m = 1:M
for j = m+1:M
- norm(reshape(q(m,n,:,:),[1 2])-reshape(q(j,n,:,:),[1 2]))^2 +  2*(reshape(q(m,n,:,:),[1 2])-reshape(q(j,n,:,:),[1 2])) * transpose(reshape(c_q(m,n,:,:),[1 2])-reshape(c_q(j,n,:,:),[1 2])) >= d_min^2;
end
end
end
``````

cvx_end

q = c_q;

E_f5 = zeros(M,N);
for m =1: M
for n =1: N-1
E_f5(m,n) = (0.5Qpow_pos(norm(reshape(q(m,n+1,:,:),[1 2])-reshape(q(m,n,:,:),[1 2])),2))/Delta;
end
end

display(['The flight energy consumed: ’ num2str(w_2*sum(E_f5(:))) ‘.’])
display(['Sub5 is ’ cvx_status '. The cumulative objective is: ’ num2str(cvx_optval) ‘.’])

Output:
The flight energy consumed: 3313.171.
Sub5 is Solved. The cumulative objective is: 31.7816.

1 Like

After CVX conclusion with optimal solution, only CVV variables are guaranteed to have their optimal values. CVX expressions (other than CVX variables) do not necessarily have their optimal values, and must be recomputed after CVX conclusion using CVX variables. Nevertheless, CVX expressions are still used correctly inside the optimization problem, but their values after CVX conclusion are not necessarily their final “optimal” values used inside the optimization problem.

So cvx_optval should have the correct value. Try recomputing the objective using the optimal value of `c_q`. it should equal cvx_optval to within roundoff error.

Also, do not use `quiet` until things are working correctly.

1 Like

I did that… the cvx_optval is 31.7816 and the value calculated using c_q is 3313.171.

cvx_begin
variable c_q(M,N, 1,2);

``````expression subq_E;
subq_E = 0;

for m = M
for n = N-1
subq_E = subq_E + (w_2*0.5*Q*pow_pos(norm(reshape(c_q(m,n+1,:,:),[1 2])-reshape(c_q(m,n,:,:),[1 2])),2))/Delta;
end
end

minimize subq_E

for m = 1:M
c_q(m,1,:,:) == c_q(m,N,:,:); %C6
end
for m = 1:M
for n = 1:N-1
pow_pos(norm(reshape(c_q(m,n+1,:,:),[1 2]) - reshape(c_q(m,n,:,:),[1 2])),2) <= S_max^2; %C7
end
end

H_cottheta = H *cottheta;
for k = 1:K
for m = 1:M
for n = 1:N
a(k,m,n) * norm(reshape(c_q(m,n,:,:),[1 2]) -W_k(k,:),2) <= H_cottheta;
end
end
end

for n = 1:N
for m = 1:M
for j = m+1:M
- norm(reshape(q(m,n,:,:),[1 2])-reshape(q(j,n,:,:),[1 2]))^2 +  2*(reshape(q(m,n,:,:),[1 2])-reshape(q(j,n,:,:),[1 2])) * transpose(reshape(c_q(m,n,:,:),[1 2])-reshape(c_q(j,n,:,:),[1 2])) >= d_min^2;
end
end
end
``````

cvx_end

E_f5 = zeros(M,N);
for m =1: M
for n =1: N-1
E_f5(m,n) = (0.5Qpow_pos(norm(reshape(c_q(m,n+1,:,:),[1 2])-reshape(c_q(m,n,:,:),[1 2])),2))/Delta;
end
end

display(['The flight energy consumed: ’ num2str(w_2*sum(E_f5(:))) ‘.’])
display(['Sub5 is ’ cvx_status '. The cumulative objective is: ’ num2str(cvx_optval) ‘.’])

1 Like

This is what you have inside your program.

``````for m = M
for n = N-1
``````

which is inconsistent with your post-CVX calculations (i.e., only one combination of m and n in the double for loop)… If you copy exactly, you should get a match. I will leave you to figure out what needs to be fixed.

1 Like

Oh my god…

I am so sorry… it is such a silly mistake…

I apologize. I am in so much stress and was only searching for some ‘big’ kind of mistakes. It is so embarassing that I wasted lot of my time and your time too

1 Like