1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/lemon/lp_glpk.cc Tue Apr 05 09:08:23 2005 +0000
1.3 @@ -0,0 +1,288 @@
1.4 +/* -*- C++ -*-
1.5 + * src/lemon/lp_glpk.cc - Part of LEMON, a generic C++ optimization library
1.6 + *
1.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.8 + * (Egervary Combinatorial Optimization Research Group, EGRES).
1.9 + *
1.10 + * Permission to use, modify and distribute this software is granted
1.11 + * provided that this copyright notice appears in all copies. For
1.12 + * precise terms see the accompanying LICENSE file.
1.13 + *
1.14 + * This software is provided "AS IS" with no warranty of any kind,
1.15 + * express or implied, and with no claim as to its suitability for any
1.16 + * purpose.
1.17 + *
1.18 + */
1.19 +
1.20 +#ifndef LEMON_LP_GLPK_CC
1.21 +#define LEMON_LP_GLPK_CC
1.22 +
1.23 +///\file
1.24 +///\brief Implementation of the LEMON-GLPK lp solver interface.
1.25 +
1.26 +#include <lemon/lp_glpk.h>
1.27 +
1.28 +namespace lemon {
1.29 +
1.30 + /// \e
1.31 + int LpGlpk::_addCol() {
1.32 + int i=lpx_add_cols(lp, 1);
1.33 + _setColLowerBound(i, -INF);
1.34 + _setColUpperBound(i, INF);
1.35 + return i;
1.36 + }
1.37 +
1.38 + /// \e
1.39 + int LpGlpk::_addRow() {
1.40 + int i=lpx_add_rows(lp, 1);
1.41 + return i;
1.42 + }
1.43 +
1.44 +
1.45 + void LpGlpk::_setRowCoeffs(int i,
1.46 + int length,
1.47 + const int * indices,
1.48 + const Value * values )
1.49 + {
1.50 + lpx_set_mat_row(lp, i, length,
1.51 + const_cast<int * >(indices) ,
1.52 + const_cast<Value * >(values));
1.53 + }
1.54 +
1.55 + void LpGlpk::_setColCoeffs(int i,
1.56 + int length,
1.57 + const int * indices,
1.58 + const Value * values)
1.59 + {
1.60 + lpx_set_mat_col(lp, i, length,
1.61 + const_cast<int * >(indices),
1.62 + const_cast<Value * >(values));
1.63 + }
1.64 +
1.65 + void LpGlpk::_setColLowerBound(int i, Value lo)
1.66 + {
1.67 + if (lo==INF) {
1.68 + //FIXME error
1.69 + }
1.70 + int b=lpx_get_col_type(lp, i);
1.71 + double up=lpx_get_col_ub(lp, i);
1.72 + if (lo==-INF) {
1.73 + switch (b) {
1.74 + case LPX_FR:
1.75 + case LPX_LO:
1.76 + lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
1.77 + break;
1.78 + case LPX_UP:
1.79 + break;
1.80 + case LPX_DB:
1.81 + case LPX_FX:
1.82 + lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
1.83 + break;
1.84 + default: ;
1.85 + //FIXME error
1.86 + }
1.87 + } else {
1.88 + switch (b) {
1.89 + case LPX_FR:
1.90 + case LPX_LO:
1.91 + lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
1.92 + break;
1.93 + case LPX_UP:
1.94 + case LPX_DB:
1.95 + case LPX_FX:
1.96 + if (lo==up)
1.97 + lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
1.98 + else
1.99 + lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
1.100 + break;
1.101 + default: ;
1.102 + //FIXME error
1.103 + }
1.104 + }
1.105 +
1.106 + }
1.107 +
1.108 + void LpGlpk::_setColUpperBound(int i, Value up)
1.109 + {
1.110 + if (up==-INF) {
1.111 + //FIXME error
1.112 + }
1.113 + int b=lpx_get_col_type(lp, i);
1.114 + double lo=lpx_get_col_lb(lp, i);
1.115 + if (up==INF) {
1.116 + switch (b) {
1.117 + case LPX_FR:
1.118 + case LPX_LO:
1.119 + break;
1.120 + case LPX_UP:
1.121 + lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
1.122 + break;
1.123 + case LPX_DB:
1.124 + case LPX_FX:
1.125 + lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
1.126 + break;
1.127 + default: ;
1.128 + //FIXME error
1.129 + }
1.130 + } else {
1.131 + switch (b) {
1.132 + case LPX_FR:
1.133 + lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
1.134 + break;
1.135 + case LPX_UP:
1.136 + lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
1.137 + break;
1.138 + case LPX_LO:
1.139 + case LPX_DB:
1.140 + case LPX_FX:
1.141 + if (lo==up)
1.142 + lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
1.143 + else
1.144 + lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
1.145 + break;
1.146 + default: ;
1.147 + //FIXME error
1.148 + }
1.149 + }
1.150 + }
1.151 +
1.152 + void LpGlpk::_setRowLowerBound(int i, Value lo)
1.153 + {
1.154 + if (lo==INF) {
1.155 + //FIXME error
1.156 + }
1.157 + int b=lpx_get_row_type(lp, i);
1.158 + double up=lpx_get_row_ub(lp, i);
1.159 + if (lo==-INF) {
1.160 + switch (b) {
1.161 + case LPX_FR:
1.162 + case LPX_LO:
1.163 + lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
1.164 + break;
1.165 + case LPX_UP:
1.166 + break;
1.167 + case LPX_DB:
1.168 + case LPX_FX:
1.169 + lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
1.170 + break;
1.171 + default: ;
1.172 + //FIXME error
1.173 + }
1.174 + } else {
1.175 + switch (b) {
1.176 + case LPX_FR:
1.177 + case LPX_LO:
1.178 + lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
1.179 + break;
1.180 + case LPX_UP:
1.181 + case LPX_DB:
1.182 + case LPX_FX:
1.183 + if (lo==up)
1.184 + lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
1.185 + else
1.186 + lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
1.187 + break;
1.188 + default: ;
1.189 + //FIXME error
1.190 + }
1.191 + }
1.192 + }
1.193 +
1.194 + void LpGlpk::_setRowUpperBound(int i, Value up)
1.195 + {
1.196 + if (up==-INF) {
1.197 + //FIXME error
1.198 + }
1.199 + int b=lpx_get_row_type(lp, i);
1.200 + double lo=lpx_get_row_lb(lp, i);
1.201 + if (up==INF) {
1.202 + switch (b) {
1.203 + case LPX_FR:
1.204 + case LPX_LO:
1.205 + break;
1.206 + case LPX_UP:
1.207 + lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
1.208 + break;
1.209 + case LPX_DB:
1.210 + case LPX_FX:
1.211 + lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
1.212 + break;
1.213 + default: ;
1.214 + //FIXME error
1.215 + }
1.216 + } else {
1.217 + switch (b) {
1.218 + case LPX_FR:
1.219 + lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
1.220 + break;
1.221 + case LPX_UP:
1.222 + lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
1.223 + break;
1.224 + case LPX_LO:
1.225 + case LPX_DB:
1.226 + case LPX_FX:
1.227 + if (lo==up)
1.228 + lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
1.229 + else
1.230 + lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
1.231 + break;
1.232 + default: ;
1.233 + //FIXME error
1.234 + }
1.235 + }
1.236 + }
1.237 +
1.238 + void LpGlpk::_setObjCoeff(int i, Value obj_coef)
1.239 + {
1.240 + lpx_set_obj_coef(lp, i, obj_coef);
1.241 + }
1.242 +
1.243 +
1.244 + LpGlpk::SolveExitStatus LpGlpk::_solve()
1.245 + {
1.246 + int i= lpx_simplex(lp);
1.247 + switch (i) {
1.248 + case LPX_E_OK:
1.249 + return SOLVED;
1.250 + break;
1.251 + default:
1.252 + return UNSOLVED;
1.253 + }
1.254 + }
1.255 +
1.256 + LpGlpk::Value LpGlpk::_getPrimal(int i)
1.257 + {
1.258 + return lpx_get_col_prim(lp,i);
1.259 + }
1.260 +
1.261 +
1.262 + LpGlpk::SolutionStatus LpGlpk::_getPrimalType()
1.263 + {
1.264 + int stat= lpx_get_status(lp);
1.265 + switch (stat) {
1.266 + case LPX_UNDEF://Undefined (no solve has been run yet)
1.267 + return UNDEFINED;
1.268 + break;
1.269 + case LPX_NOFEAS://There is no feasible solution (primal, I guess)
1.270 + case LPX_INFEAS://Infeasible
1.271 + return INFEASIBLE;
1.272 + break;
1.273 + case LPX_UNBND://Unbounded
1.274 + return INFINITE;
1.275 + break;
1.276 + case LPX_FEAS://Feasible
1.277 + return FEASIBLE;
1.278 + break;
1.279 + case LPX_OPT://Feasible
1.280 + return OPTIMAL;
1.281 + break;
1.282 + default:
1.283 + return UNDEFINED; //to avoid gcc warning
1.284 + //FIXME error
1.285 + }
1.286 + }
1.287 +
1.288 +
1.289 +} //END OF NAMESPACE LEMON
1.290 +
1.291 +#endif //LEMON_LP_GLPK_CC