# Absolute value in JuMP

**URL:** <https://discourse.julialang.org/t/absolute-value-in-jump/40911>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [June 7, 2020, 4:18am UTC](https://discourse.julialang.org/t/absolute-value-in-jump/40911 "2020-06-07T04:18:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sobhan](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@sobhan](https://discourse.julialang.org/u/sobhan)\
**Post date:** [June 7, 2020, 4:18am UTC](https://discourse.julialang.org/t/absolute-value-in-jump/40911/1 "2020-06-07T04:18:34Z")

</div>

I’m trying to optimize the absolute value of an affine function, for example, something like:

```julia
using JuMP
model = Model()
@variable(model, x)
@objective(model, Min, abs(x))

```

but i get

```julia
Closest candidates are:
  abs(::Bool) at bool.jl:83
  abs(::Float16) at float.jl:526
  abs(::Float32) at float.jl:527
  ...
Stacktrace:
 [1] top-level scope at /home/sobhan/.julia/packages/MutableArithmetics/ZGFsK/src/rewrite.jl:227
 [2] top-level scope at /home/sobhan/.julia/packages/JuMP/MnJQc/src/macros.jl:762

```

do i really have to define another variable like this?

```julia
using JuMP

model = Model()
@variable(model, x)
@variable(model, abs_x)
@constraints(model, begin abs_x >= x; abs_x >= -x end)
@objective(model, Min, abs_x)
model

```

i’m on julia 1.4 and JuMP v0.21.2

---

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [June 7, 2020, 6:42am UTC](https://discourse.julialang.org/t/absolute-value-in-jump/40911/2 "2020-06-07T06:42:13Z")

</div>

Yes you do. According to documentation,

> Use the @objective macro to set linear and quadratic objective functions in a JuMP model.

Absolute value is neither linear nor quadratic. The standard solution is to use slack variables, as you have done. Depending on the problem, there is also `@NLObjective`:

> <https://stackoverflow.com/questions/45395237/how-to-use-abs-function-in-a-objective-function-of-jump-julia>

---

<div class="post-metadata">

**Author:** ![sobhan](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@sobhan](https://discourse.julialang.org/u/sobhan)\
**Post date:** [June 7, 2020, 1:52pm UTC](https://discourse.julialang.org/t/absolute-value-in-jump/40911/3 "2020-06-07T13:52:08Z")

</div>

Thanks
