PuLP is very slow with many constraints added

I'm trying to use PuLP, but it takes 50 seconds to add 4000 constraints (with 67 variables). It only takes a fraction of a second to fix the problem.

We want to use PuLP to easily test multiple solvers for a wide range of problems.

Should this last with PuLP? Using PyGLPK directly only takes a fraction of a second including setup and solution, so I hope not. What can I do to make this step more efficient in PuLP?


Update

My constraint matrix is ​​very sparse and I was able to reduce the setup time to 4 or 5 seconds for this particular problem by only including non-zero coefficients. I can still write my own .lp or .mps formatted file, solve the problem using cbc or glpsol subprocess and parse the solution much more efficiently than PuLP, simply because I can write an input file in a few ms when PuLP takes a few seconds ... I'm still not sure why that would be.

+5


source to share


2 answers


I have no repetitions to comment on.

But you looked at this:

https://groups.google.com/forum/#!topic/pulp-or-discuss/p1N2fkVtYyM

Question asked:



"The problem is solved in less than 0.5 second.
However setting it up with PULP takes more than 10 seconds. "

      

This seems to be what you are reporting. And the solution is not to use the + = or sum (...) operators, but instead, as explained in the link, also:

yeah using the += operator with pulp is really slow, as is using sum() 
instead of lpSum()

      

So perhaps you are adding constraints 1 at a time to PuLP instead of first creating a constraint list and then adding constraints to PuLP at the end?

+9


source


I had a similar problem with an expression with> 10000 variables that I used for my objective function. The root cause was the same as the original poster. Usage sum

for an array is pulp.LpVariable

really slow compared to pulp.LpAffineExpression

. Based on the google groups discussion in the accepted answer, I was able to make my code much faster. I know this is an old question, but it will contain abstract code for the impatient.

The original goal looked like this:

sum([x * k for x in xs] + ys)

      

where xs

is a list pulp.LpVariable

, k

is a floating point constant, and ys

is another list pulp.LpVariable

.



Much faster version:

pulp.LpAffineExpression([(x, k) for x in xs] + [(y, 1.0) for y in ys])

      

I am not exactly timing any of the versions. To give an idea of ​​the time difference, while running the slow version, I was able to find on the internet the reason why pulp might be so slow, find this Stack Overflow question, read the linked discussion and update my code before the expression was done by evaluating , The second version takes a few seconds.

+1


source







All Articles