Skip to main content

Performance issues with large variables

Answered

Comments

3 comments

  • Simranjit Kaur
    • Gurobi Staff

    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,
    Simran

    1
  • Ritesh Bhalerao
    • Gurobi-versary
    • Curious
    • First Comment
    #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
  • Simranjit Kaur
    • Gurobi Staff

    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,
    Simran

    1

Please sign in to leave a comment.