Time needed to create gurobi constraints is too long
AnsweredHi,
I recently stumbled on the creation time of constraints using model.addConstrs().
p_line = {(i,j,t): m.addVar(vtype=GRB.CONTINUOUS, lb=100, ub=100, name='P_line({}{}),t={}'.format(i,j,t)) for t in set_t for i in (inner_bus  substation) for j in set_tbus[i]}
q_line = {(i,j,t): m.addVar(vtype=GRB.CONTINUOUS, lb=100, ub=100, name='Q_line({}{}),t={}'.format(i,j,t)) for t in set_t for i in (inner_bus  substation) for j in set_tbus[i]}
l_line = {(i,j,t): m.addVar(vtype=GRB.CONTINUOUS, lb=0, ub=100, name='L_line({}{}),t={}'.format(i,j,t)) for t in set_t for i in (inner_bus  substation) for j in set_tbus[i]}
v_sq = {(i,t): m.addVar(vtype=GRB.CONTINUOUS, lb=0.9025, ub=1.1025, name='v_square({}),t={}'.format(i,t)) for t in set_t for i in set_bus}
VoltageDifference = m.addConstrs((v_sq[i,t]  v_sq[j,t] == 2*(R(net,i,j)*p_line[i,j,t]+X(net,i,j)*q_line[i,j,t])  (R(net,i,j)**2+X(net,i,j)**2)*l_line[i,j,t] for i in (inner_bus  substation) for j in set_tbus[i] for t in set_t),name='Voltage_difference')
# R and X are parameters,
# p_line, q_line, and l_line are tuples (32,32,96)
# v_sq is a tuple (33,96)

Hi Panggah,
Is it related to the variables which are in the tuple form?
You construct your variables as dictionaries so this should be fine.
Do my Python and Gurobi versions matter?
They may matter. It is best to always use the latest Gurobi version if possible. To see which Python version is supported, please refer to Support Platforms.
Is there any workaround for this problem?
Very likely there is one. However, it is hard to tell from the small code snippet. Could you please provide a minimal reproducible example?
Best regards,
Jaromił0 
Hi Panggah,
Do my Python and Gurobi versions matter?
I would always try and use the latest versions for both.
Is there any workaround for this problem?
Have you tried using \(\texttt{model.addConstr}\) (this can sometimes be faster):
VoltageDifference = {
(i, j, t): m.addConstr(
(
v_sq[i, t]  v_sq[j, t]
== 2 * (R(net, i, j) * p_line[i, j, t] + X(net, i, j) * q_line[i, j, t])
 (R(net, i, j) ** 2 + X(net, i, j) ** 2) * l_line[i, j, t]
),
name=f"Voltage_difference[{i},{j},{t}]",
)
for i in (inner_bus  substation)
for j in set_tbus[i]
for t in set_t
}Cheers,
David0 
Hi, Jaromił.
Thank you for your reply.They may matter. It is best to always use the latest Gurobi version if possible. To see which Python version is supported, please refer to Support Platforms.
I am using the latest Gurobi version, but my Python is still v3.7 because I have some other libraries that force me to use it for my project. I think the Python version will not affect much, but I will give it a try.
Very likely there is one. However, it is hard to tell from the small code snippet. Could you please provide a minimal reproducible example?
I will provide you the code later for you to try :)
Thank you for your suggestions.
Regards,
Panggah0 
Hi, David.
Thank you for your reply.VoltageDifference = { (i, j, t): m.addConstr( ( v_sq[i, t]  v_sq[j, t] == 2 * (R(net, i, j) * p_line[i, j, t] + X(net, i, j) * q_line[i, j, t])  (R(net, i, j) ** 2 + X(net, i, j) ** 2) * l_line[i, j, t] ), name=f"Voltage_difference[{i},{j},{t}]", ) for i in (inner_bus  substation) for j in set_tbus[i] for t in set_t }
I exactly tried this one before I posted my problem. The creation time did not differ much, though. Below is the screenshot :)
It is quite a drawback if creating all the constraints takes almost 20 seconds while it takes only 0.3 seconds to solve. I need a quick running time that covers both creating and solving the model for the purpose of putting it into an iterative algorithm (around 1000+ iterations). As Jaromił said, I will also provide a reproducible example.
Thank you for your kind suggestion!
Regards,
Panggah0 
I think I found the culprit which is not related to Gurobi at all.
The R(net,i,j) and X(net,i,j) are basically very simple functions, yet takes a little while to call.
The R and X are set to a constant in the above picture, and the problem appears no more.
I think I do not need to provide a reproducible code as it was my own wrongdoing and has nothing to do with Gurobi :)
I am sorry for my mistake and thank you for your kind suggestions.
Regards,
Panggah0 
I am sorry for my mistake and thank you for your kind suggestions.
No problem at all and it's great to hear that you found the source of this issue.
0
Please sign in to leave a comment.
Comments
6 comments