PuLP very slow when adding many constraints

好久不见. 提交于 2019-11-30 14:43:28

I don't have reps enough to comment.

But have you looked at this:

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

The question is 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 report as well. And the solution is to not use += or sum(...) operators, but instead, as explained in the link as well:

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

So maybe you are adding the constraints 1 at a time to PuLP instead of building the list of constraints first and then, at the end, add the constraints to PuLP?

I had a similar problem, with a expression with > 10,000 variables that I was using for my objective function. The root cause was the same as the original poster saw. Using sum on an array of pulp.LpVariable is really slow compared to pulp.LpAffineExpression. Based on the Google Groups discussion in the accepted answer I was able make my code go much faster. I know this is an old questions, but will includes some abstracted code for the impatient.

The original objective looked like:

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

where xs is a list of pulp.LpVariable, k is a floating point constant, and ys is another list of pulp.LpVariable.

A much faster version is:

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

I did not precisely time either version. To give an idea of the time difference, while running the slow version, I was able to search the Internet for why pulp might be so slow, find this StackOverflow question, read the linked discussion, and update my code before the expression was done evaluating. The second version takes a few seconds.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!