glpssx.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /* glpssx.h (simplex method, bignum arithmetic) */
  2. /***********************************************************************
  3. * This code is part of GLPK (GNU Linear Programming Kit).
  4. *
  5. * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
  6. * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
  7. * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
  8. * E-mail: <mao@gnu.org>.
  9. *
  10. * GLPK is free software: you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * GLPK is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  18. * License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************/
  23. #ifndef GLPSSX_H
  24. #define GLPSSX_H
  25. #include "glpbfx.h"
  26. #include "glpenv.h"
  27. typedef struct SSX SSX;
  28. struct SSX
  29. { /* simplex solver workspace */
  30. /*----------------------------------------------------------------------
  31. // LP PROBLEM DATA
  32. //
  33. // It is assumed that LP problem has the following statement:
  34. //
  35. // minimize (or maximize)
  36. //
  37. // z = c[1]*x[1] + ... + c[m+n]*x[m+n] + c[0] (1)
  38. //
  39. // subject to equality constraints
  40. //
  41. // x[1] - a[1,1]*x[m+1] - ... - a[1,n]*x[m+n] = 0
  42. //
  43. // . . . . . . . (2)
  44. //
  45. // x[m] - a[m,1]*x[m+1] + ... - a[m,n]*x[m+n] = 0
  46. //
  47. // and bounds of variables
  48. //
  49. // l[1] <= x[1] <= u[1]
  50. //
  51. // . . . . . . . (3)
  52. //
  53. // l[m+n] <= x[m+n] <= u[m+n]
  54. //
  55. // where:
  56. // x[1], ..., x[m] - auxiliary variables;
  57. // x[m+1], ..., x[m+n] - structural variables;
  58. // z - objective function;
  59. // c[1], ..., c[m+n] - coefficients of the objective function;
  60. // c[0] - constant term of the objective function;
  61. // a[1,1], ..., a[m,n] - constraint coefficients;
  62. // l[1], ..., l[m+n] - lower bounds of variables;
  63. // u[1], ..., u[m+n] - upper bounds of variables.
  64. //
  65. // Bounds of variables can be finite as well as inifinite. Besides,
  66. // lower and upper bounds can be equal to each other. So the following
  67. // five types of variables are possible:
  68. //
  69. // Bounds of variable Type of variable
  70. // -------------------------------------------------
  71. // -inf < x[k] < +inf Free (unbounded) variable
  72. // l[k] <= x[k] < +inf Variable with lower bound
  73. // -inf < x[k] <= u[k] Variable with upper bound
  74. // l[k] <= x[k] <= u[k] Double-bounded variable
  75. // l[k] = x[k] = u[k] Fixed variable
  76. //
  77. // Using vector-matrix notations the LP problem (1)-(3) can be written
  78. // as follows:
  79. //
  80. // minimize (or maximize)
  81. //
  82. // z = c * x + c[0] (4)
  83. //
  84. // subject to equality constraints
  85. //
  86. // xR - A * xS = 0 (5)
  87. //
  88. // and bounds of variables
  89. //
  90. // l <= x <= u (6)
  91. //
  92. // where:
  93. // xR - vector of auxiliary variables;
  94. // xS - vector of structural variables;
  95. // x = (xR, xS) - vector of all variables;
  96. // z - objective function;
  97. // c - vector of objective coefficients;
  98. // c[0] - constant term of the objective function;
  99. // A - matrix of constraint coefficients (has m rows
  100. // and n columns);
  101. // l - vector of lower bounds of variables;
  102. // u - vector of upper bounds of variables.
  103. //
  104. // The simplex method makes no difference between auxiliary and
  105. // structural variables, so it is convenient to think the system of
  106. // equality constraints (5) written in a homogeneous form:
  107. //
  108. // (I | -A) * x = 0, (7)
  109. //
  110. // where (I | -A) is an augmented (m+n)xm constraint matrix, I is mxm
  111. // unity matrix whose columns correspond to auxiliary variables, and A
  112. // is the original mxn constraint matrix whose columns correspond to
  113. // structural variables. Note that only the matrix A is stored.
  114. ----------------------------------------------------------------------*/
  115. int m;
  116. /* number of rows (auxiliary variables), m > 0 */
  117. int n;
  118. /* number of columns (structural variables), n > 0 */
  119. int *type; /* int type[1+m+n]; */
  120. /* type[0] is not used;
  121. type[k], 1 <= k <= m+n, is the type of variable x[k]: */
  122. #define SSX_FR 0 /* free (unbounded) variable */
  123. #define SSX_LO 1 /* variable with lower bound */
  124. #define SSX_UP 2 /* variable with upper bound */
  125. #define SSX_DB 3 /* double-bounded variable */
  126. #define SSX_FX 4 /* fixed variable */
  127. mpq_t *lb; /* mpq_t lb[1+m+n]; alias: l */
  128. /* lb[0] is not used;
  129. lb[k], 1 <= k <= m+n, is an lower bound of variable x[k];
  130. if x[k] has no lower bound, lb[k] is zero */
  131. mpq_t *ub; /* mpq_t ub[1+m+n]; alias: u */
  132. /* ub[0] is not used;
  133. ub[k], 1 <= k <= m+n, is an upper bound of variable x[k];
  134. if x[k] has no upper bound, ub[k] is zero;
  135. if x[k] is of fixed type, ub[k] is equal to lb[k] */
  136. int dir;
  137. /* optimization direction (sense of the objective function): */
  138. #define SSX_MIN 0 /* minimization */
  139. #define SSX_MAX 1 /* maximization */
  140. mpq_t *coef; /* mpq_t coef[1+m+n]; alias: c */
  141. /* coef[0] is a constant term of the objective function;
  142. coef[k], 1 <= k <= m+n, is a coefficient of the objective
  143. function at variable x[k];
  144. note that auxiliary variables also may have non-zero objective
  145. coefficients */
  146. int *A_ptr; /* int A_ptr[1+n+1]; */
  147. int *A_ind; /* int A_ind[A_ptr[n+1]]; */
  148. mpq_t *A_val; /* mpq_t A_val[A_ptr[n+1]]; */
  149. /* constraint matrix A (see (5)) in storage-by-columns format */
  150. /*----------------------------------------------------------------------
  151. // LP BASIS AND CURRENT BASIC SOLUTION
  152. //
  153. // The LP basis is defined by the following partition of the augmented
  154. // constraint matrix (7):
  155. //
  156. // (B | N) = (I | -A) * Q, (8)
  157. //
  158. // where B is a mxm non-singular basis matrix whose columns correspond
  159. // to basic variables xB, N is a mxn matrix whose columns correspond to
  160. // non-basic variables xN, and Q is a permutation (m+n)x(m+n) matrix.
  161. //
  162. // From (7) and (8) it follows that
  163. //
  164. // (I | -A) * x = (I | -A) * Q * Q' * x = (B | N) * (xB, xN),
  165. //
  166. // therefore
  167. //
  168. // (xB, xN) = Q' * x, (9)
  169. //
  170. // where x is the vector of all variables in the original order, xB is
  171. // a vector of basic variables, xN is a vector of non-basic variables,
  172. // Q' = inv(Q) is a matrix transposed to Q.
  173. //
  174. // Current values of non-basic variables xN[j], j = 1, ..., n, are not
  175. // stored; they are defined implicitly by their statuses as follows:
  176. //
  177. // 0, if xN[j] is free variable
  178. // lN[j], if xN[j] is on its lower bound (10)
  179. // uN[j], if xN[j] is on its upper bound
  180. // lN[j] = uN[j], if xN[j] is fixed variable
  181. //
  182. // where lN[j] and uN[j] are lower and upper bounds of xN[j].
  183. //
  184. // Current values of basic variables xB[i], i = 1, ..., m, are computed
  185. // as follows:
  186. //
  187. // beta = - inv(B) * N * xN, (11)
  188. //
  189. // where current values of xN are defined by (10).
  190. //
  191. // Current values of simplex multipliers pi[i], i = 1, ..., m (which
  192. // are values of Lagrange multipliers for equality constraints (7) also
  193. // called shadow prices) are computed as follows:
  194. //
  195. // pi = inv(B') * cB, (12)
  196. //
  197. // where B' is a matrix transposed to B, cB is a vector of objective
  198. // coefficients at basic variables xB.
  199. //
  200. // Current values of reduced costs d[j], j = 1, ..., n, (which are
  201. // values of Langrange multipliers for active inequality constraints
  202. // corresponding to non-basic variables) are computed as follows:
  203. //
  204. // d = cN - N' * pi, (13)
  205. //
  206. // where N' is a matrix transposed to N, cN is a vector of objective
  207. // coefficients at non-basic variables xN.
  208. ----------------------------------------------------------------------*/
  209. int *stat; /* int stat[1+m+n]; */
  210. /* stat[0] is not used;
  211. stat[k], 1 <= k <= m+n, is the status of variable x[k]: */
  212. #define SSX_BS 0 /* basic variable */
  213. #define SSX_NL 1 /* non-basic variable on lower bound */
  214. #define SSX_NU 2 /* non-basic variable on upper bound */
  215. #define SSX_NF 3 /* non-basic free variable */
  216. #define SSX_NS 4 /* non-basic fixed variable */
  217. int *Q_row; /* int Q_row[1+m+n]; */
  218. /* matrix Q in row-like format;
  219. Q_row[0] is not used;
  220. Q_row[i] = j means that q[i,j] = 1 */
  221. int *Q_col; /* int Q_col[1+m+n]; */
  222. /* matrix Q in column-like format;
  223. Q_col[0] is not used;
  224. Q_col[j] = i means that q[i,j] = 1 */
  225. /* if k-th column of the matrix (I | A) is k'-th column of the
  226. matrix (B | N), then Q_row[k] = k' and Q_col[k'] = k;
  227. if x[k] is xB[i], then Q_row[k] = i and Q_col[i] = k;
  228. if x[k] is xN[j], then Q_row[k] = m+j and Q_col[m+j] = k */
  229. BFX *binv;
  230. /* invertable form of the basis matrix B */
  231. mpq_t *bbar; /* mpq_t bbar[1+m]; alias: beta */
  232. /* bbar[0] is a value of the objective function;
  233. bbar[i], 1 <= i <= m, is a value of basic variable xB[i] */
  234. mpq_t *pi; /* mpq_t pi[1+m]; */
  235. /* pi[0] is not used;
  236. pi[i], 1 <= i <= m, is a simplex multiplier corresponding to
  237. i-th row (equality constraint) */
  238. mpq_t *cbar; /* mpq_t cbar[1+n]; alias: d */
  239. /* cbar[0] is not used;
  240. cbar[j], 1 <= j <= n, is a reduced cost of non-basic variable
  241. xN[j] */
  242. /*----------------------------------------------------------------------
  243. // SIMPLEX TABLE
  244. //
  245. // Due to (8) and (9) the system of equality constraints (7) for the
  246. // current basis can be written as follows:
  247. //
  248. // xB = A~ * xN, (14)
  249. //
  250. // where
  251. //
  252. // A~ = - inv(B) * N (15)
  253. //
  254. // is a mxn matrix called the simplex table.
  255. //
  256. // The revised simplex method uses only two components of A~, namely,
  257. // pivot column corresponding to non-basic variable xN[q] chosen to
  258. // enter the basis, and pivot row corresponding to basic variable xB[p]
  259. // chosen to leave the basis.
  260. //
  261. // Pivot column alfa_q is q-th column of A~, so
  262. //
  263. // alfa_q = A~ * e[q] = - inv(B) * N * e[q] = - inv(B) * N[q], (16)
  264. //
  265. // where N[q] is q-th column of the matrix N.
  266. //
  267. // Pivot row alfa_p is p-th row of A~ or, equivalently, p-th column of
  268. // A~', a matrix transposed to A~, so
  269. //
  270. // alfa_p = A~' * e[p] = - N' * inv(B') * e[p] = - N' * rho_p, (17)
  271. //
  272. // where (*)' means transposition, and
  273. //
  274. // rho_p = inv(B') * e[p], (18)
  275. //
  276. // is p-th column of inv(B') or, that is the same, p-th row of inv(B).
  277. ----------------------------------------------------------------------*/
  278. int p;
  279. /* number of basic variable xB[p], 1 <= p <= m, chosen to leave
  280. the basis */
  281. mpq_t *rho; /* mpq_t rho[1+m]; */
  282. /* p-th row of the inverse inv(B); see (18) */
  283. mpq_t *ap; /* mpq_t ap[1+n]; */
  284. /* p-th row of the simplex table; see (17) */
  285. int q;
  286. /* number of non-basic variable xN[q], 1 <= q <= n, chosen to
  287. enter the basis */
  288. mpq_t *aq; /* mpq_t aq[1+m]; */
  289. /* q-th column of the simplex table; see (16) */
  290. /*--------------------------------------------------------------------*/
  291. int q_dir;
  292. /* direction in which non-basic variable xN[q] should change on
  293. moving to the adjacent vertex of the polyhedron:
  294. +1 means that xN[q] increases
  295. -1 means that xN[q] decreases */
  296. int p_stat;
  297. /* non-basic status which should be assigned to basic variable
  298. xB[p] when it has left the basis and become xN[q] */
  299. mpq_t delta;
  300. /* actual change of xN[q] in the adjacent basis (it has the same
  301. sign as q_dir) */
  302. /*--------------------------------------------------------------------*/
  303. int it_lim;
  304. /* simplex iterations limit; if this value is positive, it is
  305. decreased by one each time when one simplex iteration has been
  306. performed, and reaching zero value signals the solver to stop
  307. the search; negative value means no iterations limit */
  308. int it_cnt;
  309. /* simplex iterations count; this count is increased by one each
  310. time when one simplex iteration has been performed */
  311. double tm_lim;
  312. /* searching time limit, in seconds; if this value is positive,
  313. it is decreased each time when one simplex iteration has been
  314. performed by the amount of time spent for the iteration, and
  315. reaching zero value signals the solver to stop the search;
  316. negative value means no time limit */
  317. double out_frq;
  318. /* output frequency, in seconds; this parameter specifies how
  319. frequently the solver sends information about the progress of
  320. the search to the standard output */
  321. glp_long tm_beg;
  322. /* starting time of the search, in seconds; the total time of the
  323. search is the difference between xtime() and tm_beg */
  324. glp_long tm_lag;
  325. /* the most recent time, in seconds, at which the progress of the
  326. the search was displayed */
  327. };
  328. #define ssx_create _glp_ssx_create
  329. #define ssx_factorize _glp_ssx_factorize
  330. #define ssx_get_xNj _glp_ssx_get_xNj
  331. #define ssx_eval_bbar _glp_ssx_eval_bbar
  332. #define ssx_eval_pi _glp_ssx_eval_pi
  333. #define ssx_eval_dj _glp_ssx_eval_dj
  334. #define ssx_eval_cbar _glp_ssx_eval_cbar
  335. #define ssx_eval_rho _glp_ssx_eval_rho
  336. #define ssx_eval_row _glp_ssx_eval_row
  337. #define ssx_eval_col _glp_ssx_eval_col
  338. #define ssx_chuzc _glp_ssx_chuzc
  339. #define ssx_chuzr _glp_ssx_chuzr
  340. #define ssx_update_bbar _glp_ssx_update_bbar
  341. #define ssx_update_pi _glp_ssx_update_pi
  342. #define ssx_update_cbar _glp_ssx_update_cbar
  343. #define ssx_change_basis _glp_ssx_change_basis
  344. #define ssx_delete _glp_ssx_delete
  345. #define ssx_phase_I _glp_ssx_phase_I
  346. #define ssx_phase_II _glp_ssx_phase_II
  347. #define ssx_driver _glp_ssx_driver
  348. SSX *ssx_create(int m, int n, int nnz);
  349. /* create simplex solver workspace */
  350. int ssx_factorize(SSX *ssx);
  351. /* factorize the current basis matrix */
  352. void ssx_get_xNj(SSX *ssx, int j, mpq_t x);
  353. /* determine value of non-basic variable */
  354. void ssx_eval_bbar(SSX *ssx);
  355. /* compute values of basic variables */
  356. void ssx_eval_pi(SSX *ssx);
  357. /* compute values of simplex multipliers */
  358. void ssx_eval_dj(SSX *ssx, int j, mpq_t dj);
  359. /* compute reduced cost of non-basic variable */
  360. void ssx_eval_cbar(SSX *ssx);
  361. /* compute reduced costs of all non-basic variables */
  362. void ssx_eval_rho(SSX *ssx);
  363. /* compute p-th row of the inverse */
  364. void ssx_eval_row(SSX *ssx);
  365. /* compute pivot row of the simplex table */
  366. void ssx_eval_col(SSX *ssx);
  367. /* compute pivot column of the simplex table */
  368. void ssx_chuzc(SSX *ssx);
  369. /* choose pivot column */
  370. void ssx_chuzr(SSX *ssx);
  371. /* choose pivot row */
  372. void ssx_update_bbar(SSX *ssx);
  373. /* update values of basic variables */
  374. void ssx_update_pi(SSX *ssx);
  375. /* update simplex multipliers */
  376. void ssx_update_cbar(SSX *ssx);
  377. /* update reduced costs of non-basic variables */
  378. void ssx_change_basis(SSX *ssx);
  379. /* change current basis to adjacent one */
  380. void ssx_delete(SSX *ssx);
  381. /* delete simplex solver workspace */
  382. int ssx_phase_I(SSX *ssx);
  383. /* find primal feasible solution */
  384. int ssx_phase_II(SSX *ssx);
  385. /* find optimal solution */
  386. int ssx_driver(SSX *ssx);
  387. /* base driver to exact simplex method */
  388. #endif
  389. /* eof */