GLPK/Using GMPL (MathProg)

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

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


Introduction[edit]

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.

MathProg objects[edit]

Sets[edit]

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

MathProg syntax[edit]

MathProg suffixes[edit]

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:

Suffix Purpose
.lb lower bound
.ub upper bound
.status  status in the solution 
.val primal value
.dual dual value

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[edit]

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 T{j,m}, where T[j,m] is a set of triplets (l,i,k) from S. Using the data attribute, additionally declare a set T 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 T itself from the data section, where [2,5,4,1,3] is a permutation of the component indices: the 2nd and 5th components of every 5-tuple in S gives a pair (j,m) used to index the elements of T, and the 4th, 1st, and 3rd components of the 5-tuple are used to build the (l,i,k) triplets, which are then included in the set T[j,m]. Finally, only plain sets are supported by the data attribute.

Limitations[edit]

Ordered sets[edit]

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.