How to code an index which is defined as for each and in the same time to sum over its variable range?
AnsweredI would appreciate your help in the below constraint, where we are using for loop for defining for each and at the same time inside the constraint. This means that If t=3, I will be I[j,3] and it should be summed from 1 to 3 inside the constraint itself.
Any help?
-
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 Ibrahim,
You did not specify which terms are variables and which are scalar values so I just assumed that \(I,q,\) and \(d\) are all variables.
I think what you need can be achieved via the function addVars() and quicksum() with two \(\texttt{for}\)-loopsimport gurobipy
from gurobipy import *
model = Model("test")
Nc = [0,1,2,3,4]
T = [0,1,2,3]
I = model.addVars(Nc, T, name="I")
q = model.addVars(Nc, T, name="q")
d = model.addVars(Nc, T, name="d")
for t in T:
for j in Nc:
model.addConstr( - I[j,t] + I[j,0] + quicksum((q[j,l]-d[j,l]) for l in range(0,t+1)) == 0, "myConstr_"+str(t)+"_"+str(j))
model.write("test.lp")
model.dispose()Note that I used the write() function to write a human readable LP file and with that check whether the constructed constraints are indeed correct.
Best regards,
Jaromił0 -
Hi Dr.Jaromil
I got it, Thanks for your time and effort. I really appreciate it
0
Post is closed for comments.
Comments
3 comments