Skip to main content

Why does it take so long to build the objective?

Answered

Comments

3 comments

  • Jonasz Staszek
    Community Moderator Community Moderator
    Gurobi-versary
    Thought Leader
    First Question

    Dear Florian,

    you seem to be using numpy arrays to come up with an objective statement, which is - in your case - a Gurobi QuadExpr. I would invite you to explore that section and to build your \( obj \) object based on the methods described there. One could hope for an improvement in performance this way.

    Hope this helps.

    Best regards
    Jonasz

     

    0
  • David Torres Sanchez
    Gurobi Staff Gurobi Staff

    Hi Florian,

    You can also use the MVars and friends.
    For example, the following gives the same formulation (I think) and the total building time goes down to just under 12s. I think there must be a faster way but I am not a Matrix API whizz.

    d = model.addMVar((rows, columns), vtype=GRB.CONTINUOUS, ub=5)
    y = model.addMVar((rows, columns), vtype=GRB.CONTINUOUS, ub=4)
    model.setObjective(gp.quicksum(gp.quicksum(0.5 * B * d**2 + C * y)), GRB.MAXIMIZE)

     

    Cheers, 
    David

    0
  • Florian Götz
    Gurobi-versary
    Conversationalist
    First Question

    Hi all,

    thank you for pointing out the documentation of "QuadExpr". Especially the QuadExpr.addTerms() function has enormously sped up the construction of the target function.

    I don't need the matrix API because I construct the variables and constraints only once and sometimes redefine the objective function. For clarity, I have neglected this aspect from the toy example.

    Best regards,

    Florian

    0

Please sign in to leave a comment.