COIN-OR::LEMON - Graph Library

Changeset 1130:0759d974de81 in lemon-main for lemon/lp_base.h


Ignore:
Timestamp:
01/05/14 22:24:56 (10 years ago)
Author:
Gabor Gevay <ggab90@…>
Branch:
default
Phase:
public
Message:

STL style iterators (#325)

For

  • graph types,
  • graph adaptors,
  • paths,
  • iterable maps,
  • LP rows/cols and
  • active nodes is BellmanFord?
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/lp_base.h

    r1092 r1130  
    3232#include<lemon/bits/solver_bits.h>
    3333
     34#include<lemon/bits/stl_iterators.h>
     35
    3436///\file
    3537///\brief The interface of the LP solver interface.
     
    4648  protected:
    4749
    48     _solver_bits::VarIndex rows;
    49     _solver_bits::VarIndex cols;
     50    _solver_bits::VarIndex _rows;
     51    _solver_bits::VarIndex _cols;
    5052
    5153  public:
     
    167169      ColIt(const LpBase &solver) : _solver(&solver)
    168170      {
    169         _solver->cols.firstItem(_id);
     171        _solver->_cols.firstItem(_id);
    170172      }
    171173      /// Invalid constructor \& conversion
     
    180182      ColIt &operator++()
    181183      {
    182         _solver->cols.nextItem(_id);
     184        _solver->_cols.nextItem(_id);
    183185        return *this;
    184186      }
    185187    };
     188
     189    /// \brief Gets the collection of the columns of the LP problem.
     190    ///
     191    /// This function can be used for iterating on
     192    /// the columns of the LP problem. It returns a wrapped ColIt, which looks
     193    /// like an STL container (by having begin() and end())
     194    /// which you can use in range-based for loops, STL algorithms, etc.
     195    /// For example you can write:
     196    ///\code
     197    /// for(auto c: lp.cols())
     198    ///   doSomething(c);
     199    LemonRangeWrapper1<ColIt, LpBase> cols() {
     200      return LemonRangeWrapper1<ColIt, LpBase>(*this);
     201    }
     202
    186203
    187204    /// \brief Returns the ID of the column.
     
    262279      RowIt(const LpBase &solver) : _solver(&solver)
    263280      {
    264         _solver->rows.firstItem(_id);
     281        _solver->_rows.firstItem(_id);
    265282      }
    266283      /// Invalid constructor \& conversion
     
    275292      RowIt &operator++()
    276293      {
    277         _solver->rows.nextItem(_id);
     294        _solver->_rows.nextItem(_id);
    278295        return *this;
    279296      }
    280297    };
     298   
     299    /// \brief Gets the collection of the rows of the LP problem.
     300    ///
     301    /// This function can be used for iterating on
     302    /// the rows of the LP problem. It returns a wrapped RowIt, which looks
     303    /// like an STL container (by having begin() and end())
     304    /// which you can use in range-based for loops, STL algorithms, etc.
     305    /// For example you can write:
     306    ///\code
     307    /// for(auto c: lp.rows())
     308    ///   doSomething(c);
     309    LemonRangeWrapper1<RowIt, LpBase> rows() {
     310      return LemonRangeWrapper1<RowIt, LpBase>(*this);
     311    }
     312   
    281313
    282314    /// \brief Returns the ID of the row.
     
    935967    //Abstract virtual functions
    936968
    937     virtual int _addColId(int col) { return cols.addIndex(col); }
    938     virtual int _addRowId(int row) { return rows.addIndex(row); }
    939 
    940     virtual void _eraseColId(int col) { cols.eraseIndex(col); }
    941     virtual void _eraseRowId(int row) { rows.eraseIndex(row); }
     969    virtual int _addColId(int col) { return _cols.addIndex(col); }
     970    virtual int _addRowId(int row) { return _rows.addIndex(row); }
     971
     972    virtual void _eraseColId(int col) { _cols.eraseIndex(col); }
     973    virtual void _eraseRowId(int row) { _rows.eraseIndex(row); }
    942974
    943975    virtual int _addCol() = 0;
     
    10041036    Value obj_const_comp;
    10051037
    1006     LpBase() : rows(), cols(), obj_const_comp(0) {}
     1038    LpBase() : _rows(), _cols(), obj_const_comp(0) {}
    10071039
    10081040  public:
     
    11161148    void col(Col c, const DualExpr &e) {
    11171149      e.simplify();
    1118       _setColCoeffs(cols(id(c)), ExprIterator(e.comps.begin(), rows),
    1119                     ExprIterator(e.comps.end(), rows));
     1150      _setColCoeffs(_cols(id(c)), ExprIterator(e.comps.begin(), _rows),
     1151                    ExprIterator(e.comps.end(), _rows));
    11201152    }
    11211153
     
    11261158    DualExpr col(Col c) const {
    11271159      DualExpr e;
    1128       _getColCoeffs(cols(id(c)), InsertIterator(e.comps, rows));
     1160      _getColCoeffs(_cols(id(c)), InsertIterator(e.comps, _rows));
    11291161      return e;
    11301162    }
     
    12131245    void row(Row r, Value l, const Expr &e, Value u) {
    12141246      e.simplify();
    1215       _setRowCoeffs(rows(id(r)), ExprIterator(e.comps.begin(), cols),
    1216                     ExprIterator(e.comps.end(), cols));
    1217       _setRowLowerBound(rows(id(r)),l - *e);
    1218       _setRowUpperBound(rows(id(r)),u - *e);
     1247      _setRowCoeffs(_rows(id(r)), ExprIterator(e.comps.begin(), _cols),
     1248                    ExprIterator(e.comps.end(), _cols));
     1249      _setRowLowerBound(_rows(id(r)),l - *e);
     1250      _setRowUpperBound(_rows(id(r)),u - *e);
    12191251    }
    12201252
     
    12351267    Expr row(Row r) const {
    12361268      Expr e;
    1237       _getRowCoeffs(rows(id(r)), InsertIterator(e.comps, cols));
     1269      _getRowCoeffs(_rows(id(r)), InsertIterator(e.comps, _cols));
    12381270      return e;
    12391271    }
     
    12481280      Row r;
    12491281      e.simplify();
    1250       r._id = _addRowId(_addRow(l - *e, ExprIterator(e.comps.begin(), cols),
    1251                                 ExprIterator(e.comps.end(), cols), u - *e));
     1282      r._id = _addRowId(_addRow(l - *e, ExprIterator(e.comps.begin(), _cols),
     1283                                ExprIterator(e.comps.end(), _cols), u - *e));
    12521284      return r;
    12531285    }
     
    12611293      c.expr().simplify();
    12621294      r._id = _addRowId(_addRow(c.lowerBounded()?c.lowerBound()-*c.expr():-INF,
    1263                                 ExprIterator(c.expr().comps.begin(), cols),
    1264                                 ExprIterator(c.expr().comps.end(), cols),
     1295                                ExprIterator(c.expr().comps.begin(), _cols),
     1296                                ExprIterator(c.expr().comps.end(), _cols),
    12651297                                c.upperBounded()?c.upperBound()-*c.expr():INF));
    12661298      return r;
     
    12701302    ///\param c is the column to be deleted
    12711303    void erase(Col c) {
    1272       _eraseCol(cols(id(c)));
    1273       _eraseColId(cols(id(c)));
     1304      _eraseCol(_cols(id(c)));
     1305      _eraseColId(_cols(id(c)));
    12741306    }
    12751307    ///Erase a row (i.e a constraint) from the LP
     
    12771309    ///\param r is the row to be deleted
    12781310    void erase(Row r) {
    1279       _eraseRow(rows(id(r)));
    1280       _eraseRowId(rows(id(r)));
     1311      _eraseRow(_rows(id(r)));
     1312      _eraseRowId(_rows(id(r)));
    12811313    }
    12821314
     
    12871319    std::string colName(Col c) const {
    12881320      std::string name;
    1289       _getColName(cols(id(c)), name);
     1321      _getColName(_cols(id(c)), name);
    12901322      return name;
    12911323    }
     
    12961328    ///\param name The name to be given
    12971329    void colName(Col c, const std::string& name) {
    1298       _setColName(cols(id(c)), name);
     1330      _setColName(_cols(id(c)), name);
    12991331    }
    13001332
     
    13051337    Col colByName(const std::string& name) const {
    13061338      int k = _colByName(name);
    1307       return k != -1 ? Col(cols[k]) : Col(INVALID);
     1339      return k != -1 ? Col(_cols[k]) : Col(INVALID);
    13081340    }
    13091341
     
    13141346    std::string rowName(Row r) const {
    13151347      std::string name;
    1316       _getRowName(rows(id(r)), name);
     1348      _getRowName(_rows(id(r)), name);
    13171349      return name;
    13181350    }
     
    13231355    ///\param name The name to be given
    13241356    void rowName(Row r, const std::string& name) {
    1325       _setRowName(rows(id(r)), name);
     1357      _setRowName(_rows(id(r)), name);
    13261358    }
    13271359
     
    13321364    Row rowByName(const std::string& name) const {
    13331365      int k = _rowByName(name);
    1334       return k != -1 ? Row(rows[k]) : Row(INVALID);
     1366      return k != -1 ? Row(_rows[k]) : Row(INVALID);
    13351367    }
    13361368
     
    13411373    ///\param val is the new value of the coefficient
    13421374    void coeff(Row r, Col c, Value val) {
    1343       _setCoeff(rows(id(r)),cols(id(c)), val);
     1375      _setCoeff(_rows(id(r)),_cols(id(c)), val);
    13441376    }
    13451377
     
    13501382    ///\return the corresponding coefficient
    13511383    Value coeff(Row r, Col c) const {
    1352       return _getCoeff(rows(id(r)),cols(id(c)));
     1384      return _getCoeff(_rows(id(r)),_cols(id(c)));
    13531385    }
    13541386
     
    13591391    /// Value or -\ref INF.
    13601392    void colLowerBound(Col c, Value value) {
    1361       _setColLowerBound(cols(id(c)),value);
     1393      _setColLowerBound(_cols(id(c)),value);
    13621394    }
    13631395
     
    13681400    ///\return The lower bound for column \c c
    13691401    Value colLowerBound(Col c) const {
    1370       return _getColLowerBound(cols(id(c)));
     1402      return _getColLowerBound(_cols(id(c)));
    13711403    }
    13721404
     
    14141446    /// Value or \ref INF.
    14151447    void colUpperBound(Col c, Value value) {
    1416       _setColUpperBound(cols(id(c)),value);
     1448      _setColUpperBound(_cols(id(c)),value);
    14171449    };
    14181450
     
    14231455    /// \return The upper bound for column \c c
    14241456    Value colUpperBound(Col c) const {
    1425       return _getColUpperBound(cols(id(c)));
     1457      return _getColUpperBound(_cols(id(c)));
    14261458    }
    14271459
     
    14701502    /// Value, -\ref INF or \ref INF.
    14711503    void colBounds(Col c, Value lower, Value upper) {
    1472       _setColLowerBound(cols(id(c)),lower);
    1473       _setColUpperBound(cols(id(c)),upper);
     1504      _setColLowerBound(_cols(id(c)),lower);
     1505      _setColUpperBound(_cols(id(c)),upper);
    14741506    }
    14751507
     
    15161548    /// Value or -\ref INF.
    15171549    void rowLowerBound(Row r, Value value) {
    1518       _setRowLowerBound(rows(id(r)),value);
     1550      _setRowLowerBound(_rows(id(r)),value);
    15191551    }
    15201552
     
    15251557    ///\return The lower bound for row \c r
    15261558    Value rowLowerBound(Row r) const {
    1527       return _getRowLowerBound(rows(id(r)));
     1559      return _getRowLowerBound(_rows(id(r)));
    15281560    }
    15291561
     
    15341566    /// Value or -\ref INF.
    15351567    void rowUpperBound(Row r, Value value) {
    1536       _setRowUpperBound(rows(id(r)),value);
     1568      _setRowUpperBound(_rows(id(r)),value);
    15371569    }
    15381570
     
    15431575    ///\return The upper bound for row \c r
    15441576    Value rowUpperBound(Row r) const {
    1545       return _getRowUpperBound(rows(id(r)));
     1577      return _getRowUpperBound(_rows(id(r)));
    15461578    }
    15471579
    15481580    ///Set an element of the objective function
    1549     void objCoeff(Col c, Value v) {_setObjCoeff(cols(id(c)),v); };
     1581    void objCoeff(Col c, Value v) {_setObjCoeff(_cols(id(c)),v); };
    15501582
    15511583    ///Get an element of the objective function
    1552     Value objCoeff(Col c) const { return _getObjCoeff(cols(id(c))); };
     1584    Value objCoeff(Col c) const { return _getObjCoeff(_cols(id(c))); };
    15531585
    15541586    ///Set the objective function
     
    15571589    ///
    15581590    void obj(const Expr& e) {
    1559       _setObjCoeffs(ExprIterator(e.comps.begin(), cols),
    1560                     ExprIterator(e.comps.end(), cols));
     1591      _setObjCoeffs(ExprIterator(e.comps.begin(), _cols),
     1592                    ExprIterator(e.comps.end(), _cols));
    15611593      obj_const_comp = *e;
    15621594    }
     
    15681600    Expr obj() const {
    15691601      Expr e;
    1570       _getObjCoeffs(InsertIterator(e.comps, cols));
     1602      _getObjCoeffs(InsertIterator(e.comps, _cols));
    15711603      *e = obj_const_comp;
    15721604      return e;
     
    15871619
    15881620    ///Clear the problem
    1589     void clear() { _clear(); rows.clear(); cols.clear(); }
     1621    void clear() { _clear(); _rows.clear(); _cols.clear(); }
    15901622
    15911623    /// Set the message level of the solver
     
    19301962    /// Return the primal value of the column.
    19311963    /// \pre The problem is solved.
    1932     Value primal(Col c) const { return _getPrimal(cols(id(c))); }
     1964    Value primal(Col c) const { return _getPrimal(_cols(id(c))); }
    19331965
    19341966    /// Return the primal value of the expression
     
    19571989    /// \note Some solvers does not provide primal ray calculation
    19581990    /// functions.
    1959     Value primalRay(Col c) const { return _getPrimalRay(cols(id(c))); }
     1991    Value primalRay(Col c) const { return _getPrimalRay(_cols(id(c))); }
    19601992
    19611993    /// Return the dual value of the row
     
    19631995    /// Return the dual value of the row.
    19641996    /// \pre The problem is solved.
    1965     Value dual(Row r) const { return _getDual(rows(id(r))); }
     1997    Value dual(Row r) const { return _getDual(_rows(id(r))); }
    19661998
    19671999    /// Return the dual value of the dual expression
     
    19912023    /// \note Some solvers does not provide dual ray calculation
    19922024    /// functions.
    1993     Value dualRay(Row r) const { return _getDualRay(rows(id(r))); }
     2025    Value dualRay(Row r) const { return _getDualRay(_rows(id(r))); }
    19942026
    19952027    /// Return the basis status of the column
    19962028
    19972029    /// \see VarStatus
    1998     VarStatus colStatus(Col c) const { return _getColStatus(cols(id(c))); }
     2030    VarStatus colStatus(Col c) const { return _getColStatus(_cols(id(c))); }
    19992031
    20002032    /// Return the basis status of the row
    20012033
    20022034    /// \see VarStatus
    2003     VarStatus rowStatus(Row r) const { return _getRowStatus(rows(id(r))); }
     2035    VarStatus rowStatus(Row r) const { return _getRowStatus(_rows(id(r))); }
    20042036
    20052037    ///The value of the objective function
     
    20812113    ///
    20822114    void colType(Col c, ColTypes col_type) {
    2083       _setColType(cols(id(c)),col_type);
     2115      _setColType(_cols(id(c)),col_type);
    20842116    }
    20852117
     
    20892121    ///
    20902122    ColTypes colType(Col c) const {
    2091       return _getColType(cols(id(c)));
     2123      return _getColType(_cols(id(c)));
    20922124    }
    20932125    ///@}
     
    21062138    ///  Return the value of the row in the solution.
    21072139    /// \pre The problem is solved.
    2108     Value sol(Col c) const { return _getSol(cols(id(c))); }
     2140    Value sol(Col c) const { return _getSol(_cols(id(c))); }
    21092141
    21102142    /// Return the value of the expression in the solution
Note: See TracChangeset for help on using the changeset viewer.