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. 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. 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.
I'm not sure if you can do this in PuLP, but you certainly can in 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.
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.