AttributeError: 'dict' object has no attribute 'prod'
AnsweredWhen running the diet.py example, the line m.setObjective(buy.prod(cost), GRB.MINIMIZE) is displaying the following error: AttributeError: 'dict' object has no attribute 'prod'. To create the "buy" object, I used the line "for f in foods: buy[f] = m.addVar(name=f)". I noticed that "buy" is a dictionary type and not a tuple dictionary. Does anyone know why?
-
Official comment
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. -
In the original version of the example, the \(\texttt{buy}\) variables are added using Model.addVars() method. This method returns a tupledict object:
buy = m.addVars(foods, name="buy")
In the alternative version, \(\texttt{buy}\) is constructed as a standard Python \(\texttt{dict}\). The values of this dictionary are set to the individual Var objects returned by Model.addVar():
buy = {}
for f in foods:
buy[f] = m.addVar(name=f)You can convert \(\text{buy}\) to a tupledict using the tupledict() constructor:
buy = gp.tupledict(buy)
This should allow you to use the tupledict.prod() and tupledict.sum() methods.
0
Post is closed for comments.
Comments
2 comments