# How to linearize this if-then constraint?

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?

• The equality $y = y + x$ implies that $x = 0$. Do you maybe instead want $z=y+x$, where $z$ is a new variable? Commented Nov 2, 2022 at 15:54
• @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'. Commented Nov 2, 2022 at 16:00
• 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. Commented Nov 2, 2022 at 17:09
• The = in linear and integer programming is not an assignment. This is quite often cause of confusion. Commented Nov 8, 2022 at 9:43

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$$.

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$$

• Your (2) forces $x\ge 1$ when $\mu=0$, which makes $y=y+x$ impossible to satisfy. Commented Nov 8, 2022 at 12:54
• @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. Commented Nov 8, 2022 at 13:46
• Your constraints prevent $x \ge 1$. If you disagree, please exhibit a feasible solution $(x,y,\mu)$ with $x \ge 1$. Commented Nov 8, 2022 at 15:12
• 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. Commented Nov 9, 2022 at 2:27
• 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. Commented Nov 9, 2022 at 2:33

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.