Gurobipy Model adding constraints at a very slow rate
回答済みI'm building a fairly large linear program and had recently built it using Pyomo, but I'm trying to reduce solve times, so I switched the model over to the Gurobipy API. When I solved the model with a small dataset, it seemed to speed up the process considerably (7 seconds to solve with Pyomo vs 0.3 seconds with Gurobi API). However, I switched over to a much larger dataset, and now Pyomo is building the model significantly faster (90 seconds with Pyomo vs >20 minutes with Gurobi). I narrowed the issue down to one constraint which was taking a long time to add into the model. I've formulated the constraint add in two separate ways, as seen below, but I get similar results with each. Z, U, and Y are variables while all other capital letters are lists of tuples.
Using addConstrs:
op.addConstrs((quicksum(Z[u,t,n,k,k2] for k2 in M if (n, k, k2) in S and (t,k2) in TM) + \
quicksum(U[u,t,r,k] for r in TH if (u,r) in UTH and n == r) - \
quicksum(Y[u,t,k,n1,n] for n1 in N if (k, n1, n) in LI if (u,t) in UT) == 0 \
for (u,t) in UT for (n,k) in NM if (t,k) in TM), name = 'FlowBalanceIn')
Using for loops and addConstr:
for (u,t) in UT:
for (n,k) in NM:
if (t,k) in TM:
term1 = quicksum(Y[u,t,k,n1,n] for n1 in N if (k, n1, n) in LI if (u,t) in UT)
term2 = quicksum(U[u,t,r,k] for r in TH if (u,r) in UTH and n == r)
term3 = quicksum(Z[u,t,n,k,k2] for k2 in M if (n,k,k2) in SW and (t,k2) in TM)
name = 'FlowBalanceIn[' + u +',' + t + ','+ n + ',' + k + ']'
op.addConstr(term1 - term2 - term3 == 0, name = name)
The model has around 80,000 variables, so I understand that it might take a while to build these constraints, but after timing a few things out and looking at the .lp file, it seems that these constraints are only being added to the model at a rate of ~8.5/second, which seems awfully slow (for comparison, the model adds all 80,000 variables in 0.6 seconds and Pyomo adds all of these constraints in around 30 seconds). Any suggestions on why this specific constraint is taking so long to add to the model?
Edit: I'll also add that each of these constraints are usually only have 2-5 variables each, so these aren't working with long equations.
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Noah,
Thank you for contacting us!
You have a commercial account with us. We converted your question into a ticket and we will get back to you through the ticket.
Best regards,
Maliheh
0
投稿コメントは受け付けていません。
コメント
2件のコメント