GLPK/Scripting plus MathProg

This page covers the use of external scripting languages with MathProg.

Passing values via the command-line
Values cannot be passed as command line parameters to glpsol. Instead a script can be used to write command line parameters to a data file. This data file can be loaded together with the model.

The following Bash script test.sh takes the first command line parameter and writes it to a data file test.dat and then calls glpsol for model test.mod and data file test.dat:

You can test the script with the following model file test.mod

param p, symbolic; printf "%s\n", p; end;

by executing

The data file created test.dat will contain the following lines:

data; param p := 'Hello world'; end;

Parametric studies and MathProg
The MathProg language does not offer control structures in the interests of simplicity and efficiency (a restriction that does not apply to compiled language programming with the GLPK API). This lack of control structures prevents the direct implementation of parametric studies within MathProg &mdash; where one would like to perform a simple loop over a range of parameter values with the MathProg solve statement nested inside. A parametric study is the same as a sensitivity analysis.

One work-around is to use a scripting language to generate and run a number of GLPK model instances. A more sophisticated approach would be to use a relational database for the storage of interim solutions.

GLPSOL and AWK
AWK is a well-established scripting language with a C-like syntax. GNU AWK is present by default on most Linux distros, otherwise install the gawk package. Windows users can obtain GNU AWK binaries from gnuwin32.sourceforge.net/packages/gawk.htm.

AWK makes it possible to create GMPL data files and then invoke glpsol on these newly-created files. This means AWK is a good choice for undertaking parametric studies in association with GMPL. It is normally better to use one volatile data file for your scanned parameters and another stable data file for the rest of your model data (glpsol supports multiple data files).

The rudimentary example below is presented as a stub for developing your own parameter scanning scripts. This example repeatedly writes the parameter iter to a volatile data file test.dat and then calls glpsol to display the value of this parameter. This script also generates the model file test.mod at the outset, but normally your model file would preexist. Windows users will need to replace the Linux remove command rm --force with del.

Save this script as text file scan.awk.

Ensure that lines are separated by line feeds (0x0A). Beware, some OS X editors use carriage returns (0x0D).

Run the script from the command-line:

$ awk -f scan.awk

Edited terminal output from iteration 2 is as follows:

Iteration 2 Writing data file GLPSOL: GLPK LP/MIP Solver, v4.44 Reading model section from test.mod... Reading data section from test.dat... Model has been successfully generated GLPK Simplex Optimizer, v4.44 0 rows, 0 columns, 0 non-zeros ~    0: obj =   0.000000000e+00  infeas =  0.000e+00 OPTIMAL SOLUTION FOUND Display statement at line 4 iter = 2 Model has been successfully processed

The same basic idea can be implemented in virtually any scripting language, from bash upwards. In addition, astute readers may notice that altered model (as opposed to data) files can also be constructed on-the-fly using similar scripting methods.

GLPSOL and Visual Basic Script
Visual Basic Script is programming language delivered with Microsoft Windows.

Visual Basic Script makes it possible to create GMPL data files and then invoke glpsol on these newly-created files.

The example below demonstrates how this can be done.

Create a model file <tt>test.mod</tt> which will only print parameter <tt>p</tt>. Create a script <tt>test.vbs</tt> Run the script with

GLPSOL and Visual Basic for Applications
Visual Basic Script is programming language delivered with Microsoft Office.

VBA makes it possible to create GMPL files and then invoke glpsol on these newly-created files.

The example below demonstrates how this can be done.

Python and PyMathProg
If shell-command-based scripting (using AWK) is not flexible enough, then the Python language and the PyMathProg package provide a more powerful alternative. PyMathProg allows one to write linear and mixed-integer programming models &mdash; in a form very much like GMPL &mdash; using Python. A succinct example of how PyMathProg can be used to implement a subtour elimination heuristic is given here.

Python and Sage
Comment: this material should be extended.

Sage is an open source mathematics environment, offering both symbolic and numerical calculation and good visualization. Sage supports the Python language and GLPK is available through the Sage optimization module.

A mixed-integer model is first built using an instance of class <tt>MixedIntegerLinearProgram</tt> &mdash; and then solved using its <tt>solve</tt> method, with the solver set to GLPK:

sage: p = MixedIntegerLinearProgram(maximization=True) sage: x = p.new_variable ... sage: p.solve(solver="GLPK", log="filename.log")

The overhead for installing Sage is apparently quite high, but the environment works well.

Suppressing terminal output under Python
Terminal output may be suppressed as follows:

Save the above scripting to a file named <tt>quiet.py</tt> and then execute it:

> python quiet.py complete

GLPSOL's normal output is stored in <tt>capture</tt> for later use. The model's solution is saved as file <tt>noisy.out</tt>. And only the explicit <tt>print</tt> statement is sent to the console.

Similar techniques could be applied to other scripting languages, including Bash and Perl. Moreover, command-line arguments could be passed through to the final call to aid flexibility.