Lab Information: 1.1 Introduction To GLPK
Lab Information: 1.1 Introduction To GLPK
The GLPK (GNU Linear Programming Kit) package is intended for solving large-scale
linear programming (LP) and mixed integer programming (MIP). GLPK supports GMPL
(GNU MathProg Language), which is a modeling language (almost identical to AMPL).
The main idea of glpsol and the GNU MathProg language is to ease modeling of opti-
mization problems. The model is written on a file, and data for the problem is written on
another file.
The glpsol code is run in a terminal window, and solves a problem written in the modeling
language GMPL. The problem file and the data file are given by command line parameters.
Example: Read model file myprob.mod, data file myprob.dat, solve the problem and
write the solution to file myprob.sol.
1
1.3 A small example
The following output appears on the screen, when solving the problem.
Hint: Always check whether is says “OPTIMAL SOLUTION FOUND”, and not “PROB-
LEM HAS NO PRIMAL FEASIBLE SOLUTION”, which means that the problem has no
feasible solution, and the output is rather meaningless.
2
Afterwards, the file typex2.sol will contain the following.
Problem: typex2
Rows: 4
Columns: 4
Non-zeros: 16
Status: OPTIMAL
Objective: z = 75 (MAXimum)
Here we see that x1 and x3 are basic variables at the optimum, and that the solution is
x1 = 10, x2 = 0, x3 = 12.5, x4 = 0, with the objective function value equal to 75. The
shadow prices are 0, 7 and -3 for the three constraints.
When one solves a MIP problem, the output from glpsol contains
- number of iterations by the simplex method;
- value of the objective function for the best known feasible integer solution, which is an
upper (minimization) or lower (maximization) - global bound for the optimal value of the
objective function;
the best local bound for active nodes, which is a lower (minimization) or upper (maximiza-
tion) global bound for the optimal value of the objective function;
- the relative MIP gap, in percentage;
- number of the open (active) subproblems;
- number of explored subproblems.
Solving LP relaxation...
GLPK Simplex Optimizer, v4.47
3 rows, 4 columns, 12 non-zeros
0: obj = 0.000000000e+00 infeas = 8.000e+01 (0)
* 3: obj = 4.500000000e+01 infeas = 0.000e+00 (0)
* 4: obj = 7.500000000e+01 infeas = 0.000e+00 (0)
OPTIMAL SOLUTION FOUND
Integer optimization begins...
+ 4: mip = not found yet <= +inf (1; 0)
+ 9: >>>>> 6.800000000e+01 <= 6.800000000e+01 0.0% (4; 0)
+ 9: mip = 6.800000000e+01 <= tree is empty 0.0% (0; 7)
INTEGER OPTIMAL SOLUTION FOUND
Time used: 0.0 secs
Memory used: 0.1 Mb (127461 bytes)
4 simplex iterations were needed for solving the first LP relaxation in this example (the
previous example but with integer variables). The objective function value of this LP
3
relaxation is 75. It takes 9 simplex iterations in total to solve the integer programming
problem. The optimal objective function value is 68. It requires 7 nodes in the search tree
to find the optimal solution (and validate optimality).
Introduction
GMPL is a modeling language which allows for simplifying modeling an optimization prob-
lem with help of sets, sums etc. Here is a short and simplified description of the language.
(Model and data files described below can be used both for GMPL and AMPL.)
Each variable has a name (x1, x2 etc), the objective function has a name (z) and each
constraint has a name (con1, con2 etc). It is free to choose names, but they must be
different. To write comments to the model, use first the sign #. Each row should end with
semicolon (;).
For solving the same model with several different data, it is good to separate model and
data. One can define parameters and sets used in sums etc. Consider the following general
formulation.
Xn
max z = cj xj
j=1
n
X
st aij xj ≤ bi i = 1, . . . , m
j=1
xj ≥ 0 j = 1, . . . , n
Then model file for GMPL is created as follows. We use introduced parameters, although
they do not have numerical values yet.
param n; # number of variables
param m; # number of constraints
param c{1..n}; # Definition of objective function coefficients
param a{1..m,1..m}; # Definition of constraint matrix
param b{1..m}; # Definition of right-hand-sides
Here n, m, c, a and b are parameters (param) and x are variables (var). n is the number
of variables and m is the number of constraints. Then 1..n is the set of indices of all
variables and 1..m is the set of indices of all constraints. sum {j in 1..n} is a sum over
the set of indices of all variables.
The file with numerical data will be as follows. Here the values for all parameters which
were declared in the model file must be assigned.
param n := 4; #number of variables
param m := 3; #number of constraints
param : c :=
4
1 5
2 6
3 2
4 4;
param a : 1 2 3 4 :=
1 3 2 1 5
2 2 1 2 4
3 3 -3 4 5;
param : b :=
1 80
2 45
3 80;
end;
Observe that it is required to provide indices for vectors (c and b), and both row indices
and column indices for matrices (a).
2.1 Syntax
Below we describe some of the most common commands in GMPL. Note that GMPL
distinguishes between uppercase and lowercase letters, and GMPL does not break rows,
therefore each command or declaration must end with ; (semicolon).
param
Constants are declared with param. Assignment of values for each parameter is done
as follows.
param n := 7;
param : vec := 1 50 2 75 3 100;
param : vec2 := 1 34 2 105 3 65 4 120;
param matr : 1 2 3 : =
1 80 9 77
2 11 120 13;
The parameter n gets value 7, the vector vek is assigned values vec[1]=50, vec[2]=75
and vec[3]=100 (observe that every second number is an index) and the matrix matr
is assigned values matr[1,1]=80, matr[1,2]=9, matr[1,3]=77, matr[2,1]=11 etc.
set
A set is declared by set. It may contain both numerical and symbolic values.
set CARS := SAAB VOLVO BMW;
set OddNR := 1 3 5 7 9;
set WEEKS := 1..N;
var
All variables must be declared with var. You can choose any names for the variables.
5
sum
A sum over several variables in objective function and constraints is declared by sum.
sum{i in Cars} Weight[i] # Sum Weight over all elements in the set Cars
sum{i in 1..20} (x[i] - y[i]) # Summ x[i]-y[i] for i=1 to 20
sum{i in ORIG, j in DEST} z[i,j] # Sum over two indices
maximize / minimize
Specifies the objective function (which must be given a name). The name is followed
by : (colon) and then the objective function.
subject to
Declares one or a set of constraints. The constraints must have different names. The
name is followed by : (colon) and then the constraint.
subject to constraint1: x + y = 7;
solve
Declares that the problem should be solved.
display
Displays various information, e.g. the solution
printf
More controlled output, which allows e.g. to avoid displaying zero variables.
end
GMPL requires that both model and data files end with end. (AMPL does not
require this, but allows it.)