Example: using callbacks lpex4.c
Illustrates callbacks in the C API.
This example shows you how to use callbacks effectively with routines
from the Callable Library. It is based on lpex1.c
,
a program from the manual Getting Started. This example about
callbacks differs from that simpler one in several ways:
To make the output more interesting, this example optimizes a slightly different linear program.
- The CPLEX screen indicator (that is, the messages to screen switch
CPX_PARAM_SCRIND
) is not turned on. Only the callback function produces output. Consequently, this program callsCPXgeterrorstring
to retrieve any error messages and then prints them. After theTERMINATE:
label, the program uses separate status variables so that if an error occurred earlier, its error status will not be lost or destroyed by freeing the problem object and closing the CPLEX environment. Table 1 summarizes those status variables.Table 1. Status variables in lpex4.c
Variable Represents status returned by this routine frstatus
CPXfreeprob
clstatus
CPXcloseCPLEX
The function
mycallback
at the end of the program is called by the optimizer. This function tests whether the primal simplex optimizer has been called. If so, then a call toCPXgetcallbackinfo
gets the following information:iteration count;
feasibility indicator;
sum of infeasibilities (if infeasible);
objective value (if feasible).
The function then prints these values to indicate progress.
Before the program calls
CPXlpopt
, the default optimizer from the Callable Library, it sets the callback function by callingCPXsetlpcallbackfunc
. It unsets the callback immediately after optimization.
This callback function offers a model for graphic user interfaces
that display optimization progress as well as those GUIs that allow
a user to interrupt and stop optimization. If you want to provide
your end-user a facility like that to interrupt and stop optimization,
then you should make mycallback
return a nonzero
value to indicate the end-user interrupt.
The complete program lpex4.c
appears online in
the standard distribution at yourCPLEXinstallation /examples/src
.