# JuMP solver callback arguments

**URL:** <https://discourse.julialang.org/t/jump-solver-callback-arguments/13936>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [August 23, 2018, 12:49pm UTC](https://discourse.julialang.org/t/jump-solver-callback-arguments/13936 "2018-08-23T12:49:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zacharie\_ALES](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zacharie_ales/32/11144_2.png) [@Zacharie\_ALES](https://discourse.julialang.org/u/Zacharie_ALES)\
**Post date:** [August 23, 2018, 12:49pm UTC](https://discourse.julialang.org/t/jump-solver-callback-arguments/13936/1 "2018-08-23T12:49:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [August 23, 2018, 10:13pm UTC](https://discourse.julialang.org/t/jump-solver-callback-arguments/13936/2 "2018-08-23T22:13:44Z")

</div>

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:

> [@Cutting plane algorithm performance varies with Gurobi and CPLEX](https://discourse.julialang.org/t/cutting-plane-algorithm-performance-varies-with-gurobi-and-cplex/8597/2):
>
> Callbacks are tied in very deeply to how a MIP solver works, and they’re just not the same across solvers. (This took some experience to discover.) In JuMP 0.19 we will no longer be providing solver-independent callbacks for this reason; they create a false expectation that you can abstract away the solver when you implement the callbacks. The replacement will be solver-specific callbacks that expose the same interface as the low-level API.

You can also search old discourse posts for callback related questions. [Search results for 'jump callback' - Julia Programming Language](https://discourse.julialang.org/search?q=jump%20callback)

---

<div class="post-metadata">

**Author:** ![Zacharie\_ALES](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zacharie_ales/32/11144_2.png) [@Zacharie\_ALES](https://discourse.julialang.org/u/Zacharie_ALES)\
**Post date:** [August 24, 2018, 1:50pm UTC](https://discourse.julialang.org/t/jump-solver-callback-arguments/13936/3 "2018-08-24T13:50:54Z")

</div>

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:

```julia
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

```

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [August 24, 2018, 11:24pm UTC](https://discourse.julialang.org/t/jump-solver-callback-arguments/13936/4 "2018-08-24T23:24:06Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![Zacharie\_ALES](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zacharie_ales/32/11144_2.png) [@Zacharie\_ALES](https://discourse.julialang.org/u/Zacharie_ALES)\
**Post date:** [August 27, 2018, 9:02am UTC](https://discourse.julialang.org/t/jump-solver-callback-arguments/13936/5 "2018-08-27T09:02:06Z")

</div>

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