< GLPK

This page describes issues related to the use of GLPK as a callable library. As such this page should be primarily of interest to developers writing application programs in C or C++ and making use of the GLPK application programming interface (API).

Official documentation

The official GLPK documentation file doc/glpk.pdf contains a full reference for the GLPK API. That material is therefore not repeated here. See obtaining GLPK.

Short example

The following API example implements the linear constrained optimization model:

maximize
subject to
/* short.c */

#include <stdio.h>            /* C input/output                       */
#include <stdlib.h>           /* C standard library                   */
#include <glpk.h>             /* GNU GLPK linear/mixed integer solver */

int main(void)
{
  /* declare variables */
  glp_prob *lp;
  int ia[1+1000], ja[1+1000];
  double ar[1+1000], z, x1, x2;
  /* create problem */
  lp = glp_create_prob();
  glp_set_prob_name(lp, "short");
  glp_set_obj_dir(lp, GLP_MAX);
  /* fill problem */
  glp_add_rows(lp, 2);
  glp_set_row_name(lp, 1, "p");
  glp_set_row_bnds(lp, 1, GLP_UP, 0.0, 1.0);
  glp_set_row_name(lp, 2, "q");
  glp_set_row_bnds(lp, 2, GLP_UP, 0.0, 2.0);
  glp_add_cols(lp, 2);
  glp_set_col_name(lp, 1, "x1");
  glp_set_col_bnds(lp, 1, GLP_LO, 0.0, 0.0);
  glp_set_obj_coef(lp, 1, 0.6);
  glp_set_col_name(lp, 2, "x2");
  glp_set_col_bnds(lp, 2, GLP_LO, 0.0, 0.0);
  glp_set_obj_coef(lp, 2, 0.5);
  ia[1] = 1, ja[1] = 1, ar[1] = 1.0; /* a[1,1] = 1 */
  ia[2] = 1, ja[2] = 2, ar[2] = 2.0; /* a[1,2] = 2 */
  ia[3] = 2, ja[3] = 1, ar[3] = 3.0; /* a[2,1] = 3 */
  ia[4] = 2, ja[4] = 2, ar[4] = 1.0; /* a[2,2] = 1 */
  glp_load_matrix(lp, 4, ia, ja, ar);
  /* solve problem */
  glp_simplex(lp, NULL);
  /* recover and display results */
  z = glp_get_obj_val(lp);
  x1 = glp_get_col_prim(lp, 1);
  x2 = glp_get_col_prim(lp, 2);
  printf("z = %g; x1 = %g; x2 = %g\n", z, x1, x2);
  /* housekeeping */
  glp_delete_prob(lp);
  glp_free_env();
  return 0;
}

Production code would, of course, check the return value from function glp_simplex and react accordingly.

Linux

These instructions relate to Linux (the details for other operating systems should be similar).

Build short using your system compiler, in this case GNU GCC:

$ gcc -Wall short.c -lglpk -o short

Run the resulting binary:

$ ./short 
*     0: obj =   0.000000000e+00  infeas =  0.000e+00 (0)
*     2: obj =   4.600000000e-01  infeas =  0.000e+00 (0)
OPTIMAL SOLUTION FOUND
z = 0.46; x1 = 0.6; x2 = 0.2

You should obtain and as part of the terminal output from your program.

The GLPK runtime reporting format (the lines starting *) are covered in the callable library part of the official GLPK documentation and also here.

The same problem is coded in MathProg.

Submitting patches

GLPK patches are best submitted by posting them to the most appropriate of the two GLPK mailing lists. Note that the GLPK project does not maintain a web-based source code repository.

If creating source file patches, please refrain from using tabs and restrict line lengths to 72 chars.

Thread safety

GLPK is not inherently thread safe. Please consult the GLPK newsgroup archives for more information about the use of GLPK APIs under concurrent execution. In particular, see this discussion and, more latterly, this discussion.

If your problem involves multiple LPs (or can be structured thus), then consider running each LP as a separate processes. This makes sense only for multi-core hardware.

Reenterability

Andrew said that reenterability is a property of the code that allows running several instances of the same program in parallel using the only copy of that program in the memory. This property requires the program not to alter its own code and statically allocated data. See also Wikipedia definition of reentrancy.

The glpk code is reenterable except two internal routines tls_set_ptr and tls_get_ptr (see src/env/tls.c). In a reenterable version of glpk these two routines should be replaced with platform-specific ones.

The current version of tls.c is not reentrant:

static void *tls = NULL;

void tls_set_ptr(void *ptr)
{     tls = ptr;
      return;
}

void *tls_get_ptr(void)
{     void *ptr;
      ptr = tls;
      return ptr;
}

Andrew suggested reentrant version:

#include <pthread.h>
pthread_key_t _glp_pth_key;
/* must be initialized in the main program */

void tls_set_ptr(void *ptr)
{     pthread_setspecific(_glp_pth_key, ptr);
      return;
}

void *tls_get_ptr(void)
{     void *ptr;
      ptr = pthread_getspecific(_glp_pth_key);
      return ptr;
}

Latest GCC compilers have storage class keyword __thread :

static __thread void *tls = NULL;

void tls_set_ptr(void *ptr)
{     tls = ptr;
}

void *tls_get_ptr(void)
{     return tls;
}

Visual Studio 2005 compiler has similar storage-class modifier __declspec( thread ):

static __declspec( thread ) void *tls = NULL;

void tls_set_ptr(void *ptr)
{     tls = ptr;
}

void *tls_get_ptr(void)
{     return tls;
}

This commit makes the library re-rentrant on Windows and Linux.

Deprecated GLPK APIs

A number of GLPK APIs have been recently deprecated and replaced. Unless otherwise stated, the lpx_ prefix has been replaced with a glp_ prefix. In all cases, developers should double check with the official documentation.

As of version 4.49, developers must migrate to these new APIs. The old API routines, whose names begin with lpx_, have now been removed from the GLPK API and are no longer available.

GLPKDeprecatedComment
4.16lpx_add_cols
lpx_add_rows
lpx_create_index
lpx_create_prob
lpx_del_cols
lpx_del_rows
lpx_delete_index
lpx_delete_prob
lpx_find_col
lpx_find_row
lpx_get_col_dual
lpx_get_col_kind
lpx_get_col_lb
lpx_get_col_name
lpx_get_col_prim
lpx_get_col_stat
lpx_get_col_type
lpx_get_col_ub
lpx_get_mat_col
lpx_get_mat_row
lpx_get_num_bin
lpx_get_num_cols
lpx_get_num_int
lpx_get_num_nz
lpx_get_num_rows
lpx_get_obj_coef
lpx_get_obj_dir
lpx_get_obj_name
lpx_get_obj_val
lpx_get_prob_name
lpx_get_row_dual
lpx_get_row_lb
lpx_get_row_name
lpx_get_row_prim
lpx_get_row_stat
lpx_get_row_type
lpx_get_row_ub
lpx_ipt_col_dual
lpx_ipt_col_prim
lpx_ipt_obj_val
lpx_ipt_row_dual
lpx_ipt_row_prim
lpx_load_matrix
lpx_mip_col_val
lpx_mip_obj_val
lpx_mip_row_val
lpx_set_col_bnds
lpx_set_col_kind
lpx_set_col_name
lpx_set_col_stat
lpx_set_mat_col
lpx_set_mat_row
lpx_set_obj_coef
lpx_set_obj_dir
lpx_set_obj_name
lpx_set_prob_name
lpx_set_row_bnds
lpx_set_row_name
lpx_set_row_stat
4.18lpx_simplex
4.20lpx_integersee: glp_iotopt
4.29lpx_read_mps
lpx_read_freemps
lpx_write_mps
lpx_write_freemps
lpx_read_cpxlp
lpx_write_cpxlp
4.31lpx_scale_prob
lpx_std_basis
lpx_adv_basis
lpx_cpx_basis
4.32lpx_integer
lpx_intopt
4.33lpx_exact
lpx_get_ray_infosee: glp_unbnd_ray
lpx_interior
lpx_read_model
4.37lpx_print_sol
lpx_print_ipssee: lpx_print_ipt
lpx_print_mip
lpx_print_probsee: glp_print_lp
4.41lpx_transform_row
lpx_transform_col
lpx_prim_ratio_test
lpx_dual_ratio_test
4.42lpx_print_sens_bndssee: glp_print_ranges
4.49lpx_check_kktsee: glp_check_kkt

Recently added GLPK APIs

New APIs get added to GLPK from time to time. These are listed below. If appropriate, a new API may also be exposed by an associated GLPSOL command-line option in the same or a subsequent release.

Note: this information only dates back as far as release 4.31 in February 2007.

GLPKAddedComment
4.49glp_check_kktreplaces lpx_check_kkt
glp_mincost_relax4solve minimum cost flow problem using RELAX-IV code
4.47glp_intfeas1solve CNF-SAT problem instance with GLPK code (tentative)
4.46glp_read_cnfsatread CNF-SAT problem data in DIMACS format
glp_write_cnfsatwrite CNF-SAT problem data in DIMACS format
glp_check_cnfsatcheck CNF-SAT problem instance
glp_minisat1solve CNF-SAT problem instance with MiniSat
4.44glp_cppsolve critical path problems
4.42glp_check_dupcheck for duplicate elements in sparse matrix
glp_sort_matrixsort elements of the constraint matrix
glp_read_probread problem data in GLPK format
glp_write_probwrite problem data in GLPK format
glp_analyze_boundanalyze active bound of non-basic variable
glp_analyze_coefanalyze objective coefficient at basic variable
glp_print_rangesprint sensitivity analysis report
4.41glp_transform_rowtransform explicitly specified row
glp_transform_coltransform explicitly specified column
glp_prim_rtestperform primal ratio test
glp_dual_rtestperform dual ratio test
4.40glp_del_verticesremove vertices from graph
glp_del_arcremove arc from graph
glp_wclique_exactfind maximum weight clique with the exact algorithm developed by Prof P Ostergard
glp_read_ccdataread graph in DIMACS clique/coloring format
glp_write_ccdatawrite graph in DIMACS clique/coloring format
4.39glp_warm_up"warm up" LP basis
glp_set_vertex_nameassign (change) vertex name
glp_create_v_indexcreate vertex name index
glp_find_vertexfind vertex by its name
glp_delete_v_indexdelete vertex name index
glp_read_asnprobread assignment problem data in DIMACS format
glp_write_asnprobwrite assignment problem data in DIMACS format
glp_check_asnprobcheck correctness of assignment problem data
glp_asnprob_lpconvert assignment problem to LP
glp_asnprob_okalgsolve assignment problem with the out-of-kilter algorithm
glp_asnprob_hallfind bipartite matching of maximum cardinality with Hall's algorithm
4.37glp_print_solwrite basic solution in printable format
glp_print_iptwrite interior-point solution in printable format
glp_print_mipwrite MIP solution in printable format
glp_read_graphread (di)graph from plain text file
glp_write_graphwrite (di)graph to plain text file
glp_weak_compfind all weakly connected components
glp_strong_compfind all strongly connected components
4.36glp_mincost_okalgfind minimum-cost flow with out-of-kilter algorithm
glp_maxflow_ffalgfind maximal flow with Ford-Fulkerson algorithm
4.35glp_create_graphcreate graph
glp_set_graph_nameassign (change) graph name
glp_add_verticesadd new vertices to graph
glp_add_arcadd new arc to graph
glp_erase_grapherase graph content
glp_delete_graphdelete graph
glp_read_mincostread minimum cost flow problem data in DIMACS format
glp_write_mincostwrite minimum cost flow problem data in DIMACS format
glp_mincost_lpconvert minimum cost flow problem to LP
glp_netgenKlingman's network problem generator
glp_gridgengrid-like network problem generator
glp_read_maxflowread maximum flow problem data in DIMACS format
glp_write_maxflowwrite maximum flow problem data in DIMACS format
glp_maxflow_lpconvert maximum flow problem to LP
glp_rmfgenGoldfarb's maximum flow problem generator
4.33glp_copy_probcopy problem object content
glp_exactsolve LP in exact arithmetic (makes lpx_exact deprecated)
glp_get_unbnd_raydetermine variable causing unboundedness (makes lpx_get_ray_info deprecated)
glp_interiorsolve LP with interior-point method (makes lpx_interior deprecated)
4.32glp_ios_row_attrdetermine additional row attributes
glp_ios_pool_sizedetermine current size of the cut pool
glp_ios_add_rowadd constraint to the cut pool
glp_ios_del_rowdelete constraint from the cut pool
glp_ios_clear_pooldelete all constraints from the cut pool
4.31glp_scale_probautomatic scaling of problem data
glp_std_basisconstruct standard initial LP basis
glp_adv_basisconstruct advanced initial LP basis
glp_cpx_basisconstruct Bixby's initial LP basis
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.