Skip to main content

Can I print values of integer variable during optimization?

Answered

Comments

2 comments

  • Jaromił Najman
    • Gurobi Staff

    You can use callbacks.

    In particular, you can use the MIPSOL callback to get the latest found incumbent via the cbGetSolution method.

    Let's assume that you are interested in the value of a particular variable \(x_{\text{important}}\).

    def mycallback(model, where):
        if where == GRB.Callback.MIPSOL:
          print(model.cbGetSolution(model._importantvars))

    # [...]

    model = gp.Model()

    x_important = model.addVar(...)

    # [...]

    model._importantvars = [x_important] # you can also add more variables of interest to the list
    model.optimize(mycallback)

    Best regards, 
    Jaromił

    0
  • Rahul Mitra
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you! This was really helpful! 

    Rahul

    0

Please sign in to leave a comment.