glpbfd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* glpbfd.c (LP basis factorization driver) */
  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. typedef struct BFD BFD;
  24. #define GLPBFD_PRIVATE
  25. #include "glpapi.h"
  26. #include "glpfhv.h"
  27. #include "glplpf.h"
  28. /* CAUTION: DO NOT CHANGE THE LIMIT BELOW */
  29. #define M_MAX 100000000 /* = 100*10^6 */
  30. /* maximal order of the basis matrix */
  31. struct BFD
  32. { /* LP basis factorization */
  33. int valid;
  34. /* factorization is valid only if this flag is set */
  35. int type;
  36. /* factorization type:
  37. GLP_BF_FT - LUF + Forrest-Tomlin
  38. GLP_BF_BG - LUF + Schur compl. + Bartels-Golub
  39. GLP_BF_GR - LUF + Schur compl. + Givens rotation */
  40. FHV *fhv;
  41. /* LP basis factorization (GLP_BF_FT) */
  42. LPF *lpf;
  43. /* LP basis factorization (GLP_BF_BG, GLP_BF_GR) */
  44. int lu_size; /* luf.sv_size */
  45. double piv_tol; /* luf.piv_tol */
  46. int piv_lim; /* luf.piv_lim */
  47. int suhl; /* luf.suhl */
  48. double eps_tol; /* luf.eps_tol */
  49. double max_gro; /* luf.max_gro */
  50. int nfs_max; /* fhv.hh_max */
  51. double upd_tol; /* fhv.upd_tol */
  52. int nrs_max; /* lpf.n_max */
  53. int rs_size; /* lpf.v_size */
  54. /* internal control parameters */
  55. int upd_lim;
  56. /* the factorization update limit */
  57. int upd_cnt;
  58. /* the factorization update count */
  59. };
  60. /***********************************************************************
  61. * NAME
  62. *
  63. * bfd_create_it - create LP basis factorization
  64. *
  65. * SYNOPSIS
  66. *
  67. * #include "glpbfd.h"
  68. * BFD *bfd_create_it(void);
  69. *
  70. * DESCRIPTION
  71. *
  72. * The routine bfd_create_it creates a program object, which represents
  73. * a factorization of LP basis.
  74. *
  75. * RETURNS
  76. *
  77. * The routine bfd_create_it returns a pointer to the object created. */
  78. BFD *bfd_create_it(void)
  79. { BFD *bfd;
  80. bfd = xmalloc(sizeof(BFD));
  81. bfd->valid = 0;
  82. bfd->type = GLP_BF_FT;
  83. bfd->fhv = NULL;
  84. bfd->lpf = NULL;
  85. bfd->lu_size = 0;
  86. bfd->piv_tol = 0.10;
  87. bfd->piv_lim = 4;
  88. bfd->suhl = 1;
  89. bfd->eps_tol = 1e-15;
  90. bfd->max_gro = 1e+10;
  91. bfd->nfs_max = 100;
  92. bfd->upd_tol = 1e-6;
  93. bfd->nrs_max = 100;
  94. bfd->rs_size = 1000;
  95. bfd->upd_lim = -1;
  96. bfd->upd_cnt = 0;
  97. return bfd;
  98. }
  99. /**********************************************************************/
  100. void bfd_set_parm(BFD *bfd, const void *_parm)
  101. { /* change LP basis factorization control parameters */
  102. const glp_bfcp *parm = _parm;
  103. xassert(bfd != NULL);
  104. bfd->type = parm->type;
  105. bfd->lu_size = parm->lu_size;
  106. bfd->piv_tol = parm->piv_tol;
  107. bfd->piv_lim = parm->piv_lim;
  108. bfd->suhl = parm->suhl;
  109. bfd->eps_tol = parm->eps_tol;
  110. bfd->max_gro = parm->max_gro;
  111. bfd->nfs_max = parm->nfs_max;
  112. bfd->upd_tol = parm->upd_tol;
  113. bfd->nrs_max = parm->nrs_max;
  114. bfd->rs_size = parm->rs_size;
  115. return;
  116. }
  117. /***********************************************************************
  118. * NAME
  119. *
  120. * bfd_factorize - compute LP basis factorization
  121. *
  122. * SYNOPSIS
  123. *
  124. * #include "glpbfd.h"
  125. * int bfd_factorize(BFD *bfd, int m, int bh[], int (*col)(void *info,
  126. * int j, int ind[], double val[]), void *info);
  127. *
  128. * DESCRIPTION
  129. *
  130. * The routine bfd_factorize computes the factorization of the basis
  131. * matrix B specified by the routine col.
  132. *
  133. * The parameter bfd specified the basis factorization data structure
  134. * created with the routine bfd_create_it.
  135. *
  136. * The parameter m specifies the order of B, m > 0.
  137. *
  138. * The array bh specifies the basis header: bh[j], 1 <= j <= m, is the
  139. * number of j-th column of B in some original matrix. The array bh is
  140. * optional and can be specified as NULL.
  141. *
  142. * The formal routine col specifies the matrix B to be factorized. To
  143. * obtain j-th column of A the routine bfd_factorize calls the routine
  144. * col with the parameter j (1 <= j <= n). In response the routine col
  145. * should store row indices and numerical values of non-zero elements
  146. * of j-th column of B to locations ind[1,...,len] and val[1,...,len],
  147. * respectively, where len is the number of non-zeros in j-th column
  148. * returned on exit. Neither zero nor duplicate elements are allowed.
  149. *
  150. * The parameter info is a transit pointer passed to the routine col.
  151. *
  152. * RETURNS
  153. *
  154. * 0 The factorization has been successfully computed.
  155. *
  156. * BFD_ESING
  157. * The specified matrix is singular within the working precision.
  158. *
  159. * BFD_ECOND
  160. * The specified matrix is ill-conditioned.
  161. *
  162. * For more details see comments to the routine luf_factorize. */
  163. int bfd_factorize(BFD *bfd, int m, const int bh[], int (*col)
  164. (void *info, int j, int ind[], double val[]), void *info)
  165. { LUF *luf;
  166. int nov, ret;
  167. xassert(bfd != NULL);
  168. xassert(1 <= m && m <= M_MAX);
  169. /* invalidate the factorization */
  170. bfd->valid = 0;
  171. /* create the factorization, if necessary */
  172. nov = 0;
  173. switch (bfd->type)
  174. { case GLP_BF_FT:
  175. if (bfd->lpf != NULL)
  176. lpf_delete_it(bfd->lpf), bfd->lpf = NULL;
  177. if (bfd->fhv == NULL)
  178. bfd->fhv = fhv_create_it(), nov = 1;
  179. break;
  180. case GLP_BF_BG:
  181. case GLP_BF_GR:
  182. if (bfd->fhv != NULL)
  183. fhv_delete_it(bfd->fhv), bfd->fhv = NULL;
  184. if (bfd->lpf == NULL)
  185. bfd->lpf = lpf_create_it(), nov = 1;
  186. break;
  187. default:
  188. xassert(bfd != bfd);
  189. }
  190. /* set control parameters specific to LUF */
  191. if (bfd->fhv != NULL)
  192. luf = bfd->fhv->luf;
  193. else if (bfd->lpf != NULL)
  194. luf = bfd->lpf->luf;
  195. else
  196. xassert(bfd != bfd);
  197. if (nov) luf->new_sva = bfd->lu_size;
  198. luf->piv_tol = bfd->piv_tol;
  199. luf->piv_lim = bfd->piv_lim;
  200. luf->suhl = bfd->suhl;
  201. luf->eps_tol = bfd->eps_tol;
  202. luf->max_gro = bfd->max_gro;
  203. /* set control parameters specific to FHV */
  204. if (bfd->fhv != NULL)
  205. { if (nov) bfd->fhv->hh_max = bfd->nfs_max;
  206. bfd->fhv->upd_tol = bfd->upd_tol;
  207. }
  208. /* set control parameters specific to LPF */
  209. if (bfd->lpf != NULL)
  210. { if (nov) bfd->lpf->n_max = bfd->nrs_max;
  211. if (nov) bfd->lpf->v_size = bfd->rs_size;
  212. }
  213. /* try to factorize the basis matrix */
  214. if (bfd->fhv != NULL)
  215. { switch (fhv_factorize(bfd->fhv, m, col, info))
  216. { case 0:
  217. break;
  218. case FHV_ESING:
  219. ret = BFD_ESING;
  220. goto done;
  221. case FHV_ECOND:
  222. ret = BFD_ECOND;
  223. goto done;
  224. default:
  225. xassert(bfd != bfd);
  226. }
  227. }
  228. else if (bfd->lpf != NULL)
  229. { switch (lpf_factorize(bfd->lpf, m, bh, col, info))
  230. { case 0:
  231. /* set the Schur complement update type */
  232. switch (bfd->type)
  233. { case GLP_BF_BG:
  234. /* Bartels-Golub update */
  235. bfd->lpf->scf->t_opt = SCF_TBG;
  236. break;
  237. case GLP_BF_GR:
  238. /* Givens rotation update */
  239. bfd->lpf->scf->t_opt = SCF_TGR;
  240. break;
  241. default:
  242. xassert(bfd != bfd);
  243. }
  244. break;
  245. case LPF_ESING:
  246. ret = BFD_ESING;
  247. goto done;
  248. case LPF_ECOND:
  249. ret = BFD_ECOND;
  250. goto done;
  251. default:
  252. xassert(bfd != bfd);
  253. }
  254. }
  255. else
  256. xassert(bfd != bfd);
  257. /* the basis matrix has been successfully factorized */
  258. bfd->valid = 1;
  259. bfd->upd_cnt = 0;
  260. ret = 0;
  261. done: /* return to the calling program */
  262. return ret;
  263. }
  264. /***********************************************************************
  265. * NAME
  266. *
  267. * bfd_ftran - perform forward transformation (solve system B*x = b)
  268. *
  269. * SYNOPSIS
  270. *
  271. * #include "glpbfd.h"
  272. * void bfd_ftran(BFD *bfd, double x[]);
  273. *
  274. * DESCRIPTION
  275. *
  276. * The routine bfd_ftran performs forward transformation, i.e. solves
  277. * the system B*x = b, where B is the basis matrix, x is the vector of
  278. * unknowns to be computed, b is the vector of right-hand sides.
  279. *
  280. * On entry elements of the vector b should be stored in dense format
  281. * in locations x[1], ..., x[m], where m is the number of rows. On exit
  282. * the routine stores elements of the vector x in the same locations. */
  283. void bfd_ftran(BFD *bfd, double x[])
  284. { xassert(bfd != NULL);
  285. xassert(bfd->valid);
  286. if (bfd->fhv != NULL)
  287. fhv_ftran(bfd->fhv, x);
  288. else if (bfd->lpf != NULL)
  289. lpf_ftran(bfd->lpf, x);
  290. else
  291. xassert(bfd != bfd);
  292. return;
  293. }
  294. /***********************************************************************
  295. * NAME
  296. *
  297. * bfd_btran - perform backward transformation (solve system B'*x = b)
  298. *
  299. * SYNOPSIS
  300. *
  301. * #include "glpbfd.h"
  302. * void bfd_btran(BFD *bfd, double x[]);
  303. *
  304. * DESCRIPTION
  305. *
  306. * The routine bfd_btran performs backward transformation, i.e. solves
  307. * the system B'*x = b, where B' is a matrix transposed to the basis
  308. * matrix B, x is the vector of unknowns to be computed, b is the vector
  309. * of right-hand sides.
  310. *
  311. * On entry elements of the vector b should be stored in dense format
  312. * in locations x[1], ..., x[m], where m is the number of rows. On exit
  313. * the routine stores elements of the vector x in the same locations. */
  314. void bfd_btran(BFD *bfd, double x[])
  315. { xassert(bfd != NULL);
  316. xassert(bfd->valid);
  317. if (bfd->fhv != NULL)
  318. fhv_btran(bfd->fhv, x);
  319. else if (bfd->lpf != NULL)
  320. lpf_btran(bfd->lpf, x);
  321. else
  322. xassert(bfd != bfd);
  323. return;
  324. }
  325. /***********************************************************************
  326. * NAME
  327. *
  328. * bfd_update_it - update LP basis factorization
  329. *
  330. * SYNOPSIS
  331. *
  332. * #include "glpbfd.h"
  333. * int bfd_update_it(BFD *bfd, int j, int bh, int len, const int ind[],
  334. * const double val[]);
  335. *
  336. * DESCRIPTION
  337. *
  338. * The routine bfd_update_it updates the factorization of the basis
  339. * matrix B after replacing its j-th column by a new vector.
  340. *
  341. * The parameter j specifies the number of column of B, which has been
  342. * replaced, 1 <= j <= m, where m is the order of B.
  343. *
  344. * The parameter bh specifies the basis header entry for the new column
  345. * of B, which is the number of the new column in some original matrix.
  346. * This parameter is optional and can be specified as 0.
  347. *
  348. * Row indices and numerical values of non-zero elements of the new
  349. * column of B should be placed in locations ind[1], ..., ind[len] and
  350. * val[1], ..., val[len], resp., where len is the number of non-zeros
  351. * in the column. Neither zero nor duplicate elements are allowed.
  352. *
  353. * RETURNS
  354. *
  355. * 0 The factorization has been successfully updated.
  356. *
  357. * BFD_ESING
  358. * New basis matrix is singular within the working precision.
  359. *
  360. * BFD_ECHECK
  361. * The factorization is inaccurate.
  362. *
  363. * BFD_ELIMIT
  364. * Factorization update limit has been reached.
  365. *
  366. * BFD_EROOM
  367. * Overflow of the sparse vector area.
  368. *
  369. * In case of non-zero return code the factorization becomes invalid.
  370. * It should not be used until it has been recomputed with the routine
  371. * bfd_factorize. */
  372. int bfd_update_it(BFD *bfd, int j, int bh, int len, const int ind[],
  373. const double val[])
  374. { int ret;
  375. xassert(bfd != NULL);
  376. xassert(bfd->valid);
  377. /* try to update the factorization */
  378. if (bfd->fhv != NULL)
  379. { switch (fhv_update_it(bfd->fhv, j, len, ind, val))
  380. { case 0:
  381. break;
  382. case FHV_ESING:
  383. bfd->valid = 0;
  384. ret = BFD_ESING;
  385. goto done;
  386. case FHV_ECHECK:
  387. bfd->valid = 0;
  388. ret = BFD_ECHECK;
  389. goto done;
  390. case FHV_ELIMIT:
  391. bfd->valid = 0;
  392. ret = BFD_ELIMIT;
  393. goto done;
  394. case FHV_EROOM:
  395. bfd->valid = 0;
  396. ret = BFD_EROOM;
  397. goto done;
  398. default:
  399. xassert(bfd != bfd);
  400. }
  401. }
  402. else if (bfd->lpf != NULL)
  403. { switch (lpf_update_it(bfd->lpf, j, bh, len, ind, val))
  404. { case 0:
  405. break;
  406. case LPF_ESING:
  407. bfd->valid = 0;
  408. ret = BFD_ESING;
  409. goto done;
  410. case LPF_ELIMIT:
  411. bfd->valid = 0;
  412. ret = BFD_ELIMIT;
  413. goto done;
  414. default:
  415. xassert(bfd != bfd);
  416. }
  417. }
  418. else
  419. xassert(bfd != bfd);
  420. /* the factorization has been successfully updated */
  421. /* increase the update count */
  422. bfd->upd_cnt++;
  423. ret = 0;
  424. done: /* return to the calling program */
  425. return ret;
  426. }
  427. /**********************************************************************/
  428. int bfd_get_count(BFD *bfd)
  429. { /* determine factorization update count */
  430. xassert(bfd != NULL);
  431. xassert(bfd->valid);
  432. return bfd->upd_cnt;
  433. }
  434. /***********************************************************************
  435. * NAME
  436. *
  437. * bfd_delete_it - delete LP basis factorization
  438. *
  439. * SYNOPSIS
  440. *
  441. * #include "glpbfd.h"
  442. * void bfd_delete_it(BFD *bfd);
  443. *
  444. * DESCRIPTION
  445. *
  446. * The routine bfd_delete_it deletes LP basis factorization specified
  447. * by the parameter fhv and frees all memory allocated to this program
  448. * object. */
  449. void bfd_delete_it(BFD *bfd)
  450. { xassert(bfd != NULL);
  451. if (bfd->fhv != NULL)
  452. fhv_delete_it(bfd->fhv);
  453. if (bfd->lpf != NULL)
  454. lpf_delete_it(bfd->lpf);
  455. xfree(bfd);
  456. return;
  457. }
  458. /* eof */