Skip to main content

Variable participating (only) in general constraints appear with 0 weight in objective in LP file

Answered

Comments

4 comments

  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    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,
    Matthias

    0
  • Timo Voipio
    First Comment
    First Question

    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
  • Eli Towle
    Gurobi Staff Gurobi Staff

    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:

    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
  • Riley Clement
    Gurobi Staff Gurobi Staff

    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.