Performance issue while adding constraints #436
-
Dear all, when googling for ways to speed up the constraints generation in a lot of old SO threads it is mentioned that using "+=" is awfully slow but I have yet to find what should be used instead. There was also mention of a patch for the "+=" operator has this been implemented? If this has not been patched could this someone provide an example of what should be done instead? I think this questions has been asked by many but there hasn't been any satisfactory answer? example Thank you all in advance |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
lpSum() and a list comprehension so |
Beta Was this translation helpful? Give feedback.
-
Do you have a specific minimum reproducible example in python we could check? Stuart's answer is correct. It just may be that the inefficiencies are coming from somewhere else in the code (or in the structure of the list comprehension). In some very rare occasions, there are some very niche alternatives that can work better. That's why I'm asking for an example. And why there's no "single definitive solution" (that is, besides |
Beta Was this translation helpful? Give feedback.
-
Pulp uses the However often people come from other libraries and languages and use for b in Bars:
c = LpAffineExpression()
for w in Warehouses:
c += vars[w][b]
prob +=c >=demand[b] in this case the |
Beta Was this translation helpful? Give feedback.
-
LpProblem.extend()
will do what you want
https://github.com/coin-or/pulp/blob/b99ff24891123fae928c6571d2eccd3e073eb385/pulp/pulp.py#L1681
Stuart Mitchell
PhD Engineering Science
Extraordinary Freelance Programmer and Optimisation Guru
www.stuartmitchell.com
…On Tue, May 4, 2021 at 4:06 AM Ervin Python ***@***.***> wrote:
thanks for the reply and help! :)
But for my objective function I create a lpSum element for each individual
summation and once they are all created I add to the model which does speed
the create of the objective function example:
c1 = lpSum[(x[i][j]*c[i][j],
for i in I,
for j in J])c2 = lpSum([Y[i]*fc[i],
for i in I])
prob += c1 + c2
So there is no equivalent for constraints to be all added simultaneously?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#436 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAFUIWLQ5WZQABU5AOYFXHTTL3CW7ANCNFSM432DHM7A>
.
|
Beta Was this translation helpful? Give feedback.
Pulp uses the
+=
operator to add constraints toLpProblem
s this is idiomatic usage.However often people come from other libraries and languages and use
+=
to generate single constraints like the followingin this case the
+=
in this linec += vars[w][b]
makes the code very slow