Skip to main content

what does the summary information in logfile for callback means

Answered

Comments

5 comments

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi Amir,

    The number of calls tells you how many times your callback function has been called during the optimization. The reported time is the summed time spent over all calls of your callback function.

    For example, you have a callback function \(\texttt{myCallback}\) which you pass to Gurobi via

    m.optimize(myCallback)

    The mentioned LOG line then tells you that your function \(\texttt{myCallback}\) has been called 62 times and Gurobi spent 2.76s in it during the whole optimization process. If you are interested in the number of calls to a particular part of your callback function, you can define a private counter variable

    model._counter = 0

    and increase it in your callback function

    def myCallback(model,where):
    # some code
    model._counter += 1

    Best regards,
    Jaromił

    0
  • Amir Ghiasi Noughaby
    Gurobi-versary
    First Comment
    Curious

    Thanks for your response 

    I have added this model._counter to the callback, and when the model is solved, I print the value of it. However, it is not as same as the LOG value. Why it could happen?

     

     

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi Amir,

    Gurobi does not count every call, e.g., the POLLING callbacks are not counted. Thus, it is best to work with your own counter to determine the exact number of calls to your callback function.

    Best regards,
    Jaromił

    0
  • Jacob Jin
    Gurobi-versary
    Curious
    Collaborator

    Hi,I want to know to define a private variable with Gurobi in Julia, just like:

    model._counter = 0
    0
  • Simon Bowly
    Gurobi Staff Gurobi Staff

    Hi Jacob,

    I assume you are using JuMP and are looking for a way to track some information across callbacks as we do in some of the Python example codes? Julia does not support dynamic attribute assignment the way Python does, so you'll need to define this variable in an enclosing scope. Here is a short example:

    using JuMP, Gurobi

    function solve()

        # build your model
        model = Model(Gurobi.Optimizer)
        @variable(model, 0 <= x <= 10, Int)
        @variable(model, 0 <= y <= 10, Int)
        @constraint(model, x + y <= 5)
        @objective(model, Max, x)

        # create 'counter' in the outer scope so that the
        # callback can access it
        counter = 0
        function callback(cb_data)
            counter += 1
            println("Lazy constraint callback")
        end
        MOI.set(model, MOI.LazyConstraintCallback(), callback)

        # solve
        optimize!(model)
        println("Calls: ", counter)
    end

    solve()

    There is some further explanation given in this forum post (although, be aware that this is a few years old, so the JuMP API has changed) https://discourse.julialang.org/t/jump-solver-callback-arguments/13936/3

    Is this what you are after?

    Best regards,
    Simon

    0

Please sign in to leave a comment.