0
$\begingroup$

I have a problem under investigation which requires if, elseif and else conditions to implement as a constraint in a mixed integer program. Any leads will be appreciated. Thanks a lot.

Let $x_t$, $y_t$ be continuous variables.

if y[t] == 0:
   x[t] = 1 
elif y[t] == y[t-1]: 
   x[t] = x[t-1] * K #(constant) 
else: 
   y[t] = 1 - x[t]
$\endgroup$
3
  • $\begingroup$ Cross-posted: math.stackexchange.com/questions/4876411/… $\endgroup$
    – RobPratt
    Mar 8 at 2:03
  • 1
    $\begingroup$ Are $x_t$ and $y_t$ nonnegative or free (unrestricted in sign)? $\endgroup$
    – prubin
    Mar 8 at 3:45
  • $\begingroup$ Thanks @prubin, appreciate your response on this modeling question. The values are non-negative and continuous for both variables. One quick catch is that the values of x[t] and y[t] lies between 0 and 1 - i.e., 0 < x[t] < 1 and 0<y[t]<1. We can reduce the range of the bounds for x[t] as a <= x[t] <= b, for e.g., a =0.2 and b = 0.7. Let me know your inputs professor. $\endgroup$ Mar 8 at 15:12

2 Answers 2

1
$\begingroup$

Introduce three binary variables $z_{1t},z_{2t},z_{3t}$ corresponding to the three conditions, along with the constraint $z_{1t}+z_{2t}+z_{3t}=1$ (exactly one condition must apply).

We want to enforce the following implications:

$$z_{1t}=1\implies\left(y_{t}=0\right)\wedge\left(x_{t}=1\right) $$ $$z_{2t}=1\implies\left(y_{t}>0\right)\wedge\left(y_{t}=y_{t-1}\right)\wedge\left(x_{t}=Kx_{t-1}\right) $$ $$z_{3t}=1\implies\left(y_{t}>0\right)\wedge\left(y_{t}\neq y_{t-1}\right)\wedge\left(y_{t}=1-x_{t}\right) $$ Unfortunately, strict inequality ($>$) and "not equals" ($\neq$) cannot be used in a mathematical program, so the best we can do is an approximation using some small $\epsilon>0.$

In the following, $M_{1},\dots,M_{6}$ are sufficiently large positive constants. The first implication can be expressed as follows. $$y_{t}\le M_{1}\left(1-z_{1t}\right) $$ $$x_{t}\le1+M_{2}\left(1-z_{1t}\right) $$ $$x_{t}\ge z_{1t} $$ The second implication is approximated as follows. $$y_{t}\ge\epsilon z_{2t} $$ $$y_{t}-y_{t-1}\le M_{3}\left(1-z_{2t}\right) $$ $$y_{t-1}-y_{t}\le M_{3}\left(1-z_{2t}\right) $$ $$x_{t}-Kx_{t-1}\le M_{4}\left(1-z_{2t}\right) $$ $$Kx_{t-1}-x_{t}\le M_{4}\left(1-z_{2t}\right) $$ To handle the $\neq$ element of the third implication, we need yet another binary variable $w_{t}.$ The following approximates the third implication. $$y_{t}\ge\epsilon z_{3t} $$ $$y_{t}-y_{t-1}\ge\epsilon z_{3t}-M_{5}w_{t} $$ $$y_{t-1}-y_{t}\ge\epsilon z_{3t}-M_{5}\left(1-w_{t}\right) $$ $$y_{t}-\left(1-x_{t}\right)\le M_{6}\left(1-z_{3t}\right) $$ $$\left(1-x_{t}\right)-y_{t}\le M_{6}\left(1-z_{3t}\right) $$ This necessarily eliminates some solutions that might otherwise be feasible in the original model, such as $y_{t}=y_{t-1}$ and $x_{t}=Kx_{t-1}$ with $y_{t}\in\left(0,\epsilon\right)$.

NOTE: As pointed out by Rob Pratt in a comment, replacing $y_{t}\ge\epsilon z_{2t}$ and $y_{t}\ge\epsilon z_{3t}$ with $y_{t}\ge\epsilon (1 - z_{1t})$ is conceptually equivalent and yields a tighter relaxation.

$\endgroup$
2
  • 1
    $\begingroup$ I guess your second $y_t \ge \epsilon z_{2t}$ was intended to be $y_t \ge \epsilon z_{3t}$, but you can strengthen these two by instead imposing $y_t \ge \epsilon (z_{2t}+z_{3t})$, equivalently, $y_t \ge \epsilon (1-z_{1t})$. $\endgroup$
    – RobPratt
    Mar 8 at 19:41
  • $\begingroup$ @RobPratt: Yes, the last $z_{2t}$ was a copy-paste-forget-to-edit slip (fixed now). Thanks. I agree about the strengthening. $\endgroup$
    – prubin
    Mar 8 at 21:06
0
$\begingroup$

Try:

$1-y_t-Mz_t \le x_t \le 1-y_t+Mz_t$

$ M(z_t-1) \le y_t - y_{t-1} \le M(1-z_t)$

$ L(1-z_t)+ y_{t-1}-Mz_t \le z_t +y_t \le y_{t-1} +M (1-z_t)+Lz_t $

$Kx_{t-1}+M(z_t-1) \le x_t \le Kx_{t-1} + M(1-z_t)$

With $z_t \in \{0,1 \}$, binary, $x_t,y_t = [L,M]$ and

$\endgroup$
2
  • $\begingroup$ Did you mean $z_t \in \lbrace 0, 1 \rbrace?$ $\endgroup$
    – prubin
    Mar 8 at 17:28
  • $\begingroup$ Your proposed formulation allows $y_t=0$ and $x_t=0$, which violates the first requirement that $y_t=0 \implies x_t=1$. $\endgroup$
    – RobPratt
    Mar 8 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.