duplicate keys in Model.addVars()
回答済みI try to set two (independent) decision variables (namely, 'open' and 'cover') with the following code:
open = model.addVars(cities, vtype=GRB.BINARY, name ="open")
cover = model.addVars(CITIES, vtype=GRB.BINARY, name = "cover")
where 'cities' and 'CITIES' are two independent lists that have the same number of indices and elements. For instance, cities = ['New york', 'Austin', 'Chicago'] and CITIES = ['New york', 'Austin', 'Chicago'].
Here, I get the following error message:
Traceback (most recent call last):
File "C:/project/practice/DC location (Quikflix).py", line 63, in <module>
locate = model.addVars(cities, vtype=GRB.BINARY, name ="locate")
File "model.pxi", line 2758, in gurobipy.Model.addVars
KeyError: 'Duplicate keys in Model.addVars()'
I think this problem occurs because the decision variables have the same indices and elements. But I still need to define two variables to formulate the problem. How can I solve this issue?
-
正式なコメント
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?. -
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 errorIn 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 -
I found why the problem occurs. cities list includes duplicate strings.
Thank you very much for detailed explanations!
0
投稿コメントは受け付けていません。
コメント
3件のコメント