COIN-OR::LEMON - Graph Library

source: glpk-cmake/src/glplpx02.c @ 2:4c8956a7bdf4

Last change on this file since 2:4c8956a7bdf4 was 1:c445c931472f, checked in by Alpar Juttner <alpar@…>, 13 years ago

Import glpk-4.45

  • Generated files and doc/notes are removed
File size: 10.0 KB
Line 
1/* glplpx02.c */
2
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
25#include "glpapi.h"
26
27/***********************************************************************
28*  NAME
29*
30*  lpx_put_solution - store basic solution components
31*
32*  SYNOPSIS
33*
34*  void lpx_put_solution(glp_prob *lp, int inval, const int *p_stat,
35*     const int *d_stat, const double *obj_val, const int r_stat[],
36*     const double r_prim[], const double r_dual[], const int c_stat[],
37*     const double c_prim[], const double c_dual[])
38*
39*  DESCRIPTION
40*
41*  The routine lpx_put_solution stores basic solution components to the
42*  specified problem object.
43*
44*  The parameter inval is the basis factorization invalidity flag.
45*  If this flag is clear, the current status of the basis factorization
46*  remains unchanged. If this flag is set, the routine invalidates the
47*  basis factorization.
48*
49*  The parameter p_stat is a pointer to the status of primal basic
50*  solution, which should be specified as follows:
51*
52*  GLP_UNDEF  - primal solution is undefined;
53*  GLP_FEAS   - primal solution is feasible;
54*  GLP_INFEAS - primal solution is infeasible;
55*  GLP_NOFEAS - no primal feasible solution exists.
56*
57*  If the parameter p_stat is NULL, the current status of primal basic
58*  solution remains unchanged.
59*
60*  The parameter d_stat is a pointer to the status of dual basic
61*  solution, which should be specified as follows:
62*
63*  GLP_UNDEF  - dual solution is undefined;
64*  GLP_FEAS   - dual solution is feasible;
65*  GLP_INFEAS - dual solution is infeasible;
66*  GLP_NOFEAS - no dual feasible solution exists.
67*
68*  If the parameter d_stat is NULL, the current status of dual basic
69*  solution remains unchanged.
70*
71*  The parameter obj_val is a pointer to the objective function value.
72*  If it is NULL, the current value of the objective function remains
73*  unchanged.
74*
75*  The array element r_stat[i], 1 <= i <= m (where m is the number of
76*  rows in the problem object), specifies the status of i-th auxiliary
77*  variable, which should be specified as follows:
78*
79*  GLP_BS - basic variable;
80*  GLP_NL - non-basic variable on lower bound;
81*  GLP_NU - non-basic variable on upper bound;
82*  GLP_NF - non-basic free variable;
83*  GLP_NS - non-basic fixed variable.
84*
85*  If the parameter r_stat is NULL, the current statuses of auxiliary
86*  variables remain unchanged.
87*
88*  The array element r_prim[i], 1 <= i <= m (where m is the number of
89*  rows in the problem object), specifies a primal value of i-th
90*  auxiliary variable. If the parameter r_prim is NULL, the current
91*  primal values of auxiliary variables remain unchanged.
92*
93*  The array element r_dual[i], 1 <= i <= m (where m is the number of
94*  rows in the problem object), specifies a dual value (reduced cost)
95*  of i-th auxiliary variable. If the parameter r_dual is NULL, the
96*  current dual values of auxiliary variables remain unchanged.
97*
98*  The array element c_stat[j], 1 <= j <= n (where n is the number of
99*  columns in the problem object), specifies the status of j-th
100*  structural variable, which should be specified as follows:
101*
102*  GLP_BS - basic variable;
103*  GLP_NL - non-basic variable on lower bound;
104*  GLP_NU - non-basic variable on upper bound;
105*  GLP_NF - non-basic free variable;
106*  GLP_NS - non-basic fixed variable.
107*
108*  If the parameter c_stat is NULL, the current statuses of structural
109*  variables remain unchanged.
110*
111*  The array element c_prim[j], 1 <= j <= n (where n is the number of
112*  columns in the problem object), specifies a primal value of j-th
113*  structural variable. If the parameter c_prim is NULL, the current
114*  primal values of structural variables remain unchanged.
115*
116*  The array element c_dual[j], 1 <= j <= n (where n is the number of
117*  columns in the problem object), specifies a dual value (reduced cost)
118*  of j-th structural variable. If the parameter c_dual is NULL, the
119*  current dual values of structural variables remain unchanged. */
120
121void lpx_put_solution(glp_prob *lp, int inval, const int *p_stat,
122      const int *d_stat, const double *obj_val, const int r_stat[],
123      const double r_prim[], const double r_dual[], const int c_stat[],
124      const double c_prim[], const double c_dual[])
125{     GLPROW *row;
126      GLPCOL *col;
127      int i, j;
128      /* invalidate the basis factorization, if required */
129      if (inval) lp->valid = 0;
130      /* store primal status */
131      if (p_stat != NULL)
132      {  if (!(*p_stat == GLP_UNDEF  || *p_stat == GLP_FEAS ||
133               *p_stat == GLP_INFEAS || *p_stat == GLP_NOFEAS))
134            xerror("lpx_put_solution: p_stat = %d; invalid primal statu"
135               "s\n", *p_stat);
136         lp->pbs_stat = *p_stat;
137      }
138      /* store dual status */
139      if (d_stat != NULL)
140      {  if (!(*d_stat == GLP_UNDEF  || *d_stat == GLP_FEAS ||
141               *d_stat == GLP_INFEAS || *d_stat == GLP_NOFEAS))
142            xerror("lpx_put_solution: d_stat = %d; invalid dual status "
143               "\n", *d_stat);
144         lp->dbs_stat = *d_stat;
145      }
146      /* store objective function value */
147      if (obj_val != NULL) lp->obj_val = *obj_val;
148      /* store row solution components */
149      for (i = 1; i <= lp->m; i++)
150      {  row = lp->row[i];
151         if (r_stat != NULL)
152         {  if (!(r_stat[i] == GLP_BS ||
153                  row->type == GLP_FR && r_stat[i] == GLP_NF ||
154                  row->type == GLP_LO && r_stat[i] == GLP_NL ||
155                  row->type == GLP_UP && r_stat[i] == GLP_NU ||
156                  row->type == GLP_DB && r_stat[i] == GLP_NL ||
157                  row->type == GLP_DB && r_stat[i] == GLP_NU ||
158                  row->type == GLP_FX && r_stat[i] == GLP_NS))
159               xerror("lpx_put_solution: r_stat[%d] = %d; invalid row s"
160                  "tatus\n", i, r_stat[i]);
161            row->stat = r_stat[i];
162         }
163         if (r_prim != NULL) row->prim = r_prim[i];
164         if (r_dual != NULL) row->dual = r_dual[i];
165      }
166      /* store column solution components */
167      for (j = 1; j <= lp->n; j++)
168      {  col = lp->col[j];
169         if (c_stat != NULL)
170         {  if (!(c_stat[j] == GLP_BS ||
171                  col->type == GLP_FR && c_stat[j] == GLP_NF ||
172                  col->type == GLP_LO && c_stat[j] == GLP_NL ||
173                  col->type == GLP_UP && c_stat[j] == GLP_NU ||
174                  col->type == GLP_DB && c_stat[j] == GLP_NL ||
175                  col->type == GLP_DB && c_stat[j] == GLP_NU ||
176                  col->type == GLP_FX && c_stat[j] == GLP_NS))
177               xerror("lpx_put_solution: c_stat[%d] = %d; invalid colum"
178                  "n status\n", j, c_stat[j]);
179            col->stat = c_stat[j];
180         }
181         if (c_prim != NULL) col->prim = c_prim[j];
182         if (c_dual != NULL) col->dual = c_dual[j];
183      }
184      return;
185}
186
187/*----------------------------------------------------------------------
188-- lpx_put_mip_soln - store mixed integer solution components.
189--
190-- *Synopsis*
191--
192-- #include "glplpx.h"
193-- void lpx_put_mip_soln(glp_prob *lp, int i_stat, double row_mipx[],
194--    double col_mipx[]);
195--
196-- *Description*
197--
198-- The routine lpx_put_mip_soln stores solution components obtained by
199-- branch-and-bound solver into the specified problem object.
200--
201-- NOTE: This routine is intended for internal use only. */
202
203void lpx_put_mip_soln(glp_prob *lp, int i_stat, double row_mipx[],
204      double col_mipx[])
205{     GLPROW *row;
206      GLPCOL *col;
207      int i, j;
208      double sum;
209      /* store mixed integer status */
210#if 0
211      if (!(i_stat == LPX_I_UNDEF || i_stat == LPX_I_OPT ||
212            i_stat == LPX_I_FEAS  || i_stat == LPX_I_NOFEAS))
213         fault("lpx_put_mip_soln: i_stat = %d; invalid mixed integer st"
214            "atus", i_stat);
215      lp->i_stat = i_stat;
216#else
217      switch (i_stat)
218      {  case LPX_I_UNDEF:
219            lp->mip_stat = GLP_UNDEF; break;
220         case LPX_I_OPT:
221            lp->mip_stat = GLP_OPT;  break;
222         case LPX_I_FEAS:
223            lp->mip_stat = GLP_FEAS; break;
224         case LPX_I_NOFEAS:
225            lp->mip_stat = GLP_NOFEAS; break;
226         default:
227            xerror("lpx_put_mip_soln: i_stat = %d; invalid mixed intege"
228               "r status\n", i_stat);
229      }
230#endif
231      /* store row solution components */
232      if (row_mipx != NULL)
233      {  for (i = 1; i <= lp->m; i++)
234         {  row = lp->row[i];
235            row->mipx = row_mipx[i];
236         }
237      }
238      /* store column solution components */
239      if (col_mipx != NULL)
240      {  for (j = 1; j <= lp->n; j++)
241         {  col = lp->col[j];
242            col->mipx = col_mipx[j];
243         }
244      }
245      /* if the solution is claimed to be integer feasible, check it */
246      if (lp->mip_stat == GLP_OPT || lp->mip_stat == GLP_FEAS)
247      {  for (j = 1; j <= lp->n; j++)
248         {  col = lp->col[j];
249            if (col->kind == GLP_IV && col->mipx != floor(col->mipx))
250               xerror("lpx_put_mip_soln: col_mipx[%d] = %.*g; must be i"
251                  "ntegral\n", j, DBL_DIG, col->mipx);
252         }
253      }
254      /* compute the objective function value */
255      sum = lp->c0;
256      for (j = 1; j <= lp->n; j++)
257      {  col = lp->col[j];
258         sum += col->coef * col->mipx;
259      }
260      lp->mip_obj = sum;
261      return;
262}
263
264/* eof */
Note: See TracBrowser for help on using the repository browser.