1
votes

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.

2

2 Answers

0
votes

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.

0
votes

I have the same problem. I use np.dot to reduce my time by half. But the problem is still not completely solved.

constrain_variables = np.dot(constrain_matrix, np.array(variables))
for i in range(num_count):
    constraints.append(constrain_variables[i]<= -constant[i])