athos@1261: /* -*- C++ -*- athos@1261: * src/lemon/lp_glpk.cc - Part of LEMON, a generic C++ optimization library athos@1261: * athos@1261: * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport athos@1261: * (Egervary Combinatorial Optimization Research Group, EGRES). athos@1261: * athos@1261: * Permission to use, modify and distribute this software is granted athos@1261: * provided that this copyright notice appears in all copies. For athos@1261: * precise terms see the accompanying LICENSE file. athos@1261: * athos@1261: * This software is provided "AS IS" with no warranty of any kind, athos@1261: * express or implied, and with no claim as to its suitability for any athos@1261: * purpose. athos@1261: * athos@1261: */ athos@1261: athos@1261: #ifndef LEMON_LP_GLPK_CC athos@1261: #define LEMON_LP_GLPK_CC athos@1261: athos@1261: ///\file athos@1261: ///\brief Implementation of the LEMON-GLPK lp solver interface. athos@1261: ladanyi@1305: #include athos@1261: athos@1261: namespace lemon { athos@1261: alpar@1321: LpGlpk::LpGlpk() : Parent(), alpar@1321: lp(lpx_create_prob()) { alpar@1321: ///\todo constrol function for this: alpar@1321: lpx_set_int_parm(lp, LPX_K_DUAL, 1); alpar@1321: messageLevel(0); alpar@1321: } alpar@1321: alpar@1321: LpGlpk::~LpGlpk() { alpar@1321: lpx_delete_prob(lp); alpar@1321: } alpar@1321: alpar@1321: int LpGlpk::_addCol() { alpar@1321: int i=lpx_add_cols(lp, 1); alpar@1321: _setColLowerBound(i, -INF); alpar@1321: _setColUpperBound(i, INF); alpar@1321: return i; alpar@1321: } alpar@1321: alpar@1321: int LpGlpk::_addRow() { alpar@1321: int i=lpx_add_rows(lp, 1); alpar@1321: return i; alpar@1321: } alpar@1321: alpar@1321: alpar@1321: void LpGlpk::_setRowCoeffs(int i, alpar@1321: int length, alpar@1321: const int * indices, alpar@1321: const Value * values ) alpar@1321: { alpar@1321: lpx_set_mat_row(lp, i, length, alpar@1321: const_cast(indices) , alpar@1321: const_cast(values)); alpar@1321: } alpar@1321: alpar@1321: void LpGlpk::_setColCoeffs(int i, alpar@1321: int length, alpar@1321: const int * indices, alpar@1321: const Value * values) alpar@1321: { alpar@1321: lpx_set_mat_col(lp, i, length, alpar@1321: const_cast(indices), alpar@1321: const_cast(values)); alpar@1321: } alpar@1321: alpar@1321: void LpGlpk::_setColLowerBound(int i, Value lo) alpar@1321: { alpar@1321: if (lo==INF) { alpar@1321: //FIXME error alpar@1321: } alpar@1321: int b=lpx_get_col_type(lp, i); alpar@1321: double up=lpx_get_col_ub(lp, i); alpar@1321: if (lo==-INF) { alpar@1321: switch (b) { alpar@1321: case LPX_FR: alpar@1321: case LPX_LO: alpar@1321: lpx_set_col_bnds(lp, i, LPX_FR, lo, up); alpar@1321: break; alpar@1321: case LPX_UP: alpar@1321: break; alpar@1321: case LPX_DB: alpar@1321: case LPX_FX: alpar@1321: lpx_set_col_bnds(lp, i, LPX_UP, lo, up); alpar@1321: break; alpar@1321: default: ; alpar@1321: //FIXME error alpar@1321: } alpar@1321: } else { alpar@1321: switch (b) { alpar@1321: case LPX_FR: alpar@1321: case LPX_LO: alpar@1321: lpx_set_col_bnds(lp, i, LPX_LO, lo, up); alpar@1321: break; alpar@1321: case LPX_UP: alpar@1321: case LPX_DB: alpar@1321: case LPX_FX: alpar@1321: if (lo==up) alpar@1321: lpx_set_col_bnds(lp, i, LPX_FX, lo, up); alpar@1321: else alpar@1321: lpx_set_col_bnds(lp, i, LPX_DB, lo, up); alpar@1321: break; alpar@1321: default: ; alpar@1321: //FIXME error alpar@1321: } athos@1261: } athos@1261: alpar@1321: } alpar@1321: alpar@1321: void LpGlpk::_setColUpperBound(int i, Value up) alpar@1321: { alpar@1321: if (up==-INF) { alpar@1321: //FIXME error athos@1261: } alpar@1321: int b=lpx_get_col_type(lp, i); alpar@1321: double lo=lpx_get_col_lb(lp, i); alpar@1321: if (up==INF) { alpar@1321: switch (b) { alpar@1321: case LPX_FR: alpar@1321: case LPX_LO: alpar@1321: break; alpar@1321: case LPX_UP: alpar@1321: lpx_set_col_bnds(lp, i, LPX_FR, lo, up); alpar@1321: break; alpar@1321: case LPX_DB: alpar@1321: case LPX_FX: alpar@1321: lpx_set_col_bnds(lp, i, LPX_LO, lo, up); alpar@1321: break; alpar@1321: default: ; athos@1261: //FIXME error athos@1261: } alpar@1321: } else { alpar@1321: switch (b) { alpar@1321: case LPX_FR: alpar@1321: lpx_set_col_bnds(lp, i, LPX_UP, lo, up); alpar@1321: break; alpar@1321: case LPX_UP: alpar@1321: lpx_set_col_bnds(lp, i, LPX_UP, lo, up); alpar@1321: break; alpar@1321: case LPX_LO: alpar@1321: case LPX_DB: alpar@1321: case LPX_FX: alpar@1321: if (lo==up) alpar@1321: lpx_set_col_bnds(lp, i, LPX_FX, lo, up); alpar@1321: else alpar@1321: lpx_set_col_bnds(lp, i, LPX_DB, lo, up); alpar@1321: break; alpar@1321: default: ; athos@1261: //FIXME error athos@1261: } alpar@1321: } alpar@1321: } alpar@1321: alpar@1321: void LpGlpk::_setRowLowerBound(int i, Value lo) alpar@1321: { alpar@1321: if (lo==INF) { alpar@1321: //FIXME error alpar@1321: } alpar@1321: int b=lpx_get_row_type(lp, i); alpar@1321: double up=lpx_get_row_ub(lp, i); alpar@1321: if (lo==-INF) { alpar@1321: switch (b) { alpar@1321: case LPX_FR: alpar@1321: case LPX_LO: alpar@1321: lpx_set_row_bnds(lp, i, LPX_FR, lo, up); alpar@1321: break; alpar@1321: case LPX_UP: alpar@1321: break; alpar@1321: case LPX_DB: alpar@1321: case LPX_FX: alpar@1321: lpx_set_row_bnds(lp, i, LPX_UP, lo, up); alpar@1321: break; alpar@1321: default: ; alpar@1321: //FIXME error alpar@1321: } alpar@1321: } else { alpar@1321: switch (b) { alpar@1321: case LPX_FR: alpar@1321: case LPX_LO: alpar@1321: lpx_set_row_bnds(lp, i, LPX_LO, lo, up); alpar@1321: break; alpar@1321: case LPX_UP: alpar@1321: case LPX_DB: alpar@1321: case LPX_FX: alpar@1321: if (lo==up) alpar@1321: lpx_set_row_bnds(lp, i, LPX_FX, lo, up); alpar@1321: else alpar@1321: lpx_set_row_bnds(lp, i, LPX_DB, lo, up); alpar@1321: break; alpar@1321: default: ; alpar@1321: //FIXME error athos@1261: } athos@1261: } alpar@1321: } athos@1261: athos@1298: void LpGlpk::_setRowUpperBound(int i, Value up) athos@1298: { athos@1298: if (up==-INF) { athos@1298: //FIXME error athos@1298: } athos@1298: int b=lpx_get_row_type(lp, i); athos@1298: double lo=lpx_get_row_lb(lp, i); athos@1298: if (up==INF) { athos@1298: switch (b) { athos@1298: case LPX_FR: athos@1298: case LPX_LO: athos@1298: break; athos@1298: case LPX_UP: athos@1298: lpx_set_row_bnds(lp, i, LPX_FR, lo, up); athos@1298: break; athos@1298: case LPX_DB: athos@1298: case LPX_FX: athos@1298: lpx_set_row_bnds(lp, i, LPX_LO, lo, up); athos@1298: break; athos@1298: default: ; athos@1261: //FIXME error athos@1261: } athos@1298: } else { athos@1298: switch (b) { athos@1298: case LPX_FR: athos@1298: lpx_set_row_bnds(lp, i, LPX_UP, lo, up); athos@1298: break; athos@1298: case LPX_UP: athos@1298: lpx_set_row_bnds(lp, i, LPX_UP, lo, up); athos@1298: break; athos@1298: case LPX_LO: athos@1298: case LPX_DB: athos@1298: case LPX_FX: athos@1298: if (lo==up) athos@1298: lpx_set_row_bnds(lp, i, LPX_FX, lo, up); athos@1298: else athos@1298: lpx_set_row_bnds(lp, i, LPX_DB, lo, up); athos@1298: break; athos@1298: default: ; athos@1298: //FIXME error athos@1261: } athos@1261: } athos@1298: } athos@1261: athos@1298: void LpGlpk::_setObjCoeff(int i, Value obj_coef) athos@1298: { athos@1298: lpx_set_obj_coef(lp, i, obj_coef); athos@1298: } athos@1261: alpar@1263: alpar@1303: LpGlpk::SolveExitStatus LpGlpk::_solve() alpar@1263: { athos@1298: int i= lpx_simplex(lp); athos@1298: switch (i) { athos@1298: case LPX_E_OK: athos@1298: return SOLVED; athos@1298: break; athos@1298: default: athos@1298: return UNSOLVED; athos@1298: } alpar@1263: } alpar@1263: alpar@1293: LpGlpk::Value LpGlpk::_getPrimal(int i) alpar@1263: { athos@1298: return lpx_get_col_prim(lp,i); alpar@1263: } alpar@1263: alpar@1312: LpGlpk::Value LpGlpk::_getPrimalValue() alpar@1312: { athos@1314: return lpx_get_obj_val(lp); alpar@1312: } alpar@1312: athos@1298: alpar@1312: LpGlpk::SolutionStatus LpGlpk::_getPrimalStatus() alpar@1294: { athos@1298: int stat= lpx_get_status(lp); athos@1298: switch (stat) { athos@1298: case LPX_UNDEF://Undefined (no solve has been run yet) athos@1298: return UNDEFINED; athos@1298: break; athos@1298: case LPX_NOFEAS://There is no feasible solution (primal, I guess) athos@1298: case LPX_INFEAS://Infeasible athos@1298: return INFEASIBLE; athos@1298: break; athos@1298: case LPX_UNBND://Unbounded athos@1298: return INFINITE; athos@1298: break; athos@1298: case LPX_FEAS://Feasible alpar@1300: return FEASIBLE; athos@1298: break; athos@1298: case LPX_OPT://Feasible athos@1298: return OPTIMAL; athos@1298: break; alpar@1300: default: alpar@1300: return UNDEFINED; //to avoid gcc warning athos@1298: //FIXME error athos@1298: } alpar@1294: } alpar@1263: alpar@1263: alpar@1312: void LpGlpk::_setMax() alpar@1312: { alpar@1321: lpx_set_obj_dir(lp, LPX_MAX); alpar@1321: } alpar@1321: alpar@1312: void LpGlpk::_setMin() alpar@1312: { alpar@1321: lpx_set_obj_dir(lp, LPX_MIN); alpar@1321: } alpar@1321: alpar@1321: alpar@1321: void LpGlpk::messageLevel(int m) alpar@1321: { alpar@1321: lpx_set_int_parm(lp, LPX_K_MSGLEV, m); alpar@1321: } alpar@1312: alpar@1326: void LpGlpk::presolver(bool b) alpar@1326: { alpar@1326: lpx_set_int_parm(lp, LPX_K_PRESOL, b); alpar@1326: } alpar@1326: alpar@1312: athos@1261: } //END OF NAMESPACE LEMON athos@1261: athos@1261: #endif //LEMON_LP_GLPK_CC