Skip to main content

AddConstrs - "'Var' object is not iterable"

Answered

Comments

3 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three 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 why not try our AI Gurobot?.
  • Jaromił Najman
    • Gurobi Staff

    Hi Viktor,

    Your code is missing the information what are the sets \(M\) and \(S\).

    Nevertheless, you define your variables \(x\) and \(t\) over \(G\) and \(M\) but not over the set \(S\), which is required for the summation. Additionally, you have two \(x\) variables, where one has 3 indices and one has only 2. I assume that you want to use the \(x\) variables with 3 indices. A working code would be

    import gurobipy 
    from gurobipy import *

    m = Model('test')

    G = [1,2,3,4,5]
    M = [1,2,3,4]
    N = [1,2,3]
    S = [1,2,3]
    H = 2

    x = m.addVars(S, G, G, M, vtype=GRB.BINARY, name="x")
    ttravel = m.addVars(S, G, G, vtype=GRB.CONTINUOUS, name="t")
    tau = m.addVars(G, G, vtype=GRB.CONTINUOUS, name="tau")
    h = m.addVars(G, vtype=GRB.CONTINUOUS, name="h")

    m.addConstrs( (H * (quicksum(x[s,i,j,m] for s in S) -1) ) <=
    (tau[k,i] + quicksum(x[s,i,j,m] * ttravel[s,i,j] for s in S) + (h[j]) - (tau[i,j]))
    for k in G
    for i in G
    for m in M
    for j in N
    if k!=i
    if k!=j
    if i!=j)

    m.write("myLP.lp")

    The quicksum function requires a list of terms to be added, thus simply calling

    quicksum(x[s,i,j,m])

    is not enough, since the Var object \(\texttt{x[s,i,j,m]}\) is not iterable, because it is not a list.

    The second inequality can be fixed in an analogous way.

    Best regards,
    Jaromił

    1
  • Viktor Ihler
    • Gurobi-versary
    • First Question
    • First Comment

    Thank you for your inputs Jaromił!

    It became evident that I had cut some corners during my programming which first came to haunt me in the restriction in question. You helped clarify what was missing, and I now make great progress.

    Thank you so much for your time and help.

     

    Best regards,

    Viktor

    0

Post is closed for comments.