• Gurobi Staff

Apparently, you are using a 1-indexed list to index your variables:

X = list(range(1,n+1))x = mdl.addVars(X,vtype=GRB.BINARY, name = "x")

Later, when you are setting the objective, you are no longer looping over the X list but rather over the 0-indexed Python list range(100) that goes from 0 to 99. There is no x[0].

I suggest you drop the X list and just use range(100) consistently instead.

Cheers,
Matthias

Hi Mattihas, Thank you for your reply. As far as I understand, variables must be in dict structure, not a list or tuple, isn't it? I have another question? Is there a resource that covers modeling from the scratch in gurobi aside from your quick start document? Regards.

• Gurobi Staff

Variables will be stored in a TupleDict object when created via addVars(). You can use any other storage if you like by adding the variables one by one:

var_list = [model.addVar(name=f"x_{i}") for i in range(100)]

We actually have quite a few webinars and tutorials on our webpage dedicated to Python modeling. You should also check out our Python examples directory that is included in the installation package.

Gurobi Resources

Cheers,
Matthias

Hi Matthias. Thank you for your interest. I have another question. How can I formulate three-index formulation in quicksum. Let's say I hava an equation like this:

mdl.addConstrs((x[i,j,w]* x[j,i,w] for w in W if j != i) == 1 for i in N for j in N)

or

for i in N:  mdl.addConstrs((x[i,j,w]* x[j,i,w] for w in W if j != i) == 1 for j in N)

Which one is correct? My best regards.