Variable elimination in Gurobi
AnsweredI am solving a routing problem where I want to delete an arc x[i,j] (binary variable) if a certain condition is met. How do I efficiently do this? Do I use model.remove?
for i in N:
for j in N:
if c[i,j]>100:
model.remove(x[i,j])
or do I simply set the variable to 0?
for i in N:
for j in N:
if c[i,j]>100:
x[i,j]=0
are both the processes same?
-
You can use any of the two to get the same effect. However, the two approaches differ.
With model.remove you completely remove a given variable from the model meaning that you cannot re-use it later in the your algorithm and would have to re-add it to the model.
For the second approach to work you would either have to add a constraint which sets \(x\) to \(0\) or set its lower and upper bound to \(0\).
model.addConstr(x[i,j] == 0)
or
x[i,j].lb = 0
x[i,j].ub = 0This way you would keep the variables in your model and presolve would take care of those (i.e., presolve would remove these fixed variables from the model). Note that if you turn presolve off then these variables will still hang around in the model, possibly slowing it down.
Usually, I would go with the second approach. This way, you will have all variables in your model and will not have to think about which ones did you remove or not, i.e., you will not significantly alter your model formulation.
Best regards,
Jaromił0 -
Got it. Thank you for such a clear interpretation!
0
Please sign in to leave a comment.
Comments
2 comments