メインコンテンツへスキップ

duplicate keys in Model.addVars()

回答済み

コメント

3件のコメント

  • 正式なコメント
    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?.
  • Eli Towle
    • Gurobi Staff

    This error is raised when you pass a list to Model.addVars() and two or more elements of the list are the same. The tupledict returned by Model.addVars() maps the provided indices (list elements) to the corresponding variable objects. Two indices can't be the same, because this would require duplicate tupledict keys. E.g.:

    x = model.addVars([1, 2, 3]) # Okay
    x = model.addVars([1, 2, 1]) # Duplicate keys error

    In your case, the error is raised when you try to add the \( \texttt{locate} \) variables:

    locate = model.addVars(cities, vtype=GRB.BINARY, name="locate")

    So, the \( \texttt{cities} \) list likely includes duplicate strings. Can you check the value of \( \texttt{cities} \) immediately before this line of code?

    Note that there is no reason to maintain two separate \( \texttt{cities} \) lists. Since the lists contain the same elements, there is no difference whether you index the variables with \( \texttt{cities} \) or \( \texttt{CITIES} \).

    I would also recommend removing spaces from the strings you use to index your variables (like \( \texttt{'New york'} \)). This will cause problems when reading/writing a model file.

    0
  • Donggyu Jeon
    • Gurobi-versary
    • First Comment
    • First Question

    I found why the problem occurs. cities list includes duplicate strings.

    Thank you very much for detailed explanations!

    0

投稿コメントは受け付けていません。