# How to deal with log0 in optimization problem

I am adding some constraints to my model described in my previous post, where a discontinuous piecewise-quadratic functions is the objective to be minimized in cvx.

Here I have an additional terms, sum(cost2), that is included in the objective as:

variable p(N);
variable cost1(N);
variable cost2(N);
variable delta(N, 4);
variable gamma(N, 3);
expression cost1_exp(4);
expression cost2_exp(3);
minimize sum(cost1)+sum(cost2)
subject to
p>=0
for i = 1:N
-M*(1-delta(i, 1))<=cost1(i)-cost1_exp(1)<=M*(1-delta(i, 1))
-M*(1-delta(i, 2))<=cost1(i)-cost1_exp(2)<=M*(1-delta(i, 2))
-M*(1-delta(i, 3))<=cost1(i)-cost1_exp(3)<=M*(1-delta(i, 3))
-M*(1-delta(i, 4))<=cost1(i)-cost1_exp(4)<=M*(1-delta(i, 4))
-M*(1-delta(i, 1))<=p(i)<=M*(1-delta(i, 1))
-M*(1-delta(i, 2))<=p(i)<=5+M*(1-delta(i, 1))
-M*(1-delta(i, 3))+5<=p(i)<=10+M*(1-delta(i, 1))
-M*(1-delta(i, 4))+10<=p(i)<=100+M*(1-delta(i, 1))
sum(delta(i))==1
end
for i = 1:N
-M*(1-gamma(i, 1))<=cost2(i)-cost2_exp(1)<=M*(1-gamma(i, 1))
-M*(1-gamma(i, 2))<=cost2(i)-abs(cost2_exp(2))<=M*(1-gamma(i, 2))
-M*(1-gamma(i, 3))<=cost2(i)-abs(cost2_exp(3))<=M*(1-gamma(i, 3))
-M*(1-gamma(i, 1))<=p(i)<=M*(1-gamma(i, 1))
-M*(1-gamma(i, 2))<=p(i)<=0.01+M*(1-gamma(i, 1))
-M*(1-gamma(i, 3))+0.01<=p(i) - 10^-6<=100+M*(1-gamma(i, 1))
sum(gamma(i))==1
end


where I add the constraints provided by @Johan to linearize my problem (I have not modified the constraints for quadratic terms) and the cost1_exp is identical as here:

cost1(i) = 0, if p(i) = 0,
cost1(i) = 10*p + 23.6, if 0<p(i)<= 5,
cost1(i) = 15*p + 45.4, if 5<p(i)<=10,
cost1(i) = 20*p*p - 10*p + 375, if p(i) > 10


However, the cost function cost2_exp makes the trouble:

cost2_exp(1) = 0, if p = 0,
cost2_exp(2) = 6*(10*log10(p))^2 -10*10*log10(p), if 0<p<= 0.01,
cost2_exp(3) = 17*10*log10(p) + 42.8, if 0.01<p<=100,


I try to add constraints as for cost1, and make similar works for cost2.

However, there are still some problems: When p(i) = 0, gamma(i, 1) = 1, gamma(i, 2) = gamma(i, 3) = 0, the cost2_exp(2) and cost2_exp(3) will force the cost to be Inf and -Inf respectively to satisfy the constraints, while cost2_exp(1) force the cost to be 0. In addition, cost2_exp(2) is greater than cost2_exp(3) at p = 0.01, which will force the solver to select cost2_exp(3) when p = 0.01.

To address these problems, I add abs() to force the cost2_exps produce only positive value, which makes the solver to choose cost2 = 0 at p = 0, since the problem is minimization problem. However, this approach can not be adopted to cost2_exp(2) since it is a quadratic function, and should be divided into quadratic and linear term to handle. Also, to force the solver to choose cost2_exp(2) at p = 0.01, I add a small number 10^-6 to prevent the solver from choosing cost2_exp(3) when p = 0.01.

Thus, my question is: How can I deal with this particular case when p = 0 for cost2_exp(2), or I can just not make it an absolute value since it will produce Inf when p = 0?