Skip to main content

keyerror

Answered

Comments

4 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,

    There are two error in the code you provided. First, you define variables \(Z\) over 4 sets. The addVars function takes every suitable input as an index set unless you specify the arguments' name, i.e.,

    Z=m.addVars(station, demand, vtype=GRB.BINARY, name="z")

    Second, you are using the quicksum function, which takes a data set as input. However, you loop over the data trying to re-set the objective function in each \(\texttt{for}\)-loop iteration. The correct way would be

    m.setObjective(quicksum(Z[i,j] * weight[j] for i in station for j in demand), GRB.MAXIMIZE)

    This way, you generate a list of terms \(\texttt{Z[i,j] * weight[j]}\) which will then be summed up.

    Best regards,
    Jaromił

    0
  • chaung yun chi
    • Gurobi-versary
    • Curious
    • Conversationalist

    Thanks for reply, but I can't understand the first part, if I want to create multiple variables base on my two dict, like

    z(1,1) z(1,2).....z(1,11)

    z(2,1) z(2,2).....etc.   

    The description page said the indices represent the key of tupledict, but isn't "station" and "demand" the key of my dict? Why are they four sets?

     

     

    0
  • Jaromił Najman
    • Gurobi Staff

    The variable \(\texttt{GRB.BINARY}\) which is indeed just a character \(\texttt{'B'}\) and the character \(\texttt{"z"}\) are internally converted by Python to lists holding 1 element in order to fit the functions arguments. Thus, you produce a variable tupledict with 4 indices.

    0

Post is closed for comments.