Performance issues with large variables
AnsweredI have a distance matrix D of size 2000×2000. Mvars A and B of same size as above A is continous and B is Binary. Forming a part of objective function is the expression D*A*B representing that Dij*Aij is selected or not for all i and j in 2000×2000
But formulation of this expression takes too much time and I have many more such expressions and adding them all to model as objective function taked another big time.
How do I optimize it
-
Hi Ritesh,
Can you please share the code you are using to build the objective function of your model and the Gurobi version you are using?
Best regards,
Simran1 -
#Distance.shape = (2000,2000) type = np.matrix
B = m.addMVar((2000,2000), vtype= GRB.CONTINUOUS , lb = 0 , name = B_name )
X = m.addMVar((2000,2000), vtype= GRB.BINARY)
exp1 = (Distance * B * X).sum()
P = m.addMVar((2000,2000), vtype= GRB.CONTINUOUS , lb = 0 , name=P_name)
exp2 = (Distance * P * Z).sum()
exp3 = gp.quicksum(900 - gp.quicksum(B[i][j] for i in range(Di)) for j in range(Dj))
exp4 = gp.quicksum(1800 - gp.quicksum(P[i][j] for i in range(Di)) for j in range(Dj))
m.setObjective(exp1 + exp2 + exp3 + exp4 , GRB.MINIMIZE)Gurobi Optimizer version 10.0.2 build v10.0.2rc0 (win64)
0 -
Hi Ritesh,
Thanks for sharing your code.
For exp1 and exp2, we suggest flattening the MVars to 1D arrays and using the matrix multiplication overloaded @ to compute your expression. For example,
exp1 = B.reshape(-1) @ sp.sparse.diags(Distance.reshape(-1)) @ X.reshape(-1)
With this, the building of exp1 and exp2 will improve significantly.
For exp3 and exp4, we suggest avoiding the use of MVars to add one variable at a time in a term-based fashion, instead, you can do the following to speed up the building time of these expressions:
exp3 = 900*Dj - B[:Di,:Dj].sum()
Best regards,
Simran1
Please sign in to leave a comment.
Comments
3 comments