Different objective functions with different segments


I am trying to develop a model, solving an optimization problem which has the following objective function:

variable p(i);
minimize sum(cost)
subject to

where cost is defined as:

cost(i) = 0, if p(i) = 0,
cost(i) = 10*p + b, if 0<p(i)<= 5,
cost(i) = 15*p + c, if 5<p(i)<=10,
cost(i) = 20*p*p - 10*p + d, if p(i) > 10

As if statement is not allowed to be used in cvx, I think that declaring binary variables as indicators might work, like using the constraint:

p(i) - 5 + M*y >0,

so that y will be forced to be 1 when p(i) <= 5, and 0 otherwise.
Yet, I still have no idea how to deal with the segment like 5<p(i)<=10 by indicator variables.

Could anyone help me on this formulation?

Thank you in advance.

You haven’t told us whether your function is continuous (matches at breakpoints). You might get some ides in chapters 7 and 9 of https://docs.mosek.com/modeling-cookbook/index.html . if you need more help, I suggest https://or.stackexchange.com/ .

Hi Mark,
Thank you for your quick response! The function is not continuous (they do not match at break points. i.e., 105 + b is not equal to 155 + c). I will look into the documents you provided, and try to figure out the formulation.

Thank you once again.

Hi Mark,
I looked into the Mosek website and found that my problem is a discontinuous piecewise-quadratic functions. (I see your comment on stackexchange:) try to change the name but not sure if it is correct.)
It seems that the continuous and discontinuous ones are quite different, and I try to find some materials about the discontinuous problem. Unfortunately, most of the posts discussed the continuous ones, and only one post I found on Matlab Answers gives a solution by multiplying a condition with the corresponding piecewise function.

I have tried to modify my code and do the same thing as

variable p(i);
for i = 1:N
    obj = obj + (10*p(i) + b) * (p(i) > 0 & p(i)<=5) + ....
minimize (obj)
subject to

It does not give any error in the first round, but an error occurs at other lines later. My question is, is this approach feasible? I am wondering if it is reasonable since it does not look like a convex function if writing the formulation in this way.

Thank you!

Your code should produce an error message. You can’t use something of the form p >= 0 as a term in an expression in CVX.

Also, do you really only have a scalar (CVX) variable p rather than a vector? I doubt variable p(i) is what you want. Then you use i as a for loop index.

If cost is a vector expression, it needs to be declared as
expression cost(4)
and the individual elements of cost need to be assigned prior to their use in the objective function, otherwise they will have the default value of zero when the objective function is formed.

Hi Mark,
Currently I wrote a simple program for this problem and the vector expression for cost are directly specified in the for loop. (Like the example in my last comment.) Does this make any difference?

I think the approach you provided can make the code more clean, I will take them and modify my code.

Thank you!