Search code examples
pythonscip

Why is quicksum very slow in SCIP python interface


I'm using scip python interface on ubuntu. I am trying to add a constraint using quicksum:

m.addCons(quicksum(covfinal[i,j]*weightVars[i]*weightVars[j] \
          for i in I for j in J)<=z)

This step takes a long time for some reason. I have I=range(2500), J=range(2500). Is there any way to make this step more effecient?


Solution

  • Currently, there is no other way to add constraints than with the addCons() method. quicksum() dramatically decreases the necessary time compared to Python's builtin sum() because only one expression instance is created that is then iteratively updated with the single terms. Otherwise, lots of new expression objects would be created which is very expensive.

    The (likely) reason why the command takes some time is because you're trying to add a constraint with 6.25 million coefficients - given that covfinal is a dense matrix.

    Also, be sure to always use the latest PySCIPOpt version from GitHub: