Skip to main content

Setting up Branching Priority for GUROBI in Pyomo (Attention: RIchard from OR Stackexchange)

Answered

Comments

7 comments

  • Richard Oberdieck
    Gurobi Staff Gurobi Staff

    Hi Mike,

    thanks for reaching out. I discussed this issue with a colleague of mine, and here is the situation. BranchPriority is an "attribute", which is associated with each variable. There are three ways to add this to a variable:

    • Using our API, you write:
    x.BranchPriority = 1
    • You create an ".ord" file, and pass it to Gurobi in the command-line using InputFile:
    gurobi_cl InputFile=test.ord model.mps
    • You create an ".ord" file, and pass it to Gurobi using "model.read()":
    model.read("test.ord")

     

    The problem is that none of these options are available in Pyomo. You can though make it work with very little effort if you go to the pyomo source code. This is accurate as of the writing of this post:

    1. Open the file gurobi_direct.py
    2. Navigate to the function "_apply_solver(self) in line 149 (as of commit 2396eafbaf4a6a8b448d72dd273c60b00b7800b7)
    3. In line 182-199, it is parsing all the parameter inputs you gave in the beginning. I would add the following snippet of code:
    except TypeError:
    if key == "InputFile":
    self._solver_model.read(option)
    continue

    I have not tested this code, but it should work. You are basically telling the pyomo to use the "read" option if it hits the input file as a parameter. You may even make this a pull request to pyomo since I think it is a nice addition.

    Best regards

    Richard

     

     

    1
  • Che Han Lim
    Gurobi-versary
    Conversationalist
    First Question

    Dear Richard,

    Let me try it and get back to you should I encounter any problems though I am still leery about modifying the pyomo source code.

    Thank you.

    Yours sincerely,

    Mike

    0
  • Richard Oberdieck
    Gurobi Staff Gurobi Staff

    Hi Mike,

    I get that. I would say though that if you are having this problem, then others might as well. This is why I think making a pull request would be great since that way it will be part of the "official" pyomo.

    Let me know how you get on

    Richard

    1
  • Che Han Lim
    Gurobi-versary
    Conversationalist
    First Question

    Dear Richard,

    I have just attempted what you advised, but Pyomo is still unable to pick the ord file. 

    I have also check this site (https://www.gurobi.com/documentation/9.1/refman/inputfile.html) which explains the usage of 'InputFile', it seems that ORD file format is not included.

    Please find below the code which was appended to the original code.

    Thank you

    Yours sincerely,

    Mike

            for key, option in self.options.items():
    # When options come from the pyomo command, all
    # values are string types, so we try to cast
    # them to a numeric value in the event that
    # setting the parameter fails.
    try:
    self._solver_model.setParam(key, option)
    except TypeError:
    # we place the exception handling for
    # checking the cast of option to a float in
    # another function so that we can simply
    # call raise here instead of except
    # TypeError as e / raise e, because the
    # latter does not preserve the Gurobi stack
    # trace
    if not _is_numeric(option):
    raise
    self._solver_model.setParam(key, float(option))
    except TypeError:
    if key == "InputFile":
    self._solver_model.read(option)
    continue
    0
  • Che Han Lim
    Gurobi-versary
    Conversationalist
    First Question

    Dear Richard, 

    Please find below the full code with comments on top, alongside the appended code at the bottom. It seems that there is no mention of support for the 'InputFile' Option.

    Thank you.

    Yours sincerely,

    Mike 

     # Options accepted by gurobi (case insensitive):
    # ['Cutoff', 'IterationLimit', 'NodeLimit', 'SolutionLimit', 'TimeLimit',
    # 'FeasibilityTol', 'IntFeasTol', 'MarkowitzTol', 'MIPGap', 'MIPGapAbs',
    # 'OptimalityTol', 'PSDTol', 'Method', 'PerturbValue', 'ObjScale', 'ScaleFlag',
    # 'SimplexPricing', 'Quad', 'NormAdjust', 'BarIterLimit', 'BarConvTol',
    # 'BarCorrectors', 'BarOrder', 'Crossover', 'CrossoverBasis', 'BranchDir',
    # 'Heuristics', 'MinRelNodes', 'MIPFocus', 'NodefileStart', 'NodefileDir',
    # 'NodeMethod', 'PumpPasses', 'RINS', 'SolutionNumber', 'SubMIPNodes', 'Symmetry',
    # 'VarBranch', 'Cuts', 'CutPasses', 'CliqueCuts', 'CoverCuts', 'CutAggPasses',
    # 'FlowCoverCuts', 'FlowPathCuts', 'GomoryPasses', 'GUBCoverCuts', 'ImpliedCuts',
    # 'MIPSepCuts', 'MIRCuts', 'NetworkCuts', 'SubMIPCuts', 'ZeroHalfCuts', 'ModKCuts',
    # 'Aggregate', 'AggFill', 'PreDual', 'DisplayInterval', 'IISMethod', 'InfUnbdInfo',
    # 'LogFile', 'PreCrush', 'PreDepRow', 'PreMIQPMethod', 'PrePasses', 'Presolve',
    # 'ResultFile', 'ImproveStartTime', 'ImproveStartGap', 'Threads', 'Dummy', 'OutputFlag']
    for key, option in self.options.items():
    # When options come from the pyomo command, all
    # values are string types, so we try to cast
    # them to a numeric value in the event that
    # setting the parameter fails.
    try:
    self._solver_model.setParam(key, option)
    except TypeError:
    # we place the exception handling for
    # checking the cast of option to a float in
    # another function so that we can simply
    # call raise here instead of except
    # TypeError as e / raise e, because the
    # latter does not preserve the Gurobi stack
    # trace
    if not _is_numeric(option):
    raise
    self._solver_model.setParam(key, float(option))
    except TypeError:
    if key == "InputFile":
    #self._solver_model.read(option)
    self._solver_model.setParam(key, option)
    continue
    0
  • Che Han Lim
    Gurobi-versary
    Conversationalist
    First Question

    Dear Richard,

    The appended code should be as follows:

    Thank you.

    Yours sincerely,

    Mike

    except TypeError:
    if key == "InputFile":
    self._solver_model.read(option)
    continue
    0
  • Gwyneth Butera
    Gurobi Staff Gurobi Staff

    Continued here : Setting up Branching Priority for GUROBI in Pyomo

    0

Please sign in to leave a comment.