# JuMP with absolute value constraint on variables

**URL:** <https://discourse.julialang.org/t/jump-with-absolute-value-constraint-on-variables/69863>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [October 15, 2021, 11:08pm UTC](https://discourse.julialang.org/t/jump-with-absolute-value-constraint-on-variables/69863 "2021-10-15T23:08:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![qiujj](https://avatars.discourse-cdn.com/v4/letter/q/a88e4f/32.png) [@qiujj](https://discourse.julialang.org/u/qiujj)\
**Post date:** [October 15, 2021, 11:08pm UTC](https://discourse.julialang.org/t/jump-with-absolute-value-constraint-on-variables/69863/1 "2021-10-15T23:08:57Z")

</div>

I’m trying to optimize a function with absolute constraints on its variable (a\<=|x|\<=b), but I don’t know how to replace function “abs” in JuMP constraint or variable.  
At first I wrote this one, where x\_min and x\_max are two known 1\*2 array:

```julia
myModel = Model(GLPK.Optimizer)
@variable(myModel, x_min[i] <= abs(x[i = 1:2]) <= x_max[i])

```

But it turns out that abs cannot be used in variable. I also tried to add constraint with abs, but that also failed.  
I know that I can write -b\<=x\<=b to replace |x|\<=b, but how should I deal with a\<=|x|?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [October 15, 2021, 11:59pm UTC](https://discourse.julialang.org/t/jump-with-absolute-value-constraint-on-variables/69863/2 "2021-10-15T23:59:35Z")

</div>

> [@qiujj](#):
>
> a\<=|x|

That’s a non-convex constraint if `a` is positive and `x` can be negative or positive which I assume must be the case. You can use a mixed integer formulation to encode this.

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [October 16, 2021, 12:03pm UTC](https://discourse.julialang.org/t/jump-with-absolute-value-constraint-on-variables/69863/3 "2021-10-16T12:03:48Z")

</div>

For more information on such a formulation, you can look here:  
[https://docs.mosek.com/modeling-cookbook/mio.html#exact-absolute-value](https://docs.mosek.com/modeling-cookbook/mio.html#exact-absolute-value)  
Once you’ve reformulated |x| = t, just add the constraint x\_{min} \leq t \leq x\_{max}

See also this recent question: [Absolute value, abs(), on Gurobi solver - #3 by jd-foster](https://discourse.julialang.org/t/absolute-value-abs-on-gurobi-solver/68493/3)
