1
$\begingroup$

If $x \ge 1$ then $y = y + x$. And, if $x \le 0$ then $y = y$, where $x$ and $y$ are non-negative integer decision variables. I am using GLPK solver.

How do I linearize this if-then constraint?

$\endgroup$
4
  • 3
    $\begingroup$ The equality $y = y + x$ implies that $x = 0$. Do you maybe instead want $z=y+x$, where $z$ is a new variable? $\endgroup$
    – RobPratt
    Commented Nov 2, 2022 at 15:54
  • $\begingroup$ @RobPratt thanks for the comment. It won't tackle my problem. Actually, I want to update the value of y by x if `x>0'. $\endgroup$ Commented Nov 2, 2022 at 16:00
  • 1
    $\begingroup$ You would still need to introduce a second variable then, e.g., $y_2$, and adjust your mathematical model accordingly. Therefore, the point of @RobPratt still applies. $\endgroup$
    – PeterD
    Commented Nov 2, 2022 at 17:09
  • 1
    $\begingroup$ The = in linear and integer programming is not an assignment. This is quite often cause of confusion. $\endgroup$ Commented Nov 8, 2022 at 9:43

3 Answers 3

3
$\begingroup$

If you just want to update $y$ and not declare a constraint, then assigning $y = y + x$ already handles both cases $x \ge 1$ and $x = 0$.

$\endgroup$
1
$\begingroup$

I think @RobPratt 's anwser can surely handle your problem.
While if you want to extend x, y to real number, here's an idea in wider range to use:
You can introduce a binary variable $\mu$, and add the following constraints:
$x\le M(1-\mu)\tag{1}$ $x\ge 1-M\mu\tag{2}$ $y'\ge y+x-M\mu\tag{3}$ $y'\le y+x+M\mu\tag{4}$ $y'\le y+M(1-\mu)\tag{5}$ $y'\ge y-M(1-\mu)\tag{6}$
where $y'$ means $y$ after update in order to distinguish y before and after update, and $M$ is a big number.

when $x\le 0$, constraints (1) and (2) forces $\mu$ to be 1, so (5)(6) are active while the others are not, which indicates $y'\le y$, $y'\ge y$, therefore when $x\le 0$, $y'=y$

when $x\ge 1$, constraints (1) and (2) forces $\mu$ to be 0, so (3)(4) are active while the others are not, which indicates $y'\le y+x$, $y'\ge y+x$, therefore when $x\ge 1$, $y'=y+x$

$\endgroup$
8
  • $\begingroup$ Your (2) forces $x\ge 1$ when $\mu=0$, which makes $y=y+x$ impossible to satisfy. $\endgroup$
    – RobPratt
    Commented Nov 8, 2022 at 12:54
  • $\begingroup$ @RobPratt It is not (2) forcing $x\ge 1$ when $\mu = 0$, $\mu$ is a variable whose value is determined by the value of $x$, when $x\ge 1$, constraints (1) and (2) forces $\mu$ to be 0, and therefore constraints (3) and (4) are active, that is $y\ge y+x$ and $y\le y+x$, and when two number are both greater than each other, they are equal. Hope this clarifies. $\endgroup$
    – akio cu
    Commented Nov 8, 2022 at 13:46
  • 1
    $\begingroup$ Your constraints prevent $x \ge 1$. If you disagree, please exhibit a feasible solution $(x,y,\mu)$ with $x \ge 1$. $\endgroup$
    – RobPratt
    Commented Nov 8, 2022 at 15:12
  • $\begingroup$ let's say $(x,y,\mu)=(1,2,0)$, and here I assume 2 here means before update, and my constraints would be: $1\le M\tag{1}$ $1\ge 1\tag{2}$ $y\ge 2+1\tag{3}$ $y\le 2+1\tag{4}$ $y\le 2+M\tag{5}$ $y\ge 2-M\tag{6}$ there's no conflict, and the updated y would be 3. $\endgroup$
    – akio cu
    Commented Nov 9, 2022 at 2:27
  • $\begingroup$ You are proposing that $y=2$ on the RHS and $y=3$ on the LHS. In a system of constraints, the variables with the same name must take the same value. $\endgroup$
    – RobPratt
    Commented Nov 9, 2022 at 2:33
-2
$\begingroup$

How about small $m = 1e-6$?
So $ y >= y + \frac{x^2}{x+m}$
So if x>0 y is updated by x else not.

$\endgroup$

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.