JuMP solver callback arguments

Hello,

I use CPLEX and a callback to solve several instances of a problem.
In the callback I need to access variables (for example it could be the size of the problem or any additional information about the instance currently solved, …).

Unfortunately, the callback function only takes one argument which is “a reference to the callback management code inside JuMP” according to the documentation.

Currently I am using global variables which is not great. I can’t use constants as the value of these variables need to change for each instance considered.

Is there a better way to access these information inside of the callback? (I hope so)

For example, is it possible to give additional information directly to the “callback management code”? In that case I would be able to get what I want through the callback argument.

Thanks in advance.

Hi @Zacharie_ALES,

Can you provide a small working example of your code and what you are trying to achieve? It is much easier to offer specific advice than answer general questions.

You should also note that solver independent callbacks are going away in JuMP 0.19. Instead, JuMP will enable solver-specific callbacks that allow a much lower-level of control over the callback process. See:

You can also search old discourse posts for callback related questions. Search results for 'jump callback' - JuliaLang

Thank you for your answer. It is good to know that more efficient callbacks are on their way.

I am still interested to know if the way I currently manage variables in the callback is the best I can do.
Here is a simplified example:

using JuMP
using CPLEX

# Definition of the callback
function myCallback()

    # Do something using the current value of variables n and w
    # ...
    
end 

# Example of instance file:
# n = 3
#
# d = [1 2 10;
# 1 3 20;
# 2 3 30]
#
# w = [1 2 100;
# 1 3 50;
# 2 3 20] 
instancePath = ["./instance1.txt", "./instance2.txt", "./instance3.txt"]

for instance in instancePath

    include(instance)

    model = Model(solver = CplexSolver())
    
    # Create the model using variables n and d
    # ...

    addlazycallback(model, myCallback)

    solve(model)

    # Do something with the results
    # ...

end

You should capture the u and w variables in a closure like the following:

function run_instance(instance)
    n, d, u, w = process_data(instance)
    model = Model(solver = CplexSolver())
    function my_callback(cb_data)
        # you can use u and w here
    end
    addlazycallback(model, my_callback)
    solve(model)
    # process results
end

for instance in instance_path
    run_instance(instance)
end

Perfect thanks. I did not think about nesting the callback in another function.