KeyError 0 for summing variables in objective function (Using Python)
AnsweredHi,
I have the following code:
# Model Name
m = gp.Model("Optimal_Sizing")
# Adding Variables
u1 = m.addVars(choice1,vtype=GRB.BINARY,name="u1") # Category 1 choice (DG)
u2 = m.addVars(choice2,vtype=GRB.BINARY,name="u2") # Category 2 choice (PV)
P1 = m.addVars(choice1,T,lb=0.0,ub=P1_ub,name="P1") # Category 1 power (DG) [kW]
P2 = m.addVars(choice2,T,lb=0.0,ub=solar_ub,name="P2") # Category 2 power (PV) [kW]
m.update() # Lazy update
# Setting Objective Function
obj = quicksum(u1[a]*P1[a][b] for a in range(choice1) for b in range(T))
m.setObjective(obj, GRB.MINIMIZE)
When I run this code, I receive the following error:
<ipython-input-64-05a3aaf97bd9> in <genexpr>(.0) 79 80 # Setting Objective Function ---> 81 obj = sum((u1[j]*P1[j][t]) for j in range(choice1) for t in range(T)) 82 m.setObjective(obj, GRB.MINIMIZE) 83 KeyError: 0
Not sure why this is happening. Any help is appreciated!
Thank you in advance!
0
-
Official comment
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?. -
Hi Quentin,
\(\texttt{P1}\) is a tupledict so it has to be accessed via \(\texttt{P1[a,b]}\).
Best regards,
Jaromił1 -
Jaromil,
Thank you! Simple mistake, but I'm still new to Gurobi & Python, so hopefully I'll get better!
Cheers
0
Post is closed for comments.
Comments
3 comments