doc/glpk01.tex
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 05 Dec 2010 17:35:23 +0100
changeset 2 4c8956a7bdf4
permissions -rw-r--r--
Set up CMAKE build environment
alpar@1
     1
%* glpk01.tex *%
alpar@1
     2
alpar@1
     3
\chapter{Introduction}
alpar@1
     4
alpar@1
     5
GLPK (\underline{G}NU \underline{L}inear \underline{P}rogramming
alpar@1
     6
\underline{K}it) is a set of routines written in the ANSI C programming
alpar@1
     7
language and organized in the form of a callable library. It is intended
alpar@1
     8
for solving linear programming (LP), mixed integer programming (MIP),
alpar@1
     9
and other related problems.
alpar@1
    10
alpar@1
    11
\section{LP problem}
alpar@1
    12
\label{seclp}
alpar@1
    13
alpar@1
    14
GLPK assumes the following formulation of {\it linear programming (LP)}
alpar@1
    15
problem:
alpar@1
    16
alpar@1
    17
\medskip
alpar@1
    18
alpar@1
    19
\noindent
alpar@1
    20
\hspace{.5in} minimize (or maximize)
alpar@1
    21
$$z = c_1x_{m+1} + c_2x_{m+2} + \dots + c_nx_{m+n} + c_0 \eqno (1.1)$$
alpar@1
    22
\hspace{.5in} subject to linear constraints
alpar@1
    23
$$
alpar@1
    24
\begin{array}{r@{\:}c@{\:}r@{\:}c@{\:}r@{\:}c@{\:}r}
alpar@1
    25
x_1&=&a_{11}x_{m+1}&+&a_{12}x_{m+2}&+ \dots +&a_{1n}x_{m+n} \\
alpar@1
    26
x_2&=&a_{21}x_{m+1}&+&a_{22}x_{m+2}&+ \dots +&a_{2n}x_{m+n} \\
alpar@1
    27
\multicolumn{7}{c}
alpar@1
    28
{.\ \ .\ \ .\ \ .\ \ .\ \ .\ \ .\ \ .\ \ .\ \ .\ \ .\ \ .\ \ .\ \ .} \\
alpar@1
    29
x_m&=&a_{m1}x_{m+1}&+&a_{m2}x_{m+2}&+ \dots +&a_{mn}x_{m+n} \\
alpar@1
    30
\end{array} \eqno (1.2)
alpar@1
    31
$$
alpar@1
    32
\hspace{.5in} and bounds of variables
alpar@1
    33
$$
alpar@1
    34
\begin{array}{r@{\:}c@{\:}c@{\:}c@{\:}l}
alpar@1
    35
l_1&\leq&x_1&\leq&u_1 \\
alpar@1
    36
l_2&\leq&x_2&\leq&u_2 \\
alpar@1
    37
\multicolumn{5}{c}{.\ \ .\ \ .\ \ .\ \ .}\\
alpar@1
    38
l_{m+n}&\leq&x_{m+n}&\leq&u_{m+n} \\
alpar@1
    39
\end{array} \eqno (1.3)
alpar@1
    40
$$
alpar@1
    41
where: $x_1, x_2, \dots, x_m$ are auxiliary variables;
alpar@1
    42
$x_{m+1}, x_{m+2}, \dots, x_{m+n}$ are\linebreak structural variables;
alpar@1
    43
$z$ is the objective function;
alpar@1
    44
$c_1, c_2, \dots, c_n$ are objective coefficients;
alpar@1
    45
$c_0$ is the constant term (``shift'') of the objective function;
alpar@1
    46
$a_{11}, a_{12}, \dots, a_{mn}$ are constraint coefficients;
alpar@1
    47
$l_1, l_2, \dots, l_{m+n}$ are lower bounds of variables;
alpar@1
    48
$u_1, u_2, \dots, u_{m+n}$ are upper bounds of variables.
alpar@1
    49
alpar@1
    50
Auxiliary variables are also called {\it rows}, because they correspond
alpar@1
    51
to rows of the constraint matrix (i.e. a matrix built of the constraint
alpar@1
    52
coefficients). Similarly, structural variables are also called
alpar@1
    53
{\it columns}, because they correspond to columns of the constraint
alpar@1
    54
matrix.
alpar@1
    55
alpar@1
    56
Bounds of variables can be finite as well as infinite. Besides, lower
alpar@1
    57
and upper bounds can be equal to each other. Thus, the following types
alpar@1
    58
of variables are possible:
alpar@1
    59
\begin{center}
alpar@1
    60
\begin{tabular}{r@{}c@{}ll}
alpar@1
    61
\multicolumn{3}{c}{Bounds of variable} & Type of variable \\
alpar@1
    62
\hline
alpar@1
    63
$-\infty <$ &$\ x_k\ $& $< +\infty$ & Free (unbounded) variable \\
alpar@1
    64
$l_k \leq$ &$\ x_k\ $& $< +\infty$  & Variable with lower bound \\
alpar@1
    65
$-\infty <$ &$\ x_k\ $& $\leq u_k$  & Variable with upper bound \\
alpar@1
    66
$l_k \leq$ &$\ x_k\ $& $\leq u_k$   & Double-bounded variable \\
alpar@1
    67
$l_k =$ &$\ x_k\ $& $= u_k$         & Fixed variable \\
alpar@1
    68
\end{tabular}
alpar@1
    69
\end{center}
alpar@1
    70
\noindent
alpar@1
    71
Note that the types of variables shown above are applicable to
alpar@1
    72
structural as well as to auxiliary variables.
alpar@1
    73
alpar@1
    74
To solve the LP problem (1.1)---(1.3) is to find such values of all
alpar@1
    75
structural and auxiliary variables, which:
alpar@1
    76
alpar@1
    77
$\bullet$ satisfy to all the linear constraints (1.2), and
alpar@1
    78
alpar@1
    79
$\bullet$ are within their bounds (1.3), and
alpar@1
    80
alpar@1
    81
$\bullet$ provide the smallest (in case of minimization) or the largest
alpar@1
    82
(in case of maximization) value of the objective function (1.1).
alpar@1
    83
alpar@1
    84
\section{MIP problem}
alpar@1
    85
alpar@1
    86
{\it Mixed integer linear programming (MIP)} problem is LP problem in
alpar@1
    87
which some variables are additionally required to be integer.
alpar@1
    88
alpar@1
    89
GLPK assumes that MIP problem has the same formulation as ordinary
alpar@1
    90
(pure) LP problem (1.1)---(1.3), i.e. includes auxiliary and structural
alpar@1
    91
variables, which may have lower and/or upper bounds. However, in case of
alpar@1
    92
MIP problem some variables may be required to be integer. This
alpar@1
    93
additional constraint means that a value of each {\it integer variable}
alpar@1
    94
must be only integer number. (Should note that GLPK allows only
alpar@1
    95
structural variables to be of integer kind.)
alpar@1
    96
alpar@1
    97
\section{Using the package}
alpar@1
    98
alpar@1
    99
\subsection{Brief example}
alpar@1
   100
alpar@1
   101
In order to understand what GLPK is from the user's standpoint,
alpar@1
   102
consider the following simple LP problem:
alpar@1
   103
alpar@1
   104
\medskip
alpar@1
   105
alpar@1
   106
\noindent
alpar@1
   107
\hspace{.5in} maximize
alpar@1
   108
$$z = 10 x_1 + 6 x_2 + 4 x_3$$
alpar@1
   109
\hspace{.5in} subject to
alpar@1
   110
$$
alpar@1
   111
\begin{array}{r@{\:}c@{\:}r@{\:}c@{\:}r@{\:}c@{\:}r}
alpar@1
   112
x_1 &+&x_2 &+&x_3 &\leq 100 \\
alpar@1
   113
10 x_1 &+& 4 x_2 & +&5 x_3 & \leq 600 \\
alpar@1
   114
2 x_1 &+& 2 x_2 & +& 6 x_3 & \leq 300 \\
alpar@1
   115
\end{array}
alpar@1
   116
$$
alpar@1
   117
\hspace{.5in} where all variables are non-negative
alpar@1
   118
$$x_1 \geq 0, \ x_2 \geq 0, \ x_3 \geq 0$$
alpar@1
   119
alpar@1
   120
At first this LP problem should be transformed to the standard form
alpar@1
   121
(1.1)---(1.3). This can be easily done by introducing auxiliary
alpar@1
   122
variables, by one for each original inequality constraint. Thus, the
alpar@1
   123
problem can be reformulated as follows:
alpar@1
   124
alpar@1
   125
\medskip
alpar@1
   126
alpar@1
   127
\noindent
alpar@1
   128
\hspace{.5in} maximize
alpar@1
   129
$$z = 10 x_1 + 6 x_2 + 4 x_3$$
alpar@1
   130
\hspace{.5in} subject to
alpar@1
   131
$$
alpar@1
   132
\begin{array}{r@{\:}c@{\:}r@{\:}c@{\:}r@{\:}c@{\:}r}
alpar@1
   133
p& = &x_1 &+&x_2 &+&x_3 \\
alpar@1
   134
q& = &10 x_1 &+& 4 x_2 &+& 5 x_3 \\
alpar@1
   135
r& = &2  x_1 &+& 2 x_2 &+& 6 x_3 \\
alpar@1
   136
\end{array}
alpar@1
   137
$$
alpar@1
   138
\hspace{.5in} and bounds of variables
alpar@1
   139
$$
alpar@1
   140
\begin{array}{ccc}
alpar@1
   141
\nonumber -\infty < p \leq 100 && 0 \leq x_1 < +\infty \\
alpar@1
   142
\nonumber -\infty < q \leq 600 && 0 \leq x_2 < +\infty \\
alpar@1
   143
\nonumber -\infty < r \leq 300 && 0 \leq x_3 < +\infty \\
alpar@1
   144
\end{array}
alpar@1
   145
$$
alpar@1
   146
where $p, q, r$ are auxiliary variables (rows), and $x_1, x_2, x_3$ are
alpar@1
   147
structural variables (columns).
alpar@1
   148
alpar@1
   149
The example C program shown below uses GLPK API routines in order to
alpar@1
   150
solve this LP problem.\footnote{If you just need to solve LP or MIP
alpar@1
   151
instance, you may write it in MPS or CPLEX LP format and then use the
alpar@1
   152
GLPK stand-alone solver to obtain a solution. This is much less
alpar@1
   153
time-consuming than programming in C with GLPK API routines.}
alpar@1
   154
alpar@1
   155
\newpage
alpar@1
   156
alpar@1
   157
\begin{verbatim}
alpar@1
   158
/* sample.c */
alpar@1
   159
alpar@1
   160
#include <stdio.h>
alpar@1
   161
#include <stdlib.h>
alpar@1
   162
#include <glpk.h>
alpar@1
   163
alpar@1
   164
int main(void)
alpar@1
   165
{     glp_prob *lp;
alpar@1
   166
      int ia[1+1000], ja[1+1000];
alpar@1
   167
      double ar[1+1000], z, x1, x2, x3;
alpar@1
   168
s1:   lp = glp_create_prob();
alpar@1
   169
s2:   glp_set_prob_name(lp, "sample");
alpar@1
   170
s3:   glp_set_obj_dir(lp, GLP_MAX);
alpar@1
   171
s4:   glp_add_rows(lp, 3);
alpar@1
   172
s5:   glp_set_row_name(lp, 1, "p");
alpar@1
   173
s6:   glp_set_row_bnds(lp, 1, GLP_UP, 0.0, 100.0);
alpar@1
   174
s7:   glp_set_row_name(lp, 2, "q");
alpar@1
   175
s8:   glp_set_row_bnds(lp, 2, GLP_UP, 0.0, 600.0);
alpar@1
   176
s9:   glp_set_row_name(lp, 3, "r");
alpar@1
   177
s10:  glp_set_row_bnds(lp, 3, GLP_UP, 0.0, 300.0);
alpar@1
   178
s11:  glp_add_cols(lp, 3);
alpar@1
   179
s12:  glp_set_col_name(lp, 1, "x1");
alpar@1
   180
s13:  glp_set_col_bnds(lp, 1, GLP_LO, 0.0, 0.0);
alpar@1
   181
s14:  glp_set_obj_coef(lp, 1, 10.0);
alpar@1
   182
s15:  glp_set_col_name(lp, 2, "x2");
alpar@1
   183
s16:  glp_set_col_bnds(lp, 2, GLP_LO, 0.0, 0.0);
alpar@1
   184
s17:  glp_set_obj_coef(lp, 2, 6.0);
alpar@1
   185
s18:  glp_set_col_name(lp, 3, "x3");
alpar@1
   186
s19:  glp_set_col_bnds(lp, 3, GLP_LO, 0.0, 0.0);
alpar@1
   187
s20:  glp_set_obj_coef(lp, 3, 4.0);
alpar@1
   188
s21:  ia[1] = 1, ja[1] = 1, ar[1] =  1.0; /* a[1,1] =  1 */
alpar@1
   189
s22:  ia[2] = 1, ja[2] = 2, ar[2] =  1.0; /* a[1,2] =  1 */
alpar@1
   190
s23:  ia[3] = 1, ja[3] = 3, ar[3] =  1.0; /* a[1,3] =  1 */
alpar@1
   191
s24:  ia[4] = 2, ja[4] = 1, ar[4] = 10.0; /* a[2,1] = 10 */
alpar@1
   192
s25:  ia[5] = 3, ja[5] = 1, ar[5] =  2.0; /* a[3,1] =  2 */
alpar@1
   193
s26:  ia[6] = 2, ja[6] = 2, ar[6] =  4.0; /* a[2,2] =  4 */
alpar@1
   194
s27:  ia[7] = 3, ja[7] = 2, ar[7] =  2.0; /* a[3,2] =  2 */
alpar@1
   195
s28:  ia[8] = 2, ja[8] = 3, ar[8] =  5.0; /* a[2,3] =  5 */
alpar@1
   196
s29:  ia[9] = 3, ja[9] = 3, ar[9] =  6.0; /* a[3,3] =  6 */
alpar@1
   197
s30:  glp_load_matrix(lp, 9, ia, ja, ar);
alpar@1
   198
s31:  glp_simplex(lp, NULL);
alpar@1
   199
s32:  z = glp_get_obj_val(lp);
alpar@1
   200
s33:  x1 = glp_get_col_prim(lp, 1);
alpar@1
   201
s34:  x2 = glp_get_col_prim(lp, 2);
alpar@1
   202
s35:  x3 = glp_get_col_prim(lp, 3);
alpar@1
   203
s36:  printf("\nz = %g; x1 = %g; x2 = %g; x3 = %g\n",
alpar@1
   204
         z, x1, x2, x3);
alpar@1
   205
s37:  glp_delete_prob(lp);
alpar@1
   206
      return 0;
alpar@1
   207
}
alpar@1
   208
alpar@1
   209
/* eof */
alpar@1
   210
\end{verbatim}
alpar@1
   211
alpar@1
   212
The statement \verb|s1| creates a problem object. Being created the
alpar@1
   213
object is initially empty. The statement \verb|s2| assigns a symbolic
alpar@1
   214
name to the problem object.
alpar@1
   215
alpar@1
   216
The statement \verb|s3| calls the routine \verb|glp_set_obj_dir| in
alpar@1
   217
order to set the optimization direction flag, where \verb|GLP_MAX| means
alpar@1
   218
maximization.
alpar@1
   219
alpar@1
   220
The statement \verb|s4| adds three rows to the problem object.
alpar@1
   221
alpar@1
   222
The statement \verb|s5| assigns the symbolic name `\verb|p|' to the
alpar@1
   223
first row, and the statement \verb|s6| sets the type and bounds of the
alpar@1
   224
first row, where \verb|GLP_UP| means that the row has an upper bound.
alpar@1
   225
The statements \verb|s7|, \verb|s8|, \verb|s9|, \verb|s10| are used in
alpar@1
   226
the same way in order to assign the symbolic names `\verb|q|' and
alpar@1
   227
`\verb|r|' to the second and third rows and set their types and bounds.
alpar@1
   228
alpar@1
   229
The statement \verb|s11| adds three columns to the problem object.
alpar@1
   230
alpar@1
   231
The statement \verb|s12| assigns the symbolic name `\verb|x1|' to the
alpar@1
   232
first column, the statement \verb|s13| sets the type and bounds of the
alpar@1
   233
first column, where \verb|GLP_LO| means that the column has an lower
alpar@1
   234
bound, and the statement \verb|s14| sets the objective coefficient for
alpar@1
   235
the first column. The statements \verb|s15|---\verb|s20| are used in the
alpar@1
   236
same way in order to assign the symbolic names `\verb|x2|' and
alpar@1
   237
`\verb|x3|' to the second and third columns and set their types, bounds,
alpar@1
   238
and objective coefficients.
alpar@1
   239
alpar@1
   240
The statements \verb|s21|---\verb|s29| prepare non-zero elements of the
alpar@1
   241
constraint matrix (i.e. constraint coefficients). Row indices of each
alpar@1
   242
element are stored in the array \verb|ia|, column indices are stored in
alpar@1
   243
the array \verb|ja|, and numerical values of corresponding elements are
alpar@1
   244
stored in the array \verb|ar|. Then the statement \verb|s30| calls
alpar@1
   245
the routine \verb|glp_load_matrix|, which loads information from these
alpar@1
   246
three arrays into the problem object.
alpar@1
   247
alpar@1
   248
Now all data have been entered into the problem object, and therefore
alpar@1
   249
the statement \verb|s31| calls the routine \verb|glp_simplex|, which is
alpar@1
   250
a driver to the simplex method, in order to solve the LP problem. This
alpar@1
   251
routine finds an optimal solution and stores all relevant information
alpar@1
   252
back into the problem object.
alpar@1
   253
alpar@1
   254
The statement \verb|s32| obtains a computed value of the objective
alpar@1
   255
function, and the statements \verb|s33|---\verb|s35| obtain computed
alpar@1
   256
values of structural variables (columns), which correspond to the
alpar@1
   257
optimal basic solution found by the solver.
alpar@1
   258
alpar@1
   259
The statement \verb|s36| writes the optimal solution to the standard
alpar@1
   260
output. The printout may look like follows:
alpar@1
   261
alpar@1
   262
{\footnotesize
alpar@1
   263
\begin{verbatim}
alpar@1
   264
*     0:   objval =   0.000000000e+00   infeas =   0.000000000e+00 (0)
alpar@1
   265
*     2:   objval =   7.333333333e+02   infeas =   0.000000000e+00 (0)
alpar@1
   266
OPTIMAL SOLUTION FOUND
alpar@1
   267
alpar@1
   268
z = 733.333; x1 = 33.3333; x2 = 66.6667; x3 = 0
alpar@1
   269
\end{verbatim}
alpar@1
   270
alpar@1
   271
}
alpar@1
   272
alpar@1
   273
Finally, the statement \verb|s37| calls the routine
alpar@1
   274
\verb|glp_delete_prob|, which frees all the memory allocated to the
alpar@1
   275
problem object.
alpar@1
   276
alpar@1
   277
\subsection{Compiling}
alpar@1
   278
alpar@1
   279
The GLPK package has the only header file \verb|glpk.h|, which should
alpar@1
   280
be available on compiling a C (or C++) program using GLPK API routines.
alpar@1
   281
alpar@1
   282
If the header file is installed in the default location
alpar@1
   283
\verb|/usr/local/include|, the following typical command may be used to
alpar@1
   284
compile, say, the example C program described above with the GNU C
alpar@1
   285
compiler:
alpar@1
   286
alpar@1
   287
\begin{verbatim}
alpar@1
   288
   $ gcc -c sample.c
alpar@1
   289
\end{verbatim}
alpar@1
   290
alpar@1
   291
If \verb|glpk.h| is not in the default location, the corresponding
alpar@1
   292
directory containing it should be made known to the C compiler through
alpar@1
   293
\verb|-I| option, for example:
alpar@1
   294
alpar@1
   295
\begin{verbatim}
alpar@1
   296
   $ gcc -I/foo/bar/glpk-4.15/include -c sample.c
alpar@1
   297
\end{verbatim}
alpar@1
   298
alpar@1
   299
In any case the compilation results in an object file \verb|sample.o|.
alpar@1
   300
alpar@1
   301
\subsection{Linking}
alpar@1
   302
alpar@1
   303
The GLPK library is a single file \verb|libglpk.a|. (On systems which
alpar@1
   304
support shared libraries there may be also a shared version of the
alpar@1
   305
library \verb|libglpk.so|.)
alpar@1
   306
alpar@1
   307
If the library is installed in the default
alpar@1
   308
location \verb|/usr/local/lib|, the following typical command may be
alpar@1
   309
used to link, say, the example C program described above against with
alpar@1
   310
the library:
alpar@1
   311
alpar@1
   312
\begin{verbatim}
alpar@1
   313
   $ gcc sample.o -lglpk -lm
alpar@1
   314
\end{verbatim}
alpar@1
   315
alpar@1
   316
If the GLPK library is not in the default location, the corresponding
alpar@1
   317
directory containing it should be made known to the linker through
alpar@1
   318
\verb|-L| option, for example:
alpar@1
   319
alpar@1
   320
\begin{verbatim}
alpar@1
   321
   $ gcc -L/foo/bar/glpk-4.15 sample.o -lglpk -lm
alpar@1
   322
\end{verbatim}
alpar@1
   323
alpar@1
   324
Depending on configuration of the package linking against with the GLPK
alpar@1
   325
library may require the following optional libraries:
alpar@1
   326
alpar@1
   327
\bigskip
alpar@1
   328
alpar@1
   329
\begin{tabular}{@{}ll}
alpar@1
   330
\verb|-lgmp|  & the GNU MP bignum library; \\
alpar@1
   331
\verb|-lz|    & the zlib data compression library; \\
alpar@1
   332
\verb|-lltdl| & the GNU ltdl shared support library. \\
alpar@1
   333
\end{tabular}
alpar@1
   334
alpar@1
   335
\bigskip
alpar@1
   336
alpar@1
   337
\noindent
alpar@1
   338
in which case corresponding libraries should be also made known to the
alpar@1
   339
linker, for example:
alpar@1
   340
alpar@1
   341
\begin{verbatim}
alpar@1
   342
   $ gcc sample.o -lglpk -lz -lltdl -lm
alpar@1
   343
\end{verbatim}
alpar@1
   344
alpar@1
   345
For more details about configuration options of the GLPK package see
alpar@1
   346
Appendix \ref{install}, page \pageref{install}.
alpar@1
   347
alpar@1
   348
%* eof *%