# Dynamically setting the constraint sense in JuMP

**URL:** <https://discourse.julialang.org/t/dynamically-setting-the-constraint-sense-in-jump/64155>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [July 6, 2021, 3:38pm UTC](https://discourse.julialang.org/t/dynamically-setting-the-constraint-sense-in-jump/64155 "2021-07-06T15:38:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mastomaki](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@Mastomaki](https://discourse.julialang.org/u/Mastomaki)\
**Post date:** [July 6, 2021, 3:38pm UTC](https://discourse.julialang.org/t/dynamically-setting-the-constraint-sense-in-jump/64155/1 "2021-07-06T15:38:19Z")

</div>

Hello!

I was wondering if it is possible to specify different types (i.e. \<=, == and \>=) constraints within the same JuMP container. For example

```julia
@constraint(model, con[i = 1:3], i * x <= i + 1)

```

So that whether \<=, == or \>= is specified depends on i?

---

<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 6, 2021, 9:16pm UTC](https://discourse.julialang.org/t/dynamically-setting-the-constraint-sense-in-jump/64155/2 "2021-07-06T21:16:53Z")

</div>

> I was wondering if it is possible to specify different types (i.e. \<=, == and \>=) constraints within the same JuMP container.

Not easily\*.

Why do you want to do this? Why not just write out different constraints?

\*By not easily, I mean you _could_ do the following, but it’s probably a sign that you could restructure your code some other way to avoid this.

```Julia
julia> using JuMP

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> @variable(model, x)
x

julia> s = [MOI.LessThan{Float64}, MOI.GreaterThan{Float64}, MOI.EqualTo{Float64}]
3-element Vector{DataType}:
 MathOptInterface.LessThan{Float64}
 MathOptInterface.GreaterThan{Float64}
 MathOptInterface.EqualTo{Float64}

julia> @constraint(model, con[i=1:3], i * x in s[i](i + 1))
3-element Vector{ConstraintRef{Model, C, ScalarShape} where C}:
 con[1] : x ≤ 2.0
 con[2] : 2 x ≥ 3.0
 con[3] : 3 x = 4.0

```

---

<div class="post-metadata">

**Author:** ![Mastomaki](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@Mastomaki](https://discourse.julialang.org/u/Mastomaki)\
**Post date:** [July 7, 2021, 7:14am UTC](https://discourse.julialang.org/t/dynamically-setting-the-constraint-sense-in-jump/64155/3 "2021-07-07T07:14:28Z")

</div>

Suppose the _user_ gives these constraints, e.g. empirical constraints for a chemical process. It feels artificial to first group them according to the constraint sense.

Your solution looks good. I improved the readability like this:

```julia
function sense_constraint(sense::Symbol, rhs)

    if sense == :>=
        return ( MOI.GreaterThan{Float64}(rhs) )
    elseif sense == :<=
        return (MOI.LessThan{Float64}(rhs) )
    else
        return (MOI.EqualTo{Float64}(rhs) )
    end
end

  

#constraints
@constraint(mymod, q_1[i=1:3], i*x in sense_constraint(:<=, i+1) )
print(q_1)

```
