# 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
by executing
The data file created test.dat will contain the following lines:

## 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 — 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:
Edited terminal output from iteration 2 is as follows:
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 test.mod which will only print parameter p.
Create a script test.vbs
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 — in a form very much like GMPL — 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 MixedIntegerLinearProgram — and then solved using its solve method, with the solver set to GLPK:
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 quiet.py and then execute it:
GLPSOL's normal output is stored in capture for later use. The model's solution is saved as file noisy.out.
And only the explicit print 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.