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.

478 lines
16 KiB

  1. /* glpscl.c (problem scaling routines) */
  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, 2011, 2013 Andrew Makhorin, Department for Applied
  7. * Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
  8. * reserved. 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. #include "env.h"
  24. #include "misc.h"
  25. #include "prob.h"
  26. /***********************************************************************
  27. * min_row_aij - determine minimal |a[i,j]| in i-th row
  28. *
  29. * This routine returns minimal magnitude of (non-zero) constraint
  30. * coefficients in i-th row of the constraint matrix.
  31. *
  32. * If the parameter scaled is zero, the original constraint matrix A is
  33. * assumed. Otherwise, the scaled constraint matrix R*A*S is assumed.
  34. *
  35. * If i-th row of the matrix is empty, the routine returns 1. */
  36. static double min_row_aij(glp_prob *lp, int i, int scaled)
  37. { GLPAIJ *aij;
  38. double min_aij, temp;
  39. xassert(1 <= i && i <= lp->m);
  40. min_aij = 1.0;
  41. for (aij = lp->row[i]->ptr; aij != NULL; aij = aij->r_next)
  42. { temp = fabs(aij->val);
  43. if (scaled) temp *= (aij->row->rii * aij->col->sjj);
  44. if (aij->r_prev == NULL || min_aij > temp)
  45. min_aij = temp;
  46. }
  47. return min_aij;
  48. }
  49. /***********************************************************************
  50. * max_row_aij - determine maximal |a[i,j]| in i-th row
  51. *
  52. * This routine returns maximal magnitude of (non-zero) constraint
  53. * coefficients in i-th row of the constraint matrix.
  54. *
  55. * If the parameter scaled is zero, the original constraint matrix A is
  56. * assumed. Otherwise, the scaled constraint matrix R*A*S is assumed.
  57. *
  58. * If i-th row of the matrix is empty, the routine returns 1. */
  59. static double max_row_aij(glp_prob *lp, int i, int scaled)
  60. { GLPAIJ *aij;
  61. double max_aij, temp;
  62. xassert(1 <= i && i <= lp->m);
  63. max_aij = 1.0;
  64. for (aij = lp->row[i]->ptr; aij != NULL; aij = aij->r_next)
  65. { temp = fabs(aij->val);
  66. if (scaled) temp *= (aij->row->rii * aij->col->sjj);
  67. if (aij->r_prev == NULL || max_aij < temp)
  68. max_aij = temp;
  69. }
  70. return max_aij;
  71. }
  72. /***********************************************************************
  73. * min_col_aij - determine minimal |a[i,j]| in j-th column
  74. *
  75. * This routine returns minimal magnitude of (non-zero) constraint
  76. * coefficients in j-th column of the constraint matrix.
  77. *
  78. * If the parameter scaled is zero, the original constraint matrix A is
  79. * assumed. Otherwise, the scaled constraint matrix R*A*S is assumed.
  80. *
  81. * If j-th column of the matrix is empty, the routine returns 1. */
  82. static double min_col_aij(glp_prob *lp, int j, int scaled)
  83. { GLPAIJ *aij;
  84. double min_aij, temp;
  85. xassert(1 <= j && j <= lp->n);
  86. min_aij = 1.0;
  87. for (aij = lp->col[j]->ptr; aij != NULL; aij = aij->c_next)
  88. { temp = fabs(aij->val);
  89. if (scaled) temp *= (aij->row->rii * aij->col->sjj);
  90. if (aij->c_prev == NULL || min_aij > temp)
  91. min_aij = temp;
  92. }
  93. return min_aij;
  94. }
  95. /***********************************************************************
  96. * max_col_aij - determine maximal |a[i,j]| in j-th column
  97. *
  98. * This routine returns maximal magnitude of (non-zero) constraint
  99. * coefficients in j-th column of the constraint matrix.
  100. *
  101. * If the parameter scaled is zero, the original constraint matrix A is
  102. * assumed. Otherwise, the scaled constraint matrix R*A*S is assumed.
  103. *
  104. * If j-th column of the matrix is empty, the routine returns 1. */
  105. static double max_col_aij(glp_prob *lp, int j, int scaled)
  106. { GLPAIJ *aij;
  107. double max_aij, temp;
  108. xassert(1 <= j && j <= lp->n);
  109. max_aij = 1.0;
  110. for (aij = lp->col[j]->ptr; aij != NULL; aij = aij->c_next)
  111. { temp = fabs(aij->val);
  112. if (scaled) temp *= (aij->row->rii * aij->col->sjj);
  113. if (aij->c_prev == NULL || max_aij < temp)
  114. max_aij = temp;
  115. }
  116. return max_aij;
  117. }
  118. /***********************************************************************
  119. * min_mat_aij - determine minimal |a[i,j]| in constraint matrix
  120. *
  121. * This routine returns minimal magnitude of (non-zero) constraint
  122. * coefficients in the constraint matrix.
  123. *
  124. * If the parameter scaled is zero, the original constraint matrix A is
  125. * assumed. Otherwise, the scaled constraint matrix R*A*S is assumed.
  126. *
  127. * If the matrix is empty, the routine returns 1. */
  128. static double min_mat_aij(glp_prob *lp, int scaled)
  129. { int i;
  130. double min_aij, temp;
  131. min_aij = 1.0;
  132. for (i = 1; i <= lp->m; i++)
  133. { temp = min_row_aij(lp, i, scaled);
  134. if (i == 1 || min_aij > temp)
  135. min_aij = temp;
  136. }
  137. return min_aij;
  138. }
  139. /***********************************************************************
  140. * max_mat_aij - determine maximal |a[i,j]| in constraint matrix
  141. *
  142. * This routine returns maximal magnitude of (non-zero) constraint
  143. * coefficients in the constraint matrix.
  144. *
  145. * If the parameter scaled is zero, the original constraint matrix A is
  146. * assumed. Otherwise, the scaled constraint matrix R*A*S is assumed.
  147. *
  148. * If the matrix is empty, the routine returns 1. */
  149. static double max_mat_aij(glp_prob *lp, int scaled)
  150. { int i;
  151. double max_aij, temp;
  152. max_aij = 1.0;
  153. for (i = 1; i <= lp->m; i++)
  154. { temp = max_row_aij(lp, i, scaled);
  155. if (i == 1 || max_aij < temp)
  156. max_aij = temp;
  157. }
  158. return max_aij;
  159. }
  160. /***********************************************************************
  161. * eq_scaling - perform equilibration scaling
  162. *
  163. * This routine performs equilibration scaling of rows and columns of
  164. * the constraint matrix.
  165. *
  166. * If the parameter flag is zero, the routine scales rows at first and
  167. * then columns. Otherwise, the routine scales columns and then rows.
  168. *
  169. * Rows are scaled as follows:
  170. *
  171. * n
  172. * a'[i,j] = a[i,j] / max |a[i,j]|, i = 1,...,m.
  173. * j=1
  174. *
  175. * This makes the infinity (maximum) norm of each row of the matrix
  176. * equal to 1.
  177. *
  178. * Columns are scaled as follows:
  179. *
  180. * m
  181. * a'[i,j] = a[i,j] / max |a[i,j]|, j = 1,...,n.
  182. * i=1
  183. *
  184. * This makes the infinity (maximum) norm of each column of the matrix
  185. * equal to 1. */
  186. static void eq_scaling(glp_prob *lp, int flag)
  187. { int i, j, pass;
  188. double temp;
  189. xassert(flag == 0 || flag == 1);
  190. for (pass = 0; pass <= 1; pass++)
  191. { if (pass == flag)
  192. { /* scale rows */
  193. for (i = 1; i <= lp->m; i++)
  194. { temp = max_row_aij(lp, i, 1);
  195. glp_set_rii(lp, i, glp_get_rii(lp, i) / temp);
  196. }
  197. }
  198. else
  199. { /* scale columns */
  200. for (j = 1; j <= lp->n; j++)
  201. { temp = max_col_aij(lp, j, 1);
  202. glp_set_sjj(lp, j, glp_get_sjj(lp, j) / temp);
  203. }
  204. }
  205. }
  206. return;
  207. }
  208. /***********************************************************************
  209. * gm_scaling - perform geometric mean scaling
  210. *
  211. * This routine performs geometric mean scaling of rows and columns of
  212. * the constraint matrix.
  213. *
  214. * If the parameter flag is zero, the routine scales rows at first and
  215. * then columns. Otherwise, the routine scales columns and then rows.
  216. *
  217. * Rows are scaled as follows:
  218. *
  219. * a'[i,j] = a[i,j] / sqrt(alfa[i] * beta[i]), i = 1,...,m,
  220. *
  221. * where:
  222. * n n
  223. * alfa[i] = min |a[i,j]|, beta[i] = max |a[i,j]|.
  224. * j=1 j=1
  225. *
  226. * This allows decreasing the ratio beta[i] / alfa[i] for each row of
  227. * the matrix.
  228. *
  229. * Columns are scaled as follows:
  230. *
  231. * a'[i,j] = a[i,j] / sqrt(alfa[j] * beta[j]), j = 1,...,n,
  232. *
  233. * where:
  234. * m m
  235. * alfa[j] = min |a[i,j]|, beta[j] = max |a[i,j]|.
  236. * i=1 i=1
  237. *
  238. * This allows decreasing the ratio beta[j] / alfa[j] for each column
  239. * of the matrix. */
  240. static void gm_scaling(glp_prob *lp, int flag)
  241. { int i, j, pass;
  242. double temp;
  243. xassert(flag == 0 || flag == 1);
  244. for (pass = 0; pass <= 1; pass++)
  245. { if (pass == flag)
  246. { /* scale rows */
  247. for (i = 1; i <= lp->m; i++)
  248. { temp = min_row_aij(lp, i, 1) * max_row_aij(lp, i, 1);
  249. glp_set_rii(lp, i, glp_get_rii(lp, i) / sqrt(temp));
  250. }
  251. }
  252. else
  253. { /* scale columns */
  254. for (j = 1; j <= lp->n; j++)
  255. { temp = min_col_aij(lp, j, 1) * max_col_aij(lp, j, 1);
  256. glp_set_sjj(lp, j, glp_get_sjj(lp, j) / sqrt(temp));
  257. }
  258. }
  259. }
  260. return;
  261. }
  262. /***********************************************************************
  263. * max_row_ratio - determine worst scaling "quality" for rows
  264. *
  265. * This routine returns the worst scaling "quality" for rows of the
  266. * currently scaled constraint matrix:
  267. *
  268. * m
  269. * ratio = max ratio[i],
  270. * i=1
  271. * where:
  272. * n n
  273. * ratio[i] = max |a[i,j]| / min |a[i,j]|, 1 <= i <= m,
  274. * j=1 j=1
  275. *
  276. * is the scaling "quality" of i-th row. */
  277. static double max_row_ratio(glp_prob *lp)
  278. { int i;
  279. double ratio, temp;
  280. ratio = 1.0;
  281. for (i = 1; i <= lp->m; i++)
  282. { temp = max_row_aij(lp, i, 1) / min_row_aij(lp, i, 1);
  283. if (i == 1 || ratio < temp) ratio = temp;
  284. }
  285. return ratio;
  286. }
  287. /***********************************************************************
  288. * max_col_ratio - determine worst scaling "quality" for columns
  289. *
  290. * This routine returns the worst scaling "quality" for columns of the
  291. * currently scaled constraint matrix:
  292. *
  293. * n
  294. * ratio = max ratio[j],
  295. * j=1
  296. * where:
  297. * m m
  298. * ratio[j] = max |a[i,j]| / min |a[i,j]|, 1 <= j <= n,
  299. * i=1 i=1
  300. *
  301. * is the scaling "quality" of j-th column. */
  302. static double max_col_ratio(glp_prob *lp)
  303. { int j;
  304. double ratio, temp;
  305. ratio = 1.0;
  306. for (j = 1; j <= lp->n; j++)
  307. { temp = max_col_aij(lp, j, 1) / min_col_aij(lp, j, 1);
  308. if (j == 1 || ratio < temp) ratio = temp;
  309. }
  310. return ratio;
  311. }
  312. /***********************************************************************
  313. * gm_iterate - perform iterative geometric mean scaling
  314. *
  315. * This routine performs iterative geometric mean scaling of rows and
  316. * columns of the constraint matrix.
  317. *
  318. * The parameter it_max specifies the maximal number of iterations.
  319. * Recommended value of it_max is 15.
  320. *
  321. * The parameter tau specifies a minimal improvement of the scaling
  322. * "quality" on each iteration, 0 < tau < 1. It means than the scaling
  323. * process continues while the following condition is satisfied:
  324. *
  325. * ratio[k] <= tau * ratio[k-1],
  326. *
  327. * where ratio = max |a[i,j]| / min |a[i,j]| is the scaling "quality"
  328. * to be minimized, k is the iteration number. Recommended value of tau
  329. * is 0.90. */
  330. static void gm_iterate(glp_prob *lp, int it_max, double tau)
  331. { int k, flag;
  332. double ratio = 0.0, r_old;
  333. /* if the scaling "quality" for rows is better than for columns,
  334. the rows are scaled first; otherwise, the columns are scaled
  335. first */
  336. flag = (max_row_ratio(lp) > max_col_ratio(lp));
  337. for (k = 1; k <= it_max; k++)
  338. { /* save the scaling "quality" from previous iteration */
  339. r_old = ratio;
  340. /* determine the current scaling "quality" */
  341. ratio = max_mat_aij(lp, 1) / min_mat_aij(lp, 1);
  342. #if 0
  343. xprintf("k = %d; ratio = %g\n", k, ratio);
  344. #endif
  345. /* if improvement is not enough, terminate scaling */
  346. if (k > 1 && ratio > tau * r_old) break;
  347. /* otherwise, perform another iteration */
  348. gm_scaling(lp, flag);
  349. }
  350. return;
  351. }
  352. /***********************************************************************
  353. * NAME
  354. *
  355. * scale_prob - scale problem data
  356. *
  357. * SYNOPSIS
  358. *
  359. * #include "glpscl.h"
  360. * void scale_prob(glp_prob *lp, int flags);
  361. *
  362. * DESCRIPTION
  363. *
  364. * The routine scale_prob performs automatic scaling of problem data
  365. * for the specified problem object. */
  366. static void scale_prob(glp_prob *lp, int flags)
  367. { static const char *fmt =
  368. "%s: min|aij| = %10.3e max|aij| = %10.3e ratio = %10.3e\n";
  369. double min_aij, max_aij, ratio;
  370. xprintf("Scaling...\n");
  371. /* cancel the current scaling effect */
  372. glp_unscale_prob(lp);
  373. /* report original scaling "quality" */
  374. min_aij = min_mat_aij(lp, 1);
  375. max_aij = max_mat_aij(lp, 1);
  376. ratio = max_aij / min_aij;
  377. xprintf(fmt, " A", min_aij, max_aij, ratio);
  378. /* check if the problem is well scaled */
  379. if (min_aij >= 0.10 && max_aij <= 10.0)
  380. { xprintf("Problem data seem to be well scaled\n");
  381. /* skip scaling, if required */
  382. if (flags & GLP_SF_SKIP) goto done;
  383. }
  384. /* perform iterative geometric mean scaling, if required */
  385. if (flags & GLP_SF_GM)
  386. { gm_iterate(lp, 15, 0.90);
  387. min_aij = min_mat_aij(lp, 1);
  388. max_aij = max_mat_aij(lp, 1);
  389. ratio = max_aij / min_aij;
  390. xprintf(fmt, "GM", min_aij, max_aij, ratio);
  391. }
  392. /* perform equilibration scaling, if required */
  393. if (flags & GLP_SF_EQ)
  394. { eq_scaling(lp, max_row_ratio(lp) > max_col_ratio(lp));
  395. min_aij = min_mat_aij(lp, 1);
  396. max_aij = max_mat_aij(lp, 1);
  397. ratio = max_aij / min_aij;
  398. xprintf(fmt, "EQ", min_aij, max_aij, ratio);
  399. }
  400. /* round scale factors to nearest power of two, if required */
  401. if (flags & GLP_SF_2N)
  402. { int i, j;
  403. for (i = 1; i <= lp->m; i++)
  404. glp_set_rii(lp, i, round2n(glp_get_rii(lp, i)));
  405. for (j = 1; j <= lp->n; j++)
  406. glp_set_sjj(lp, j, round2n(glp_get_sjj(lp, j)));
  407. min_aij = min_mat_aij(lp, 1);
  408. max_aij = max_mat_aij(lp, 1);
  409. ratio = max_aij / min_aij;
  410. xprintf(fmt, "2N", min_aij, max_aij, ratio);
  411. }
  412. done: return;
  413. }
  414. /***********************************************************************
  415. * NAME
  416. *
  417. * glp_scale_prob - scale problem data
  418. *
  419. * SYNOPSIS
  420. *
  421. * void glp_scale_prob(glp_prob *lp, int flags);
  422. *
  423. * DESCRIPTION
  424. *
  425. * The routine glp_scale_prob performs automatic scaling of problem
  426. * data for the specified problem object.
  427. *
  428. * The parameter flags specifies scaling options used by the routine.
  429. * Options can be combined with the bitwise OR operator and may be the
  430. * following:
  431. *
  432. * GLP_SF_GM perform geometric mean scaling;
  433. * GLP_SF_EQ perform equilibration scaling;
  434. * GLP_SF_2N round scale factors to nearest power of two;
  435. * GLP_SF_SKIP skip scaling, if the problem is well scaled.
  436. *
  437. * The parameter flags may be specified as GLP_SF_AUTO, in which case
  438. * the routine chooses scaling options automatically. */
  439. void glp_scale_prob(glp_prob *lp, int flags)
  440. { if (flags & ~(GLP_SF_GM | GLP_SF_EQ | GLP_SF_2N | GLP_SF_SKIP |
  441. GLP_SF_AUTO))
  442. xerror("glp_scale_prob: flags = 0x%02X; invalid scaling option"
  443. "s\n", flags);
  444. if (flags & GLP_SF_AUTO)
  445. flags = (GLP_SF_GM | GLP_SF_EQ | GLP_SF_SKIP);
  446. scale_prob(lp, flags);
  447. return;
  448. }
  449. /* eof */