Skip to main content

Wrapper for Gurobi var?

Answered

Comments

2 comments

  • Riley Clement
    • Gurobi Staff Gurobi Staff

    Hi Bruno,

    An alternative to extension through inheritance, is composition.  You can also make use of automatic delegation.

    However trying to use either approach, to substitute an object from a 3rd party API (such as gurobipy) is very risky as there may be type checking under the hood, and cause unexpected behavior that you do not realize.

    Would it be possible in your case to just define a method to switch between the solution values and monkey patch it on (for aesthetics)?  E.g.

    def set_BD_solution(var, x):
      var._BD_solution = x

    def get_BD_solution(var):
      return var._BD_solution

    def switch_X(var, BD=False):
        if BD:
          return get_BD_solution(var)
      return var.X

    # monkey patch if required
    gp.Var._X = switch_X
    gp.Var._set_BD_solution = set_BD_solution
    gp.Var._get_BD_solution = get_BD_solution

    # define variables after monkeypatching

    # for a variable "var" access like so
    var._X()  # optimal solution
    var._X(BD=True)  # Benders solution


    - Riley

    1
  • Bruno Colonetti
    • Gurobi-versary
    • First Question
    • First Comment

    Hey, Riley

    Thanks for the tip! At the end, I chose to follow your advice and it's working just fine

    Thanks!

    0

Please sign in to leave a comment.