# using "max" in nonlinear constraints

**URL:** <https://discourse.julialang.org/t/using-max-in-nonlinear-constraints/21606>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [March 7, 2019, 9:45pm UTC](https://discourse.julialang.org/t/using-max-in-nonlinear-constraints/21606 "2019-03-07T21:45:24Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhangxz1123](https://avatars.discourse-cdn.com/v4/letter/z/858c86/32.png) [@zhangxz1123](https://discourse.julialang.org/u/zhangxz1123)\
**Post date:** [March 7, 2019, 9:45pm UTC](https://discourse.julialang.org/t/using-max-in-nonlinear-constraints/21606/1 "2019-03-07T21:45:24Z")

</div>

Hi there,  
I’m trying to use the max function in one of my NLconstraint. Following @ **[odow](https://github.com/odow)**’s advice, I use auto-differentiation to allow max into the constraint. The problem is that the length of the array that I want to take max over is not fixed and depend on problem instances, so ideally I want to be able to write

```julia
m = Model(solver=IpoptSolver(print_level=0))
f(array) = maximum(array)
JuMP.register(m, :f, 1, f, autodiff=true)
...
@variable(m, x[1:T])
@variable(m, y)
@NLconstraint(m, y == f(x))

```

However, due to the restriction to scalar operation in NLconstraint, right now I’m forced to write, e.g. when L = 2:

```julia
m = Model(solver=IpoptSolver(print_level=0))
f(a,b) = max(a,b)
JuMP.register(m, :f, 2, f, autodiff=true)
...
@variable(m, x[1:T])
@variable(m, y)
@NLconstraint(m, y == f(x[1],x[2]))

```

and for different L, the code will have to change. Any idea to help me write proper code?

Thanks!!!

---

<div class="post-metadata">

**Author:** ![Azamat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azamat/32/6892_2.png) [@Azamat](https://discourse.julialang.org/u/Azamat)\
**Post date:** [March 7, 2019, 10:11pm UTC](https://discourse.julialang.org/t/using-max-in-nonlinear-constraints/21606/2 "2019-03-07T22:11:05Z")

</div>

Note that `maximum(array)` is equivalent to `reduce(max, array)`. Not sure if it solves your problem though.

---

<div class="post-metadata">

**Author:** ![zhangxz1123](https://avatars.discourse-cdn.com/v4/letter/z/858c86/32.png) [@zhangxz1123](https://discourse.julialang.org/u/zhangxz1123)\
**Post date:** [March 7, 2019, 10:25pm UTC](https://discourse.julialang.org/t/using-max-in-nonlinear-constraints/21606/3 "2019-03-07T22:25:22Z")

</div>

Thanks for the comment, but it doesn’t exactly solve my problem. I think the main problem is in the restriction of NLconstraint that doesn’t allow vector input, so I can’t use a `maximum(array)` or `reduce(max, array)` syntax, but instead have to use  
`max(array[1],array[2],...,array[L])`  
It’s nice that max can take any number of input, but is there any syntax that allows me to pass a variable number of inputs into the `max` function, just like the `...` sign above.

---

<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 7, 2019, 10:34pm UTC](https://discourse.julialang.org/t/using-max-in-nonlinear-constraints/21606/4 "2019-03-07T22:34:02Z")

</div>

@zhangxz1123, you’re looking for the [splatting syntax](http://www.juliaopt.org/JuMP.jl/latest/nlp/#Syntax-notes-1) (note: I’m not sure if this works on JuMP v0.18, which it looks like you’re using. It does on the latest JuMP v0.19.):

```julia
using JuMP, Ipopt
m = Model(with_optimizer(Ipopt.Optimizer))
N = 4
@variable(m, x[i=1:N] >= i)
f(y...) = max(y...)
JuMP.register(m, :f, N, f, autodiff=true)
@NLobjective(m, Min, f(x...))
optimize!(m)
```

---

<div class="post-metadata">

**Author:** ![zhangxz1123](https://avatars.discourse-cdn.com/v4/letter/z/858c86/32.png) [@zhangxz1123](https://discourse.julialang.org/u/zhangxz1123)\
**Post date:** [March 8, 2019, 5:22am UTC](https://discourse.julialang.org/t/using-max-in-nonlinear-constraints/21606/5 "2019-03-08T05:22:54Z")

</div>

@odow, this is neat, thank you! I’m trying to update my JuMP package, but seems to have some problem with it. My current JuMP version is v0.18.5, but doing `Pkg.update()` doesn’t update JuMP to v0.19. Below is the output that I get

```julia
  Updating registry at `~/.julia/registries/General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`
 Resolving package versions...
  Updating `~/.julia/environments/v1.0/Project.toml`
 [no changes]
  Updating ~/.julia/environments/v1.0/Manifest.toml`
 [no changes]

```

Any clue why?

---

<div class="post-metadata">

**Author:** ![Azamat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azamat/32/6892_2.png) [@Azamat](https://discourse.julialang.org/u/Azamat)\
**Post date:** [March 8, 2019, 6:22am UTC](https://discourse.julialang.org/t/using-max-in-nonlinear-constraints/21606/6 "2019-03-08T06:22:33Z")

</div>

Looks like JuMP is lower bounded Julia at 1.1. So you need to update Julia to 1.1 first.

---

<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 8, 2019, 12:42pm UTC](https://discourse.julialang.org/t/using-max-in-nonlinear-constraints/21606/7 "2019-03-08T12:42:43Z")

</div>

The lower bound is Julia v1.0, to Julia v1.1, see

> <https://github.com/jump-dev/JuMP.jl/blob/c01802926c34aa30e5d272ac1063f1595084aa4a/REQUIRE#L1>

---

<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 8, 2019, 12:44pm UTC](https://discourse.julialang.org/t/using-max-in-nonlinear-constraints/21606/8 "2019-03-08T12:44:56Z")

</div>

You might have a package that does not support JuMP v0.19 yet, you can try

```julia
] add JuMP@v0.19.0

```

and see the error you get

---

<div class="post-metadata">

**Author:** ![zhangxz1123](https://avatars.discourse-cdn.com/v4/letter/z/858c86/32.png) [@zhangxz1123](https://discourse.julialang.org/u/zhangxz1123)\
**Post date:** [March 8, 2019, 5:21pm UTC](https://discourse.julialang.org/t/using-max-in-nonlinear-constraints/21606/9 "2019-03-08T17:21:02Z")

</div>

@Azamat, @blegat, thanks! Upgrading Julia to 1.1 indeed solves the problem.

---

<div class="post-metadata">

**Author:** ![zhangxz1123](https://avatars.discourse-cdn.com/v4/letter/z/858c86/32.png) [@zhangxz1123](https://discourse.julialang.org/u/zhangxz1123)\
**Post date:** [March 8, 2019, 6:26pm UTC](https://discourse.julialang.org/t/using-max-in-nonlinear-constraints/21606/10 "2019-03-08T18:26:50Z")

</div>

@odow, a follow-up question on splatting syntax. It seems that currently the splatting only works on the variable itself. However, in my problem, the variable is saved as a matrix, but the max is only taken over one column of the variable, e.g.

```julia
using JuMP, Ipopt
m = Model(with_optimizer(Ipopt.Optimizer))
M=5
N=2
@variable(m, x[1:M,1:N])
f(y...) = max(y...)
JuMP.register(m, :f, N, f, autodiff=true)
@NLobjective(m, Min, f(x[1,:]...)
optimize!(m)

```

But this is not allowed. I tried to make `f` act on `x` itself, but didn’t get it to work. Is there any elegant solution?

---

<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 8, 2019, 6:57pm UTC](https://discourse.julialang.org/t/using-max-in-nonlinear-constraints/21606/11 "2019-03-08T18:57:10Z")

</div>

```julia
y = x[1,:]
@NLobjective(m, Min, f(y...)

```

---

<div class="post-metadata">

**Author:** ![zhangxz1123](https://avatars.discourse-cdn.com/v4/letter/z/858c86/32.png) [@zhangxz1123](https://discourse.julialang.org/u/zhangxz1123)\
**Post date:** [March 8, 2019, 7:22pm UTC](https://discourse.julialang.org/t/using-max-in-nonlinear-constraints/21606/12 "2019-03-08T19:22:27Z")

</div>

In fact, I need to compute `f(x[i,:]...)` for all `i=1:M`, so does explicitly claim a variable `y` still work? It seems that I need to claim M explicit variables in this case.
