# Indexing on Vectorized Constraints in JuMP

**URL:** <https://discourse.julialang.org/t/indexing-on-vectorized-constraints-in-jump/79382>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [April 12, 2022, 11:54am UTC](https://discourse.julialang.org/t/indexing-on-vectorized-constraints-in-jump/79382 "2022-04-12T11:54:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)\
**Post date:** [April 12, 2022, 11:54am UTC](https://discourse.julialang.org/t/indexing-on-vectorized-constraints-in-jump/79382/1 "2022-04-12T11:54:57Z")

</div>

I was looking at the example for vectorized constraints in the docs ([Constraints · JuMP](https://jump.dev/JuMP.jl/stable/manual/constraints/#Vectorized-constraints)), which creates the constraint:

```julia
@constraint(model, con, A * x .== b)

2-element Vector{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.EqualTo{Float64}}, ScalarShape}}:
 con : x[1] + 2 x[2] = 5.0
 con : 3 x[1] + 4 x[2] = 6.0

```

I am curious as to why the constraint name does not get indexed (i.e., con[1], con[2]), as would be the case if indexing were used instead of the dot notation to create the constraint:

```julia
@constraint(model, con1[i=1:2], A[i,:]' * x == b[i])

2-element Vector{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.EqualTo{Float64}}, ScalarShape}}:
 con1[1] : x[1] + 2 x[2] == 5.0
 con1[2] : 3 x[1] + 4 x[2] == 6.0

```

Would it make more sense to index it so that both don’t have the same name associated with it?

---

<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:** [April 12, 2022, 6:53pm UTC](https://discourse.julialang.org/t/indexing-on-vectorized-constraints-in-jump/79382/2 "2022-04-12T18:53:04Z")

</div>

See [https://github.com/jump-dev/JuMP.jl/issues/1166](https://github.com/jump-dev/JuMP.jl/issues/1166). You can always call `set_name` to change the name of each constraint if you like.

---

<div class="post-metadata">

**Author:** ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)\
**Post date:** [April 12, 2022, 7:05pm UTC](https://discourse.julialang.org/t/indexing-on-vectorized-constraints-in-jump/79382/3 "2022-04-12T19:05:50Z")

</div>

Thanks for the link. However, this may discourage users from using vectorized constraints if you can’t distinguish the name unless you set them in a separate line.

---

<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:** [April 12, 2022, 7:56pm UTC](https://discourse.julialang.org/t/indexing-on-vectorized-constraints-in-jump/79382/4 "2022-04-12T19:56:51Z")

</div>

> may discourage users from using vectorized constraints if you can’t distinguish the name unless you set them in a separate line.

A few things:

- Changing the name at this point would be a breaking change, so we won’t be doing that

- Calling `set_name` is pretty trivial, but there’s more than one way to do things, so if users would prefer to use the `con[i=1:n]` syntax instead of vectorized, that’s perfectly okay.

- The answer isn’t as obvious as it seems. What would you do in this case?

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

julia> S = ["1", "2"]
2-element Vector{String}:
 "1"
 "2"

julia> @variable(model, x[S])
1-dimensional DenseAxisArray{VariableRef,1,...} with index sets:
    Dimension 1, ["1", "2"]
And data, a 2-element Vector{VariableRef}:
 x[1]
 x[2]

julia> @constraint(model, con, x .== 0)
1-dimensional DenseAxisArray{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.EqualTo{Float64}}, ScalarShape},1,...} with index sets:
    Dimension 1, ["1", "2"]
And data, a 2-element Vector{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.EqualTo{Float64}}, ScalarShape}}:
 con : x[1] = 0.0
 con : x[2] = 0.0

```

We probably shouldn’t name the constraints `con[1]` and `con[2]`, but `con["1"]`?

---

<div class="post-metadata">

**Author:** ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)\
**Post date:** [April 12, 2022, 11:46pm UTC](https://discourse.julialang.org/t/indexing-on-vectorized-constraints-in-jump/79382/5 "2022-04-12T23:46:21Z")

</div>

That example makes sense.
