# GLPK/Using GLPSOL

This page lists some tips and hints when using GLPSOL, the GLPK command-line solver.
Note that GLPSOL is built, by default, when GLPK is built. Hence GLPSOL should be generally present whenever GLPK is installed.

## Usage

Usage:
   glpsol [options...] filename
For example, using the options in their long-form:

## Options

The following information is derived from the usage option --help.
General options:
LP basis factorization options:
Options specific to the simplex solver:
Options specific to the interior-point solver:
Options specific to the MIP solver:
Options for CNF-SAT satisfiability problems:

## Compressed files

The size of output files can be reduced by appending the suffix .gz. This indicates gzip compression. For instance:
Similarly, GLPSOL will automatically decompress any file that it encounters that ends with the extension .gz.

## Standard streams

GLPSOL supports the following three standard streams:
* /dev/stdin
* /dev/stdout
* /dev/stderr
These are treated as special filenames by the GLPK I/O routines in a platform-independent manner. Hence the following command should work equally on Windows, Mac OS X, and Linux, delivering the solution report to the terminal instead of to a regular file:

## Passing values via the command-line

If you need to pass a parameter value via the command-line, the following example shows how to do this using a shell script.
The shell script, either test.bat or test.sh, first creates a dynamic data file test.dat, which contains the required command-line value. The shell script then invokes GLPSOL with a hard-coded model name which, in this example, simply prints the value originally passed in from the command-line.
| Step | Microsoft Windows | Linux and Mac OS X |
| create a model file | test.mod param p; printf "p = %f\n", p; end; | test.mod param p; printf "p = %f\n", p; end; |
| create a shell script file | test.bat echo data;param p := %1;end; > test.dat glpsol.exe -m test.mod -d test.dat | test.sh #!/bin/sh echo data\;param p := $1\;end\; > test.dat glpsol -m test.mod -d test.dat |
| make that file executable | do nothing | chmod u+x test.sh |
| run the shell script | test.bat 3.14159 | ./test.sh 3.14159 |
Observe that you can specify multiple data files within the shell script, for instance:
With a little more ingenuity, a whole raft of information could be passed to the shell script and the script itself could exercise some intelligence when making its internal calls (cf. GLPK/Scripting_plus_MathProg).

## xglpsol bash script

The xglpsol bash script (which you can rename to suit your needs) offers a simplified interface to the GLPSOL command-line solver.
For example the command:
produces and runs the GLPSOL call:
It then saves the screen capture to short.grab and diff's that against the previous screen capture file.
This script enables the user to rapidly change and view the effects of different GLPSOL options. Other GLPSOL options could be easily added, for instance, those governing MIP behavior. The help message from xglpsol is shown for reference purposes.
The bash source can be copied from the collapsed table below. xglpsol is licensed under a Creative Commons BY-SA.
* xglpsol.sh
* #! /bin/bash
* # LEGAL
* # Copyright: (c) 2011 Robbie Morrison. Some rights reserved.
* # License: this code is licensed under a Creative Commons BY-SA.
* # http://www.creativecommons.org/licenses/by-sa/3.0/
* # Version: $Id: xglpsol 6703 2011-05-07 07:56:57Z robbie $
* # OVERVIEW
* # This script provides a simplified interface to the
* # GLPK GLPSOL command-line solver. It pumps out
* # GLPSOL command-lines, can diff recent output, and
* # generally facilitates experimentation.
* # The script was developed on Linux Ubuntu 10.04 using
* # Bash 4.1.5. It should work on older versions of
* # Bash too.
* # user modifiable
* EXTN="glpk" # assumed extension if no extension given
* WIDTH=$(stty size | awk '{ print $2 }') # the 'diff' default is often 130 but my screen is wider
* DIFF="diff --side-by-side --suppress-common-lines --width=$WIDTH"
* # preamble
* GLPSOL="glpsol" # GLPSOL call
* LOG="grab" # terminal capture extension
* SCRIPT=$(basename "$0")
* # script exit codes
* E_SUCCESS=0
* E_FAILURE=1
* E_USAGE=2
* E_GLPSOL_NOT_FOUND=3
* E_FILE_NOT_FOUND=4
* E_EXTN_NOT_SUPPORTED=5
* E_OTHER=64
* # display_help
* function display_help
* cat << EOM
* usage: $SCRIPT model[.ext] basic GLPSOL run
* $SCRIPT -- model[.ext] basic GLPSOL run (for convenience)
* $SCRIPT -acdklmrswy model[.ext] extended run
* $SCRIPT --help display this message and exit (takes priority)
* run options: -a implies -lms
* -c check model --check
* -l add back LP presolve --nopresol | --presol
* -m add back MIP presolve --nointopt | --intopt
* -s add back scaling --noscale | --scale
* output options: -d diff with the last available capture (implies -k)
* -k keep console output (with existing file backed up)
* -r display sensitivity analysis (simplex only) --ranges
* -w display solution (includes KKT) --output
* -y dry run, simply print the GLPSOL call
* extensions: model.glpk GLPK problem --glp
* model.lp CPLEX LP problem --lp
* model.mod MathProg model --math
* model.mps free MPS problem --freemps
* purpose: simplified interface to GLPK GLPSOL command-line solver
* hardcodes: GLPSOL utility = $GLPSOL
* default extension = .$EXTN
* terminal capture extension = .$LOG
* diff call = $DIFF
* EOM
* # process long-form options
* case "$1" in
* --help|--hel|--he|--h|-help|-hel|-he|-h|"-?")
* display_help
* exit $E_SUCCESS
* esac
* # process short-form options
* # capture command-line
* cline=$(echo $SCRIPT $*)
* # set the default flags
* usageflag="0"
* storeflag="0"
* rangeflag="0"
* writeflag="0"
* sdiffflag="0"
* dummyflag="0"
* lppreflag="0"
* mipreflag="0"
* scaleflag="0"
* checkflag="0"
* # process options
* while getopts ":-acdhklmrswy" option # CAUTION: the leading : should be correct
* do
* case "$option" in
* -) : ;; # this codes for option "--" and do nothing is correct
* h) usageflag="1" ;;
* d) sdiffflag="1" ;;
* k) storeflag="1" ;;
* r) rangeflag="1" ;;
* w) writeflag="1" ;;
* y) dummyflag="1" ;;
* a) lppreflag="1"; mipreflag="1"; scaleflag="1" ;;
* c) checkflag="1" ;;
* l) lppreflag="1" ;;
* m) mipreflag="1" ;;
* s) scaleflag="1" ;;
* echo "$SCRIPT: incorrect usage, try --help"
* exit $E_USAGE
* esac
* done
* shift $(($OPTIND - 1))
* # the above decrements the argument pointer so it points to next
* # argument, hence $1 now references the first non-option supplied
* # on the command-line, in the event that substantive arguments
* # were given
* # process help in multiple options
* case "$usageflag" in
* 1)
* display_help
* exit $E_SUCCESS
* esac
* file="$1"
* # rework the flags for some cases
* case "$checkflag" in
* 1) sdiffflag="0" ;;
* esac
* case "$dummyflag" in
* 1) sdiffflag="0"; storeflag="0" ;;
* esac
* case "$sdiffflag" in
* 1) storeflag="1" ;;
* esac
* # lead-up code
* # presume success
* exitval=0
* # confirm glpsol
* test $(which "$GLPSOL") ||
* echo "$SCRIPT: GLPSOL not found: $GLPSOL"
* exit $E_GLPSOL_NOT_FOUND
* # process filename
* extn=${file##*.} # grab extension
* test "$extn" == "$file" && # indicates no extension given
* extn="$EXTN"
* file="$file.$extn"
* case "$extn" in
* glpk) fileopt="--glp" ;;
* lp) fileopt="--lp" ;;
* mod) fileopt="--math" ;;
* mps) fileopt="--freemps" ;;
* echo "$SCRIPT: FATAL: model extension not supported: .$extn"
* exit $E_EXTN_NOT_SUPPORTED
* esac
* stub=$(basename "$file" ".$extn")
* # confirm model file
* test -f "$file" ||
* echo "$SCRIPT: FATAL: input file not found: $file"
* exit $E_FILE_NOT_FOUND
* # obtain some run-time details
* lines=$(wc --lines "$file" | gawk '{ print $1 }')
* tstamp=$(date "+%Z %z %a %d-%b-%Y %H:%M:%S")
* # create GLPSOL call
* REDIRECT="/dev/stdout" # location of STDOUT
* REDIRECT="\"$REDIRECT\"" # place in double-quotes
* options=""
* case "$checkflag" in
* 0) : ;;
* 1) options="$options --check" ;;
* esac
* case "$scaleflag" in
* 0) options="$options --noscale" ;;
* 1) options="$options --scale" ;;
* esac
* case "$lppreflag" in
* 0) options="$options --nopresol" ;;
* 1) options="$options --presol" ;;
* esac
* case "$mipreflag" in
* 0) options="$options --nointopt" ;;
* 1) options="$options --intopt" ;;
* esac
* case "$rangeflag" in
* 0) : ;;
* 1) options="$options --ranges $REDIRECT" ;;
* esac
* case "$writeflag" in
* 0) : ;;
* 1) options="$options --output $REDIRECT" ;;
* esac
* call="$GLPSOL$options $fileopt $file"
* # call code
* test -f "$stub.$LOG" && mv --force "$stub.$LOG" "$stub.$LOG~"
* { # local block used to redirect output
* echo
* echo " time : $tstamp"
* echo " cline : $cline"
* echo " model : $file"
* echo " lines : $lines"
* echo " call : '$call'"
* echo
* case "$dummyflag" in
* 0)
* eval "$call"
* exitval=$? # capture return
* 1)
* echo " dry run, GLPSOL call shown above"
* esac
* echo
* echo " return : $exitval"
* echo " elapsed : $SECONDS"
* echo
* } >> "$stub.$LOG"
* cat "$stub.$LOG"
* test "$storeflag" == "0" && rm --force "$stub.$LOG"
* # diff code
* case "$sdiffflag" in
* 1)
* test -f "$stub.$LOG" -a -f "$stub.$LOG~" &&
* echo " diff output : $stub.$LOG : $stub.$LOG~"
* echo
* $DIFF "$stub.$LOG" "$stub.$LOG~"
* echo
* esac
* # housekeeping
* exit $exitval
* # end of file

## Using encrypted models

Model files may contain confidential data which deserves protection via encryption. A model file can easily be encrypted using the GNU Privacy Guard.
To avoid writing the decrypted model to disk it can be passed via a pipe to glpsol.

## Notes

* ↑ Given that GLPK was originally built with support for file compression.
* ↑ More generally, any POSIX-compliant operating system.
* ↑ If noclobber is set for your shell, replace > by >|.
* ↑ Windows uses the extension .bat to recognize an executable shell script.