HOW CAN I solve the issue “Cannot perform the operation: {log-affine} .* {real affine} ”

HOW CAN I solve the issue“错误使用 .* (line 173)
Disciplined convex programming error:
Cannot perform the operation: {log-affine} .* {real affine}

minimize(delta_T)
subject to
%约束2
for i = 1:N
    for j = 1:Smax
        city_indices = M_star(i, j); % 获取第i个城市的序号
        x_coordinates = citys(city_indices, 1); % 获取城市的坐标
        y_coordinates = citys(city_indices, 2);
        (rou*P(i,j))/(H^2 + (Xur(r,j) - x_coordinates)^2 + y_coordinates^2) >= exp(Z(i,j))*(Z(i,j)...
            - Z_B(i,j) + 1);
    end
end

exp(Z)*Z for `Z being a CVX variable violates CVX’s rules for log-convexity, as described in @mcg’s post at Log of sigmoid function - #3 by mcg

However, .exp(Z)*Z is the lambert W function, which can be handled in CVX by using the formulation in section 5.2.9 “Lambert W-function” of the Mosek Modeling Cookbook.

You will need to use rotated_lorentz for the rotated second order cone (adjusted with an appropriate factor for CVX vs. the Mosek Modeling Cookbook due to the different convention used in CVX. And exponential cone arguments are in a different order in CVX.

I believe the Mosek Modeling Cookbook formulation in CVX for exp(Z)*Z would be:(if I haven’t made a mistake)

{1,u,t} == rotated_lorentz(1)
{u,t,Z} == exponential(1)

You’ll need to put this in a double loop over i and j, and have u,t,Z` all of the same dimensions as Z., and then index all of them by i,j. I was a little rushed, so might have made mistakes; so please check it.

1 Like

Thank you very much for your reply, I will try this again

Sorry, I still don’t understand the solution you proposed, here is my complete function code, could you please explain it again for me

function [delta_T, P] = cvx_update_P(rou, r, H, Smax, N, num_SNs, citys, Xur, M_star, Pmax)

cvx_begin

variable P(Smax,N)
variable M(Smax,N)
variable Z(Smax,N)
variable Z_B(Smax,N)
variable V(Smax,N)
variable V_B(Smax,N)    
variable delta_T(Smax,N)
minimize(delta_T)
subject to
%约束2
for i = 1:N
    for j = 1:Smax
        city_indices = M_star(i, j); % 获取第i个城市的序号
        x_coordinates = citys(city_indices, 1); % 获取城市的坐标
        y_coordinates = citys(city_indices, 2);
        (rou*P(i,j))/(H^2 + (Xur(r,j) - x_coordinates)^2 + y_coordinates^2) >= exp(Z(i,j))*(Z(i,j)...
            - Z_B(i,j) + 1);
    end
end

%约束3
sums_A = zeros(1, 100);
for i = 1:N
    for j = 1:Smax
        city_indices = M_star(i, j); % 获取第i个城市的序号
        x_coordinates = citys(city_indices, 1); % 获取城市的坐标
        y_coordinates = citys(city_indices, 2);
        A(i, j) = (rou*P(i,j))/(H^2 + (Xur(r,j) - x_coordinates)^2 + y_coordinates^2);
    end
    sums_A(:, i) = sum(A(:, i));
    sums_A(:, i) + 1 <= exp(V(i,j))*(V(i,j)...
            - V_B(i,j) + 1);
end
    
%约束4
for i = 1:N
    for j = 1:Smax
    log2(1 + exp(Z_B(i,j) - V_B(i,j)))...
        + exp(Z_B(i,j) - V_B(i,j))/(1 + exp(Z_B(i,j) - V_B(i,j)))*log(2)...
        * (Z(i,j) - Z_B(i,j) - V(i,j) + V_B(i,j)) >= M(i,j);
    end
end

%约束5
for i = 1:N
    for j = 1:Smax
        1/delta_T <= M(i,j) * N / re * Tth;
    end
end

%约束6
for i = 1:N
    for j = 1:Smax
        P(i,j) >= 0;
        P(i,j) <= Pmax;
    end
end    

cvx_end

end

I currently have the breakpoint set at constraint two, and have not tested it subsequently

You need to use Z(i.j) in place of exp(Z(i.j)*Z(i.j) and add constraints as follows:

variables Z(Smax,N)  t(Smax,N)  u(Smax,N) 
for i=1:Smax
  for j=1:N
    LHS >= Z(i.j)  -  Z_B(i,j) + 1
    {1,u(i,j),t(i,j)} == rotated_lorentz(1)
    {u(i,j),t(i,j),Z(i,j)} == exponential(1)
  end
end

You might need to fix up i vs. j ordering, because your code is inconsistent. I leave that fixing up to you.

1 Like

Thank you very much for all the help and explanations you gave me, I put it into my simulation and solved the problems I encountered with constraint 2, and now I’m starting to debug my constraint 3.Thanks again for all the help you gave me!

I am very sorry that I will bother you again, I have this problem when simulating constraint 4.
%约束4
for i = 1:Smax
for j = 1:N
{1,u(i,j),t(i,j)} == rotated_lorentz(1);
{u(i,j),t(i,j),Z(i,j)} == exponential(1);
(Z_B(i,j) - V_B(i,j)) * (Z(i,j) - Z_B(i,j) - V(i,j) + V_B(i,j)) >= (M(i,j)…
- (log(1 + exp(Z_B(i,j) - V_B(i,j)))/log(2))) * (1 + (Z_B(i,j) - V_B(i,j)))

% log(1 + (Z_B(i,j) - V_B(i,j)))/log(2)…
% + (Z_B(i,j) - V_B(i,j))/(1 + (Z_B(i,j) - V_B(i,j)))*log(2)…
% * (Z(i,j) - Z_B(i,j) - V(i,j) + V_B(i,j)) >= M(i,j);

% log(1 + exp(Z_B(i,j) - V_B(i,j)))/log(2)…
% + exp(Z_B(i,j) - V_B(i,j))/(1 + exp(Z_B(i,j) - V_B(i,j)))*log(2)…
% * (Z(i,j) - Z_B(i,j) - V(i,j) + V_B(i,j)) >= M(i,j);
end
end

I tried several ways to enter this constraint, but did not find the correct solution. Once again, I would like to trouble you

%约束4
for i = 1:Smax
    for j = 1:N
        {1,u(i,j),t(i,j)} == rotated_lorentz(1);
        {u(i,j),t(i,j),Z(i,j)} == exponential(1);
        (Z_B(i,j) - V_B(i,j)) * (Z(i,j) - Z_B(i,j) - V(i,j) + V_B(i,j)) >= (M(i,j)...
            - (log(1 + exp(Z_B(i,j) - V_B(i,j)))/log(2))) * (1 + (Z_B(i,j) - V_B(i,j)))
        
        log(1 + (Z_B(i,j) - V_B(i,j)))/log(2)...
            + (Z_B(i,j) - V_B(i,j))/(1 + (Z_B(i,j) - V_B(i,j)))*log(2)...
            * (Z(i,j) - Z_B(i,j) - V(i,j) + V_B(i,j)) >= M(i,j);

        log(1 + exp(Z_B(i,j) - V_B(i,j)))/log(2)...
            + exp(Z_B(i,j) - V_B(i,j))/(1 + exp(Z_B(i,j) - V_B(i,j)))*log(2)...
            * (Z(i,j) - Z_B(i,j) - V(i,j) + V_B(i,j)) >= M(i,j);
    end
end

        log(1 + (Z_B(i,j) - V_B(i,j)))/log(2)...
            + (Z_B(i,j) - V_B(i,j)) * inv_pos(1 + (Z_B(i,j) - V_B(i,j)))/log(2)...
            * (Z(i,j) - Z_B(i,j) - V(i,j) + V_B(i,j)) >= M(i,j);

I have now transformed the constraint to this form, but it comes up: Cannot perform the operation: {real affine} . * {convex}

Have you proven that is a convex constraint?

Thank you for your reply. I may need to ask for your help again.
I replace Z[n],V[n] on the left side of the constraint with x, y and make a first-order Taylor approximation, which allows me to find that the Hessian matrix is semi-definite.
I apologize for just forgetting to post my error report.
Here’s my complete simulation code.
function [delta_T, P] = cvx_update_P(rou, r, H, Smax, N, num_SNs, citys, Xur, M_star, Pmax, re, Tth)

cvx_begin

variable P(Smax,N)
variable M(Smax,N)
variable Z(Smax,N)
variable Z_B(Smax,N) 
variable t(Smax,N)  
variable u(Smax,N)
variable V(Smax,N)
variable V_B(Smax,N)    
variable delta_T(Smax,N)
variable sums_A(1, 100);
minimize(delta_T)
subject to
%约束2
for i = 1:Smax
    for j = 1:N
        city_indices = M_star(i, j); % 获取第i个城市的序号
        x_coordinates = citys(city_indices, 1); % 获取城市的坐标
        y_coordinates = citys(city_indices, 2);

% LHS >= Z(i.j) - Z_B(i,j) + 1
% (rouP(i,j))/(H^2 + (Xur(r,j) - x_coordinates)^2 + y_coordinates^2) >= exp(Z(i,j))(Z(i,j)…
% - Z_B(i,j) + 1);
(rou*P(i,j))/(H^2 + (Xur(r,j) - x_coordinates)^2 + y_coordinates^2) >= Z(i,j) - Z_B(i,j) + 1;
{1,u(i,j),t(i,j)} == rotated_lorentz(1);
{u(i,j),t(i,j),Z(i,j)} == exponential(1);
end
end

%约束3

for i = 1:Smax
    for j = 1:N
        city_indices = M_star(i, j); % 获取第i个城市的序号
        x_coordinates = citys(city_indices, 1); % 获取城市的坐标
        y_coordinates = citys(city_indices, 2);
        A(i, j) = (rou*P(i,j))/(H^2 + (Xur(r,j) - x_coordinates)^2 + y_coordinates^2);
    end
    sums_A(:, i) == sum(A(:, i));
    {1,u(i,j),t(i,j)} == rotated_lorentz(1);
    {u(i,j),t(i,j),V(i,j)} == exponential(1);
    sums_A(:, i) + 1 <= V(i,j)- V_B(i,j) + 1;

% sums_A(:, i) + 1 <= exp(V(i,j))*(V(i,j)- V_B(i,j) + 1);
end

%约束4
for i = 1:Smax
    for j = 1:N
        {1,u(i,j),t(i,j)} == rotated_lorentz(1);
        {u(i,j),t(i,j),Z(i,j)} == exponential(1);
        log(1 + (Z_B(i,j) - V_B(i,j)))/log(2)...
            + (Z_B(i,j) - V_B(i,j))/(1 + (Z_B(i,j) - V_B(i,j)))*log(2)...
            * (Z(i,j) - Z_B(i,j) - V(i,j) + V_B(i,j)) >= M(i,j);
    end
end

This is the constraint 4 error.

错误使用 .* (line 173)
Disciplined convex programming error:
Cannot perform the operation: {real affine} ./ {real affine}

出错 ./ (line 19)
z = times( x, y, ‘./’ );

出错 * (line 36)
z = feval( oper, x, y );

出错 / (line 15)
z = mtimes( x, y, ‘rdivide’ );

出错 cvx_update_P (line 55)
+ (Z_B(i,j) - V_B(i,j))/(1 + (Z_B(i,j) - V_B(i,j)))*log(2)…

Have you proven that is a convex constraint?

YES
I replace Z[n],V[n] on the left side of the constraint with x, y and make a first-order Taylor approximation, which allows me to find that the Hessian matrix is semi-definite.
So I think it’s a convex constraint

Please show your proof, which needs to meet the standards of the link.

I’m very sorry about that. I’m not quite sure now if this constraint strictly satisfies the standard rules, I’ll rederive the constraint.
Could you please give me some advice on the derivation?

You’re the one claiming the constraint is convex, not me. So you should know how to prove it. And please very carefully re-read the link.

Ok, I will check my constraints again according to the link, thank you very much for your patience with me!

I’m so sorry to bother you again.
I computed the Hansen matrix for the left part of this constraint. Due to the fact that the Hessian matrix of F(x, y) is positive semi-definite, this bivariate function is convex with respect to x and y.So I think this constraint is convex, but I won’t implement this constraint in cvx, or maybe something is wrong but I don’t see it, can you point it out for me?

Can you write down the constraint, in terms of simple things, such as x and y, and make clear which are the variable and which is input data? Write it in “straightforward” CVX code, ignoring the fact that you would be violating CVX’s rules. I want to see clearly what the constraint is. Don’t bother with the Hessian, I’ll worry about that. Don’t include any extraneous equals signs or anything else.