Be sparse in constructing Vars and Constrs or let Gurobi deal with it
AnsweredHi experts,
I have a dilemma about how to model things.
First way is: do not create unnecessary Vars and Constraints, but that can make your model code harder to understand and less flexible to change.
Second way, create as much Vars and Constraints as you need to make your model lean and hope that it will not hurt performance, that solver will remove unneeded Vars and Constrs.
Here is an example of it: I have a production variable with one dimensions being production_day that can take one of 21 days (3 weeks). I have a min production constraint for sum of production per day. But that constraint doesn't apply for last week (last 7 days) and for first 7 days constraint may be violated so I add slack variable.
So first option would be add just 7 slack variables and 14 constraints. Second is add variable and constraint for all 21 days. Later state that slack variable from day 8 till 21 has upper limit of 0 and having minimum limit of 0 for last 7 days will act as no constraint has been set. This wha I can later decide to add limit for last 7 days or make violations of limit possible in other days with little effort.
Which approach is better from point of Gurobi, does it make sense to trade a bit of performance for clarity of model (yes I am a Software Engineer too)...
-
Hi Zlatan,
This is not so easy to answer.
It is indeed the case that Gurobi will remove unneeded variables and constraints in presolve.
I do not know with which number of removable variables/constraints this would be remarkable in performance. However, adding many more variables and/or constraints than actually needed can also lead to a longer model-building time. So, you could save time twice by not adding redundant variables and/or constraints.I think the following questions could help to decide on the approach:
- How important is a little performance improvement?
- How often is the model adapted?
(Who will make the adaptions?) - What is the ratio of the number of redundant variables/constraints to the total number of variables and constraints?
I assume you chose the community on purpose for this question. I just want to remind you that since you are a commercial user of Gurobi, you can also open a support request.
Best regards,
Marika0
Please sign in to leave a comment.
Comments
1 comment