GLPK/Using GMPL (MathProg)

This page gives examples and tips about the syntax and usage of GMPL (MathProg).

Introduction
The first thing to note is that MathProg is not a procedural language. For example, unlike most programming languages, MathProg has no assignment statement. Rather MathProg is a functional language. MathProg is modeled on the AMPL language and provides a subset of that language. And like AMPL, its syntax is tailored to the expression of mathematical optimization problems.

Sets
Within MathProg, sets are unordered collections of elements (unlike arrays in programming languages), so the element positions are indeterminate.

MathProg suffixes
As from GLPK 4.44 released 03 June 2010, MathProg supports suffixes for constraints and variables. The use of suffixes is covered in the official documentation under appendix A of doc/gmpl.pdf. The following table provides a summary:

The MathProg printf statement can be used to create customized output. In the following case, the dual value of constraint c1 will be printed to the terminal:

... solve; printf "dual value of constraint c1: %g\n", c1.dual;

Terminal output can also be duplicated to a text file with the GLPSOL command-line option --log filename.log.

Set statement
The data parameter of the set statement is (as of May 2011) undocumented, but the idea is quite simple. Imagine that you declare a plain set S consisting of 5-tuples (i,j,k,l,m):

set S, dimen 5;

which will be provided with data in the data section. But what you actually want is an array of sets <tt>T{j,m}</tt>, where <tt>T[j,m]</tt> is a set of triplets <tt>(l,i,k)</tt> from <tt>S</tt>. Using the <tt>data</tt> attribute, additionally declare a set <tt>T</tt> as follows:

set T{j,m}, data S[2,5,4,1,3];

This statement has the same effect as providing the corresponding data for <tt>T</tt> itself from the data section, where <tt>[2,5,4,1,3]</tt> is a permutation of the component indices: the 2nd and 5th components of every 5-tuple in <tt>S</tt> gives a pair <tt>(j,m)</tt> used to index the elements of <tt>T</tt>, and the 4th, 1st, and 3rd components of the 5-tuple are used to build the <tt>(l,i,k)</tt> triplets, which are then included in the set <tt>T[j,m]</tt>. Finally, only plain sets are supported by the <tt>data</tt> attribute.

Ordered sets
Ordered sets are not supported in MathProg. The discussions here and here provide some background as to why not. This 2006 thread describes one particular work-around.