GNU MathProg is a high-level language for creating mathematical programming models. MathProg is specific to GLPK, but resembles a subset of AMPL. MathProg can also be referred to as GMPL (GNU Mathematical Programming Language), the two terms being interchangeable.
Detailed examples can be found in the dedicated sub-pages beneath this page, grouped according to their problem domain. This page, in contrast, provides an overview and gives a short example.
Fourer, Gay, and Kernighan (2002)
The following MathProg example implements the linear constrained optimization model:
In this model, there is no requirement for and to be non-negative.
# short example var x1; var x2; maximize obj: 0.6 * x1 + 0.5 * x2; s.t. c1: x1 + 2 * x2 <= 1; s.t. c2: 3 * x1 + x2 <= 2; solve; display x1, x2; end;
To solve this model with GLPK, save the above text as short.mod and then invoke:
$ glpsol --math short.mod
You should obtain and as part of the terminal output from the GLPK solver.
Official GLPK examples
The examples directory in the official GLPK distribution contains about 60 MathProg (as well as some application programming) examples:
- sample MathProg model (*.mod) files — with and without embedded data
- sample MathProg data (*.dat) files — external data
- sample database (*.dbf) files — table-based external data
- sample data (*.csv) files — table-based external data
Note the instructions for downloading GLPK tarballs.
Very large models
Certain kinds of very large MathProg models can take a long time to parse, possibly hours. A large model might, for instance, comprise half a million graph edges. The GLPK MathProg translator is non-optimizing — meaning that it cannot recognize viable shortcuts during the parsing process. In some cases though, a MathProg model can be re-framed to work-around particular bottlenecks but the modeler will need to understand the mechanics of parsing first. In this May 2011 thread, GLPK author Andrew Makhorin suggests:
"The MathProg translator is not intended (by design) to process very large models. In such cases, it is better to use the GLPK API or to generate model data, for example in the GLPK LP format, with a specialized program or script." (grammar corrected)
That said, if the MathProg translator completes, then the resulting model will be satisfactory. Furthermore, the MathProg translator is not normally memory bound, so running a parse job overnight might provide an acceptable strategy.
If you encounter problems with your MathProg model, you can investigate further by specifying the GLPSOL options --nopresol to disable the LP presolver and --output filename.out to write the final solution to a text file.
If you receive the message "PROBLEM HAS NO PRIMAL FEASIBLE SOLUTION", then hunt through the solution file for the variables that violate their bounds. This should allow you to identify which constraints are contradictory and the kinds of adjustment that might lead to a feasible system.
- Fourer, Robert; Gay, David M.; Kernighan, Brian W. (2002). AMPL: a Modelling Language for Mathematical Programming (2 ed.). Duxbury. ISBN 0-534-38809-4.
- Sottinen, Tommi (2009). Operations research with GNU Linear Programming Kit. ORMS1020 course notes. Department of Mathematics and Statistics, University of Vaasa, Finland. http://lipas.uwasa.fi/~tsottine/lecture_notes/or.pdf.