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