glpapi10.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* glpapi10.c (solution checking 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 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. #include "glpapi.h"
  24. void _glp_check_kkt(glp_prob *P, int sol, int cond, double *_ae_max,
  25. int *_ae_ind, double *_re_max, int *_re_ind)
  26. { /* check feasibility and optimality conditions */
  27. int m = P->m;
  28. int n = P->n;
  29. GLPROW *row;
  30. GLPCOL *col;
  31. GLPAIJ *aij;
  32. int i, j, ae_ind, re_ind;
  33. double e, sp, sn, t, ae_max, re_max;
  34. if (!(sol == GLP_SOL || sol == GLP_IPT || sol == GLP_MIP))
  35. xerror("glp_check_kkt: sol = %d; invalid solution indicator\n",
  36. sol);
  37. if (!(cond == GLP_KKT_PE || cond == GLP_KKT_PB ||
  38. cond == GLP_KKT_DE || cond == GLP_KKT_DB ||
  39. cond == GLP_KKT_CS))
  40. xerror("glp_check_kkt: cond = %d; invalid condition indicator "
  41. "\n", cond);
  42. ae_max = re_max = 0.0;
  43. ae_ind = re_ind = 0;
  44. if (cond == GLP_KKT_PE)
  45. { /* xR - A * xS = 0 */
  46. for (i = 1; i <= m; i++)
  47. { row = P->row[i];
  48. sp = sn = 0.0;
  49. /* t := xR[i] */
  50. if (sol == GLP_SOL)
  51. t = row->prim;
  52. else if (sol == GLP_IPT)
  53. t = row->pval;
  54. else if (sol == GLP_MIP)
  55. t = row->mipx;
  56. else
  57. xassert(sol != sol);
  58. if (t >= 0.0) sp += t; else sn -= t;
  59. for (aij = row->ptr; aij != NULL; aij = aij->r_next)
  60. { col = aij->col;
  61. /* t := - a[i,j] * xS[j] */
  62. if (sol == GLP_SOL)
  63. t = - aij->val * col->prim;
  64. else if (sol == GLP_IPT)
  65. t = - aij->val * col->pval;
  66. else if (sol == GLP_MIP)
  67. t = - aij->val * col->mipx;
  68. else
  69. xassert(sol != sol);
  70. if (t >= 0.0) sp += t; else sn -= t;
  71. }
  72. /* absolute error */
  73. e = fabs(sp - sn);
  74. if (ae_max < e)
  75. ae_max = e, ae_ind = i;
  76. /* relative error */
  77. e /= (1.0 + sp + sn);
  78. if (re_max < e)
  79. re_max = e, re_ind = i;
  80. }
  81. }
  82. else if (cond == GLP_KKT_PB)
  83. { /* lR <= xR <= uR */
  84. for (i = 1; i <= m; i++)
  85. { row = P->row[i];
  86. /* t := xR[i] */
  87. if (sol == GLP_SOL)
  88. t = row->prim;
  89. else if (sol == GLP_IPT)
  90. t = row->pval;
  91. else if (sol == GLP_MIP)
  92. t = row->mipx;
  93. else
  94. xassert(sol != sol);
  95. /* check lower bound */
  96. if (row->type == GLP_LO || row->type == GLP_DB ||
  97. row->type == GLP_FX)
  98. { if (t < row->lb)
  99. { /* absolute error */
  100. e = row->lb - t;
  101. if (ae_max < e)
  102. ae_max = e, ae_ind = i;
  103. /* relative error */
  104. e /= (1.0 + fabs(row->lb));
  105. if (re_max < e)
  106. re_max = e, re_ind = i;
  107. }
  108. }
  109. /* check upper bound */
  110. if (row->type == GLP_UP || row->type == GLP_DB ||
  111. row->type == GLP_FX)
  112. { if (t > row->ub)
  113. { /* absolute error */
  114. e = t - row->ub;
  115. if (ae_max < e)
  116. ae_max = e, ae_ind = i;
  117. /* relative error */
  118. e /= (1.0 + fabs(row->ub));
  119. if (re_max < e)
  120. re_max = e, re_ind = i;
  121. }
  122. }
  123. }
  124. /* lS <= xS <= uS */
  125. for (j = 1; j <= n; j++)
  126. { col = P->col[j];
  127. /* t := xS[j] */
  128. if (sol == GLP_SOL)
  129. t = col->prim;
  130. else if (sol == GLP_IPT)
  131. t = col->pval;
  132. else if (sol == GLP_MIP)
  133. t = col->mipx;
  134. else
  135. xassert(sol != sol);
  136. /* check lower bound */
  137. if (col->type == GLP_LO || col->type == GLP_DB ||
  138. col->type == GLP_FX)
  139. { if (t < col->lb)
  140. { /* absolute error */
  141. e = col->lb - t;
  142. if (ae_max < e)
  143. ae_max = e, ae_ind = m+j;
  144. /* relative error */
  145. e /= (1.0 + fabs(col->lb));
  146. if (re_max < e)
  147. re_max = e, re_ind = m+j;
  148. }
  149. }
  150. /* check upper bound */
  151. if (col->type == GLP_UP || col->type == GLP_DB ||
  152. col->type == GLP_FX)
  153. { if (t > col->ub)
  154. { /* absolute error */
  155. e = t - col->ub;
  156. if (ae_max < e)
  157. ae_max = e, ae_ind = m+j;
  158. /* relative error */
  159. e /= (1.0 + fabs(col->ub));
  160. if (re_max < e)
  161. re_max = e, re_ind = m+j;
  162. }
  163. }
  164. }
  165. }
  166. else if (cond == GLP_KKT_DE)
  167. { /* A' * (lambdaR - cR) + (lambdaS - cS) = 0 */
  168. for (j = 1; j <= n; j++)
  169. { col = P->col[j];
  170. sp = sn = 0.0;
  171. /* t := lambdaS[j] - cS[j] */
  172. if (sol == GLP_SOL)
  173. t = col->dual - col->coef;
  174. else if (sol == GLP_IPT)
  175. t = col->dval - col->coef;
  176. else
  177. xassert(sol != sol);
  178. if (t >= 0.0) sp += t; else sn -= t;
  179. for (aij = col->ptr; aij != NULL; aij = aij->c_next)
  180. { row = aij->row;
  181. /* t := a[i,j] * (lambdaR[i] - cR[i]) */
  182. if (sol == GLP_SOL)
  183. t = aij->val * row->dual;
  184. else if (sol == GLP_IPT)
  185. t = aij->val * row->dval;
  186. else
  187. xassert(sol != sol);
  188. if (t >= 0.0) sp += t; else sn -= t;
  189. }
  190. /* absolute error */
  191. e = fabs(sp - sn);
  192. if (ae_max < e)
  193. ae_max = e, ae_ind = m+j;
  194. /* relative error */
  195. e /= (1.0 + sp + sn);
  196. if (re_max < e)
  197. re_max = e, re_ind = m+j;
  198. }
  199. }
  200. else if (cond == GLP_KKT_DB)
  201. { /* check lambdaR */
  202. for (i = 1; i <= m; i++)
  203. { row = P->row[i];
  204. /* t := lambdaR[i] */
  205. if (sol == GLP_SOL)
  206. t = row->dual;
  207. else if (sol == GLP_IPT)
  208. t = row->dval;
  209. else
  210. xassert(sol != sol);
  211. /* correct sign */
  212. if (P->dir == GLP_MIN)
  213. t = + t;
  214. else if (P->dir == GLP_MAX)
  215. t = - t;
  216. else
  217. xassert(P != P);
  218. /* check for positivity */
  219. if (row->type == GLP_FR || row->type == GLP_LO)
  220. { if (t < 0.0)
  221. { e = - t;
  222. if (ae_max < e)
  223. ae_max = re_max = e, ae_ind = re_ind = i;
  224. }
  225. }
  226. /* check for negativity */
  227. if (row->type == GLP_FR || row->type == GLP_UP)
  228. { if (t > 0.0)
  229. { e = + t;
  230. if (ae_max < e)
  231. ae_max = re_max = e, ae_ind = re_ind = i;
  232. }
  233. }
  234. }
  235. /* check lambdaS */
  236. for (j = 1; j <= n; j++)
  237. { col = P->col[j];
  238. /* t := lambdaS[j] */
  239. if (sol == GLP_SOL)
  240. t = col->dual;
  241. else if (sol == GLP_IPT)
  242. t = col->dval;
  243. else
  244. xassert(sol != sol);
  245. /* correct sign */
  246. if (P->dir == GLP_MIN)
  247. t = + t;
  248. else if (P->dir == GLP_MAX)
  249. t = - t;
  250. else
  251. xassert(P != P);
  252. /* check for positivity */
  253. if (col->type == GLP_FR || col->type == GLP_LO)
  254. { if (t < 0.0)
  255. { e = - t;
  256. if (ae_max < e)
  257. ae_max = re_max = e, ae_ind = re_ind = m+j;
  258. }
  259. }
  260. /* check for negativity */
  261. if (col->type == GLP_FR || col->type == GLP_UP)
  262. { if (t > 0.0)
  263. { e = + t;
  264. if (ae_max < e)
  265. ae_max = re_max = e, ae_ind = re_ind = m+j;
  266. }
  267. }
  268. }
  269. }
  270. else
  271. xassert(cond != cond);
  272. if (_ae_max != NULL) *_ae_max = ae_max;
  273. if (_ae_ind != NULL) *_ae_ind = ae_ind;
  274. if (_re_max != NULL) *_re_max = re_max;
  275. if (_re_ind != NULL) *_re_ind = re_ind;
  276. return;
  277. }
  278. /* eof */