Correct way to define vectorized variables and constraints with JuMP

using JuMP

# Block1
M =  Model()
@variable(M, x[1:3] >= 1)

# Block2
M =  Model()
@variable(M, x[1:3] .>= 1)

# Block3
M =  Model()
@variable(M, x[1:3])
@constraint(M, x[1:3] >= 1)

# Block4
M =  Model()
@variable(M, x[1:3])
@constraint(M, x[1:3] .>= 1)

Block1: OK.
Block2: OK.
Block3: Not OK.
Block4: OK.

Why? And what is the most correct way to define vectorized variables and constraints with JuMP?

Many thanks!

  1. This is the recommended way. It creates a vector x of three variables and sets the lower bound to 1
  2. I didn’t think this worked, but I guess it does. It is identical to (1)
  3. This does not work, because x[1:3] >= 1 has a vector x[1:3] on the left-hand side and a constant on the right-hand side
  4. This has a vector x[1:3] on the left-hand side, and a constant on the right-hand side, but it uses broadcasting to add three constraints. This does not modify variable bounds, but instead adds three new rows to the constraint matrix, effectively creating [1 0 0; 0 1 0; 0 0 1] * x >= [1, 1, 1]
1 Like