> Advanced Programming Techniques > Using Callbacks > Implementing Callbacks in ILOG CPLEX with Concert Technology > Writing Callbacks with Macros

This is how to implement a callback using macros. Since macros are not supported in Java, this technique will only apply to C++ applications.

Start by determining which callback you want to implement and how many arguments to pass to the callback function. These two pieces of information determine the macro you need to use.

For example, to implement a simplex callback with one parameter, the macro is ILOSIMPLEXCALLBACK1. Generally, for every callback type XXX and any number of parameters n from 0 to 7, there is a macro called ILOXXXCALLBACKn. Table 22.1 lists the callbacks and the corresponding macros and classes (where n is a placeholder for 0 to 7).

Table 22.1 Callback Macros
Callback 
Macro 
Class 
presolve 
ILOPRESOLVECALLBACKn 
IloCplex::PresolveCallbackI 
continuous 
ILOCONTINUOUSCALLBACKn 
IloCplex::ContinuousCallbackI 
simplex 
ILOSIMPLEXCALLBACKn 
IloCplex::SimplexCallbackI 
barrier 
ILOBARRIERCALLBACKn 
IloCplex::BarrierCallbackI 
crossover 
ILOCROSSOVERCALLBACKn 
IloCplex::CrossoverCallbackI 
network 
ILONETWORKCALLBACKn 
IloCplex::NetworkCallbackI 
MIP 
ILOMIPCALLBACKn 
IloCplex::MIPCallbackI 
probing 
ILOPROBINGCALLBACKn 
IloCplex::ProbingCallbackI 
fractional cut  
ILOFRACTIONALCUTCALLBACKn 
IloCplex::FractionalCutCallbackI 
disjunctive cut 
ILODISJUNCTIVECUTCALLBACKn 
IloCplex::DisjunctiveCutCallbackI 

The protected methods of the corresponding class and its base classes determine the functions that can be called for implementing your callback. See the ILOG CPLEX Reference Manual.

Here is an example of how to implement a simplex callback with the name MyCallback that takes one parameter:

ILOSIMPLEXCALLBACK1(MyCallback, IloInt, num) {
if ( getNiterations() == num ) abort();
}

This callback aborts the simplex algorithm at the iteration indicated by the number num. It queries the current iteration number by calling the function getNiterations, a protected method of the class IloCplex::ContinuousCallbackI.

To use this callback with an IloCplex object cplex, simply call:

IloCplex::Callback mycallback = cplex.use(MyCallback(env, 10));

The callback that is added to cplex is returned by the method use and stored in variable mycallback. This allows you to call mycallback.endto remove the callback from cplex. If you do not intend accessing your callback, for example in order to delete it before ending the environment, you may safely leave out the declaration and initialization of variable mycallback.