How to add conditionals in quicksum for loop?
AnsweredHi there,
I have a constraint like this one.
When I try to add (and j!=i) to the gurobi model, it shows j is not defined. How do I solve this problem?
for i in range(self.noCells):
for t in range(self.noHours - 1):
self.model.addConstr(
self.alpha * self.SoH[i, t] * self.soc[i, t] +
quicksum(self.gcrPower[l] * self.g[i, t, l] for l in range(self.noL)) / self.noCells +
quicksum(self.solPower[m] * self.r[i, t, m] for m in range(self.noM)) / self.noCells -
quicksum(self.dcrPower[n] * self.d[i, t, n] for n in range(self.noN)) / self.noCells +
quicksum(self.b_plus[j, i, t] for j in range(self.noCells) and j!=i) -
quicksum(self.b_minus[i, j, t] for j in range(self.noCells) and j!=i) -
self.SoH[i, t+1] * self.soc[i, t+1] == 0
)
Thanks.
0
-
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. -
Hi Scott,
You may try doing this instead:
quicksum(self.b_plus[j, i, t] for j in range(self.noCells) if j!=i) - quicksum(self.b_minus[i, j, t] for j in range(self.noCells) if j!=i)
0 -
U r the most clever. That really makes sense.
Thank you, Michel
0
Post is closed for comments.
Comments
3 comments