简体   繁体   中英

PuLP slow when adding many constraints

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

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM