How can we speed up for loops in Pulp
An answer to this question on Stack Overflow.
Question
My Problem has 1500 constraints and 1000 equations and solution is time sensitive, adding constraints to my model via for loop is taking the significant amount of time.
Presently I am adding constraints and equations to model like
for i in range(6000):
lputpay +=(x[i]*lput(pp[i],pst[i])) #x[i] is pulp variable
This thing takes 60% time of the solution process, I have tried Numba, Cython but doesn't support non-python objects.
Answer
I had this same problem and did not find a way to rectify it using Python. Python is a slow language unless you're able to find a clever way to leverage libraries. Since you've already tried Numba and Cython, you've exhausted most of your available options on that front.
I solved my problem by switching languages to use Julia's JuMP. Julia hasn't always worked out well for me (I usually can get a significant speed boost by switching to C++), but turned out to be perfect for this problem: the representation was still high-level, but the performance was much better than Python.
A quick example:
using JuMP
using Cbc
lpModel = Model(solver = CbcSolver(seconds = 3600))
@variable(lpModel, x >= 5)
@variable(lpModel, y >= 6)
@constraint(lpModel, x+y == 18)
@constraint(lpModel, 20x + 30y <= 470)
@objective(lpModel, Max, 2x + 3y)
status = JuMP.solve(lpModel)
println("Number of Tea Cups: $(getvalue(x))")
println("Number of Coffee Mugs : $(getvalue(y))")