Why the value of expression is not equal to the assignment?

Hello everyone. When I use cvx tool to solve the following convex problem, I found the values of expressions T_f_auxi, T_w, T_tr and Y_time are not equal to the corresponding assignments, which made the optimal value of objective function seem to be wrong. I want to know if the following code is written incorrectly, which leads to this result. I would appreciate it if you could help me.

function [E, Q, d_tol, z, actT_h, T_h, Y_time, T_f, T_f_auxi, T_w, T_tr, T_com] = optHori1(M,q,alpha,Data,Freq,X,Y,B,p,h_0,sigma,gamma_th,mu,nu,Phi,varphi,iniQ,H,d_th,v,E_max,N,UAV_USVlist,x0,y0)

q0 = [x0 y0 0];
gamma_0 = p*h_0/sigma;

v_in = 4.03; % mean rotor induced velocity in hovering
v_tip = 120; % tip speed of rotor blade
d0 = 0.6; % fuselage drag ratio
varrho = 1.225; % air density
beta = 0.05; % rotor solidity
A = 0.503; % rotor disc area
p0 = 2592varrhobetaA;
p1 = 1.1
20^(3/2)/sqrt(2varrhoA);
p_h = p0+p1;
p_f = zeros(1,N);
for k=1:N
p_f(k) = p0*(sqrt(1+v(k)^4/(4v_in^4))-v(k)^2/(2v_in^2))^(1/2)+p1*(1+3v(k)^2/(v_tip^2))+1/2d0varrhobetaAv(k)^3;
end

% Square of distance between USV and UAV
d_hat = zeros(M,N);
for i=1:M
for k=1:N
d_hat(i,k) = norm(iniQ(k,:)-q(i,:))^2 + H^2;
end
end

d_th_bar = repmat(d_th^2, N, N); %Minimum safe distance between drones(UAVs)

% computation time cost of UAVs
UAV_f = zeros(1,N); % computation capacity of UAVs
for k=1:N
runTaskNum = length(find(alpha(:,k)));
UAV_f(k) = mu^(Phi - runTaskNum)/nu;
end
T_com = zeros(M,N); % the computing time of tasks
for i=1:M
for k=1:N
T_com(i,k) = Freq(i)/UAV_f(k);
end
end
Y_bar = repmat(Y,1,N);

% computation energy consumption of UAvs
E_com = zeros(1,N);
for k=1:N
[~,~,curUAV_USVlist] = find(UAV_USVlist(k,:));
% USV_num = length(curUAV_USVlist);
% curUAV_f = mu^(Phi-USV_num)/nu*1e9;
USV_freq = 0;
for j = 1:length(curUAV_USVlist)
USV_freq = USV_freq + Freq(curUAV_USVlist(j));
end
E_com(k) = varphi * UAV_f(k)^2 * USV_freq;
end

cvx_begin quiet

variable Q(N,2) nonnegative  % deployment of UAVS
variable d_tol(1,N) nonnegative  % flight distance of UAV from original point to deployment point
variable z(M,N) nonnegative  
variable T_h(1,N) nonnegative; %hovering time of UAVs


expression d(M,N);
for i=1:M
    for k=1:N
        d(i,k) =  square_pos(norm(Q(k,:) - q(i,:))) + H^2;
    end
end
expression r_lb(M,N);
for i=1:M
    for k=1:N
        r_lb(i,k) = B*log2(1+gamma_0/d_hat(i,k))-...
                        (B * gamma_0 / (log(2)*(d_hat(i,k))*(d_hat(i,k)+gamma_0)) * (d(i,k) - d_hat(i,k)));
    end
end
% lower bound of distance between UAVs
expression d_UU_lb(N,N); 
for k=1:N
    for j=1:N
        if j == k
            d_UU_lb(k,j) = d_th ^ 3; 
        else
            d_UU_lb(k,j) = norm(iniQ(k,:)-iniQ(j,:))^2 + 2* (iniQ(k,:) - iniQ(j,:)) * ((Q(k,:) - Q(j,:)) - (iniQ(k,:)-iniQ(j,:))).';
        end
    end
end


expression Y_time(M,N); % time windows 
expression T_f(1,N);
expression T_f_auxi(1,N);
expression T_w(M,N);
expression T_tr(M,N);
expression d_tol_auxi(1,N);
for k = 1:N
    T_f(k) = norm([Q(k,:),H] - q0)/v(k);
    d_tol_auxi(k) = power(d_tol(k),1/2);
    T_f_auxi(k) = d_tol_auxi(k)/v(k);
end
for i = 1:M
    for k = 1:N
        T_w(i,k) = pos(X(i) - T_f_auxi(k));
        T_tr(i,k) = Data(i)* inv_pos(z(i,k));
        Y_time(i,k) = T_f(k) + T_w(i,k) + T_tr(i,k) + T_com(i,k);

% Y_time(i,k) = norm([Q(k,:),H] - q0)/v(k) + pos(X(i) - sqrt(d_tol(k))/v(k)) + Data(i)* inv_pos(z(i,k)) + T_com(i,k);
% Y_time(i,k) = norm([Q(k,:),H] - q0)/v(k) + pos(X(i) - T_a(k)) + Data(i)* inv_pos(z(i,k)) + T_com(i,k);
end
end

expression actT_h(M,N);
T_h_bar = repmat(T_h,M,1); % 将盘旋时间扩充成 M*N,便于约束条件书写
for i=1:M
    for k=1:N
        actT_h(i,k) = pos(X(i) - sqrt(d_tol(k))/v(k)) + Data(i)*inv_pos(z(i,k)) + T_com(i,k);
    end
end

% energy constraint
expression E(1,N);
expression E_h(1,N); % hovering energy consumption of UAVs
expression E_f(1,N); % flying energy consumption of UAVs
expression d_tol_lb(1,N);
for k=1:N
    E_h(k) = p_h * T_h(k);
    E_f(k) = p_f(k) * T_f(k);
    E(k) = E_h(k) + E_f(k) + E_com(k);
    d_tol_lb(k) = norm(iniQ(k,:) - [x0, y0])^2 + 2* (iniQ(k,:)-[x0 y0])* (Q(k,:)-iniQ(k,:)).' + H^2;
end


minimize(sum(E)); % 目标函数 

subject to
    alpha.*d <= gamma_0/gamma_th;
    d_UU_lb >= d_th_bar; % safe distance constraint of UAVs
    alpha.*Y_time <= Y_bar;  % time windows constraint 
    d_tol <= d_tol_lb;
    alpha.*actT_h <= T_h_bar; % hovering time constraint
    E <= E_max; %maximum energy constraint
    z <= r_lb; 

cvx_end
end

After CVX has solved or inaccurate/solved, the CVX variables have their optimal values. But CVX expressions do not necessarily.correspond to the optimal values of CVX variables. CVX makes sure the expressions are properly evaluated for purposes of conducting the optimization and populating cvx_optval, but the numerical values of CVX expressions after CVX completes do not necessarily correspond to the optimal values of the variables. Therefore, you should recompute CVX expression values using the optimal values of CVX variables if you wish to know the optimal values of CVX expressions. This reflects a design decision, different than that in YALMIP, to not automatically do this for you.

Thank you for your really helpful reply and I’m sorry I didn’t reply in time. In fact, I asked the above question precisely because I found that the value of the optimal solution of variable T_h did not match the actual situation of my problem, and I thought that the above situation happened because of the error in the assignment process. Can I ask you one more question? Why did I still get the wrong optimal solution after cvx has solved the problem?

Please show the solver and CVX output, together with whatever evidence and explanation you have as to why the reported optimal solution is “wrong”.

Calling SeDuMi 1.3.4: 542 variables, 232 equality constraints
For improved efficiency, SeDuMi is solving the dual problem.

SeDuMi 1.3.4 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 = 232, order n = 437, dim = 543, blocks = 105
nnz(A) = 836 + 0, nnz(ADA) = 1308, nnz(L) = 826
it : by gap delta rate t/tP t/tD* feas cg cg prec
0 : 9.96E+07 0.000
1 : 6.30E+05 5.95E+07 0.000 0.5979 0.9000 0.9000 2.42 1 1 1.5E+02
2 : 9.01E+05 2.07E+07 0.000 0.3471 0.9000 0.9000 1.84 1 1 3.8E+01
3 : 7.75E+05 6.09E+06 0.000 0.2948 0.9000 0.9000 1.59 1 1 8.7E+00
4 : 5.83E+05 2.60E+06 0.000 0.4272 0.9000 0.9000 1.53 1 1 3.2E+00
5 : 2.54E+05 7.68E+05 0.000 0.2952 0.9000 0.9000 1.43 1 1 9.5E-01
6 : 4.76E+04 3.13E+05 0.000 0.4081 0.9000 0.9000 1.40 1 1 4.6E-01
7 : -5.01E+03 9.57E+04 0.000 0.3054 0.9000 0.9000 1.30 1 1 2.6E-01
8 : -1.74E+04 3.33E+04 0.000 0.3481 0.9000 0.9000 1.25 1 1 2.1E-01
9 : -1.04E+04 1.82E+04 0.000 0.5453 0.9000 0.9000 1.14 1 1 2.0E-01
10 : -3.03E+03 9.58E+03 0.000 0.5272 0.9000 0.9000 1.10 1 1 1.8E-01
11 : -4.21E+03 4.77E+03 0.000 0.4984 0.9000 0.9000 1.07 1 1 2.0E-01
12 : -4.26E+03 1.28E+03 0.000 0.2675 0.9000 0.9000 1.12 1 1 1.8E-01
13 : -4.37E+03 4.88E+02 0.000 0.3819 0.9000 0.9000 1.30 2 2 4.9E-02
14 : -4.37E+03 8.24E+01 0.000 0.1690 0.9000 0.0000 1.30 4 22 1.2E-02
15 : -4.45E+03 2.67E+00 0.000 0.0324 0.9244 0.9000 1.30 14 22 6.0E-04
16 : -4.50E+03 1.03E-14 0.147 0.0000 0.9663 0.9900 1.16 7 25 4.7E-07
17 : -4.50E+03 3.96E-15 0.000 0.3847 0.9305 0.9000 1.02 13 21 2.0E-07
18 : -4.50E+03 1.53E-15 0.000 0.3872 0.9000 0.0000 1.01 15 15 1.0E-07
19 : -4.51E+03 3.51E-16 0.000 0.2287 0.9183 0.9000 1.00 14 14 2.7E-08
20 : -4.51E+03 7.27E-17 0.000 0.2075 0.9000 0.9006 0.99 15 17 5.7E-09

iter seconds digits cx by
20 1.0 Inf -4.5078896257e+03 -4.5076575877e+03
|Ax-b| = 4.2e-07, [Ay-c]_+ = 9.1E-04, |x|= 1.9e+02, |y|= 2.6e+06

Detailed timing (sec)
Pre IPM Post
4.000E-02 4.040E-01 7.996E-03
Max-norms: ||b||=1.684842e+02, ||c|| = 1.127106e+06,
Cholesky |add|=21, |skip| = 0, ||L.L|| = 1074.27.

Status: Solved
Optimal value (cvx_optval): +8657.66

The above is cvx output.

First, according to the cvx output, the value of T_h is -8.3737e-04. However, in my problem, T_h needs to satisfy the constraint alpha.*actT_h <= T_h_bar ( T_h_bar is obtained by replicating T_h to have the same dimension as the left side of the inequality ), where
actT_h(i,k) = pos(X(i) - sqrt(d_tol(k))/v(k)) + Data(i)*inv_pos(z(i,k)) + T_com(i,k).

Take one of the relevant sets of values as an example. alpha: 1, X: 24, d_tol:9.6715e03, v: 6.2293, Data: 9e06, z: 1.4270e05, T_com: 4.9. Based on these values and equation actT_h(i,k) = pos(X(i) - sqrt(d_tol(k))/v(k)) + Data(i)*inv_pos(z(i,k)) + T_com(i,k), the approximate value of actT_h can be obtained, which is 75.57. However, the value of T_h is -8.3737e-04, which violates the constraint alpha.*actT_h <= T_h_bar. Thus, I thought the optimal value of T_h is wrong.

By the way, if you’re interested, after CVX stops, Comment out the lines with “expression”, “variable”, “minimize/maximize”. Delete the “;” of the constraint lines. Select all the lines between cvx_begin and cvx_end but not including those two. Right click->“run the selected code”? Then you can locate the unsatisfied constraints based on the true/false output. If they are all true, then no constraints are violated. I don’t know whether the procedures above work, since I dont know how CVX expression works specifically. But if they work, the true/false output might be the best proof of your constraint violation statement.

I am confused as to what you did.

To see whether constraints were satisfied (within tolerance), you need to start with the CVX variable values, and then build up the expression values starting from those when evaluating the LHS or RHS of any constraints involving expressions. That is the point of my earlier post.