Skip to main content

Efficiency in building large number of constraints

Answered

Comments

5 comments

  • David Torres Sanchez
    Gurobi Staff Gurobi Staff

    That seems like the best way to do it using the Python API. Have you already tried this?
    Alternatively, you can maybe also try using the Python Matrix API (see an example and some relevant articles).

    Depending on the formulation, with several million constraints, it may be the case the model building is actually the bottleneck.
    You may also find some useful tips in the article: How do I improve the time to build my model? 

    Cheers,
    David

    0
  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Hi Álvaro,

    Responsible for the slow model building is most likely not Gurobi itself but the inner Python for-loop. The embedded if-clause is probably pretty expensive and to speed up the whole process, you should try simplifying or preparing your data structures to avoid this if-clause.

    Cheers,
    Matthias

    0
  • Alvaro Garcia Sanchez
    Gurobi-versary
    First Comment
    First Question

    Many thanks, David and Matthias.

    I will take a look at what David mentions.

    I will try first to avoid the loop within the addconstraint preparing the data to confirm that.

    I assume that serveral million constraints should not be an issue. Am I right?

    Regards, 

    Álvaro.

    0
  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Well, model instances with a few million constraints are pretty large but constructing such models should not be an issue. To benchmark your model construction, you could also write out the model file as MPS and compare how much time is needed to read that file in.

    Cheers,
    Matthias

    0
  • Alvaro Garcia Sanchez
    Gurobi-versary
    First Comment
    First Question

    Hi,

    I removed the if from the quicksum as Matthias suggested and the building time has improved.

    Now the problem is preprocessing data, but that is nothing related with Gurobi. When I get that data efficiently processed, I will see how fast I can build the model.

    Many thanks,

    Álvaro.

    0

Please sign in to leave a comment.