# Converting a piecewise function to linear equations

I am trying to build a MILP model. In this model, I have a dependent variable (alpha) that its value depends on the value of some other variables (or different combination of some other variables). In fact, one option will be selected as the final value of alpha given the values of other variables. Please see an example below.

$$\alpha=\left\{\begin{array}{ccc} 10 & \text { if } & x=1 ; \\ 12 & \text { els if } & x+y=2 ; \\ \vdots & & \vdots \\ 25 & \text { else if } & x+\cdots+z=4 ; \end{array}\right.$$

I would like to know how I can come up with a standard equations to formulate this function in order to obtain the value of alpha. Please note that the "if statements" and their variables are just an example.

• Are the variables $x, \ldots, z$ binary?
– joni
Feb 20 at 10:16
• Yes, all the variables except alpha are binary. I should note that the sum of those binary variables must not neccessrily be equal to 1. I have editted the equations.
– Sam
Feb 20 at 10:46
• Could you please elaborate on your constraints? For instance, $x + \ldots + z = 4$ is not really clear. Also, how does the $i$-th constraint look like?
– joni
Feb 20 at 11:25
• Sure ! Let me explain it through an example. basically, we have a number of binary variables. Let's call them x1, x10. Also, we have a dependent variable, alpha which its value depend on the value of other variables. We have been told that if x1=1, then alpha will be 10. If x1 + x2 = 2, the alpha will be 20. If x1 + x3 + x7 =3, then alphe will be 25. finally, if x1 + x3 + x9 +x 10=4, then alpha will be 30. Please note that I make the numbers. But, the whole idea is like that. I hope I could make it more clear now.
– Sam
Feb 20 at 11:52
• So if $x1=x2=1,$ does $\alpha$ equal 10 (because $x1=1$) or 20 (because $x1 + x2 = 2$? Your conditions are not mutually exclusive.
– prubin
Feb 20 at 19:20

Judging by the additional remarks in the comments, it seems like you want to linearize:

$$\alpha = \begin{cases} \alpha_1, \quad \text{if} \quad \sum_{i \in J_1} x_i = |J_1| = 1 \\ \alpha_2, \quad \text{if} \quad \sum_{i \in J_2} x_i = |J_2| = 2 \\ \vdots \\ \alpha_k, \quad \text{if} \quad \sum_{i \in J_k} x_i = |J_k| = k \\ \vdots \\ \alpha_N, \quad \text{if} \quad \sum_{i \in J_N} x_i = |J_N| = N \\ \end{cases}$$

for given constants $$\alpha_1, \ldots, \alpha_N$$, given binary variables $$x_1, \ldots, x_N$$, and given sets $$J_k \subseteq I = \{1, \ldots, N \}$$ with $$|J_k| = k$$ for all $$k = 1, \ldots, N$$.

By adding extra binary variables $$b_1, \ldots, b_N$$, you can represent the above using the following constraints:

\begin{align} \alpha &= \sum_{k=1}^{N} \alpha_k b_k \\ \sum_{k=1}^{N} b_k &= 1 \\ b_k &\geq 1 - |J_k| + \sum_{i \in J_k} x_i \quad \forall k = 1, \ldots, N. \end{align}

In this case, the last constraint ensures that

$$\sum_{i \in J_k} x_i = |J_k| = k \implies b_k = 1 \quad \forall k = 1, \ldots, N.$$

• This formulation does not enforce the “else if” part of the rule. The value of $\alpha$ is supposed to depend on the first condition that is satisfied. Feb 20 at 13:27
• Yes, I agree. The constraints are in conflicts with eachother. Basically, last constraint emposes that all the b_k>=1 and as b_k is abinary one, so we force b_k =1. In my opinion, if we change the sign of the last constraint to <=, the constraints may work.
– Sam
Mar 1 at 14:34

I guess the last constraint needs to be as below
$$N(b_k-1) \le 1 - k + \sum_{i=1}^{N} x_i \le b_k +N(1-b_k) \quad \forall k = 1...n$$