# Introduction

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:
* a callable library, written in C, for solving large-scale linear (LP), mixed integer (MIP), and related optimization problems
* the GNU MathProg mathematical programming language for specifying such problems at a high level
* the GLPSOL command-line application for translating and solving models written in MathProg
* comprehensive documentation for the above
The GLPK project itself is hosted at www.gnu.org/software/glpk.
The GLPK wikipedia entry can be found at en.wikipedia.org/wiki/GLPK.
The GLPK project maintains two mailing lists: help-glpk@gnu.org and bug-glpk@gnu.org. To subscribe to either of these lists, visit lists.gnu.org/mailman/listinfo/help-glpk or lists.gnu.org/mailman/listinfo/bug-glpk 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:
* pre-built binaries for common platforms, including Windows and Linux
* language bindings for languages other than C and C++
* spreadsheet and database connectivity under MathProg
* integrated development environments (IDE) for MathProg
* problem and/or solution visualization techniques
* unofficial GLPK documentation, including this Wikibook.
A key reason for this Wikibook is to provide a location in which to collect and compare these often diverse initiatives.

## Closure

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.

## Examples
### Traveling salesman problem

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.
Run the model (40 seconds on a Intel Core i5 processor):
This is what the created output file out.svg looks like (some lines removed):
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).

### Clustering

The example below concerns a clustering problem. Out of 500 towns select 20 to be cluster centers and assign the other towns to the cluster such that the sum of the population weighted euclidian distances between towns and centers is minimized.

# Gnuplot

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

Gnuplot expects data for histograms to be in multiple columns.
The following example is based on examples/transp.mod from the GLPK source distribution:
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:
Using gnuplot, a histogram of transp1.dat can be drawn and then saved as a PNG image:
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 transp1.gp and then run as a script from within gnuplot:
Finally, check the resulting transp1.png with any bitmap viewer, perhaps gthumb:

## 3D histogram

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:
As before, a 3D histogram of transp2.dat can be drawn using gnuplot and saved as a PNG image:

# 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
* Julia 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

Java is an object-oriented application programming language.

## GLPK for Java

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

### Installation

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.

### Documentation

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 Test.java.
Then compile this class under 64-bit Windows:
Or compile it under Linux:
Run the resulting file on 64-bit Windows:
Or run the file on Linux (the file paths may need adjustment to match your installation):
The output will be your GLPK version number, for example: 4.47.

### Usage with 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:
Rows can be created and populated like this:
You may download the code using subversion:
For using this library in your Maven project enter the following repository and dependency in your pom.xml (adjust the version number as needed).
When testing with Maven it may be necessary to indicate the installation path of the GLPK for Java shared library (.so or .dll).
The exec:java target may require to indicate the installation path of the GLPK for Java shared library in MAVEN_OPTS, e.g.

## 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 http://javailp.sourceforge.net/ provides an example code. According to the homepage GLPK ≥ 4.43 is supported.
Development seems to have stopped. The last code update was on 2012-08-02.

## OptimJ

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.
Development seems to have stopped. The latest release dates back to 2011.

## Java Native Access

Java Native Access (JNA) can be used to call shared native libraries from Java. The project home is https://github.com/twall/jna.
Using JNA has the following drawbacks:
* Errors occuring in the GLPK library will terminate the main process.
* Callbacks cannot be used.
The following example uses JNA to display the GLPK library version number.
Compile and run with
Adjust the classpath according to your JNA installation path.

## 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.

# Python

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.

## PyGLPK

PyGLPK is an encapsulation of GLPK in Python objects (currently maintained 2021). 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

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.

## Pyomo

The Python Optimization Modeling Objects (Pyomo) package
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:
equates to, in Pyomo:

## Python-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 (unfortunatly this package is no longer maintained (2021)). 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:
* Python
* SWIG
* PLY (Python Lex-Yacc) — available separately thru Debian packages python-ply and python-ply-doc

### Usage

The following minimalistic program will show the GLPK version number:

### 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: wget http://www.dcc.fc.up.pt/~jpp/code/python-glpk/python-glpk_0.4.43.orig.tar.gz
* 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: python test.py

## CVXOPT

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

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

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:
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).

## yaposib

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.

## ecyglpki

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:
External resources:
* Page on PyPI: https://pypi.python.org/pypi/ecyglpki
* Documentation for the latest release: http://pythonhosted.org/ecyglpki/
* Source on GitHub: https://github.com/equaeghe/ecyglpki
* Documentation of a ‘recent’ development version: https://equaeghe.github.io/ecyglpki/
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.

## swiglpk

Simple swig bindings for the GNU Linear Programming Kit
A description, installation instructions, and an example are available on PyPI: https://pypi.python.org/pypi/swiglpk
The source is available on GitHub: https://github.com/biosustain/swiglpk

## ctypes

The ctypes library allows to wrap native library calls.
This example displays the GLPK version number:

## 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

## References

* ↑ Hart, William E. (2008). "Python optimization modeling objects (Pyomo)" (PDF). {{cite journal}}: Cite journal requires |journal= (help)

# R

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.

## Rglpk

Package Rglpk is provided by CRAN — which also contains the reference manual.
To install from repository within R:
To install from local archive within R:
Debian-based Linux users can also install Rglpk via the Debian package r-cran-rglpk. But it would pay to check on currency.
Rglpk provides two functions:
* Rglpk_read_file reads a linear problem from a file in either of the following formats: fixed MPS, free MPS, CPLEX, and GMPL.
* Rglpk_solve_LP solves a linear and mixed linear problems.
The following commands solve a problem provided in GMPL file /home/user/test.mod

## glpkAPI

The package glpkAPI has not been maintained since 2015.

## glpk

The deprecated package glpk has been archived by CRAN. It was based on GLPK 4.8 and never updated since 2006.