glplpx02.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #pragma GCC diagnostic ignored "-Wall"
  2. /* glplpx02.c */
  3. /***********************************************************************
  4. * This code is part of GLPK (GNU Linear Programming Kit).
  5. *
  6. * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
  7. * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
  8. * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
  9. * E-mail: <mao@gnu.org>.
  10. *
  11. * GLPK is free software: you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * GLPK is distributed in the hope that it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  18. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  19. * License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
  23. ***********************************************************************/
  24. #include "glpapi.h"
  25. /***********************************************************************
  26. * NAME
  27. *
  28. * lpx_put_solution - store basic solution components
  29. *
  30. * SYNOPSIS
  31. *
  32. * void lpx_put_solution(glp_prob *lp, int inval, const int *p_stat,
  33. * const int *d_stat, const double *obj_val, const int r_stat[],
  34. * const double r_prim[], const double r_dual[], const int c_stat[],
  35. * const double c_prim[], const double c_dual[])
  36. *
  37. * DESCRIPTION
  38. *
  39. * The routine lpx_put_solution stores basic solution components to the
  40. * specified problem object.
  41. *
  42. * The parameter inval is the basis factorization invalidity flag.
  43. * If this flag is clear, the current status of the basis factorization
  44. * remains unchanged. If this flag is set, the routine invalidates the
  45. * basis factorization.
  46. *
  47. * The parameter p_stat is a pointer to the status of primal basic
  48. * solution, which should be specified as follows:
  49. *
  50. * GLP_UNDEF - primal solution is undefined;
  51. * GLP_FEAS - primal solution is feasible;
  52. * GLP_INFEAS - primal solution is infeasible;
  53. * GLP_NOFEAS - no primal feasible solution exists.
  54. *
  55. * If the parameter p_stat is NULL, the current status of primal basic
  56. * solution remains unchanged.
  57. *
  58. * The parameter d_stat is a pointer to the status of dual basic
  59. * solution, which should be specified as follows:
  60. *
  61. * GLP_UNDEF - dual solution is undefined;
  62. * GLP_FEAS - dual solution is feasible;
  63. * GLP_INFEAS - dual solution is infeasible;
  64. * GLP_NOFEAS - no dual feasible solution exists.
  65. *
  66. * If the parameter d_stat is NULL, the current status of dual basic
  67. * solution remains unchanged.
  68. *
  69. * The parameter obj_val is a pointer to the objective function value.
  70. * If it is NULL, the current value of the objective function remains
  71. * unchanged.
  72. *
  73. * The array element r_stat[i], 1 <= i <= m (where m is the number of
  74. * rows in the problem object), specifies the status of i-th auxiliary
  75. * variable, which should be specified as follows:
  76. *
  77. * GLP_BS - basic variable;
  78. * GLP_NL - non-basic variable on lower bound;
  79. * GLP_NU - non-basic variable on upper bound;
  80. * GLP_NF - non-basic free variable;
  81. * GLP_NS - non-basic fixed variable.
  82. *
  83. * If the parameter r_stat is NULL, the current statuses of auxiliary
  84. * variables remain unchanged.
  85. *
  86. * The array element r_prim[i], 1 <= i <= m (where m is the number of
  87. * rows in the problem object), specifies a primal value of i-th
  88. * auxiliary variable. If the parameter r_prim is NULL, the current
  89. * primal values of auxiliary variables remain unchanged.
  90. *
  91. * The array element r_dual[i], 1 <= i <= m (where m is the number of
  92. * rows in the problem object), specifies a dual value (reduced cost)
  93. * of i-th auxiliary variable. If the parameter r_dual is NULL, the
  94. * current dual values of auxiliary variables remain unchanged.
  95. *
  96. * The array element c_stat[j], 1 <= j <= n (where n is the number of
  97. * columns in the problem object), specifies the status of j-th
  98. * structural variable, which should be specified as follows:
  99. *
  100. * GLP_BS - basic variable;
  101. * GLP_NL - non-basic variable on lower bound;
  102. * GLP_NU - non-basic variable on upper bound;
  103. * GLP_NF - non-basic free variable;
  104. * GLP_NS - non-basic fixed variable.
  105. *
  106. * If the parameter c_stat is NULL, the current statuses of structural
  107. * variables remain unchanged.
  108. *
  109. * The array element c_prim[j], 1 <= j <= n (where n is the number of
  110. * columns in the problem object), specifies a primal value of j-th
  111. * structural variable. If the parameter c_prim is NULL, the current
  112. * primal values of structural variables remain unchanged.
  113. *
  114. * The array element c_dual[j], 1 <= j <= n (where n is the number of
  115. * columns in the problem object), specifies a dual value (reduced cost)
  116. * of j-th structural variable. If the parameter c_dual is NULL, the
  117. * current dual values of structural variables remain unchanged. */
  118. void lpx_put_solution(glp_prob *lp, int inval, const int *p_stat,
  119. const int *d_stat, const double *obj_val, const int r_stat[],
  120. const double r_prim[], const double r_dual[], const int c_stat[],
  121. const double c_prim[], const double c_dual[])
  122. { GLPROW *row;
  123. GLPCOL *col;
  124. int i, j;
  125. /* invalidate the basis factorization, if required */
  126. if (inval) lp->valid = 0;
  127. /* store primal status */
  128. if (p_stat != NULL)
  129. { if (!(*p_stat == GLP_UNDEF || *p_stat == GLP_FEAS ||
  130. *p_stat == GLP_INFEAS || *p_stat == GLP_NOFEAS))
  131. xerror("lpx_put_solution: p_stat = %d; invalid primal statu"
  132. "s\n", *p_stat);
  133. lp->pbs_stat = *p_stat;
  134. }
  135. /* store dual status */
  136. if (d_stat != NULL)
  137. { if (!(*d_stat == GLP_UNDEF || *d_stat == GLP_FEAS ||
  138. *d_stat == GLP_INFEAS || *d_stat == GLP_NOFEAS))
  139. xerror("lpx_put_solution: d_stat = %d; invalid dual status "
  140. "\n", *d_stat);
  141. lp->dbs_stat = *d_stat;
  142. }
  143. /* store objective function value */
  144. if (obj_val != NULL) lp->obj_val = *obj_val;
  145. /* store row solution components */
  146. for (i = 1; i <= lp->m; i++)
  147. { row = lp->row[i];
  148. if (r_stat != NULL)
  149. { if (!(r_stat[i] == GLP_BS ||
  150. row->type == GLP_FR && r_stat[i] == GLP_NF ||
  151. row->type == GLP_LO && r_stat[i] == GLP_NL ||
  152. row->type == GLP_UP && r_stat[i] == GLP_NU ||
  153. row->type == GLP_DB && r_stat[i] == GLP_NL ||
  154. row->type == GLP_DB && r_stat[i] == GLP_NU ||
  155. row->type == GLP_FX && r_stat[i] == GLP_NS))
  156. xerror("lpx_put_solution: r_stat[%d] = %d; invalid row s"
  157. "tatus\n", i, r_stat[i]);
  158. row->stat = r_stat[i];
  159. }
  160. if (r_prim != NULL) row->prim = r_prim[i];
  161. if (r_dual != NULL) row->dual = r_dual[i];
  162. }
  163. /* store column solution components */
  164. for (j = 1; j <= lp->n; j++)
  165. { col = lp->col[j];
  166. if (c_stat != NULL)
  167. { if (!(c_stat[j] == GLP_BS ||
  168. col->type == GLP_FR && c_stat[j] == GLP_NF ||
  169. col->type == GLP_LO && c_stat[j] == GLP_NL ||
  170. col->type == GLP_UP && c_stat[j] == GLP_NU ||
  171. col->type == GLP_DB && c_stat[j] == GLP_NL ||
  172. col->type == GLP_DB && c_stat[j] == GLP_NU ||
  173. col->type == GLP_FX && c_stat[j] == GLP_NS))
  174. xerror("lpx_put_solution: c_stat[%d] = %d; invalid colum"
  175. "n status\n", j, c_stat[j]);
  176. col->stat = c_stat[j];
  177. }
  178. if (c_prim != NULL) col->prim = c_prim[j];
  179. if (c_dual != NULL) col->dual = c_dual[j];
  180. }
  181. return;
  182. }
  183. /*----------------------------------------------------------------------
  184. -- lpx_put_mip_soln - store mixed integer solution components.
  185. --
  186. -- *Synopsis*
  187. --
  188. -- #include "glplpx.h"
  189. -- void lpx_put_mip_soln(glp_prob *lp, int i_stat, double row_mipx[],
  190. -- double col_mipx[]);
  191. --
  192. -- *Description*
  193. --
  194. -- The routine lpx_put_mip_soln stores solution components obtained by
  195. -- branch-and-bound solver into the specified problem object.
  196. --
  197. -- NOTE: This routine is intended for internal use only. */
  198. void lpx_put_mip_soln(glp_prob *lp, int i_stat, double row_mipx[],
  199. double col_mipx[])
  200. { GLPROW *row;
  201. GLPCOL *col;
  202. int i, j;
  203. double sum;
  204. /* store mixed integer status */
  205. #if 0
  206. if (!(i_stat == LPX_I_UNDEF || i_stat == LPX_I_OPT ||
  207. i_stat == LPX_I_FEAS || i_stat == LPX_I_NOFEAS))
  208. fault("lpx_put_mip_soln: i_stat = %d; invalid mixed integer st"
  209. "atus", i_stat);
  210. lp->i_stat = i_stat;
  211. #else
  212. switch (i_stat)
  213. { case LPX_I_UNDEF:
  214. lp->mip_stat = GLP_UNDEF; break;
  215. case LPX_I_OPT:
  216. lp->mip_stat = GLP_OPT; break;
  217. case LPX_I_FEAS:
  218. lp->mip_stat = GLP_FEAS; break;
  219. case LPX_I_NOFEAS:
  220. lp->mip_stat = GLP_NOFEAS; break;
  221. default:
  222. xerror("lpx_put_mip_soln: i_stat = %d; invalid mixed intege"
  223. "r status\n", i_stat);
  224. }
  225. #endif
  226. /* store row solution components */
  227. if (row_mipx != NULL)
  228. { for (i = 1; i <= lp->m; i++)
  229. { row = lp->row[i];
  230. row->mipx = row_mipx[i];
  231. }
  232. }
  233. /* store column solution components */
  234. if (col_mipx != NULL)
  235. { for (j = 1; j <= lp->n; j++)
  236. { col = lp->col[j];
  237. col->mipx = col_mipx[j];
  238. }
  239. }
  240. /* if the solution is claimed to be integer feasible, check it */
  241. if (lp->mip_stat == GLP_OPT || lp->mip_stat == GLP_FEAS)
  242. { for (j = 1; j <= lp->n; j++)
  243. { col = lp->col[j];
  244. if (col->kind == GLP_IV && col->mipx != floor(col->mipx))
  245. xerror("lpx_put_mip_soln: col_mipx[%d] = %.*g; must be i"
  246. "ntegral\n", j, DBL_DIG, col->mipx);
  247. }
  248. }
  249. /* compute the objective function value */
  250. sum = lp->c0;
  251. for (j = 1; j <= lp->n; j++)
  252. { col = lp->col[j];
  253. sum += col->coef * col->mipx;
  254. }
  255. lp->mip_obj = sum;
  256. return;
  257. }
  258. /* eof */