0

based on this PuLP very slow when adding many constraints

I am not sure the patch that has been implemented actually solves the problem. I am referring to:

"actually allow "+=" simply by using iadd of the class"

Is there any update on this? Is someone able to provide a "faster" version of this bit of code please?

import pulp
vars = pulp.LpVariable.dicts("var",range(1000),0,None,pulp.LpContinuous)
coeffs = range(1000)
expr = pulp.LpAffineExpression()
import time
start_time = time.time()
for n in range(1000):  #Ten times building an expression of 1000 elements
    #print n
    for i in range(1000): # 1000 elements
        expr += coeffs[i] * vars[i]
print("--- %s seconds ---" % (time.time() - start_time))

Thanks

1 Answers1

1

This should be much faster

import pulp
vars = pulp.LpVariable.dicts("var",range(1000),0,None,pulp.LpContinuous)
coeffs = range(1000)
import time
start_time = time.time()
for n in range(1000):  #Ten times building an expression of 1000 elements
    #print n
    pulp.lpSum([coeffs[i] * vars[i] for i in range(1000)])
print("--- %s seconds ---" % (time.time() - start_time))
Stuart Mitchell
  • 1,060
  • 6
  • 7