3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
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>.
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.
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.
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 ***********************************************************************/
27 /***********************************************************************
30 * lpx_put_solution - store basic solution components
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[])
41 * The routine lpx_put_solution stores basic solution components to the
42 * specified problem object.
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.
49 * The parameter p_stat is a pointer to the status of primal basic
50 * solution, which should be specified as follows:
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.
57 * If the parameter p_stat is NULL, the current status of primal basic
58 * solution remains unchanged.
60 * The parameter d_stat is a pointer to the status of dual basic
61 * solution, which should be specified as follows:
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.
68 * If the parameter d_stat is NULL, the current status of dual basic
69 * solution remains unchanged.
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
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:
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.
85 * If the parameter r_stat is NULL, the current statuses of auxiliary
86 * variables remain unchanged.
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.
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.
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:
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.
108 * If the parameter c_stat is NULL, the current statuses of structural
109 * variables remain unchanged.
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.
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. */
121 void 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[])
128 /* invalidate the basis factorization, if required */
129 if (inval) lp->valid = 0;
130 /* store primal status */
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"
136 lp->pbs_stat = *p_stat;
138 /* store dual status */
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 "
144 lp->dbs_stat = *d_stat;
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++)
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];
163 if (r_prim != NULL) row->prim = r_prim[i];
164 if (r_dual != NULL) row->dual = r_dual[i];
166 /* store column solution components */
167 for (j = 1; j <= lp->n; j++)
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];
181 if (c_prim != NULL) col->prim = c_prim[j];
182 if (c_dual != NULL) col->dual = c_dual[j];
187 /*----------------------------------------------------------------------
188 -- lpx_put_mip_soln - store mixed integer solution components.
192 -- #include "glplpx.h"
193 -- void lpx_put_mip_soln(glp_prob *lp, int i_stat, double row_mipx[],
194 -- double col_mipx[]);
198 -- The routine lpx_put_mip_soln stores solution components obtained by
199 -- branch-and-bound solver into the specified problem object.
201 -- NOTE: This routine is intended for internal use only. */
203 void lpx_put_mip_soln(glp_prob *lp, int i_stat, double row_mipx[],
209 /* store mixed integer status */
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"
219 lp->mip_stat = GLP_UNDEF; break;
221 lp->mip_stat = GLP_OPT; break;
223 lp->mip_stat = GLP_FEAS; break;
225 lp->mip_stat = GLP_NOFEAS; break;
227 xerror("lpx_put_mip_soln: i_stat = %d; invalid mixed intege"
228 "r status\n", i_stat);
231 /* store row solution components */
232 if (row_mipx != NULL)
233 { for (i = 1; i <= lp->m; i++)
235 row->mipx = row_mipx[i];
238 /* store column solution components */
239 if (col_mipx != NULL)
240 { for (j = 1; j <= lp->n; j++)
242 col->mipx = col_mipx[j];
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++)
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);
254 /* compute the objective function value */
256 for (j = 1; j <= lp->n; j++)
258 sum += col->coef * col->mipx;