Skip to main content

KeyError: addConstr

Answered

Comments

1 comment

  • Jaromił Najman
    • Gurobi Staff

    Hi Tanvir,

    The error you get occurs because you have to introduce \(\texttt{LC[item]}\) as an optimization variable first.

    for item in road_travel_time:
      route_set = []
      for element in which_links_in_route:
          if item in which_links_in_route[element]:
              route_set.append(element)
    LC[item] = mdl.addVar(lb=-GRB.INFINITY, name="LC[{0}]".format(item))
      mdl.addConstr(LC[item]==quicksum(X[i,j] for i in pot_route for j in route_set), name="L[{0}]".format(item))

    However, this results in another key error which is caused by how you construct your \(\texttt{route_set}\).

    KeyError: ('10_OD2', 1234)

    This means, that you try to access \(\texttt{X['10_OD2', 1234]}\) which is not defined. I don't know what you are trying to achieve with the \(\texttt{route_set}\) so I will leave this fix up to you. Maybe the following is already enough

    dl.addConstr(LC[item]==quicksum(X[i,j] for i in pot_route for j in route_set if j in pot_route[i]), name="L[{0}]".format(item))

    Best regards, 
    Jaromił

    0

Please sign in to leave a comment.