From my answer on SO:
in no case can you dynamically create a binding like
x_i
By this I really mean it. Don’t try interpolating expressions into the macros, don’t use Symbol(...)
, and don’t use eval
(not that you have, but people have tried this in the past).
What you should do instead is create an appropriate data structure to hold the variables. That might be a Vector
, a Dict
, or some user-defined struct
, but the choice is problem-dependent.
In your case, I would do something like this:
types = ["b","r","b"]
model = Model()
var_list = [
@variable(model, [1:5], binary = t == "b") for t in types
]
model = Model()
@variable(model, var_list[t=1:3, [1:5]], binary = types[t] == "b")
Edit: “I thought a bit more, and remember this board” Great I prefer discourse for back-and-forth discussion. Conversations on SO can get shut-down by the mods.