# HG changeset patch # User alpar # Date 1137228299 0 # Node ID 5b01801efbc0b9bd59236ff2427d2b1c2578e8f0 # Parent f794a0bb40c9cf53783c65841c7a18238bed555f - colName() added (untested on CPLEX) - possibility to set lower/upper bounds of several cols at once - setObj() -> obj() - setRow() -> row() diff -r f794a0bb40c9 -r 5b01801efbc0 lemon/lp_base.h --- a/lemon/lp_base.h Sat Jan 14 08:17:00 2006 +0000 +++ b/lemon/lp_base.h Sat Jan 14 08:44:59 2006 +0000 @@ -572,6 +572,8 @@ virtual int _addRow() = 0; virtual void _eraseCol(int col) = 0; virtual void _eraseRow(int row) = 0; + virtual void _getColName(int col, std::string & name) = 0; + virtual void _setColName(int col, const std::string & name) = 0; virtual void _setRowCoeffs(int i, int length, int const * indices, @@ -808,7 +810,7 @@ ///a better one. ///\todo Option to control whether a constraint with a single variable is ///added or not. - void setRow(Row r, Value l,const Expr &e, Value u) { + void row(Row r, Value l,const Expr &e, Value u) { std::vector indices; std::vector values; indices.push_back(0); @@ -829,8 +831,8 @@ ///\param r is the row to be modified ///\param c is a linear expression (see \ref Constr) - void setRow(Row r, const Constr &c) { - setRow(r, + void row(Row r, const Constr &c) { + row(r, c.lowerBounded()?c.lowerBound():-INF, c.expr(), c.upperBounded()?c.upperBound():INF); @@ -846,7 +848,7 @@ ///a better one. Row addRow(Value l,const Expr &e, Value u) { Row r=addRow(); - setRow(r,l,e,u); + row(r,l,e,u); return r; } @@ -856,7 +858,7 @@ ///\return The created row. Row addRow(const Constr &c) { Row r=addRow(); - setRow(r,c); + row(r,c); return r; } ///Erase a coloumn (i.e a variable) from the LP @@ -876,23 +878,80 @@ rows.erase(r.id); } - ///Set an element of the coefficient matrix of the LP + /// Get the name of a column + + ///\param c is the coresponding coloumn + ///\return The name of the colunm + std::string ColName(Col c){ + std::string name; + _getColName(cols.floatingId(c.id), name); + return name; + } + + /// Set the name of a column + + ///\param c is the coresponding coloumn + ///\param name The name to be given + void ColName(Col c, const std::string & name){ + _setColName(cols.floatingId(c.id), name); + } + + /// Set an element of the coefficient matrix of the LP ///\param r is the row of the element to be modified ///\param c is the coloumn of the element to be modified ///\param val is the new value of the coefficient - void setCoeff(Row r, Col c, Value val){ + + void Coeff(Row r, Col c, Value val){ _setCoeff(rows.floatingId(r.id),cols.floatingId(c.id), val); } /// Set the lower bound of a column (i.e a variable) - /// The upper bound of a variable (column) has to be given by an + /// The lower bound of a variable (column) has to be given by an /// extended number of type Value, i.e. a finite number of type /// Value or -\ref INF. void colLowerBound(Col c, Value value) { _setColLowerBound(cols.floatingId(c.id),value); } + + ///\brief Set the lower bound of several columns + ///(i.e a variables) at once + /// + ///This magic function takes a container as its argument + ///and applies the function on all of its elements. + /// The lower bound of a variable (column) has to be given by an + /// extended number of type Value, i.e. a finite number of type + /// Value or -\ref INF. +#ifdef DOXYGEN + template + void colLowerBound(T &t, Value value) { return 0;} +#else + template + typename enable_if::type + colLowerBound(T &t, Value value,dummy<0> = 0) { + for(typename T::iterator i=t.begin();i!=t.end();++i) { + colLowerBound(*i, value); + } + } + template + typename enable_if::type + colLowerBound(T &t, Value value,dummy<1> = 1) { + for(typename T::iterator i=t.begin();i!=t.end();++i) { + colLowerBound(i->second, value); + } + } + template + typename enable_if::type + colLowerBound(T &t, Value value,dummy<2> = 2) { + for(typename T::MapIt i(t); i!=INVALID; ++i){ + colLowerBound(*i, value); + } + } +#endif + /// Set the upper bound of a column (i.e a variable) /// The upper bound of a variable (column) has to be given by an @@ -901,6 +960,44 @@ void colUpperBound(Col c, Value value) { _setColUpperBound(cols.floatingId(c.id),value); }; + + ///\brief Set the lower bound of several columns + ///(i.e a variables) at once + /// + ///This magic function takes a container as its argument + ///and applies the function on all of its elements. + /// The upper bound of a variable (column) has to be given by an + /// extended number of type Value, i.e. a finite number of type + /// Value or \ref INF. +#ifdef DOXYGEN + template + void colUpperBound(T &t, Value value) { return 0;} +#else + template + typename enable_if::type + colUpperBound(T &t, Value value,dummy<0> = 0) { + for(typename T::iterator i=t.begin();i!=t.end();++i) { + colUpperBound(*i, value); + } + } + template + typename enable_if::type + colUpperBound(T &t, Value value,dummy<1> = 1) { + for(typename T::iterator i=t.begin();i!=t.end();++i) { + colUpperBound(i->second, value); + } + } + template + typename enable_if::type + colUpperBound(T &t, Value value,dummy<2> = 2) { + for(typename T::MapIt i(t); i!=INVALID; ++i){ + colUpperBound(*i, value); + } + } +#endif + /// Set the lower and the upper bounds of a column (i.e a variable) /// The lower and the upper bounds of @@ -912,6 +1009,44 @@ _setColUpperBound(cols.floatingId(c.id),upper); } + ///\brief Set the lower and the upper bound of several columns + ///(i.e a variables) at once + /// + ///This magic function takes a container as its argument + ///and applies the function on all of its elements. + /// The lower and the upper bounds of + /// a variable (column) have to be given by an + /// extended number of type Value, i.e. a finite number of type + /// Value, -\ref INF or \ref INF. +#ifdef DOXYGEN + template + void colBounds(T &t, Value lower, Value upper) { return 0;} +#else + template + typename enable_if::type + colBounds(T &t, Value lower, Value upper,dummy<0> = 0) { + for(typename T::iterator i=t.begin();i!=t.end();++i) { + colBounds(*i, lower, upper); + } + } + template + typename enable_if::type + colBounds(T &t, Value lower, Value upper,dummy<1> = 1) { + for(typename T::iterator i=t.begin();i!=t.end();++i) { + colBounds(i->second, lower, upper); + } + } + template + typename enable_if::type + colBounds(T &t, Value lower, Value upper,dummy<2> = 2) { + for(typename T::MapIt i(t); i!=INVALID; ++i){ + colBounds(*i, lower, upper); + } + } +#endif + // /// Set the lower bound of a row (i.e a constraint) // /// The lower bound of a linear expression (row) has to be given by an @@ -945,6 +1080,7 @@ ///Set the objective function ///\param e is a linear expression of type \ref Expr. + ///\bug Is should be called obj() void setObj(Expr e) { _clearObj(); for (Expr::iterator i=e.begin(); i!=e.end(); ++i) diff -r f794a0bb40c9 -r 5b01801efbc0 lemon/lp_cplex.cc --- a/lemon/lp_cplex.cc Sat Jan 14 08:17:00 2006 +0000 +++ b/lemon/lp_cplex.cc Sat Jan 14 08:44:59 2006 +0000 @@ -80,7 +80,17 @@ void LpCplex::_eraseRow(int i) { CPXdelrows(env, lp, i, i); } - + + void LpCplex::_getColName(int col, std::string &name) + { + ///\bug Unimplemented + } + + void LpCplex::_setColName(int col, const std::string &name) + { + ///\bug Untested + CPXchgcolname(env, lp, 1, &col, const_cast(name.c_str())); + } ///\warning Data at index 0 is ignored in the arrays. void LpCplex::_setRowCoeffs(int i, diff -r f794a0bb40c9 -r 5b01801efbc0 lemon/lp_cplex.h --- a/lemon/lp_cplex.h Sat Jan 14 08:17:00 2006 +0000 +++ b/lemon/lp_cplex.h Sat Jan 14 08:44:59 2006 +0000 @@ -57,6 +57,8 @@ virtual int _addRow(); virtual void _eraseCol(int i); virtual void _eraseRow(int i); + virtual void _getColName(int col, std::string & name); + virtual void _setColName(int col, const std::string & name); virtual void _setRowCoeffs(int i, int length, const int * indices, diff -r f794a0bb40c9 -r 5b01801efbc0 lemon/lp_glpk.cc --- a/lemon/lp_glpk.cc Sat Jan 14 08:17:00 2006 +0000 +++ b/lemon/lp_glpk.cc Sat Jan 14 08:44:59 2006 +0000 @@ -104,6 +104,19 @@ lpx_del_rows(lp, 1, rows); } + void LpGlpk::_getColName(int col, std::string & name) + { + + char *n = lpx_get_col_name(lp,col); + name = n?n:""; + } + + + void LpGlpk::_setColName(int col, const std::string & name) + { + lpx_set_col_name(lp,col,const_cast(name.c_str())); + } + void LpGlpk::_setRowCoeffs(int i, int length, const int * indices, diff -r f794a0bb40c9 -r 5b01801efbc0 lemon/lp_glpk.h --- a/lemon/lp_glpk.h Sat Jan 14 08:17:00 2006 +0000 +++ b/lemon/lp_glpk.h Sat Jan 14 08:44:59 2006 +0000 @@ -52,7 +52,8 @@ virtual int _addRow(); virtual void _eraseCol(int i); virtual void _eraseRow(int i); - + virtual void _getColName(int col, std::string & name); + virtual void _setColName(int col, const std::string & name); virtual void _setRowCoeffs(int i, int length, const int * indices, diff -r f794a0bb40c9 -r 5b01801efbc0 lemon/lp_skeleton.cc --- a/lemon/lp_skeleton.cc Sat Jan 14 08:17:00 2006 +0000 +++ b/lemon/lp_skeleton.cc Sat Jan 14 08:44:59 2006 +0000 @@ -48,6 +48,14 @@ void LpSkeleton::_eraseRow(int) { } + void LpSkeleton::_getColName(int, std::string &) { + } + + + void LpSkeleton::_setColName(int, const std::string &) { + } + + void LpSkeleton::_setRowCoeffs(int, int, int const *, diff -r f794a0bb40c9 -r 5b01801efbc0 lemon/lp_skeleton.h --- a/lemon/lp_skeleton.h Sat Jan 14 08:17:00 2006 +0000 +++ b/lemon/lp_skeleton.h Sat Jan 14 08:44:59 2006 +0000 @@ -41,6 +41,11 @@ /// \e virtual void _eraseRow(int i); /// \e + virtual void _getColName(int col, std::string & name); + /// \e + virtual void _setColName(int col, const std::string & name); + + /// \e /// \warning Arrays are indexed from 1 (datum at index 0 is ignored) /// diff -r f794a0bb40c9 -r 5b01801efbc0 test/lp_test.cc --- a/test/lp_test.cc Sat Jan 14 08:17:00 2006 +0000 +++ b/test/lp_test.cc Sat Jan 14 08:44:59 2006 +0000 @@ -27,21 +27,31 @@ std::vector x(10); // for(int i=0;i<10;i++) x.push_back(lp.addCol()); lp.addColSet(x); - + lp.colLowerBound(x,1); + lp.colUpperBound(x,1); + lp.colBounds(x,1,2); #ifndef GYORSITAS std::vector y(10); lp.addColSet(y); + lp.colLowerBound(y,1); + lp.colUpperBound(y,1); + lp.colBounds(y,1,2); + std::map z; z.insert(std::make_pair(12,INVALID)); z.insert(std::make_pair(2,INVALID)); z.insert(std::make_pair(7,INVALID)); z.insert(std::make_pair(5,INVALID)); - + lp.addColSet(z); + lp.colLowerBound(z,1); + lp.colUpperBound(z,1); + lp.colBounds(z,1,2); + { LP::Expr e,f,g; LP::Col p1,p2,p3,p4,p5;