Disciplined convex programming error: Cannot perform the operation: {real affine} .* {convex}"

Please see my problem below: the problem has 51 variables when I assume the variable of number 51 (u(51))is given I get a solution.
When I assume the variable number 51 is not given and try to solve for all 51 variables, I get the following message “Disciplined convex programming error: Cannot perform the operation: {real affine} .* {convex}”

I guess it is b/c in db equation the Cap which is u(51) is multiplied by the other variables (u)

N=51
L = 0.5*tril(ones(50))

cvx_begin
    variables u(N)
    Cap=u(51)
    E0=Cap/2
    db = 1000*Cap*max(1/eta*u(1:50),eta*u(1:50));
    Es = E0+Cap*L*u(1:50);
    N_PV=100
minimize sum(Load+db'-N_PV*PV)/(2*1000)*rate+rate_max*max((Load+db' 
N_PV*PV)/1000)
    u(1:50) <= 0.4;
    u(1:50) >= -1;
    u(51)>=0
    u(51)<=100
    Es <= Cap;
    Es >= 0;
cvx_end
  1. Welcome to our community!!

  2. Have you proven that the objective function is convex?

1 Like

Thank you. when I assume the variable number 51 is given the cvx will solve and I have seen some papers solving the problem assuming convex optimization. But when I assume the variable number 51 is not given, the problem will have local minimum which I believe that means it is not convex. I was wondering if there is a way to convert it to convex in that case. Also I have another question if you can help me. How can I use if statement inside the convex optimization?

The problem is not convex unless one of the “varaiables” being multiplied is actually fixed, rather than being an optimization variable.

As for if statements, if a CVX variable or expression would appear in the condition being evaluated for the if statement, that is not allowed in CVX, and you would need to handle that by Big M logical constraint modeling See https://or.stackexchange.com/search?q=Big+M+logical+constraints using CVX’s MIDCP capability.

1 Like

Thank you so much.
For example, in the problem I have shown you previously:
rate=10 if u>=0
rate =1 if u<0

Is that double to assign a value for other constant based on the CVX variable value.

Please excuse my little expertise in this field.
Your help is appreciated.

If u is a CVX variable or expression, you need to apply Big M per the links at the link. if u is a numerically populated MATLAB variable, you can enter this using if else.

1 Like

Thank you. Can I have your final word on this?
I added the following:
variables u(N)
variable b binary
M=10^12;% large number
rate=9b+1
u<=h+m
b
u>=h-(1-b)M
but it say Cannot perform the operation: {convex} .
{real affine}.
I guess my problem can not be solved here (cvx) since u and b are multiplied by each other: u is cvx optimization variable and b is the binary variable.

I do appreciate your help!

Here is a good link to handle exactly what yo want to do. https://stackoverflow.com/questions/55899166/build-milp-constraint-from-if-else-statements .

You should not choose M = 10^12. That may result in things working out incorrectly due to solver integrality tolerance. Instead, M should be chosen based on lower and upper bounds on 'u`.