How to get a more compacted version of some constraints?!
AnsweredDear all,
I have the next information:
R = [1,2,3,4,5] # no of requests r
b = {1:1,2:-1,3:-1,4:1,5:1} # takes 1 or -1
Cr1 = {1: C[0:3], 2:C[3:6], 3:C[6:9], 4:C[9:12], 5:C[12:15] }
Cr2 = {1: C[15:18],2:C[18:21],3:C[21:24], 4:C[24:27], 5:C[27:30]}
Ar1= {r:[(i,j) for i in Cr1[r] for j in Cr1[r]] for r in R }
Ar2= {r:[(i,j) for i in Cr2[r] for j in Cr2[r]] for r in R }
z1 = m.addVars(((r,i,j) for r in R for (i,j) in Ar1[r]), vtype=GRB.BINARY, name='z1')
z2 = m.addVars(((r,i,j) for r in R for (i,j) in Ar2[r]), vtype=GRB.BINARY, name='z2')
I formulated the following constraints, but I believe I can obtain a more compacted version, I appreciate any idea of how to shrink them.....
m.addConstrs((quicksum(z1[r,i,j] for i in Cr1[r] for j in Cr1[r] if i<j)==0 for r in R if b[r]==1),'20')
m.addConstrs((quicksum(z1[r,i,j] for i in Cr1[r] for j in Cr1[r] if i>j)==0 for r in R if b[r]==-1),'21')
m.addConstrs((quicksum(z2[r,i,j] for i in Cr2[r] for j in Cr2[r] if i<j)==0 for r in R if b[r]==1),'22')
m.addConstrs((quicksum(z2[r,i,j] for i in Cr2[r] for j in Cr2[r] if i>j)==0 for r in R if b[r]==-1),'23')
Best Regards,
Sabreen
-
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?. -
Dear Sabreen,
Hello, I guess you are trying to export a compact formulation like A_1*x=b_1 & A_2x<b_2, right?
If so, you can export the model structure variable to get the matrix A_1, A_2, b_1, and b_2, which are generally sparse matrixes in the model structure variable.
In my case, I use YALMIP and MATLAB to call Gurobi, so I can directly use the command export() to get the matrixes. Maybe you can try my way.
Thanks,
Xianbang0 -
Dear Sabreen,
To me, the 4 \(\texttt{addConstrs}\) lines look already very compact. It is not bad to have a few lines more of code if the final code is understandable and can be easily maintained by you (and possibly others). I am sure there is a way to save a line or two but this will most likely result in a rather complex piece of code.
Best regards,
Jaromił0 -
@Xianbang and @ Jaromił , thanks a lot for your prompt response.
Jaromił, thanks to your reply I paid attention to a specific point in my model in general. As Jaromił has mentioned and by try and error the first two sets compacted to
m.addConstrs((quicksum(z1[r,i,j] for i in Cr1[r] for j in Cr1[r] if i<j and b[r]==1 or i>j and b[r]==-1)==0 for r in R ),'20')
Gurobi accepted this syntax and it worked fine.
Best regards,
Sabreen
0
Post is closed for comments.
Comments
4 comments