How to give information into a callback function


#1

Hi everyone.

I’m implementing a callback function for lazy constraints (Benders decomposition). I’m not sure if it is possible to give and recover information to the callback. The idea is to pass a global variable which can change its value according to some criteria into the function. So far, It seems like the cb function is not recognizing global variables, declared previously, when I try to use it inside (i.e. the interesting one). I got “ERROR: UndefRefError: access to undefined reference”. Furthermore, when the variable’s value is modified, I don’t know how to recover it. The value of that variable is important at each time when cb function is called.

Is there any way to do that?

Thanks in advance!


#2

Hi @CDRPico, please post a minimum working example that demonstrates what you are trying to do and the error you are getting.

You may also be interested in this Benders decomponsition example.


#3

If the callback needs to have a state, maybe make it a callable struct? Silly Example:

mutable struct Foo
    a::int;
end

function (foo::Foo)(x)
    r = x+foo.a
    foo.a = x
    return r
end

Running it:

# constructing the "callback"
julia> f=Foo(1)
Foo(1)

# calling it
julia> f(3)
4

# its state has changed
julia> f
Foo(3)