You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

343 lines
12 KiB

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