Skip to main content

quicksum-list

Comments

4 comments

  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Hi Adem,

    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

    0
  • Adem akgül
    Gurobi-versary
    First Comment
    First Question

    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.

    0
  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Hi Adem,

    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

    0
  • Adem akgül
    Gurobi-versary
    First Comment
    First Question

    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.

    0

Please sign in to leave a comment.