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