Skip to main content

KeyError: (0, 1) in Gurobi in Python

Answered

Comments

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

    The indents are incorrect. You are trying to access element \(\texttt{x[0,1]}\) before it is created. The correct code would be

    def pmedian(m, n, c, p): 
    model
    = gp.Model("p-median")
    y,x = {}, {}
    for j in range(m):
    y[j]
    = model.addVar(obj=0, vtype="B", name="y[%s]"%j)
    for i in range(n):
    x[i,j]
    = model.addVar(obj=c[i,j], vtype="B", name="x[%s,%s]"%(i,j)) model.update()

    coef = [1 for i in range(n) for j in range(m)]
    var
    = [x[i,j] for i in range(n) for j in range(m)]

    Best regards,
    Jaromił

    0
  • Jaromił Najman
    • Gurobi Staff

    This is a duplicate of this stackoverflow post.

    0
  • Nicola Brucker
    • Gurobi-versary
    • First Comment

    Hey i have the same problem with this line, maybe someone can help me.

    Thanks

    model.addConstrs(((gurobipy.quicksum(arc[i, j] for i in locations_without_dummy if i != j)) == 1
    for j in all_locations_connections), name='everyNode')
    0
  • Jaromił Najman
    • Gurobi Staff

    Hi Nicola,

    The error means that \(\texttt{arc[0,1]}\) is not present. You could print your arcs

    print(arc)

    to see which arcs are actually present. Depending on whether arc 0-1 should be present or not, you have to either fix your arc generation or only sum over accessible arcs.

    Please note that it is very hard to provide help without a minimal reproducible example.

    Best regards, 
    Jaromił

    0
  • Nicola Brucker
    • Gurobi-versary
    • First Comment

    Thanks for the Quick Answer and sorry im a beginner and have to try to fix the code.

    It should be a vehicle routing problem with time windows.

    Data is insert by a csv.file

    The constraint should provide, that every node will be visited.

    Maybe this will help to understand my problem:

    Key Error: ((1,2), (0,1))

    node = [0, 1, 2, 3, 4, 5, 6] # Dummy,P1,2K,2W,KW,PW,PW2
    instance_data['node'] = node
    all_locations_connections = [(node_0, node_1) for node_0 in instance_data['node']
    for node_1 in instance_data['node']
    if node_0 != node_1]

    locations_without_dummy = []
    for element in all_locations_connections:
    if not (element[0] == 0 or element[1] == 0):
    locations_without_dummy.append(element)
    travel_times = {(node_0, node_1): instance_data['distancematrix'][node_0][node_1]
    for (node_0, node_1) in all_locations_connections}
    arc = model.addVars(travel_times.keys(), vtype=GRB.BINARY, obj=travel_times.values(), name='arc')
    #This is where the erroer is coming from
    model.addConstrs(((gurobipy.quicksum(arc[i, j] for i in locations_without_dummy if i != j)) == 1
    for j in all_locations_connections), name='everyNode')


    0
  • Jaromił Najman
    • Gurobi Staff

    Hi Nicola,

    Your example is not reproducible, please refer to the description of a minimal reproducible example. In particular, the definition of \(\texttt{instance_data['distancematrix'][node_0][node_1]}\) is missing.

    You define your arcs over \(\texttt{travel_times.keys()}\). Thus, you can access arcs via \(\texttt{arc[i,j]}\) with \(\texttt{i in node}\) and \(\texttt{j in node}\) with \(\texttt{i != j}\).

    In the addConstrs call you are looping over \(\texttt{locations_without_dummy}\) and \(\texttt{all_locations_connections}\) of which the elements are tuples, e.g., \(\texttt{(1,2)}\). Thus what your code is trying to do is to access \(\texttt{arc[(1,2),(0,1)]}\). You have to either redefine your lists \(\texttt{all_locations_connections}\), \(\texttt{locations_without_dummy}\) or rethink the access of nodes in the addConstrs call.

    To better understand what I mean you can print the respective objects

    arc = model.addVars(travel_times.keys(), vtype=GRB.BINARY, obj=travel_times.values(), name='arc')

    model.update()
    print(arc)
    print(locations_without_dummy)
    print(all_locations_connections)

    Best regards, 
    Jaromił

    0

Post is closed for comments.