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?
-
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
Please sign in to leave a comment.
Comments
1 comment