# How can I get the maximum of an absolute value of an objective

**URL:** https://discourse.julialang.org/t/how-can-i-get-the-maximum-of-an-absolute-value-of-an-objective/101356
**Category:** Optimization (Mathematical)
**Tags:** question, package, jump
**Created:** [July 8, 2023, 5:00pm UTC](https://discourse.julialang.org/t/how-can-i-get-the-maximum-of-an-absolute-value-of-an-objective/101356 "2023-07-08T17:00:42Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![That\_ind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/that_ind/32/51372_2.png) [@That\_ind](https://discourse.julialang.org/u/That_ind)
#### Post date: [July 8, 2023, 5:00pm UTC](https://discourse.julialang.org/t/how-can-i-get-the-maximum-of-an-absolute-value-of-an-objective/101356/1 "2023-07-08T17:00:42Z")

</div>

Hi,

How can I get the maximum of an absolute value of an objective? Something like

```julia
@objective(model, Max, abs(var_ref))

```

But I got the following

```julia
ERROR: LoadError: MethodError: no method matching abs(::AffExpr)

```

---

<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: [July 8, 2023, 5:08pm UTC](https://discourse.julialang.org/t/how-can-i-get-the-maximum-of-an-absolute-value-of-an-objective/101356/2 "2023-07-08T17:08:17Z")

</div>

See [Tips and tricks · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/linear/tips_and_tricks/#Absolute-value)

---

<div class="post-metadata">

### Author: ![That\_ind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/that_ind/32/51372_2.png) [@That\_ind](https://discourse.julialang.org/u/That_ind)
#### Post date: [July 8, 2023, 5:24pm UTC](https://discourse.julialang.org/t/how-can-i-get-the-maximum-of-an-absolute-value-of-an-objective/101356/3 "2023-07-08T17:24:11Z")

</div>

Thank you for your swift reply! But I found in the link “They do not work if you are trying to maximize |x|”, which is exactly what I hope to do.

---

<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: [July 8, 2023, 5:34pm UTC](https://discourse.julialang.org/t/how-can-i-get-the-maximum-of-an-absolute-value-of-an-objective/101356/4 "2023-07-08T17:34:38Z")

</div>

Oops. I didnt see that. I thought your original code had `Min`.

The reformulation is a bit more involved because you need binary variables. I’m on my phone so its hard to type, so someone else may beat me to replying. I dont think its in the docs, so we should add it.

---

<div class="post-metadata">

### Author: ![That\_ind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/that_ind/32/51372_2.png) [@That\_ind](https://discourse.julialang.org/u/That_ind)
#### Post date: [July 8, 2023, 5:41pm UTC](https://discourse.julialang.org/t/how-can-i-get-the-maximum-of-an-absolute-value-of-an-objective/101356/5 "2023-07-08T17:41:23Z")

</div>

Sorry for the typo in my original code 😅 and thanks a lot for your reply.

---

<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: [July 8, 2023, 5:42pm UTC](https://discourse.julialang.org/t/how-can-i-get-the-maximum-of-an-absolute-value-of-an-objective/101356/6 "2023-07-08T17:42:22Z")

</div>

Heres a link to the math you need

[https://docs.mosek.com/modeling-cookbook/mio.html#exact-absolute-value](https://docs.mosek.com/modeling-cookbook/mio.html#exact-absolute-value)

---

<div class="post-metadata">

### Author: ![That\_ind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/that_ind/32/51372_2.png) [@That\_ind](https://discourse.julialang.org/u/That_ind)
#### Post date: [July 11, 2023, 2:04pm UTC](https://discourse.julialang.org/t/how-can-i-get-the-maximum-of-an-absolute-value-of-an-objective/101356/7 "2023-07-11T14:04:56Z")

</div>

> [@odow](#):
>
> The reformulation is a bit more involved because you need binary variables.

Thanks a lot! I have read the link. I wonder if there is any difference between using binary variables and simply writing

```julia
@constraint(model, var_ref >= 0.0)
@objective(model, Max, var_ref)

```

---

<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: [July 12, 2023, 1:06am UTC](https://discourse.julialang.org/t/how-can-i-get-the-maximum-of-an-absolute-value-of-an-objective/101356/8 "2023-07-12T01:06:20Z")

</div>

If you want a variable that takes only non-negative values, then yes, you can just do that. But it’s not equivalent to adding a variable and taking the absolute value.

The reformulation would be something like this:

```julia
function add_abs_constraint(model, x)
     l, u = lower_bound(x), upper_bound(x)
     pos = @variable(model, lower_bound = 0, upper_bound = u)
     neg = @variable(model, lower_bound = 0, upper_bound = l)
     z = @variable(model, binary = true)
     @constraint(model, pos <= u * z)
     @constraint(model, neg <= l * (1 - z))
     @constraint(model, x == pos - neg)
     return pos + neg
end

model = Model()
@variable(model, -2 <= x <= 2)
abs_x = add_abs_constraint(model, x)
@objective(model, Max, abs_x)

```

---

<div class="post-metadata">

### Author: ![That\_ind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/that_ind/32/51372_2.png) [@That\_ind](https://discourse.julialang.org/u/That_ind)
#### Post date: [July 12, 2023, 11:58am UTC](https://discourse.julialang.org/t/how-can-i-get-the-maximum-of-an-absolute-value-of-an-objective/101356/9 "2023-07-12T11:58:15Z")

</div>

Many thanks! But I tried this and get `status is INFEASIBLE`.

If `l` equals `-2`, then 0\<= `neg` \<=-2? This seems not to be straightforward to me. Also, in the link

> [https://docs.mosek.com/modeling-cookbook/mio.html#exact-absolute-value](https://docs.mosek.com/modeling-cookbook/mio.html#exact-absolute-value)

you posted, there seems should be

```julia
@constraint(model, pos <= M * z)
@constraint(model, neg <= M * (1 - z))

```

and the constant `M` is an a priori known upper bound on |x|, which should be positive.

And indeed in my case the objective to be optimized is of type `AffExpr`, so I added a line

```julia
@constraint(model, x == x_AffExpr)

```

where `x_AffExpr` is a known expression.

It would be great if you can explain a little bit more. Thanks!

---

<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: [July 14, 2023, 4:30am UTC](https://discourse.julialang.org/t/how-can-i-get-the-maximum-of-an-absolute-value-of-an-objective/101356/10 "2023-07-14T04:30:40Z")

</div>

The example above can be tweaked to change

> [@odow](#):
>
> ```
> l, u = lower_bound(x), upper_bound(x)
> 
> ```

to

```julia
     l, u = abs(lower_bound(x)), abs(upper_bound(x))

```

The full example would then be:

```julia
using JuMP

function add_abs_constraint(model, x)
     l, u = abs(lower_bound(x)), abs(upper_bound(x))
     pos = @variable(model, lower_bound = 0, upper_bound = u)
     neg = @variable(model, lower_bound = 0, upper_bound = l)
     z = @variable(model, binary = true)
     @constraint(model, pos <= u * z)
     @constraint(model, neg <= l * (1 - z))
     @constraint(model, x == pos - neg)
     return pos + neg
end

model = Model()
@variable(model, -3 <= x <= -2)
abs_x = add_abs_constraint(model, x)
@objective(model, Max, abs_x)

## Solve:
using HiGHS
JuMP.set_optimizer(model, HiGHS.Optimizer)

JuMP.optimize!(model)
JuMP.objective_value(model)
# 3.0

@objective(model, Min, abs_x)
JuMP.optimize!(model)
JuMP.objective_value(model)
# 2.0

```
