What is a context for a generic callback?

Generic callbacks are invoked within a context.

As a multi-purpose function, a generic callback can be invoked in many different places during the search for a solution. The context in which CPLEX invokes a generic callback determines what you can reasonably do from that generic callback.

So how do you specify the context? Initially, the argument contextmask in the routine CPXXcallbacksetfunc and CPXcallbacksetfunc that declares the function of the generic callback is a bit-mask. This bit-mask is the bit-wise OR of possibly multiple contexts. Further, there is the argument contextid passed to the callback function itself when CPLEX invokes the callback. This contextid is a single context value and indicates in which context this particular invocation occurs.

For a detailed explanation of the various contexts in which the generic callback can be invoked, see the documentation of the following constants in the reference manual of the CPLEX Callable Library (C API): (These are the constants that can be combined by means of a bit-wise OR in the value of the contextmask argument passed to CPXXcallbacksetfunc and CPXcallbacksetfunc.)

If you specify an appropriate context, the generic callback can monitor progress of the solution process: CPX_CALLBACKCONTEXT_LOCAL_PROGRESS or CPX_CALLBACKCONTEXT_GLOBAL_PROGRESS.

Also, according to the appropriate context and routine, the generic callback can control or guide the solution process. For instance, from all contexts but CPX_CALLBACKCONTEXT_THREAD_UP and CPX_CALLBACKCONTEXT_THREAD_DOWN, the user can post feasible solutions to CPLEX with CPXXcallbackpostheursoln and CPXcallbackpostheursoln, while other actions are allowed depending on the specific context. Please refer to the documentation of CPX_CALLBACKCONTEXT_CANDIDATE, CPX_CALLBACKCONTEXT_RELAXATION, and CPX_CALLBACKCONTEXT_BRANCHING, for details on all operations supported to control the solution process.

Possible errors

Not all information is available in all contexts. (For example, Benders algorithm is incompatible with the contexts CPX_CALLBACKCONTEXT_CANDIDATE, CPX_CALLBACKCONTEXT_RELAXATION, and CPX_CALLBACKCONTEXT_BRANCHING.) Consequently, routines that query information that is not available in the current context return an error. Users have to be prepared for that.