lemon/lp_base.h
changeset 1336 0759d974de81
parent 1270 dceba191c00d
child 1353 760a5f690163
     1.1 --- a/lemon/lp_base.h	Thu Apr 02 22:34:03 2015 +0200
     1.2 +++ b/lemon/lp_base.h	Sun Jan 05 22:24:56 2014 +0100
     1.3 @@ -31,6 +31,8 @@
     1.4  #include<lemon/core.h>
     1.5  #include<lemon/bits/solver_bits.h>
     1.6  
     1.7 +#include<lemon/bits/stl_iterators.h>
     1.8 +
     1.9  ///\file
    1.10  ///\brief The interface of the LP solver interface.
    1.11  ///\ingroup lp_group
    1.12 @@ -45,8 +47,8 @@
    1.13  
    1.14    protected:
    1.15  
    1.16 -    _solver_bits::VarIndex rows;
    1.17 -    _solver_bits::VarIndex cols;
    1.18 +    _solver_bits::VarIndex _rows;
    1.19 +    _solver_bits::VarIndex _cols;
    1.20  
    1.21    public:
    1.22  
    1.23 @@ -166,7 +168,7 @@
    1.24        ///
    1.25        ColIt(const LpBase &solver) : _solver(&solver)
    1.26        {
    1.27 -        _solver->cols.firstItem(_id);
    1.28 +        _solver->_cols.firstItem(_id);
    1.29        }
    1.30        /// Invalid constructor \& conversion
    1.31  
    1.32 @@ -179,11 +181,26 @@
    1.33        ///
    1.34        ColIt &operator++()
    1.35        {
    1.36 -        _solver->cols.nextItem(_id);
    1.37 +        _solver->_cols.nextItem(_id);
    1.38          return *this;
    1.39        }
    1.40      };
    1.41  
    1.42 +    /// \brief Gets the collection of the columns of the LP problem.
    1.43 +    ///
    1.44 +    /// This function can be used for iterating on
    1.45 +    /// the columns of the LP problem. It returns a wrapped ColIt, which looks
    1.46 +    /// like an STL container (by having begin() and end())
    1.47 +    /// which you can use in range-based for loops, STL algorithms, etc.
    1.48 +    /// For example you can write:
    1.49 +    ///\code
    1.50 +    /// for(auto c: lp.cols())
    1.51 +    ///   doSomething(c);
    1.52 +    LemonRangeWrapper1<ColIt, LpBase> cols() {
    1.53 +      return LemonRangeWrapper1<ColIt, LpBase>(*this);
    1.54 +    }
    1.55 +
    1.56 +
    1.57      /// \brief Returns the ID of the column.
    1.58      static int id(const Col& col) { return col._id; }
    1.59      /// \brief Returns the column with the given ID.
    1.60 @@ -261,7 +278,7 @@
    1.61        ///
    1.62        RowIt(const LpBase &solver) : _solver(&solver)
    1.63        {
    1.64 -        _solver->rows.firstItem(_id);
    1.65 +        _solver->_rows.firstItem(_id);
    1.66        }
    1.67        /// Invalid constructor \& conversion
    1.68  
    1.69 @@ -274,10 +291,25 @@
    1.70        ///
    1.71        RowIt &operator++()
    1.72        {
    1.73 -        _solver->rows.nextItem(_id);
    1.74 +        _solver->_rows.nextItem(_id);
    1.75          return *this;
    1.76        }
    1.77      };
    1.78 +    
    1.79 +    /// \brief Gets the collection of the rows of the LP problem.
    1.80 +    ///
    1.81 +    /// This function can be used for iterating on
    1.82 +    /// the rows of the LP problem. It returns a wrapped RowIt, which looks
    1.83 +    /// like an STL container (by having begin() and end())
    1.84 +    /// which you can use in range-based for loops, STL algorithms, etc.
    1.85 +    /// For example you can write:
    1.86 +    ///\code
    1.87 +    /// for(auto c: lp.rows())
    1.88 +    ///   doSomething(c);
    1.89 +    LemonRangeWrapper1<RowIt, LpBase> rows() {
    1.90 +      return LemonRangeWrapper1<RowIt, LpBase>(*this);
    1.91 +    }
    1.92 +    
    1.93  
    1.94      /// \brief Returns the ID of the row.
    1.95      static int id(const Row& row) { return row._id; }
    1.96 @@ -934,11 +966,11 @@
    1.97  
    1.98      //Abstract virtual functions
    1.99  
   1.100 -    virtual int _addColId(int col) { return cols.addIndex(col); }
   1.101 -    virtual int _addRowId(int row) { return rows.addIndex(row); }
   1.102 +    virtual int _addColId(int col) { return _cols.addIndex(col); }
   1.103 +    virtual int _addRowId(int row) { return _rows.addIndex(row); }
   1.104  
   1.105 -    virtual void _eraseColId(int col) { cols.eraseIndex(col); }
   1.106 -    virtual void _eraseRowId(int row) { rows.eraseIndex(row); }
   1.107 +    virtual void _eraseColId(int col) { _cols.eraseIndex(col); }
   1.108 +    virtual void _eraseRowId(int row) { _rows.eraseIndex(row); }
   1.109  
   1.110      virtual int _addCol() = 0;
   1.111      virtual int _addRow() = 0;
   1.112 @@ -1003,7 +1035,7 @@
   1.113      //Constant component of the objective function
   1.114      Value obj_const_comp;
   1.115  
   1.116 -    LpBase() : rows(), cols(), obj_const_comp(0) {}
   1.117 +    LpBase() : _rows(), _cols(), obj_const_comp(0) {}
   1.118  
   1.119    public:
   1.120  
   1.121 @@ -1115,8 +1147,8 @@
   1.122      ///a better one.
   1.123      void col(Col c, const DualExpr &e) {
   1.124        e.simplify();
   1.125 -      _setColCoeffs(cols(id(c)), ExprIterator(e.comps.begin(), rows),
   1.126 -                    ExprIterator(e.comps.end(), rows));
   1.127 +      _setColCoeffs(_cols(id(c)), ExprIterator(e.comps.begin(), _rows),
   1.128 +                    ExprIterator(e.comps.end(), _rows));
   1.129      }
   1.130  
   1.131      ///Get a column (i.e a dual constraint) of the LP
   1.132 @@ -1125,7 +1157,7 @@
   1.133      ///\return the dual expression associated to the column
   1.134      DualExpr col(Col c) const {
   1.135        DualExpr e;
   1.136 -      _getColCoeffs(cols(id(c)), InsertIterator(e.comps, rows));
   1.137 +      _getColCoeffs(_cols(id(c)), InsertIterator(e.comps, _rows));
   1.138        return e;
   1.139      }
   1.140  
   1.141 @@ -1212,10 +1244,10 @@
   1.142      ///\param u is the upper bound (\ref INF means no bound)
   1.143      void row(Row r, Value l, const Expr &e, Value u) {
   1.144        e.simplify();
   1.145 -      _setRowCoeffs(rows(id(r)), ExprIterator(e.comps.begin(), cols),
   1.146 -                    ExprIterator(e.comps.end(), cols));
   1.147 -      _setRowLowerBound(rows(id(r)),l - *e);
   1.148 -      _setRowUpperBound(rows(id(r)),u - *e);
   1.149 +      _setRowCoeffs(_rows(id(r)), ExprIterator(e.comps.begin(), _cols),
   1.150 +                    ExprIterator(e.comps.end(), _cols));
   1.151 +      _setRowLowerBound(_rows(id(r)),l - *e);
   1.152 +      _setRowUpperBound(_rows(id(r)),u - *e);
   1.153      }
   1.154  
   1.155      ///Set a row (i.e a constraint) of the LP
   1.156 @@ -1234,7 +1266,7 @@
   1.157      ///\return the expression associated to the row
   1.158      Expr row(Row r) const {
   1.159        Expr e;
   1.160 -      _getRowCoeffs(rows(id(r)), InsertIterator(e.comps, cols));
   1.161 +      _getRowCoeffs(_rows(id(r)), InsertIterator(e.comps, _cols));
   1.162        return e;
   1.163      }
   1.164  
   1.165 @@ -1247,8 +1279,8 @@
   1.166      Row addRow(Value l,const Expr &e, Value u) {
   1.167        Row r;
   1.168        e.simplify();
   1.169 -      r._id = _addRowId(_addRow(l - *e, ExprIterator(e.comps.begin(), cols),
   1.170 -                                ExprIterator(e.comps.end(), cols), u - *e));
   1.171 +      r._id = _addRowId(_addRow(l - *e, ExprIterator(e.comps.begin(), _cols),
   1.172 +                                ExprIterator(e.comps.end(), _cols), u - *e));
   1.173        return r;
   1.174      }
   1.175  
   1.176 @@ -1260,8 +1292,8 @@
   1.177        Row r;
   1.178        c.expr().simplify();
   1.179        r._id = _addRowId(_addRow(c.lowerBounded()?c.lowerBound()-*c.expr():-INF,
   1.180 -                                ExprIterator(c.expr().comps.begin(), cols),
   1.181 -                                ExprIterator(c.expr().comps.end(), cols),
   1.182 +                                ExprIterator(c.expr().comps.begin(), _cols),
   1.183 +                                ExprIterator(c.expr().comps.end(), _cols),
   1.184                                  c.upperBounded()?c.upperBound()-*c.expr():INF));
   1.185        return r;
   1.186      }
   1.187 @@ -1269,15 +1301,15 @@
   1.188  
   1.189      ///\param c is the column to be deleted
   1.190      void erase(Col c) {
   1.191 -      _eraseCol(cols(id(c)));
   1.192 -      _eraseColId(cols(id(c)));
   1.193 +      _eraseCol(_cols(id(c)));
   1.194 +      _eraseColId(_cols(id(c)));
   1.195      }
   1.196      ///Erase a row (i.e a constraint) from the LP
   1.197  
   1.198      ///\param r is the row to be deleted
   1.199      void erase(Row r) {
   1.200 -      _eraseRow(rows(id(r)));
   1.201 -      _eraseRowId(rows(id(r)));
   1.202 +      _eraseRow(_rows(id(r)));
   1.203 +      _eraseRowId(_rows(id(r)));
   1.204      }
   1.205  
   1.206      /// Get the name of a column
   1.207 @@ -1286,7 +1318,7 @@
   1.208      ///\return The name of the colunm
   1.209      std::string colName(Col c) const {
   1.210        std::string name;
   1.211 -      _getColName(cols(id(c)), name);
   1.212 +      _getColName(_cols(id(c)), name);
   1.213        return name;
   1.214      }
   1.215  
   1.216 @@ -1295,7 +1327,7 @@
   1.217      ///\param c is the coresponding column
   1.218      ///\param name The name to be given
   1.219      void colName(Col c, const std::string& name) {
   1.220 -      _setColName(cols(id(c)), name);
   1.221 +      _setColName(_cols(id(c)), name);
   1.222      }
   1.223  
   1.224      /// Get the column by its name
   1.225 @@ -1304,7 +1336,7 @@
   1.226      ///\return the proper column or \c INVALID
   1.227      Col colByName(const std::string& name) const {
   1.228        int k = _colByName(name);
   1.229 -      return k != -1 ? Col(cols[k]) : Col(INVALID);
   1.230 +      return k != -1 ? Col(_cols[k]) : Col(INVALID);
   1.231      }
   1.232  
   1.233      /// Get the name of a row
   1.234 @@ -1313,7 +1345,7 @@
   1.235      ///\return The name of the row
   1.236      std::string rowName(Row r) const {
   1.237        std::string name;
   1.238 -      _getRowName(rows(id(r)), name);
   1.239 +      _getRowName(_rows(id(r)), name);
   1.240        return name;
   1.241      }
   1.242  
   1.243 @@ -1322,7 +1354,7 @@
   1.244      ///\param r is the coresponding row
   1.245      ///\param name The name to be given
   1.246      void rowName(Row r, const std::string& name) {
   1.247 -      _setRowName(rows(id(r)), name);
   1.248 +      _setRowName(_rows(id(r)), name);
   1.249      }
   1.250  
   1.251      /// Get the row by its name
   1.252 @@ -1331,7 +1363,7 @@
   1.253      ///\return the proper row or \c INVALID
   1.254      Row rowByName(const std::string& name) const {
   1.255        int k = _rowByName(name);
   1.256 -      return k != -1 ? Row(rows[k]) : Row(INVALID);
   1.257 +      return k != -1 ? Row(_rows[k]) : Row(INVALID);
   1.258      }
   1.259  
   1.260      /// Set an element of the coefficient matrix of the LP
   1.261 @@ -1340,7 +1372,7 @@
   1.262      ///\param c is the column of the element to be modified
   1.263      ///\param val is the new value of the coefficient
   1.264      void coeff(Row r, Col c, Value val) {
   1.265 -      _setCoeff(rows(id(r)),cols(id(c)), val);
   1.266 +      _setCoeff(_rows(id(r)),_cols(id(c)), val);
   1.267      }
   1.268  
   1.269      /// Get an element of the coefficient matrix of the LP
   1.270 @@ -1349,7 +1381,7 @@
   1.271      ///\param c is the column of the element
   1.272      ///\return the corresponding coefficient
   1.273      Value coeff(Row r, Col c) const {
   1.274 -      return _getCoeff(rows(id(r)),cols(id(c)));
   1.275 +      return _getCoeff(_rows(id(r)),_cols(id(c)));
   1.276      }
   1.277  
   1.278      /// Set the lower bound of a column (i.e a variable)
   1.279 @@ -1358,7 +1390,7 @@
   1.280      /// extended number of type Value, i.e. a finite number of type
   1.281      /// Value or -\ref INF.
   1.282      void colLowerBound(Col c, Value value) {
   1.283 -      _setColLowerBound(cols(id(c)),value);
   1.284 +      _setColLowerBound(_cols(id(c)),value);
   1.285      }
   1.286  
   1.287      /// Get the lower bound of a column (i.e a variable)
   1.288 @@ -1367,7 +1399,7 @@
   1.289      /// (this might be -\ref INF as well).
   1.290      ///\return The lower bound for column \c c
   1.291      Value colLowerBound(Col c) const {
   1.292 -      return _getColLowerBound(cols(id(c)));
   1.293 +      return _getColLowerBound(_cols(id(c)));
   1.294      }
   1.295  
   1.296      ///\brief Set the lower bound of  several columns
   1.297 @@ -1413,7 +1445,7 @@
   1.298      /// extended number of type Value, i.e. a finite number of type
   1.299      /// Value or \ref INF.
   1.300      void colUpperBound(Col c, Value value) {
   1.301 -      _setColUpperBound(cols(id(c)),value);
   1.302 +      _setColUpperBound(_cols(id(c)),value);
   1.303      };
   1.304  
   1.305      /// Get the upper bound of a column (i.e a variable)
   1.306 @@ -1422,7 +1454,7 @@
   1.307      /// (this might be \ref INF as well).
   1.308      /// \return The upper bound for column \c c
   1.309      Value colUpperBound(Col c) const {
   1.310 -      return _getColUpperBound(cols(id(c)));
   1.311 +      return _getColUpperBound(_cols(id(c)));
   1.312      }
   1.313  
   1.314      ///\brief Set the upper bound of  several columns
   1.315 @@ -1469,8 +1501,8 @@
   1.316      /// extended number of type Value, i.e. a finite number of type
   1.317      /// Value, -\ref INF or \ref INF.
   1.318      void colBounds(Col c, Value lower, Value upper) {
   1.319 -      _setColLowerBound(cols(id(c)),lower);
   1.320 -      _setColUpperBound(cols(id(c)),upper);
   1.321 +      _setColLowerBound(_cols(id(c)),lower);
   1.322 +      _setColUpperBound(_cols(id(c)),upper);
   1.323      }
   1.324  
   1.325      ///\brief Set the lower and the upper bound of several columns
   1.326 @@ -1515,7 +1547,7 @@
   1.327      /// extended number of type Value, i.e. a finite number of type
   1.328      /// Value or -\ref INF.
   1.329      void rowLowerBound(Row r, Value value) {
   1.330 -      _setRowLowerBound(rows(id(r)),value);
   1.331 +      _setRowLowerBound(_rows(id(r)),value);
   1.332      }
   1.333  
   1.334      /// Get the lower bound of a row (i.e a constraint)
   1.335 @@ -1524,7 +1556,7 @@
   1.336      /// (this might be -\ref INF as well).
   1.337      ///\return The lower bound for row \c r
   1.338      Value rowLowerBound(Row r) const {
   1.339 -      return _getRowLowerBound(rows(id(r)));
   1.340 +      return _getRowLowerBound(_rows(id(r)));
   1.341      }
   1.342  
   1.343      /// Set the upper bound of a row (i.e a constraint)
   1.344 @@ -1533,7 +1565,7 @@
   1.345      /// extended number of type Value, i.e. a finite number of type
   1.346      /// Value or -\ref INF.
   1.347      void rowUpperBound(Row r, Value value) {
   1.348 -      _setRowUpperBound(rows(id(r)),value);
   1.349 +      _setRowUpperBound(_rows(id(r)),value);
   1.350      }
   1.351  
   1.352      /// Get the upper bound of a row (i.e a constraint)
   1.353 @@ -1542,22 +1574,22 @@
   1.354      /// (this might be -\ref INF as well).
   1.355      ///\return The upper bound for row \c r
   1.356      Value rowUpperBound(Row r) const {
   1.357 -      return _getRowUpperBound(rows(id(r)));
   1.358 +      return _getRowUpperBound(_rows(id(r)));
   1.359      }
   1.360  
   1.361      ///Set an element of the objective function
   1.362 -    void objCoeff(Col c, Value v) {_setObjCoeff(cols(id(c)),v); };
   1.363 +    void objCoeff(Col c, Value v) {_setObjCoeff(_cols(id(c)),v); };
   1.364  
   1.365      ///Get an element of the objective function
   1.366 -    Value objCoeff(Col c) const { return _getObjCoeff(cols(id(c))); };
   1.367 +    Value objCoeff(Col c) const { return _getObjCoeff(_cols(id(c))); };
   1.368  
   1.369      ///Set the objective function
   1.370  
   1.371      ///\param e is a linear expression of type \ref Expr.
   1.372      ///
   1.373      void obj(const Expr& e) {
   1.374 -      _setObjCoeffs(ExprIterator(e.comps.begin(), cols),
   1.375 -                    ExprIterator(e.comps.end(), cols));
   1.376 +      _setObjCoeffs(ExprIterator(e.comps.begin(), _cols),
   1.377 +                    ExprIterator(e.comps.end(), _cols));
   1.378        obj_const_comp = *e;
   1.379      }
   1.380  
   1.381 @@ -1567,7 +1599,7 @@
   1.382      ///Expr.
   1.383      Expr obj() const {
   1.384        Expr e;
   1.385 -      _getObjCoeffs(InsertIterator(e.comps, cols));
   1.386 +      _getObjCoeffs(InsertIterator(e.comps, _cols));
   1.387        *e = obj_const_comp;
   1.388        return e;
   1.389      }
   1.390 @@ -1586,7 +1618,7 @@
   1.391      void min() { _setSense(MIN); }
   1.392  
   1.393      ///Clear the problem
   1.394 -    void clear() { _clear(); rows.clear(); cols.clear(); }
   1.395 +    void clear() { _clear(); _rows.clear(); _cols.clear(); }
   1.396  
   1.397      /// Set the message level of the solver
   1.398      void messageLevel(MessageLevel level) { _messageLevel(level); }
   1.399 @@ -1929,7 +1961,7 @@
   1.400  
   1.401      /// Return the primal value of the column.
   1.402      /// \pre The problem is solved.
   1.403 -    Value primal(Col c) const { return _getPrimal(cols(id(c))); }
   1.404 +    Value primal(Col c) const { return _getPrimal(_cols(id(c))); }
   1.405  
   1.406      /// Return the primal value of the expression
   1.407  
   1.408 @@ -1956,13 +1988,13 @@
   1.409      /// \pre The problem is solved and the dual problem is infeasible.
   1.410      /// \note Some solvers does not provide primal ray calculation
   1.411      /// functions.
   1.412 -    Value primalRay(Col c) const { return _getPrimalRay(cols(id(c))); }
   1.413 +    Value primalRay(Col c) const { return _getPrimalRay(_cols(id(c))); }
   1.414  
   1.415      /// Return the dual value of the row
   1.416  
   1.417      /// Return the dual value of the row.
   1.418      /// \pre The problem is solved.
   1.419 -    Value dual(Row r) const { return _getDual(rows(id(r))); }
   1.420 +    Value dual(Row r) const { return _getDual(_rows(id(r))); }
   1.421  
   1.422      /// Return the dual value of the dual expression
   1.423  
   1.424 @@ -1990,17 +2022,17 @@
   1.425      /// \pre The problem is solved and the primal problem is infeasible.
   1.426      /// \note Some solvers does not provide dual ray calculation
   1.427      /// functions.
   1.428 -    Value dualRay(Row r) const { return _getDualRay(rows(id(r))); }
   1.429 +    Value dualRay(Row r) const { return _getDualRay(_rows(id(r))); }
   1.430  
   1.431      /// Return the basis status of the column
   1.432  
   1.433      /// \see VarStatus
   1.434 -    VarStatus colStatus(Col c) const { return _getColStatus(cols(id(c))); }
   1.435 +    VarStatus colStatus(Col c) const { return _getColStatus(_cols(id(c))); }
   1.436  
   1.437      /// Return the basis status of the row
   1.438  
   1.439      /// \see VarStatus
   1.440 -    VarStatus rowStatus(Row r) const { return _getRowStatus(rows(id(r))); }
   1.441 +    VarStatus rowStatus(Row r) const { return _getRowStatus(_rows(id(r))); }
   1.442  
   1.443      ///The value of the objective function
   1.444  
   1.445 @@ -2080,7 +2112,7 @@
   1.446      ///Sets the type of the given column to the given type.
   1.447      ///
   1.448      void colType(Col c, ColTypes col_type) {
   1.449 -      _setColType(cols(id(c)),col_type);
   1.450 +      _setColType(_cols(id(c)),col_type);
   1.451      }
   1.452  
   1.453      ///Gives back the type of the column.
   1.454 @@ -2088,7 +2120,7 @@
   1.455      ///Gives back the type of the column.
   1.456      ///
   1.457      ColTypes colType(Col c) const {
   1.458 -      return _getColType(cols(id(c)));
   1.459 +      return _getColType(_cols(id(c)));
   1.460      }
   1.461      ///@}
   1.462  
   1.463 @@ -2105,7 +2137,7 @@
   1.464  
   1.465      ///  Return the value of the row in the solution.
   1.466      /// \pre The problem is solved.
   1.467 -    Value sol(Col c) const { return _getSol(cols(id(c))); }
   1.468 +    Value sol(Col c) const { return _getSol(_cols(id(c))); }
   1.469  
   1.470      /// Return the value of the expression in the solution
   1.471