[英]PuLP slow adding many constraints
I've read a lot of questions regarding this problem, but i've haven't figured out how to solve it for myself.我已经阅读了很多关于这个问题的问题,但我还没有想出如何为自己解决。 Basically i need to add a lot of constraints to a LP problem, but it takes several minutes to add the constraints.基本上我需要为 LP 问题添加很多约束,但是添加约束需要几分钟。 It seems like the problem is, that i'm using the "prob +=" for every loop, but i'm not sure how to get around that.问题似乎是,我对每个循环都使用“prob + =”,但我不确定如何解决这个问题。 My code looks like this:我的代码如下所示:
for i in range(0,numpy.size(Aeq,0)-1):
prob += lpSum(Aeq.getrow(i).toarray()*x)==0
prob += lpSum(Aeq.getrow(numpy.size(Aeq,0)-1).toarray()*x)==1
Any help speeding this up is much appreciated.非常感谢任何帮助加快速度。
It looks as though the for loop is just performing a matrix multiplication, in which case you should be able to take a submatrix and perform your math on it.看起来 for 循环只是执行矩阵乘法,在这种情况下,您应该能够获取子矩阵并对其进行数学运算。
I'm not sure if you can do this in PuLP, but you certainly can in CVXPY:我不确定你是否可以在 PuLP 中做到这一点,但你肯定可以在 CVXPY 中做到:
import cvxpy as cp
import numpy as np
Aeq = np.random.random((10,10))
x = cp.Variable(10)
constraints = [
cp.sum(Aeq[:-1,:]@x, axis=1)==0,
cp.sum(Aeq[-1,:]@x)==1
]
JuMP may be another technology to look into. JuMP可能是另一种需要研究的技术。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.