# How to deal with the sum of absolute functions in a JuMP model?

**URL:** <https://discourse.julialang.org/t/how-to-deal-with-the-sum-of-absolute-functions-in-a-jump-model/56452>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [March 3, 2021, 11:27pm UTC](https://discourse.julialang.org/t/how-to-deal-with-the-sum-of-absolute-functions-in-a-jump-model/56452 "2021-03-03T23:27:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Post date:** [March 3, 2021, 11:27pm UTC](https://discourse.julialang.org/t/how-to-deal-with-the-sum-of-absolute-functions-in-a-jump-model/56452/1 "2021-03-03T23:27:58Z")

</div>

I would like to add a constraint that is the sum of absolute values, something like

```julia
using JuMP
model = Model(Ipopt.Optimizer)
@variable(model, w[1:n] >=0)
...
@constraint(model, sum(abs.(w - p)) <= alpha) # not possible

```

I understand that the absolute function is not available (`ERROR: LoadError: MethodError: no method matching abs(::JuMP.AffExpr`). Also tried

```julia
@NLconstraint(model, sum(abs(w[i] - p[i]) for i in 1:n) <= alpha)

```

but got, as I was expecting, the same error. It is not clear to me how to proceed, for instance, using slack variables if that makes sense. How may I do it? Thanks in advance!

---

<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:** [March 4, 2021, 2:42am UTC](https://discourse.julialang.org/t/how-to-deal-with-the-sum-of-absolute-functions-in-a-jump-model/56452/2 "2021-03-04T02:42:30Z")

</div>

Use a linear reformulation

```nohighlight
model = Model()
@variable(model, w >= 0)
p = rand()
@variable(model, t)
@constraint(model, t >= w - p)
@constraint(model, t >= p - w)
@constraint(model, t <= alpha)

```

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [March 4, 2021, 12:37pm UTC](https://discourse.julialang.org/t/how-to-deal-with-the-sum-of-absolute-functions-in-a-jump-model/56452/3 "2021-03-04T12:37:24Z")

</div>

Alternatively, you can use the [`MOI.NormOneCone`](https://jump.dev/MathOptInterface.jl/stable/apireference/#MathOptInterface.NormOneCone).  
If the solvers does not support it, it will be automatically reformulated by the [`MOI.Bridges.Constraint.NormOneBridge`](https://jump.dev/MathOptInterface.jl/stable/apireference/#MathOptInterface.Bridges.Constraint.NormOneBridge) into the form given by @odow

```julia
model = Model()
@variable(model, w[1:n] >= 0)
p = rand(n)
@variable(model, t)
@constraint(model, [t; w - p] in MOI.NormOneCone(n + 1))

```

---

<div class="post-metadata">

**Author:** ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Post date:** [March 4, 2021, 12:45pm UTC](https://discourse.julialang.org/t/how-to-deal-with-the-sum-of-absolute-functions-in-a-jump-model/56452/4 "2021-03-04T12:45:49Z")

</div>

@odow and @blegat thank you very much!
