glpapi05.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* glpapi05.c (LP basis constructing 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. /***********************************************************************
  25. * NAME
  26. *
  27. * glp_set_row_stat - set (change) row status
  28. *
  29. * SYNOPSIS
  30. *
  31. * void glp_set_row_stat(glp_prob *lp, int i, int stat);
  32. *
  33. * DESCRIPTION
  34. *
  35. * The routine glp_set_row_stat sets (changes) status of the auxiliary
  36. * variable associated with i-th row.
  37. *
  38. * The new status of the auxiliary variable should be specified by the
  39. * parameter stat as follows:
  40. *
  41. * GLP_BS - basic variable;
  42. * GLP_NL - non-basic variable;
  43. * GLP_NU - non-basic variable on its upper bound; if the variable is
  44. * not double-bounded, this means the same as GLP_NL (only in
  45. * case of this routine);
  46. * GLP_NF - the same as GLP_NL (only in case of this routine);
  47. * GLP_NS - the same as GLP_NL (only in case of this routine). */
  48. void glp_set_row_stat(glp_prob *lp, int i, int stat)
  49. { GLPROW *row;
  50. if (!(1 <= i && i <= lp->m))
  51. xerror("glp_set_row_stat: i = %d; row number out of range\n",
  52. i);
  53. if (!(stat == GLP_BS || stat == GLP_NL || stat == GLP_NU ||
  54. stat == GLP_NF || stat == GLP_NS))
  55. xerror("glp_set_row_stat: i = %d; stat = %d; invalid status\n",
  56. i, stat);
  57. row = lp->row[i];
  58. if (stat != GLP_BS)
  59. { switch (row->type)
  60. { case GLP_FR: stat = GLP_NF; break;
  61. case GLP_LO: stat = GLP_NL; break;
  62. case GLP_UP: stat = GLP_NU; break;
  63. case GLP_DB: if (stat != GLP_NU) stat = GLP_NL; break;
  64. case GLP_FX: stat = GLP_NS; break;
  65. default: xassert(row != row);
  66. }
  67. }
  68. if (row->stat == GLP_BS && stat != GLP_BS ||
  69. row->stat != GLP_BS && stat == GLP_BS)
  70. { /* invalidate the basis factorization */
  71. lp->valid = 0;
  72. }
  73. row->stat = stat;
  74. return;
  75. }
  76. /***********************************************************************
  77. * NAME
  78. *
  79. * glp_set_col_stat - set (change) column status
  80. *
  81. * SYNOPSIS
  82. *
  83. * void glp_set_col_stat(glp_prob *lp, int j, int stat);
  84. *
  85. * DESCRIPTION
  86. *
  87. * The routine glp_set_col_stat sets (changes) status of the structural
  88. * variable associated with j-th column.
  89. *
  90. * The new status of the structural variable should be specified by the
  91. * parameter stat as follows:
  92. *
  93. * GLP_BS - basic variable;
  94. * GLP_NL - non-basic variable;
  95. * GLP_NU - non-basic variable on its upper bound; if the variable is
  96. * not double-bounded, this means the same as GLP_NL (only in
  97. * case of this routine);
  98. * GLP_NF - the same as GLP_NL (only in case of this routine);
  99. * GLP_NS - the same as GLP_NL (only in case of this routine). */
  100. void glp_set_col_stat(glp_prob *lp, int j, int stat)
  101. { GLPCOL *col;
  102. if (!(1 <= j && j <= lp->n))
  103. xerror("glp_set_col_stat: j = %d; column number out of range\n"
  104. , j);
  105. if (!(stat == GLP_BS || stat == GLP_NL || stat == GLP_NU ||
  106. stat == GLP_NF || stat == GLP_NS))
  107. xerror("glp_set_col_stat: j = %d; stat = %d; invalid status\n",
  108. j, stat);
  109. col = lp->col[j];
  110. if (stat != GLP_BS)
  111. { switch (col->type)
  112. { case GLP_FR: stat = GLP_NF; break;
  113. case GLP_LO: stat = GLP_NL; break;
  114. case GLP_UP: stat = GLP_NU; break;
  115. case GLP_DB: if (stat != GLP_NU) stat = GLP_NL; break;
  116. case GLP_FX: stat = GLP_NS; break;
  117. default: xassert(col != col);
  118. }
  119. }
  120. if (col->stat == GLP_BS && stat != GLP_BS ||
  121. col->stat != GLP_BS && stat == GLP_BS)
  122. { /* invalidate the basis factorization */
  123. lp->valid = 0;
  124. }
  125. col->stat = stat;
  126. return;
  127. }
  128. /***********************************************************************
  129. * NAME
  130. *
  131. * glp_std_basis - construct standard initial LP basis
  132. *
  133. * SYNOPSIS
  134. *
  135. * void glp_std_basis(glp_prob *lp);
  136. *
  137. * DESCRIPTION
  138. *
  139. * The routine glp_std_basis builds the "standard" (trivial) initial
  140. * basis for the specified problem object.
  141. *
  142. * In the "standard" basis all auxiliary variables are basic, and all
  143. * structural variables are non-basic. */
  144. void glp_std_basis(glp_prob *lp)
  145. { int i, j;
  146. /* make all auxiliary variables basic */
  147. for (i = 1; i <= lp->m; i++)
  148. glp_set_row_stat(lp, i, GLP_BS);
  149. /* make all structural variables non-basic */
  150. for (j = 1; j <= lp->n; j++)
  151. { GLPCOL *col = lp->col[j];
  152. if (col->type == GLP_DB && fabs(col->lb) > fabs(col->ub))
  153. glp_set_col_stat(lp, j, GLP_NU);
  154. else
  155. glp_set_col_stat(lp, j, GLP_NL);
  156. }
  157. return;
  158. }
  159. /* eof */