< GLPK

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 GLP_IHEUR is returned, set and solve your desired starting solution as follows:

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

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

// caution: this code is buggy and will be
// updated as soon as a solution is found

void callback(glp_tree* tree, void* info)
{
  if (glp_ios_reason(tree) == GLP_IHEUR && glp_ios_curr_node(tree) == 1)
    {
      glp_ios_heur_sol(tree, solution);
    }
}

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

Karush-Kuhn-Tucker optimality conditions (obsolete)

Please note the call lpx_check_ktt was upgraded to glp_check_ktt with version 4.49. The struct LPXKKT is no longer used.

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

void lpx_check_kkt(glp_prob *lp,    // GLPK problem object pointer 
                   int scaled,      // 0 = use unscaled, non-zero = use internally scaled
                   LPXKKT *kkt)     // LPXKKT C struct pointer (meaning address)

The parameter scaled 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:

LPXKKT kkt;                      // declaration of a suitable C struct
lpx_check_kkt(lp, 1, &kkt);      // fill call, note the address-of operator

This call fills the struct kkt, which can then be interrogated.

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.