Variable participating (only) in general constraints appear with 0 weight in objective in LP file
AnsweredHi all,
my question relates to "unnecessary" variables appearing in the Objective specification in a MIP problem LP file. This is likely related to https://support.gurobi.com/hc/en-us/community/posts/360048150752-Remove-variables-with-0-coefficient-in-the-LP-file and https://support.gurobi.com/hc/en-us/community/posts/20549609141137-Indicator-constraints-and-objective-function . The latter referred to Eli Towle's explanation in the former.
However, after reading Eli's explanation a few times, I am still missing something. I have a variable (some 200.000 of them, in fact, created with GurobiPy `Model.addVars(..., name="foo")`) participating in general constraints, `foo_2[i] = gurobipy.max_(foo[i], constant=constant_value)`.
As far as I can tell, the variables `foo` are "used in constraints" as they are used to define general constraints, but they still appear in the objective function with 0 weight.
Could you please help me understand what's going on in here? Is the criterion stricter than "used in a constraint"?
`gurobi` package installed from Conda channel, ver 11.0.0
-
Hi Timo,
I may be missing something here, but why are you interested in explicitly removing those variables from the objective function? They will just be ignored when the model is read. File sizes are largely irrelevant because model files should always be compressed (typically, 90% of the file size can be reduced).
Cheers,
Matthias0 -
Hi Matthias,
I would like to remove the unnecessary variables from the LP file in order to make the LP file actually useful for debugging the modeling process. I would not use the LP file for transferring the complete model structure as Gurobi seems to recommend against it and also the LP file does not seem to capture the model completely accurately in any case.
In concrete terms, the objective function in the LP file is rather difficult to read if it has about 5 lines of meaningful terms and roughly 200,000 lines of unnecessary 0-weight terms.
0 -
The condition I described in the post you referenced is a sufficient condition for a variable to appear in the objective function of an LP file with a \( 0 \) coefficient:
Variables that are added to the model but are not used in the constraints/objective appear in the objective function of the LP file with a 0 coefficient.
You are right that it is not a necessary condition. I think the necessary and sufficient conditions for a variable to appear in the objective function of an LP file with a \( 0 \) coefficient are:
- The variable appears in no linear constraints,
- The variable appears in no quadratic constraints,
- The variable appears in no linear or quadratic objective terms, and
- The variable does not have a piecewise-linear objective term.
In your case, the variables only appear in general constraints, so they will appear in the LP file's objective function with coefficient \( 0 \). I'm not aware of any workaround for this.
0 -
It's a bit of a hack, but you can transfer the clutter in the objective, to the constraints, by creating trivial redundant constraints (such as x >= x.lb). You can then optionally remove these lines based on the constraint names, eg:
import gurobipy as gp
m = gp.read("original.lp")
for v in m.getVars():
if v.Obj == 0: # sufficient, not necessary
m.addConstr(v >= v.lb, "redundant")
m.write("new.lp")
with open("new.lp", "r") as f:
newlines = [line for line in f.readlines() if not line.startswith(" redundant")]
with open("new.lp", "w") as f:
f.writelines(newlines)* don't use this updated file for reading with Gurobi, only debugging with human eyes.
0
Please sign in to leave a comment.
Comments
4 comments