# HG changeset patch # User athos # Date 1165507854 0 # Node ID b4931ae52069bc47c68df9f748fa349fdab32d18 # Parent 596e48d6e77b33df52e38cdecd1be363d973dfb6 Query functions have been implemented for GLPK (CPLEX breaks at the moment, I guess): These functions include: retrieving one element of the coeff. matrix retrieving one element of the obj function lower bd for a variable upper bound for a variable lower and upper bounds for a row (these can not be handled separately at the moment) direction of the optimization (is_max() function) diff -r 596e48d6e77b -r b4931ae52069 lemon/lp_base.h --- a/lemon/lp_base.h Mon Dec 04 18:09:09 2006 +0000 +++ b/lemon/lp_base.h Thu Dec 07 16:10:54 2006 +0000 @@ -706,10 +706,14 @@ virtual Value _getCoeff(int row, int col) = 0; virtual void _setColLowerBound(int i, Value value) = 0; + virtual Value _getColLowerBound(int i) = 0; virtual void _setColUpperBound(int i, Value value) = 0; + virtual Value _getColUpperBound(int i) = 0; // virtual void _setRowLowerBound(int i, Value value) = 0; // virtual void _setRowUpperBound(int i, Value value) = 0; virtual void _setRowBounds(int i, Value lower, Value upper) = 0; + virtual void _getRowBounds(int i, Value &lower, Value &upper)=0; + virtual void _setObjCoeff(int i, Value obj_coef) = 0; virtual Value _getObjCoeff(int i) = 0; virtual void _clearObj()=0; @@ -1024,6 +1028,15 @@ void colLowerBound(Col c, Value value) { _setColLowerBound(_lpId(c),value); } + + /// Get the lower bound of a column (i.e a variable) + + /// This function returns the lower bound for column (variable) \t c + /// (this might be -\ref INF as well). + ///\return The lower bound for coloumn \t c + Value colLowerBound(Col c) { + return _getColLowerBound(_lpId(c)); + } ///\brief Set the lower bound of several columns ///(i.e a variables) at once @@ -1071,7 +1084,16 @@ _setColUpperBound(_lpId(c),value); }; - ///\brief Set the lower bound of several columns + /// Get the upper bound of a column (i.e a variable) + + /// This function returns the upper bound for column (variable) \t c + /// (this might be \ref INF as well). + ///\return The upper bound for coloumn \t c + Value colUpperBound(Col c) { + return _getColUpperBound(_lpId(c)); + } + + ///\brief Set the upper bound of several columns ///(i.e a variables) at once /// ///This magic function takes a container as its argument @@ -1176,15 +1198,30 @@ /// Set the lower and the upper bounds of a row (i.e a constraint) - /// The lower and the upper bounds of + /// The lower and the upper bound of /// a constraint (row) have to be given by an /// extended number of type Value, i.e. a finite number of type - /// Value, -\ref INF or \ref INF. + /// Value, -\ref INF or \ref INF. There is no separate function for the + /// lower and the upper bound because that would have been hard to implement + /// for CPLEX. void rowBounds(Row c, Value lower, Value upper) { _setRowBounds(_lpId(c),lower, upper); - // _setRowUpperBound(_lpId(c),upper); } + /// Get the lower and the upper bounds of a row (i.e a constraint) + + /// The lower and the upper bound of + /// a constraint (row) are + /// extended numbers of type Value, i.e. finite numbers of type + /// Value, -\ref INF or \ref INF. + /// \todo There is no separate function for the + /// lower and the upper bound because we had problems with the + /// implementation of the setting functions for CPLEX: + /// check out whether this can be done for these functions. + void getRowBounds(Row c, Value &lower, Value &upper) { + _getRowBounds(_lpId(c),lower, upper); + } + ///Set an element of the objective function void objCoeff(Col c, Value v) {_setObjCoeff(_lpId(c),v); }; diff -r 596e48d6e77b -r b4931ae52069 lemon/lp_cplex.cc --- a/lemon/lp_cplex.cc Mon Dec 04 18:09:09 2006 +0000 +++ b/lemon/lp_cplex.cc Thu Dec 07 16:10:54 2006 +0000 @@ -43,6 +43,7 @@ } LpSolverBase &LpCplex::_copyLp() { + ///\bug FixID data is not copied! //The first approach opens a new environment LpCplex* newlp=new LpCplex(); //The routine CPXcloneprob can be used to create a new CPLEX problem diff -r 596e48d6e77b -r b4931ae52069 lemon/lp_glpk.cc --- a/lemon/lp_glpk.cc Mon Dec 04 18:09:09 2006 +0000 +++ b/lemon/lp_glpk.cc Thu Dec 07 16:10:54 2006 +0000 @@ -213,10 +213,25 @@ } } - LpGlpk::Value LpGlpk::_getCoeff(int, int) + LpGlpk::Value LpGlpk::_getCoeff(int row, int col) { - ///\todo This is not yet implemented!!! + + int length=lpx_get_mat_row(lp, row, 0, 0); + + std::vector indices(length + 2); + std::vector values(length + 2); + + lpx_get_mat_row(lp, row, &indices[0], &values[0]); + + //The following code does not suppose that the elements of the + //array indices are sorted + for (int i = 1; i <= length; ++i) { + if (indices[i]==col){ + return values[i]; + } + } return 0; + } @@ -262,6 +277,19 @@ } } + + LpGlpk::Value LpGlpk::_getColLowerBound(int i) + { + int b=lpx_get_col_type(lp, i); + switch (b) { + case LPX_LO: + case LPX_DB: + case LPX_FX: + return lpx_get_col_lb(lp, i); + default: ; + return -INF; + } + } void LpGlpk::_setColUpperBound(int i, Value up) { @@ -306,6 +334,19 @@ } } } + + LpGlpk::Value LpGlpk::_getColUpperBound(int i) + { + int b=lpx_get_col_type(lp, i); + switch (b) { + case LPX_UP: + case LPX_DB: + case LPX_FX: + return lpx_get_col_ub(lp, i); + default: ; + return INF; + } + } // void LpGlpk::_setRowLowerBound(int i, Value lo) // { @@ -424,6 +465,30 @@ } } + + void LpGlpk::_getRowBounds(int i, Value &lb, Value &ub) + { + + int b=lpx_get_row_type(lp, i); + switch (b) { + case LPX_FR: + case LPX_UP: + lb = -INF; + break; + default: + lb=lpx_get_row_lb(lp, i); + } + + switch (b) { + case LPX_FR: + case LPX_LO: + ub = INF; + break; + default: + ub=lpx_get_row_ub(lp, i); + } + + } void LpGlpk::_setObjCoeff(int i, Value obj_coef) { diff -r 596e48d6e77b -r b4931ae52069 lemon/lp_glpk.h --- a/lemon/lp_glpk.h Mon Dec 04 18:09:09 2006 +0000 +++ b/lemon/lp_glpk.h Thu Dec 07 16:10:54 2006 +0000 @@ -63,10 +63,14 @@ virtual Value _getCoeff(int row, int col); virtual void _setColLowerBound(int i, Value value); + virtual Value _getColLowerBound(int i); virtual void _setColUpperBound(int i, Value value); + virtual Value _getColUpperBound(int i); + // virtual void _setRowLowerBound(int i, Value value); // virtual void _setRowUpperBound(int i, Value value); virtual void _setRowBounds(int i, Value lower, Value upper); + virtual void _getRowBounds(int i, Value &lb, Value &ub); virtual void _setObjCoeff(int i, Value obj_coef); virtual Value _getObjCoeff(int i); virtual void _clearObj(); diff -r 596e48d6e77b -r b4931ae52069 lemon/lp_skeleton.cc --- a/lemon/lp_skeleton.cc Mon Dec 04 18:09:09 2006 +0000 +++ b/lemon/lp_skeleton.cc Thu Dec 07 16:10:54 2006 +0000 @@ -78,9 +78,19 @@ { } + LpSkeleton::Value LpSkeleton::_getColLowerBound(int) + { + return 0; + } + void LpSkeleton::_setColUpperBound(int, Value) { } + + LpSkeleton::Value LpSkeleton::_getColUpperBound(int) + { + return 0; + } // void LpSkeleton::_setRowLowerBound(int, Value) // { @@ -93,6 +103,10 @@ void LpSkeleton::_setRowBounds(int, Value, Value) { } + + void LpSkeleton::_getRowBounds(int, Value&, Value&) + { + } void LpSkeleton::_setObjCoeff(int, Value) { diff -r 596e48d6e77b -r b4931ae52069 lemon/lp_skeleton.h --- a/lemon/lp_skeleton.h Mon Dec 04 18:09:09 2006 +0000 +++ b/lemon/lp_skeleton.h Thu Dec 07 16:10:54 2006 +0000 @@ -64,12 +64,22 @@ virtual void _setColLowerBound(int i, Value value); /// \e + /// The lower bound of a variable (column) is an + /// extended number of type Value, i.e. a finite number of type + /// Value or -\ref INF. + virtual Value _getColLowerBound(int i); + /// The upper bound of a variable (column) have to be given by an /// extended number of type Value, i.e. a finite number of type /// Value or \ref INF. virtual void _setColUpperBound(int i, Value value); /// \e + /// The upper bound of a variable (column) is an + /// extended number of type Value, i.e. a finite number of type + /// Value or \ref INF. + virtual Value _getColUpperBound(int i); + // /// The lower bound of a linear expression (row) have to be given by an // /// extended number of type Value, i.e. a finite number of type // /// Value or -\ref INF. @@ -89,6 +99,14 @@ /// \e + /// The lower and the upper bound of + /// a constraint (row) are + /// extended numbers of type Value, i.e. finite numbers of type + /// Value, -\ref INF or \ref INF. + virtual void _getRowBounds(int i, Value &lb, Value &ub); + /// \e + + /// \e virtual void _clearObj(); /// \e diff -r 596e48d6e77b -r b4931ae52069 test/lp_test.cc --- a/test/lp_test.cc Mon Dec 04 18:09:09 2006 +0000 +++ b/test/lp_test.cc Thu Dec 07 16:10:54 2006 +0000 @@ -297,7 +297,14 @@ //Testing the problem retrieving routines check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!"); check(lp.is_max(),"This is a maximization!"); - check(lp.coeff(upright,x1)==0,"The coefficient in question is 1!"); + check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!"); + // std::cout<