Skip to main content

KeyError: addConstr

Answered

Comments

2 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than two 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 try Gurobot, our chatbot interface offering instant, expert-level support.
  • 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

Post is closed for comments.