Gurobi/Python: How to write the for statement correctly in a constraint ?
Answered
I have the above constraint. I have written both the left-hand side and right- hand side. My problem is how to code the ''for'' statement where R, N, Cr are all sets. N\Cr means all elements in N not in Cr
R = [1,2,3,4]
N = [0,1,2,3,4............................................................18]
C = [ 10, 11,.................................18]
Cr = {1: C[0:3], 2 : C[3:6],3: C[6:9], 4: C[9:12] }
My trial as follows:
'''
for r in R for i in N not in Cr[r]
'''
It does not work........
-
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,
You can achieve your goal using an if statement:
R = list(range(1,5))
N = list(range(0,19))
C = list(range(10,19))
Cr = {1 : C[0:3], 2 : C[3:6], 3 : C[6:9], 4 : C[9:12]}
for r in R:
for i in N:
if i not in Cr[r]:
print("r: ", r, " i: ",i)Please note that \(\texttt{Cr[4]}\) is empty since \(\texttt{C}\) has only 9 elements.
If the order does not matter to you, you can also use sets
R = set(range(1,5))
N = set(range(0,19))
C = set(range(10,19))
Cr = { 1 : set(range(10,13)), 2 : set(range(13,16)), 3 : set(range(16,19)), 4 : set(range(19,22))}
for r in R:
for i in (N-Cr[r]):
print("r: ", r, " i: ",i);However, writing the \(\texttt{Cr}\) sets is no longer possible in the same way as for lists.
Best regards,
Jaromił1 -
@Jaromił Najman thanks a lot for your reply. With regard to set C , it is my mistake, it is actually as follow
C = set(range(10,22))
Returning back to the constraint in the image I have posted in my question and given that I have coded the parameter fr as follows
fr={}
for r in R:
for i in N:
fr[r,i] = 1 if i in P else -1 if i in D else 0Then is the next code for the constraint I shared in my question is right
m.addConstrs((quicksum(yr[r,i,j] for j in N if j!=i)-quicksum(yr[r,j,i] for j in N if j!=i)== fr[r,i] for r in R for i in N if i not in Cr[r] ),'7-)
Additionnaly, excuse me I didnot capture what does it mean
"However, writing the sets is no longer possible in the same way as for lists"0 -
Hi,
I am not sure why you need the \(\texttt{i!=j}\) in your constraint, as you are already only iterating over the \(\texttt{i}\) which are not in \(\texttt{Cr}\).
Regarding writing the \(\texttt{Cr}\) set, I rather meant the initialization of the sets. If you use sets to construct \(\texttt{Cr}\), you cannot directly use the set \(\texttt{C}\), since the items in \(\texttt{C}\) are not ordered and thus you cannot be sure which items from \(\texttt{C}\) you get.
Best regards,
Jaromił0 -
Thanks a lot, Jaromił, I need i!=j because I iterate over i which are not in Cr but it will be in N\Cr and j is in N.
As you may be noticed from my newer posts I am working more on getting ride of any unnecessary arcs.
Regards,
Sabreen
0
Post is closed for comments.
Comments
5 comments