# How to linearize a conditional constraint in JuMP?

**URL:** https://discourse.julialang.org/t/how-to-linearize-a-conditional-constraint-in-jump/89685
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [November 3, 2022, 2:01am UTC](https://discourse.julialang.org/t/how-to-linearize-a-conditional-constraint-in-jump/89685 "2022-11-03T02:01:01Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Alex\_ricci](https://avatars.discourse-cdn.com/v4/letter/a/a4c791/32.png) [@Alex\_ricci](https://discourse.julialang.org/u/Alex_ricci)
#### Post date: [November 3, 2022, 2:01am UTC](https://discourse.julialang.org/t/how-to-linearize-a-conditional-constraint-in-jump/89685/1 "2022-11-03T02:01:01Z")

</div>

Any proper way for linearizing this conditional constraint?  
if x \leq 0 then y=0  
if x \> 0 then y=1

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [November 3, 2022, 3:03am UTC](https://discourse.julialang.org/t/how-to-linearize-a-conditional-constraint-in-jump/89685/2 "2022-11-03T03:03:11Z")

</div>

Use a big-M:

```julia
model = Model()
@variable(model, x)
@variable(model, y, Bin)
# x > 0 => y = 1
M = 10_000
@constraint(model, x <= M * y)
# x < 0 => y = 0
@constraint(model, -x <= M * (1 - y))
# The x = 0 case is ambiguous, could fix with something like x > eps => y = 1
@constraint(model, x - 1e-6 <= M * y)

```

Since you’ve asked a few of these MIP reformulation questions recently, you might want to do some wider reading:

- [Tips and tricks · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/linear/tips_and_tricks)
- [9 Mixed integer optimization — MOSEK Modeling Cookbook 3.3.0](https://docs.mosek.com/modeling-cookbook/mio.html)
- [Introduction into Modeling and Optimization of Linear Systems - Advanced modelling techniques: big M constraints](https://hegyhati.github.io/IMOLS/pages/modelling_bigM.html) (at the bottom)
- [https://web.mit.edu/15.053/www/AMP-Chapter-09.pdf](https://web.mit.edu/15.053/www/AMP-Chapter-09.pdf)

I assume most textbooks should also cover various reformulation tricks, but I don’t have any to hand that I ca list.

You should also read:

- [Dealing with big-M constraints](https://www.gurobi.com/documentation/9.5/refman/dealing_with_big_m_constra.html)
- [OR in an OB World: Perils of "Big M"](https://orinanobworld.blogspot.com/2011/07/perils-of-big-m.html)
