I get a optimal solution with cvx, but I can't get the same result when I use the optimal solution

clc;clear all
% 吞吐量和公平性权重因子
Lambda = 0.5;
% wifi系统用户数
N=10;
%% D2D-U系统仿真参数
R=500;L=50;
Pm=20;M=5;
%% 变量
rth =1 ;%系统满足用户的最小速率
Rm=4;%MOS用户满意不超过4.5时的最大速率
Pmw=10^(Pm/10-3); %单位换算为w
BPSD=7.962143411069940e-14;

%% 撒点 生成g,p矩阵  
g=[1.20400768243480e-09,1.97390048654673e-15,4.55482681665684e-14,2.88729797725590e-12,1.16263023804519e-08;2.03940187624467e-15,4.18357806194726e-08,1.55177799695453e-14,3.60251902390694e-15,1.92868749024042e-15;5.23383943203430e-14,1.29823956091715e-14,6.83827372245750e-08,7.86944277228367e-14,4.11367261904270e-14;7.21881610685714e-13,3.82543091213158e-15,7.78273974757130e-14,7.80449461386999e-10,8.82427223501170e-13;1.33229063264012e-08,2.07861873054549e-15,5.81092379365689e-14,1.98577179569490e-12,2.72611292094715e-10];

P = Pmw.*ones(1,M);

tf2 = zeros(1,M);    

    
  %% 使用cvx工具箱算p
    cvx_begin

    variable p(1,M)
        cvx_solver SeDuMi
        
        % 计算凹函数  f2
            a=0;f2=0;
            for m=1:1:M
                for j=1:1:M
                    a = P(1,j)*g(j,m)+a;
                end
                b = a-P(1,m)*g(m,m);
                f2 = log2(b+BPSD)+f2;
            end
            %梯度 f2
            c=0;d=0;
            for m=1:M
                for l=1:1:M
                    for i=1:1:M
                        c = P(1,i)*g(i,l)+c;
                    end
                    c = c - P(1,l)*g(l,l);
                    d = d + g(m,l)/(log(2*c)+BPSD); 
                end
                h=0;
                for i=1:1:M
                    h = P(1,i)*g(i,m)+h;
                end
                h = h - P(1,m)*g(m,m);
                
                tf2m = d-g(m,m)/(log(2*h)+BPSD);
                tf2(1,m)=tf2m;
            end
            % D2D-U系统SINR
            e=0;
            for j=1:1:M
                    e = P(1,j)*g(j,m)+ e;
            end
                f = e-P(1,m)*g(m,m);
            r = P(1,m)*g(m,m)/f;

           %f1
            a1 = 0;f1=0;
             for m=1:1:M
                    for j=1:1:M
                        a1 = p(1,j)*g(j,m)+a1;
                    end
                    a2=a1+BPSD;
                    f1 = -rel_entr(1,a2) / log(2)+f1;   
             end

         F = f1 - ( f2 + tf2 * ((p(1,M)-P)'));
         
        maximize( F );
        
        subject to
        %C1
            e1=0;
            for m=1:M
                 for j=1:1:M
                    e1 = P(1,j)*g(j,m)+ e1;
                 end
                f = e1-P(1,m)*g(m,m);
                p(1,m)*g(m,m)+(1-2^rth)*(f+BPSD) >= 0;
            end
            %C2
            e2=0;
            for m=1:M
                 for j=1:1:M
                    e2 = P(1,j)*g(j,m)+ e2;
                 end
                f = e2-P(1,m)*g(m,m);
                (2^Rm-1)*(f+BPSD)- p(1,m)*g(m,m) >= 0;
            end
            %C3
            for m=1:M
                Pmw-p(1,m) >= 0;
            end
            %C4
             for m=1:M
                p(1,m) >= 0;
             end
             %C5
             e3=0;
             for m=1:M
                e3=e3+p(1,m);
             end
             M * Pmw >= e3; 
            
     cvx_end

I get a optimal solution :
p=[0.046445131679162,0.060273018686142,0.063984495326789,0.045027139481617,0.051124241213016]
F=+21.7095

Next I use p to get F

clc;clear all
% 吞吐量和公平性权重因子
Lambda = 0.5;
% wifi系统用户数
N=10;
%% D2D-U系统仿真参数
R=500;L=50;
Pm=20;M=5;
%% 变量
rth =1 ;%系统满足用户的最小速率
Rm=4;%MOS用户满意不超过4.5时的最大速率
Pmw=10^(Pm/10-3); %单位换算为w
BPSD=7.962143411069940e-14;

%% 撒点 生成g,p矩阵  
g=[1.20400768243480e-09,1.97390048654673e-15,4.55482681665684e-14,2.88729797725590e-12,1.16263023804519e-08;2.03940187624467e-15,4.18357806194726e-08,1.55177799695453e-14,3.60251902390694e-15,1.92868749024042e-15;5.23383943203430e-14,1.29823956091715e-14,6.83827372245750e-08,7.86944277228367e-14,4.11367261904270e-14;7.21881610685714e-13,3.82543091213158e-15,7.78273974757130e-14,7.80449461386999e-10,8.82427223501170e-13;1.33229063264012e-08,2.07861873054549e-15,5.81092379365689e-14,1.98577179569490e-12,2.72611292094715e-10];

P = Pmw.*ones(1,M);

tf2 = zeros(1,M);    

    
  %% 使用cvx工具箱算p
  
 p=[0.046445131679162,0.060273018686142,0.063984495326789,0.045027139481617,0.051124241213016];     
 
        % 计算凹函数  f2
            a=0;f2=0;
            for m=1:1:M
                for j=1:1:M
                    a = P(1,j)*g(j,m)+a;
                end
                b = a-P(1,m)*g(m,m);
                f2 = log2(b+BPSD)+f2;
            end
            %梯度 f2
            c=0;d=0;
            for m=1:M
                for l=1:1:M
                    for i=1:1:M
                        c = P(1,i)*g(i,l)+c;
                    end
                    c = c - P(1,l)*g(l,l);
                    d = d + g(m,l)/(log(2*c)+BPSD); 
                end
                h=0;
                for i=1:1:M
                    h = P(1,i)*g(i,m)+h;
                end
                h = h - P(1,m)*g(m,m);
                
                tf2m = d-g(m,m)/(log(2*h)+BPSD);
                tf2(1,m)=tf2m;
            end
            % D2D-U系统SINR
            e=0;
            for j=1:1:M
                    e = P(1,j)*g(j,m)+ e;
            end
                f = e-P(1,m)*g(m,m);
            r = P(1,m)*g(m,m)/f;

           %f1
            a1 = 0;f1=0;
             for m=1:1:M
                    for j=1:1:M
                        a1 = p(1,j)*g(j,m)+a1;
                    end
                    a2=a1+BPSD;
                    f1 = -rel_entr(1,a2) / log(2)+f1;   
             end

         F = f1 - ( f2 + tf2 * ((p(1,M)-P)'));
         F

F =

-0.6908

Why it is so different

1 Like

I haven’t really looked at what you have done, and why/whether the 2nd calculation should, other than roundoff or solver tolerance, match the first calculation.

However, the scaling on your problem is atrocious. Please recall the guidance provided by @Erling on your previous forum question When I used CVXQUAD,I still get Failed You need to try to get the input numbers in your problem to be either exactly zero, or within a small number of orders of magnitude of one.

Due to yur horrible scaling, if you get a solution to your first CVX invocation in your post above, it is meaningless junk. SeDuMi ran into numerical problems and CVX reported Inaccurate.solved. SDPT3 and Mosek 9 reported it infeasible.

Sorry, I can’t understand the mean of the crazy numbers. Is it too small? Should I use the number nearly 1 in cvx?

Numbers should be close to one in magnitude., or they should be exactly zero.

Here are some rough rules of thumb when using double precision solvers, as are all solvers called by CVX.

Numbers such as positive or negative 1e-3, 1e-2, 1e-1, 1, 1e1, 1e2, 1e3 are o.k.

Numbers such as positive or negative 1e-6, 1e-5, 1e-4, 1e4, 1e5, 1e6 are not great, nut might not be terrible.

Numbers such as positive or negative 1e-7, 1e-10, 1e-13, 1e7, 1e10, 1e13 are getting very bad.

If the ratio of the absolute value of the largest and smallest magnitude numbers (other than those which are exactly zer0, is greater than 1e8, it is probably very bad. If that ratio is more than 1e12, your chances for success are very, very low.

You should try to change the units used in your problem to achieve the guidelines.

I want to ask how to solve your problem