ILOG CPLEX 11.0 User's Manual > Advanced Programming Techniques > Using Optimization 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 nor in .NET, this technique will only apply to C++ applications.

Start by deciding 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 argument, the macro is ILOSIMPLEXCALLBACK1. Generally, for every callback type XXX and any number of arguments n from 0 to 7, there is a macro called ILOXXXCALLBACKn. Table 30.2 lists the callbacks and the corresponding macros and classes (where n is a placeholder for 0 to 7).

Table 30.2 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 info 
ILOMIPINFOCALLBACKn 
IloCplex::MIPInfoCallbackI 
MIP 
ILOMIPCALLBACKn 
IloCplex::MIPCallbackI 
probing info 
ILOPROBINGINFOCALLBACKn 
IloCplex::ProbingCallbackI 
probing 
ILOPROBINGCALLBACKn 
IloCplex::ProbingCallbackI 
fractional cut info 
ILOFRACTIONALCUTINFOCALLBACKn 
IloCplex::FractionalCutCallbackI 
fractional cut  
ILOFRACTIONALCUTCALLBACKn 
IloCplex::FractionalCutCallbackI 
disjunctive cut info 
ILODISJUNCTIVECUTINFOCALLBACKn 
IloCplex::DisjunctiveCutCallbackI 
disjunctive cut 
ILODISJUNCTIVECUTCALLBACKn 
IloCplex::DisjunctiveCutCallbackI 
flow MIR cut info 
ILOFLOMIRCUTINFOCALLBACKn 
IloCplex::FlowMIRCutInfoCallbackI 
flow MIR cut 
ILOFLOMIRCUTCALLBACKn 
IloCplex::FlowMIRCutCallbackI 

The protected methods of the corresponding class and its base classes specify the functions that can be called for implementing your callback. See the ILOG CPLEX Reference Manual with respect to these classes for details of which functions can be called.

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

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 the variable mycallback. This allows you to call mycallback.endto remove the callback from cplex. If you do not intend to access your callback (for example, in order to delete it before ending the environment), you may safely leave out the declaration and initialization of the variable mycallback.