# Max and min Gurobi constraint in Julia

**URL:** <https://discourse.julialang.org/t/max-and-min-gurobi-constraint-in-julia/103370>\
**Category:** Optimization (Mathematical)\
**Tags:** question, gurobi\
**Created:** [August 30, 2023, 2:10pm UTC](https://discourse.julialang.org/t/max-and-min-gurobi-constraint-in-julia/103370 "2023-08-30T14:10:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![manoj.centura](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manoj.centura/32/216443_2.png) [@manoj.centura](https://discourse.julialang.org/u/manoj.centura)\
**Post date:** [August 30, 2023, 2:10pm UTC](https://discourse.julialang.org/t/max-and-min-gurobi-constraint-in-julia/103370/1 "2023-08-30T14:10:02Z")

</div>

Hi All,

- MAX constraint: The constraint ![\</span\>r = \max\{x\_1,\ldots,x\_k,c\}\<span\>
- MIN constraint: Similar to a MAX constraint, the constraint ![\</span\>r = \min\{x\_1,\ldots,x\_k,c\}\<span\>]

can we code max and min constraint available in gurobi directly in JuMP.

---

<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:** [August 30, 2023, 8:43pm UTC](https://discourse.julialang.org/t/max-and-min-gurobi-constraint-in-julia/103370/2 "2023-08-30T20:43:22Z")

</div>

There is _some_ support for this, but you need to use the Gurobi C API. Here’s the example from [https://www.gurobi.com/documentation/current/refman/c\_agc\_max.html](https://www.gurobi.com/documentation/current/refman/c_agc_max.html)

```julia
using JuMP, Gurobi
model = direct_model(Gurobi.Optimizer())
l = [1, 2, 3, 4, 0]
@variable(model, x[i = 1:5] >= l[I])
@objective(model, Min, sum(x))
# x5 = max(x1, x3, x4, 2.0)
grb = backend(model)
columns = Cint.(Gurobi.column.(grb, index.(x)) .- 1)
ind = columns[[1, 3, 4]]
GRBaddgenconstrMax(grb, "maxconstr", columns[5], 3, ind, 2.0)
optimize!(model)
value.(x) # [1, 2, 3, 4, 4]

```

---

<div class="post-metadata">

**Author:** ![manoj.centura](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manoj.centura/32/216443_2.png) [@manoj.centura](https://discourse.julialang.org/u/manoj.centura)\
**Post date:** [September 1, 2023, 10:13am UTC](https://discourse.julialang.org/t/max-and-min-gurobi-constraint-in-julia/103370/3 "2023-09-01T10:13:21Z")

</div>

Thank you very much.
