Create variable based on array

I have a tuple that contains pairs and I want to create a variable based on these pairs as follows

E = [(1, 2), (10, 16), (18, 24), (32, 33), (12, 10), (19, 18), (31, 30), (17, 36)]
for (i,j) in E
   @variable(model,x[i,j], binary = true)
end

and I got this error message

An object of name x is already attached to this model. If this is intended, consider using the anonymous construction syntax, e.g., x = @variable(model, [1:N], …) where the name of the object does not appear inside the macro.

1 Like

Welcome!

It would be easier to reproduce this if you provided a MWE (e.g.)

using JuMP
model = Model()
E = [(1, 2), (10, 16), (18, 24), (32, 33), (12, 10), (19, 18), (31, 30), (17, 36)]
for (i,j) in E
   @variable(model,x[i,j], binary = true)
end

Is this what you are looking for:

using JuMP
model = Model()
E = [(1, 2), (10, 16), (18, 24), (32, 33), (12, 10), (19, 18), (31, 30), (17, 36)]
@variable(model, x[E], binary = true)
print(model)
#=
Feasibility
Subject to
 x[(1, 2)] binary
 x[(10, 16)] binary
 x[(18, 24)] binary
 x[(32, 33)] binary
 x[(12, 10)] binary
 x[(19, 18)] binary
 x[(31, 30)] binary
 x[(17, 36)] binary
=#
2 Likes