GLPK/Print version

From Wikibooks, open books for an open world
Jump to: navigation, search


Welcome to the GNU Linear Programming Kit Wikibook!

About this Wikibook

This Wikibook covers the mainline GLPK project and a number of separate initiatives. It is designed to complement rather than duplicate the official GLPK documentation.

The GLPK project

The core GNU Linear Programming Kit (GLPK) project comprises:

The GLPK project itself is hosted at

The GLPK wikipedia entry can be found at

The GLPK project maintains two mailing lists: and To subscribe to either of these lists, visit or respectively. These lists are solely for traffic concerning the mainline GLPK project — matters relating to third-party initiatives should be directed to those initiatives.

The official GLPK documentation can be found in the doc subdirectory after unpacking the latest GLPK tarfile. To obtain this tarfile, find a suitable GNU FTP mirror, open the glpk directory, and download the latest glpk-0.00.tar.gz entry. Alternatively, follow these step-by-step instructions.

In the first instance, GLPK is distributed as source code with the expectation that users will build their own binaries (libraries and executables) — to suit their individual constellation of operating system, processor architecture, and C compiler and their choice of GLPK configuration options. Some users prefer to download pre-compiled files instead — particularly users running Windows systems or common Linux distros — such binaries are often available from the wider GLPK community or from Linux software repositories, respectively.

Andrew Makhorin, Moscow Aviation Institute, is the lead developer and maintainer of GLPK. The first public release was October 2000.

Parallel initiatives

The GLPK project has spawned a number of parallel initiatives, some of which may migrate back into the main codebase and some of which will remain distinct and different.

Solver and language development

In terms of solver and language development, initiatives include:

  • the provision of specialist reporting
  • support for new problem formats and similar solver-wide features
  • refinements to the GLPK branch-and-bound solution process for particular applications
  • extensions to the MathProg language.

Source code contributions are normally provided as patches to the maintainer. There is no web-based code hosting for GLPK at present.

Deployment and usability

In terms of deployment and usability, initiatives include:

A key reason for this Wikibook is to provide a location in which to collect and compare these often diverse initiatives.


Remember to consult the official GLPK documentation, in the first instance, for issues that relate directly to GLPK. The author of GLPK, Andrew Makhorin, works hard to keep the official documentation current and comprehensive.

Try to run the latest version of GLPK. The codebase is under continual improvement and you do yourself a disservice by running stale code. You will also receive more interest on the Help-glpk mailing list if you raise an issue that relates to the latest release.

Scalable Vector Graphics

Scalable vector graphics (SVG) is an XML file format to describe two dimensional graphics. SVG 1.1 is the most recent version of the specification.

With a little effort, the MathProg printf statement can be used to generate SVG code.


SVG output generated with GLPK

The task in the traveling salesman problem is to find the shortest cyclic path through a given set of cities, visiting each city exactly once, and then returning to the start.

The model below shows how to output the solution as a scalable vector graphic.

# This file demonstrates output to a scalable vector graphic (SVG).
# Solve with option --nomip to see the difference
# Traveling saleman problem

# output file
param filename, symbolic := "out.svg";
# number of cities
param n := 35;

# set of cities
set N := {1..n};
# set of bidirectional arcs
set E := setof{(i,j) in N cross N : i > j} (i,j);
# set of unidirectional arcs
set F := setof{(i,j) in N cross N : i != j} (i,j);

# random locations for the cities
param cx{i in N} := Uniform01();
param cy{i in N} := Uniform01();
#sum of x- and y- distance
#param d{(i,j) in E} := abs(cx[i]-cx[j])+abs(cy[i]-cy[j]);
#maximum of x- and y- distance
#param d{(i,j) in E} := max(abs(cx[i]-cx[j]),abs(cy[i]-cy[j]));
#euclidean distance
param d{(i,j) in E} := sqrt((cx[i]-cx[j])^2+(cy[i]-cy[j])^2);

# connection
var x{(i,j) in E}, >=0, <= 1, binary;
# flow
var f{(i,j) in F}, >= 0;

# Objective
minimize dist :
  sum{(i,j) in E} x[i,j] * d[i,j];

# Every city must have two connections for a roundtrip
s.t. two{ i in N } :
  sum{j in N : i > j} x[i,j] + sum{j in N : i < j} x[j,i] = 2;

# The following constraints force the graph to be connected
# Flow is controlled by binaries
s.t. flow1 {(i,j) in F} :
  f[i,j] <= (if (i==1) then n else n-1) 
          * (if (i < j) then x[j,i] else x[i,j]);

# One unit is consumed in each node
s.t. flow2 {i in N} :
  sum{(i,j) in F} (f[i,j]-f[j,i]) <= if (i==1) then n-1 else -1;

# There must be flow into every node
s.t. flow3 { i in N } :
  sum{(i,j) in F} f[j,i] >= 1;


# Output the solution as scalable vector graphic
# write header
printf "<?xml version=""1.0"" standalone=""no""?>\n" > filename;
printf "<!DOCTYPE svg PUBLIC ""-//W3C//DTD SVG 1.1//EN"" \n" >> filename;
printf """"">\n" >> filename;
printf "<svg width=""100\%"" height=""100\%"" version=""1.0"" \n" >> filename;
printf "xmlns="""">\n" >> filename;
# draw circles for cities
for {i in N}
  printf "<circle cx=""%f"" cy=""%f"" r=""5"" stroke=""black"" stroke-width=" &
         """2"" fill=""red""/>\n", cx[i] * 500, cy[i] * 500 >> filename;
# draw solid black lines for integer connections
for {(i,j) in E : x[i,j] == 1}
  printf "<line x1=""%f"" y1=""%f"" x2=""%f"" y2=""%f""" &
         " style=""stroke:black;stroke-width:2""/>\n", 
         cx[i] * 500, cy[i] * 500, cx[j] * 500, cy[j] * 500 >> filename;
# draw dashed red lines for fractional connections
for {(i,j) in E : x[i,j] > 0 && x[i,j] < 1}
  printf "<line x1=""%f"" y1=""%f"" x2=""%f"" y2=""%f""",
         cx[i] * 500, cy[i] * 500, cx[j] * 500, cy[j] * 500 >> filename;
  printf " style=""stroke:red;stroke-dasharray: 3 3;stroke-width:2""/>\n"
         >> filename;
printf "</svg>\n" >> filename;

Run the model (40 seconds on a Intel Core i5 processor):

$ glpsol --math traveling.mod

This is what the created output file out.svg looks like (some lines removed):

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
<svg width="100%" height="100%" version="1.0" 
<circle cx="14.843435" cy="64.155902" r="5" stroke="black" stroke-width="2" fill="red"/>
<circle cx="276.895963" cy="4.798338" r="5" stroke="black" stroke-width="2" fill="red"/>
<line x1="103.248022" y1="381.131207" x2="127.779724" y2="409.131953" style="stroke:black;stroke-width:2"/>
<line x1="103.248022" y1="381.131207" x2="96.365578" y2="282.627837" style="stroke:black;stroke-width:2"/>

The resulting file can be viewed in an up-to-date web browser (like Firefox 3.6 or Internet Explorer 9) or viewed and modified using a (multi-platform) SVG editor (like Inkscape).


Gnuplot is a program for generating two and three dimensional plots of functions, data, and data fits. Gnuplot is published under the GNU General Public License.

2D histogram

2D histogram generated with gnuplot from a GLPK solution

Gnuplot expects data for histograms to be in multiple columns.

The following example is based on examples/transp.mod from the GLPK source distribution:

printf '""' > 'transp1.dat';
printf {j in J} ' "%s"', j >> 'transp1.dat';
printf '\n' >> 'transp1.dat';
for {i in I} {
  printf '"%s"', i >> 'transp1.dat';
  printf {j in J} ' %f', x[i,j] >> 'transp1.dat';
  printf '\n' >> 'transp1.dat';

The above MathProg statements (inserted before the data statements of transp.mod and saved as a file named transp1.mod) will create, using the command glpsol --math transp1.mod, the following content in the file transp1.dat:

"" "New-York" "Chicago" "Topeka"
"Seattle" 50.000000 300.000000 0.000000
"San-Diego" 275.000000 0.000000 275.000000 

Using gnuplot, a histogram of transp1.dat can be drawn and then saved as a PNG image:

set terminal png truecolor transparent font "Arial, 16" size 800x600
set output "transp1.png"
set title 'Result of transp.mod'
set style data histogram
set style histogram cluster gap 1
set style fill solid border &minus;1
set boxwidth 0.9
set bmargin 5
set grid y
set xrange [&minus;0.5:2.5]
set xtics out nomirror
plot 'transp1.dat' \
  using 2:xtic(1) title columnheader(2), \
  for [i=3:4] '' using i title columnheader(i)

The above commands can either be hand entered into an interactive gnuplot session. Invoke gnuplot from the command-line to start such a session. Alternatively, the same commands can be saved in a text file and then run as a script from within gnuplot:

gnuplot> load ""

Finally, check the resulting transp1.png with any bitmap viewer, perhaps gthumb:

gthumb transp1.png &

3D histogram

3D histogram generated with gnuplot from a GLPK solution

Gnuplot does not directly support native 3D histograms. Surfaces with rectangular grids can be passed to gnuplot using the following rules:

  • provide one text line per point, with each field separated by a space
  • consecutive points of one raster line should be on consecutive text lines
  • place a blank text line between points of consecutive raster lines.

To create a 3D histogram it is necessary to provide the 4 corner points of each pillar of the histogram. The following example is again based on examples/transp.mod:


printf '' > 'transp2.dat';
for { i in I  } {
  for { j in J } {
    printf '%i "%s"', sum{k in I: k < i} 1, i >> 'transp2.dat';
    printf ' %i "%s"', sum{l in J: l < j} 1, j >> 'transp2.dat';
    printf ' %f', x[i,j] >> 'transp2.dat';
    printf '\n' >> 'transp2.dat';

    printf '%i "%s"', sum{k in I: k < i} 1, i >> 'transp2.dat';
    printf ' %i "%s"', sum{l in J: l <= j} 1, '' >> 'transp2.dat';
    printf ' %f', x[i,j] >> 'transp2.dat';
    printf '\n' >> 'transp2.dat';
    printf '\n' >> 'transp2.dat';
  for { j in J } {
    printf '%i "%s"', sum{k in I: k <= i} 1, '' >> 'transp2.dat';
    printf ' %i "%s"', sum{l in J: l < j} 1, j >> 'transp2.dat';
    printf ' %f', x[i,j] >> 'transp2.dat';
    printf '\n' >> 'transp2.dat';

    printf '%i "%s"', sum{k in I: k <= i} 1, '' >> 'transp2.dat';
    printf ' %i "%s"', sum{l in J: l <= j} 1, '' >> 'transp2.dat';
    printf ' %f', x[i,j] >> 'transp2.dat';
    printf '\n' >> 'transp2.dat';
    printf '\n' >> 'transp2.dat';

set I := San-Diego Seattle;

set J := Chicago New-York Topeka;

As before, a 3D histogram of transp2.dat can be drawn using gnuplot and saved as a PNG image:

set terminal png font "Arial, 16" transparent size 800,800
set output "transp2.png"
set title 'Result of transp.mod'
set xtic offset first 0.5, first −0.25, first 0 mirror
set ytic offset first 0.25, first 0.5, first 0 mirror
set nokey
set pm3d
set palette gray
set grid x y z
splot 'transp2.dat' using 1:3:5:xtic(2):ytic(4) with pm3d

Language Bindings

To use the GLPK library with languages other than C and C++, it is necessary to create code which can interface between the client and library languages. This code is known as a language binding.

Caution: GLPK is undergoing an overhaul of its application programming interface, with many APIs being deprecated. In parallel, several new APIs have been added. Users should therefore exercise considerable caution when evaluating or using language bindings that are not under active maintenance.

Specific languages

The following language bindings have dedicated pages:

  • Ada language bindings
  • C# language bindings
  • Fortran language bindings
  • GAMS language bindings
  • Java language bindings
  • JavaScript language bindings
  • Matlab (and Octave) language bindings.
  • OCaml language bindings
  • Octave language bindings
  • OptimJ language bindings
  • Python language bindings
  • R language bindings
  • Ruby language bindings

The following language bindings do not yet have dedicated pages (please make them if you think it appropriate):

  • Common lisp language bindings — last active 2007, read this revealing posting
  • Erlang language bindings — last active GLPK 4.38 (02 May 2009)
  • Perl language bindings — last active 2007


Java is an object-oriented application programming language.

Obsolete interfaces

GLPK 3.3 offered a Java language binding, written by Yuri Victorovich. This binding was removed from official GLPK distribution in version 4.7, because, at that time, no GNU GPL-compliant Java implementation was available. Third party projects now provide Java bindings for GLPK.

The GLPK 4.8 Java Interface was published by Björn Frank. It is no longer maintained and cannot be used with current versions of GLPK. In particular, users have reported faulty floating point arithmetic when deployed on 64-bit Linux systems.

GLPK for Java

GLPK for Java comes with a bunch of examples. demonstrates the use of callbacks and terminal output redirection.

GLPK for Java uses SWIG to generate code for its Java language binding. This binding is published under the GNU General Public License.


GLPK for Java is available through the Debian package libglpk-java, which can also be used with Ubuntu. Windows binaries are provided as part of the GLPK for Windows project.

Makefiles for Windows and POSIX-compliant systems (which includes all Linux distros) are available for manual builds on other systems. You will need to install GLPK and SWIG beforehand if you choose this option.


The GLPK for Java file doc/glpk-java.pdf contains a short description. Practical examples can be found in the examples/java directory. For the usage of the individual methods refer to doc/glpk.pdf of the GLPK source distribution.

Compiling and running

Below is a minimal Java class. Save it as file

import org.gnu.glpk.GLPK;
public class Test {
  public static void main(String[] args) {
    System.out.println( GLPK.glp_version());

Then compile this class under 64-bit Windows:

"%JAVA_HOME%\bin\javac" -classpath "C:\Program Files\GLPK\glpk-4.47\w64\glpk-java.jar"

Or compile it under Linux:

$JAVA_HOME/bin/javac -classpath /usr/local/share/java/glpk-java.jar

Run the resulting file on 64-bit Windows:

java -Djava.library.path="C:\Program Files\GLPK\glpk-4.47\w64" -classpath "C:\Program Files\GLPK\glpk-4.47\w64\glpk-java.jar";. Test

Or run the file on Linux (the file paths may need adjustment to match your installation):

java -Djava.library.path=/usr/local/lib/jni \
-classpath /usr/local/share/java/glpk-java.jar:. \

The output will be your GLPK version number, for example: 4.47.

Usage with Eclipse

Project property settings needed in Eclipse

To use GLPK for Java with Eclipse the GLPK for Java jar library has to be added to the project properties. Furthermore the path to the native DLL library has to be set here. (See screenshot).

Linear Optimization Wrapper for Java

The Linear Optimization Wrapper for Java provides a more intuitive interface to the GLPK for Java API. Columns and rows can be directly accessed via names and indices.

Columns are created like this:

Problem p = new Problem().
    setName("Cutting Stock");
// x(i,j) : x pieces of product j are cut from stock i
for (int i = 0; i < stock.length; i++) {
    for (int j = 0; j < product.length; j++) {
    p.column("x", i, j).
        bounds(0.0, null);

Rows can be created and populated like this:

// demand(j) = sum( x(i,j) )
for (int j = 0; j < product.length; j++) {
    p.row("demand", j).bounds(demand[j], demand[j]);
    for (int i = 0; i < stock.length; i++) {
        p.row("demand", j).
            add(1.0, "x", i, j);

You may download the code using subversion:

svn checkout linopt

For using this library in your Maven project enter the following repository and dependency in your pom.xml (adjust the version number as needed).

            <name>Xypron Release</name>

When testing with Maven it may be necessary to indicate the installation path of the GLPK for Java shared library (.so or .dll).

mvn clean install -DargLine='-Djava.library.path=/usr/local/lib/jni:/usr/lib/jni'

The exec:java target may require to indicate the installation path of the GLPK for Java shared library in MAVEN_OPTS, e.g.

export MAVEN_OPTS="-Djava.library.path=/usr/local/lib/jni:/usr/lib/jni"
mvn exec:java

Java ILP

Java ILP provides a java interface to several linear programming solvers including GLPK. It is licences under the GNU Lesser General Public License (LGPL). The link to GLPK uses GLPK for Java.

The homepage provides an example code. According to the homepage GLPK ≥ 4.43 is supported.


OptimJ is a Java-based modeling language and optimization environment. OptimJ is available with several commercial and non-commercial solvers to select from, including GLPK, and is offered under a variety of licensing, free (of charge) download, and purchase arrangements. OptimJ originates from Ateji, a software company based in Paris, France.

Java Native Access

Java Native Access (JNA) can be used to interface with GLPK.

See this January 2013 thread (and the surrounding posting) for information on some of the issues involved.


There are several Python language bindings to choose from. Each provides a differing level of abstraction. All are open source software.

The Scripting plus MathProg page offers further information on the use of Python and GLPK.


Python-GLPK by Rogério Reis is a Python language binding for GLPK created using SWIG and licensed under the GNU General Public License. It is also available through the Debian package python-glpk.

SWIG allows for easy maintenance as there is very little GLPK specific code present. SWIG also ensures that almost any GLPK library function is available. On the other hand, the client-side calling methods are somewhat clumsy.

The following dependencies (at least) are required for building Python-GLPK:


The following minimalistic program will show the GLPK version number:

import glpk
print glpk.glp_version()

Build and install from source

If you cannot (or choose not to) use Debian package python-glpk, you can build and install Python-GLPK from source. Root privileges are required. Here are the steps:

  • Switch to a suitable subdirectory and download the source:
  • Unpack the archive:
    tar -xzf python-glpk_0.4.43.orig.tar.gz
  • Change to the src directory:
    cd python-glpk-0.4.43/src/
  • Build and install the software in one hit (note that make install first executes make all):
    sudo make install
  • Change to the examples directory:
    cd ../examples
  • Run the confirmation test:


PyGLPK is an encapsulation of GLPK in Python objects. In contrast to Python-GLPK, the language bindings are "handcrafted", thereby enabling a smoother integration within the Python language. PyGLPK is licensed under the GNU General Public License.

PyGLPK 0.3 has been provided 30 May 2010, but is based on the GLPK 4.31 API. Testing (make; make test) fails against GLPK 4.45.


PyMathProg builds on PyGLPK. PyMathProg is also licensed under the GNU General Public License.

PyMathProg provides a domain-specific language that enables the formulation of linear problems in a form very much like GLPK MathProg (GMPL) or AMPL.


The Python Optimization Modeling Objects (Pyomo) package [1] from Sandia National Laboratories is an open source tool for modeling optimization applications in Python. Pyomo uses the GLPK solver by default, although other solvers can be selected. Pyomo is distributed under a BSD license.

GLPK is interfaced by creating a LP file and running it through GLPSOL via the command line interface and then interpreting the output files.

Strictly speaking Pyomo is not a set of low-level Python language bindings for GLPK — rather Pyomo offers high-level linear programming constructs (similar in expression to MathProg) as well as the normal features of the Python language.

Pyomo is less terse than GLPK MathProg or AMPL as it must be parsed as Python. For instance, the following MathProg statement:

maximize f: 0.6 * x1 + 0.5 * x2;

equates to, in Pyomo:

model.f = Objective( expr=0.6 * model.x + 0.5 * model.y, sense=maximize )


CVXOPT is a package for convex optimization, based on the Python language. It provides interfaces to different linear (GLPK, Mosek) and quadratic (Mosek) programming solvers. CVXOPT is being developed by Joachim Dahl and Lieven Vandenberghe.


Sage is general mathematical software based on Python. While Sage is strictly more than Python, it is nonetheless listed on this page.

The user can elect to link to GLPK, COIN Branch-and-Cut, and CPLEX (as of November 2010, with SCIP support planned). But GLPK remains the default solver for reasons of licensing. Sage can be used for both mixed integer programming and for graph theory problems.


PuLP is an LP modeling module for Python. It generates MPS or LP files and submits these to GLPK, COIN CLP/CBC, CPLEX, or XPRESS via the command-line. An MIT license is used. For GLPK, PuLP writes the problem to a CPLEX LP file and then executes a command like the following in a new process:

glpsol --cpxlp %d-pulp.lp --o %d-pulp.sol [--nomip]

On completion, the solution file in analyzed. Both intermediate files are deleted.

PuLP also has a direct python binding to GLPK (the example above spawns a new process and issues a system command). As of August 2012, this feature was implemented with PyGLPK bindings, but the next version should make use of Python-GLPK bindings (the code has been written and is being evaluated).


The Yet Another Python OSI Binding or yabosib project provides OSI bindings — in other words, yaposib wraps the OSI API in python classes. It is slated for official inclusion in COIN-OR suite. yaposib is also designed to work within PuLP.


A Cython GLPK interface

While using an object-oriented style, these bindings stay relatively close to the GLPK C API. For example, most GLPK function names are essentially retained (dropping the glp_ prefix) and a few similar ones have been added. One of the added functionalities is that row and column names can be used as well as integer indices in most functions.

Some absurdly simple code to give a feel for the bindings:

# set up the problem
lp = ecyglpki.Problem()
lp.add_named_rows('first', 'second')
lp.set_row_bnds('first', None, 0)
lp.add_named_cols('x', 'y')
lp.set_col_bnds('x', -1, None)
lp.set_mat_col('x', {'first': -1, 'second': 1})
lp.set_col_kind('y', 'binary')
lp.set_obj_coef('x', 1)
lp.set_obj_coef('y', -3)
# configure and solve
iocp = ecyglpki.IntOptControls() # (default) int. opt. control parameters
iocp.presolve = True
status = lp.intopt(iocp)
if status != 'optimal':
    raise RuntimeError('Error while solving...: ' + status)
# fix the binary variable to its computed value and find exact solution
yval = lp.mip_col_val('y')
lp.set_col_bnds('y', yval, yval)
smcp = ecyglpki.SimplexControls() # (default) simplex control parameters
smcp.presolve = True
status = lp.simplex(smcp)  # solve
if status != 'optimal':
    raise RuntimeError('Error while solving...: ' + status)
smcp.presolve = False
status = lp.exact(smcp)  # now solve exactly
if status != 'optimal':
    raise RuntimeError('Error while solving...: ' + status)

External resources:

The documentation consists of a description of the API, but also contains examples for which the source code is available and can be inspected to get a feel for how to use the package.


Simple swig bindings for the GNU Linear Programming Kit

A description, installation instructions, and an example are available on PyPI:

The source is available on GitHub:

User recommendations

This thread in early-2011 discusses the merits of the various Python bindings:

  • one user reports being unable to build Python-GLPK under Mac OS X, despite several attempts
  • one user recommends PyMathProg and suggests compiling GLPK from scratch, rather than rely on third-party binaries



R is a free software environment for statistical computing and graphics. The R project is responsible for R. Packages for R are provided by the Comprehensive R Archive Network (CRAN). R is cross-platform and supported on Windows, MacOS X, and Linux.

In addition, R scripts (as can gnuplot scripts) can be use to process GLPK output directly, without the use of dedicated R packages.


Package glpk is provided by CRAN — which also contains the reference manual. Glpk is based on GLPK 4.8 and has not been updated since 2006.

To install (from within R):


It would seem sensible to update the GLPK library before using this package for substantial work. Many of the 4.8 GLPK calls have been deprecated, so an upgrade will involve some additional programming effort. That said, the basic structure of the package should still serve as a template.


Package Rglpk is provided by CRAN — which also contains the reference manual.

To install (from within R):


Rglpk offers an R interface to GLPK (version 4.42 as at 30 June 2010) and a high-level R interface to the MPS and CPLEX LP readers of GLPK. As such, Rglpk provides a way of running MPS and CPLEX LP models from within R, but does not provide support for MathProg-style programming from within R. (Other R packages might well support AMPL or GAMS-style programming?)

Debian-based Linux users can also install Rglpk via the Debian package r-cran-rglpk. But it would pay to check on currency.