简体   繁体   English

添加许多约束时PuLP速度变慢

[英]PuLP slow when adding many constraints

based on this PuLP very slow when adding many constraints 当添加许多约束时,基于此PuLP的速度非常慢

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" “实际上仅通过使用该类的iadd就允许“ + =”

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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