Very long execution time for adding constraints
AnsweredHello,
I hope everyone is doing fine,
I'm trying to add some constraints but it takes so much time
over 2 hours and half
list1=['x1'+str(i)for i in range(100)]
list2=['x2'+str(i)for i in range(100)]
list3=['x3'+str(i)for i in range(100)]
list4=['x4'+str(i)for i in range(100)]
list5=['x5'+str(i) for i in range(10)]
list6=['x6'+str(i) for i in range(100)]
list7=['x7'+str(i)for i in range(10)]
from tqdm import tqdm
for x1 in tqdm(list1):
for x2 in list2:
for x3 in list3:
Constr=sum(var11[x1,x2,x3,x4] for x4 in list4) + sum(var12[x1,x2,x3,x4] for x4 in list5) - sum(Matrice.iloc[x1][x2]*var13[x1,x2,x4] for x4 in list6) - sum(Matrice.iloc[x1][x2]*var14[x1,x4,x5] for x4 in list6 for x5 in list7)
model.addConstr(Constr==0)
Constr=sum(var21[x1,x2,x3,x4] for x4 in list4) + sum(var22[x1,x2,x3,x4] for x4 in list5) - sum(Matrice.iloc[x1][x2]*var23[x1,x2,x4] for x4 in list6) - sum(Matrice.iloc[x1][x2]*var24[x1,x4,x5] for x4 in list6 for x5 in list7)
model.addConstr(Constr==0)
I tried using sum(var11[x1,x2,x3,'*']) but i get a key error
KeyError: ('*', 'x1', 'X2', 'X4')
-
There are many useful suggestions on how to improve the time to build your model in our Knowledge Base article How do I improve the time to build my model?
In particular, I see that you repeat the same query on the data frame many times:
Matrice.iloc[x1][x2]
I think you can probably save a lot of time by storing that value in a variable just before the inner loop:
for x3 in list3:
You can probably find other ways to improve the time spent iterating in the various loops.
G.
0 -
Hello,
thank you very much for you response,i tried the trick you mentionned above and the execution time dropped from
2 hours and a half
to
1 hour.
I'm trying to explore other options.
Best regards,
Hamza
0
Please sign in to leave a comment.
Comments
2 comments