model.addVars
AnsweredHello,
I have two lists of tuples, as follows :
L = [('BE212', 'BE352', 0, 0), ('BE212', 'BE211', 0, 0), ('BE242', 'BE100', 0, 0), ('BE236', 'BE232', 0, 0), ('BE212', 'BE343', 0, 0)]
R = [('BE310', 'BE242', 0), ('BE334', 'BE332', 0), ('BE257', 'BE254', 0), ('BE341', 'BE241', 0), ('BE223', 'BE332', 0), ('BE258', 'BE256', 0), ('BE310', 'BE352', 0), ('BE242', 'BE100', 0), ('BE212', 'BE236', 0), ('BE235', 'BE234', 0)]
Using Gurobipy, I would like to create a binary variable x_rl with the index r being the tuples in R and the index l being the tuples in L. I want to be able to reference my variables with the tuples directly, and not through for instance x_13 with r=1 standing for the first element in R and l=3 standing for the thirdh elemnt in L. I want to be able to reference my viriable as x_('BE310', 'BE242', 0)('BE242', 'BE100', 0, 0).
The following doesn't work :
x = model.addVars(R, L, vtype=gp.GRB.BINARY)
Thank you for your help !
-
Official comment
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
You can do that by looping over both your lists:
x = gp.tupledict()
for l in L:
for r in R:
x[l,r] = m.addVar(vtype="B", name=f"x_{l}{r}")The reasons why your code doesn't work can be found here.
Hope this helps.
Best regards
Jonasz0 -
You can also appeal to itertools.product to create a single index set, but this will run a bit slower than the solution from Jonasz
import itertools
x = m.addVars(itertools.product(R,L), vtype="B", name="x_")- Riley
0
Post is closed for comments.
Comments
3 comments