GLPK/API usage

This page provides examples of API usage for commonly encountered tasks.

Prescribed starts
A solution can be read from file using the following API routines:
 * glp_read_sol - read basic solution
 * glp_read_ipt - read interior point solution
 * glp_read_mip - read MIP solution

The solution read with glp_read_sol can serve as initial basis for solving a LP using the simplex algorithm (this is what glpsol command line parameter --ini does).

For mixed integer problems an integer feasible (but possibly suboptimal) starting solution can be specified using the GLPK API (a feature not available through GLPSOL).

The callback hook from the integer solver is used for this purpose. First write a custom callback routine and register this with the MIP solver using the MIP control parameter structure glp_iocp and field cb_func.

Then call glp_ios_reason from your callback routine and if <tt>GLP_IHEUR</tt> is returned, set and solve your desired starting solution as follows:


 * preset the integer variables in <tt>tree->mip</tt>
 * default initialize the parameter struct <tt>parm</tt> by calling: <tt>glp_init_smcp(&parm)</tt>
 * reset the parameter struct field thus: <tt>parm.meth = GLP_DUALP</tt>
 * solve the LP: <tt>int ret = glp_simplex(tree->mip, &parm)</tt>
 * pass all variables by calling function <tt>glp_ios_heur_sol</tt>

This early-2013 <tt>[Help-glpk]</tt> thread here and here contains relevant information and the following code snippet (while noting that the use of  is not good style):

where solution is a <tt>const double</tt> array which contains the primal values of all the variables in locations <tt>x[1],...,x[n]</tt> where n is the number of columns in the original problem object.

Karush-Kuhn-Tucker optimality conditions (obsolete)
Please note the call <tt>lpx_check_ktt</tt> was upgraded to <tt>glp_check_ktt</tt> with version 4.49. The struct <tt>LPXKKT</tt> is no longer used.

GLPK provides the <tt>lpx_check_ktt</tt> routine to check the KKT optimality conditions for a basic solution. The function signature is:

The parameter <tt>scaled</tt> can be set to 0 to check the original unscaled problem or non-zero to check the internally scaled problem. A typical call might be:

This call fills the struct <tt>kkt</tt>, which can then be interrogated.