# HG changeset patch # User athos # Date 1111764258 0 # Node ID 9d0deeea8c08de671242c62d010d49c7d2eef555 # Parent d8491fce67515037da41497a92a0c24ad7ca9fcd Low level interface for GLPK (Marci ut?n szabadon) diff -r d8491fce6751 -r 9d0deeea8c08 src/work/athos/lp/lp_glpk.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/athos/lp/lp_glpk.cc Fri Mar 25 15:24:18 2005 +0000 @@ -0,0 +1,238 @@ +/* -*- C++ -*- + * src/lemon/lp_glpk.cc - Part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport + * (Egervary Combinatorial Optimization Research Group, EGRES). + * + * Permission to use, modify and distribute this software is granted + * provided that this copyright notice appears in all copies. For + * precise terms see the accompanying LICENSE file. + * + * This software is provided "AS IS" with no warranty of any kind, + * express or implied, and with no claim as to its suitability for any + * purpose. + * + */ + +#ifndef LEMON_LP_GLPK_CC +#define LEMON_LP_GLPK_CC + +///\file +///\brief Implementation of the LEMON-GLPK lp solver interface. + +#include "lp_glpk.h" + +namespace lemon { + + /// \e + int LpGlpk::_addCol() { + int i=lpx_add_cols(lp, 1); + _setColLowerBound(i, -INF); + _setColUpperBound(i, INF); + return i; + } + + /// \e + int LpGlpk::_addRow() { + int i=lpx_add_rows(lp, 1); + return i; + } + + + void LpGlpk::_setRowCoeffs(int i, + int length, + int * indices, + Value * values ) + { + lpx_set_mat_row(lp, i, length, indices, values); + } + + void LpGlpk::_setColCoeffs(int i, + int length, + int * indices, + Value * values) + { + lpx_set_mat_col(lp, i, length, indices, values); + } + + void LpGlpk::_setColLowerBound(int i, Value lo) + { + if (lo==INF) { + //FIXME error + } + int b=lpx_get_col_type(lp, i); + double up=lpx_get_col_ub(lp, i); + if (lo==-INF) { + switch (b) { + case LPX_FR: + case LPX_LO: + lpx_set_col_bnds(lp, i, LPX_FR, lo, up); + break; + case LPX_UP: + break; + case LPX_DB: + case LPX_FX: + lpx_set_col_bnds(lp, i, LPX_UP, lo, up); + break; + default: ; + //FIXME error + } + } else { + switch (b) { + case LPX_FR: + case LPX_LO: + lpx_set_col_bnds(lp, i, LPX_LO, lo, up); + break; + case LPX_UP: + case LPX_DB: + case LPX_FX: + if (lo==up) + lpx_set_col_bnds(lp, i, LPX_FX, lo, up); + else + lpx_set_col_bnds(lp, i, LPX_DB, lo, up); + break; + default: ; + //FIXME error + } + } + + } + + void LpGlpk::_setColUpperBound(int i, Value up) + { + if (up==-INF) { + //FIXME error + } + int b=lpx_get_col_type(lp, i); + double lo=lpx_get_col_lb(lp, i); + if (up==INF) { + switch (b) { + case LPX_FR: + case LPX_LO: + break; + case LPX_UP: + lpx_set_col_bnds(lp, i, LPX_FR, lo, up); + break; + case LPX_DB: + case LPX_FX: + lpx_set_col_bnds(lp, i, LPX_LO, lo, up); + break; + default: ; + //FIXME error + } + } else { + switch (b) { + case LPX_FR: + lpx_set_col_bnds(lp, i, LPX_UP, lo, up); + break; + case LPX_UP: + lpx_set_col_bnds(lp, i, LPX_UP, lo, up); + break; + case LPX_LO: + case LPX_DB: + case LPX_FX: + if (lo==up) + lpx_set_col_bnds(lp, i, LPX_FX, lo, up); + else + lpx_set_col_bnds(lp, i, LPX_DB, lo, up); + break; + default: ; + //FIXME error + } + } + } + + void LpGlpk::_setRowLowerBound(int i, Value lo) + { + if (lo==INF) { + //FIXME error + } + int b=lpx_get_row_type(lp, i); + double up=lpx_get_row_ub(lp, i); + if (lo==-INF) { + switch (b) { + case LPX_FR: + case LPX_LO: + lpx_set_row_bnds(lp, i, LPX_FR, lo, up); + break; + case LPX_UP: + break; + case LPX_DB: + case LPX_FX: + lpx_set_row_bnds(lp, i, LPX_UP, lo, up); + break; + default: ; + //FIXME error + } + } else { + switch (b) { + case LPX_FR: + case LPX_LO: + lpx_set_row_bnds(lp, i, LPX_LO, lo, up); + break; + case LPX_UP: + case LPX_DB: + case LPX_FX: + if (lo==up) + lpx_set_row_bnds(lp, i, LPX_FX, lo, up); + else + lpx_set_row_bnds(lp, i, LPX_DB, lo, up); + break; + default: ; + //FIXME error + } + } + } + + void LpGlpk::_setRowUpperBound(int i, Value up) + { + if (up==-INF) { + //FIXME error + } + int b=lpx_get_row_type(lp, i); + double lo=lpx_get_row_lb(lp, i); + if (up==INF) { + switch (b) { + case LPX_FR: + case LPX_LO: + break; + case LPX_UP: + lpx_set_row_bnds(lp, i, LPX_FR, lo, up); + break; + case LPX_DB: + case LPX_FX: + lpx_set_row_bnds(lp, i, LPX_LO, lo, up); + break; + default: ; + //FIXME error + } + } else { + switch (b) { + case LPX_FR: + lpx_set_row_bnds(lp, i, LPX_UP, lo, up); + break; + case LPX_UP: + lpx_set_row_bnds(lp, i, LPX_UP, lo, up); + break; + case LPX_LO: + case LPX_DB: + case LPX_FX: + if (lo==up) + lpx_set_row_bnds(lp, i, LPX_FX, lo, up); + else + lpx_set_row_bnds(lp, i, LPX_DB, lo, up); + break; + default: ; + //FIXME error + } + } + } + + void LpGlpk::_setObjCoeff(int i, Value obj_coef) + { + lpx_set_obj_coef(lp, i, obj_coef); + } + +} //END OF NAMESPACE LEMON + +#endif //LEMON_LP_GLPK_CC diff -r d8491fce6751 -r 9d0deeea8c08 src/work/athos/lp/lp_glpk.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/athos/lp/lp_glpk.h Fri Mar 25 15:24:18 2005 +0000 @@ -0,0 +1,74 @@ +/* -*- C++ -*- + * src/lemon/lp_glpk.h - Part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport + * (Egervary Combinatorial Optimization Research Group, EGRES). + * + * Permission to use, modify and distribute this software is granted + * provided that this copyright notice appears in all copies. For + * precise terms see the accompanying LICENSE file. + * + * This software is provided "AS IS" with no warranty of any kind, + * express or implied, and with no claim as to its suitability for any + * purpose. + * + */ + +#ifndef LEMON_LP_GLPK_H +#define LEMON_LP_GLPK_H + +///\file +///\brief Header of the LEMON-GLPK lp solver interface. + +#include "lp_base.h" +extern "C" { +#include "glpk.h" +} + +namespace lemon { + + + /// \brief Wrapper for GLPK solver + /// + /// This class implements a lemon wrapper for GLPK. + class LpGlpk : public LpSolverBase { + + public: + + typedef LpSolverBase Parent; + + /// \e + LPX* lp; + + /// \e + LpGlpk() : Parent(), + lp(lpx_create_prob()) { + lpx_set_int_parm(lp, LPX_K_DUAL, 1); + } + /// \e + ~LpGlpk() { + lpx_delete_prob(lp); + } + + protected: + virtual int _addCol(); + virtual int _addRow(); + virtual void _setRowCoeffs(int i, + int length, + int * indices, + Value * values ); + virtual void _setColCoeffs(int i, + int length, + int * indices, + Value * values); + virtual void _setColLowerBound(int i, Value value); + virtual void _setColUpperBound(int i, Value value); + virtual void _setRowLowerBound(int i, Value value); + virtual void _setRowUpperBound(int i, Value value); + virtual void _setObjCoeff(int i, Value obj_coef); + + }; +} //END OF NAMESPACE LEMON + +#endif //LEMON_LP_GLPK_H +