Changeset 1130:0759d974de81 in lemon-main for lemon/lp_base.h
- Timestamp:
- 01/05/14 22:24:56 (10 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/lp_base.h
r1092 r1130 32 32 #include<lemon/bits/solver_bits.h> 33 33 34 #include<lemon/bits/stl_iterators.h> 35 34 36 ///\file 35 37 ///\brief The interface of the LP solver interface. … … 46 48 protected: 47 49 48 _solver_bits::VarIndex rows;49 _solver_bits::VarIndex cols;50 _solver_bits::VarIndex _rows; 51 _solver_bits::VarIndex _cols; 50 52 51 53 public: … … 167 169 ColIt(const LpBase &solver) : _solver(&solver) 168 170 { 169 _solver-> cols.firstItem(_id);171 _solver->_cols.firstItem(_id); 170 172 } 171 173 /// Invalid constructor \& conversion … … 180 182 ColIt &operator++() 181 183 { 182 _solver-> cols.nextItem(_id);184 _solver->_cols.nextItem(_id); 183 185 return *this; 184 186 } 185 187 }; 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 186 203 187 204 /// \brief Returns the ID of the column. … … 262 279 RowIt(const LpBase &solver) : _solver(&solver) 263 280 { 264 _solver-> rows.firstItem(_id);281 _solver->_rows.firstItem(_id); 265 282 } 266 283 /// Invalid constructor \& conversion … … 275 292 RowIt &operator++() 276 293 { 277 _solver-> rows.nextItem(_id);294 _solver->_rows.nextItem(_id); 278 295 return *this; 279 296 } 280 297 }; 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 281 313 282 314 /// \brief Returns the ID of the row. … … 935 967 //Abstract virtual functions 936 968 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); } 942 974 943 975 virtual int _addCol() = 0; … … 1004 1036 Value obj_const_comp; 1005 1037 1006 LpBase() : rows(),cols(), obj_const_comp(0) {}1038 LpBase() : _rows(), _cols(), obj_const_comp(0) {} 1007 1039 1008 1040 public: … … 1116 1148 void col(Col c, const DualExpr &e) { 1117 1149 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)); 1120 1152 } 1121 1153 … … 1126 1158 DualExpr col(Col c) const { 1127 1159 DualExpr e; 1128 _getColCoeffs( cols(id(c)), InsertIterator(e.comps,rows));1160 _getColCoeffs(_cols(id(c)), InsertIterator(e.comps, _rows)); 1129 1161 return e; 1130 1162 } … … 1213 1245 void row(Row r, Value l, const Expr &e, Value u) { 1214 1246 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); 1219 1251 } 1220 1252 … … 1235 1267 Expr row(Row r) const { 1236 1268 Expr e; 1237 _getRowCoeffs( rows(id(r)), InsertIterator(e.comps,cols));1269 _getRowCoeffs(_rows(id(r)), InsertIterator(e.comps, _cols)); 1238 1270 return e; 1239 1271 } … … 1248 1280 Row r; 1249 1281 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)); 1252 1284 return r; 1253 1285 } … … 1261 1293 c.expr().simplify(); 1262 1294 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), 1265 1297 c.upperBounded()?c.upperBound()-*c.expr():INF)); 1266 1298 return r; … … 1270 1302 ///\param c is the column to be deleted 1271 1303 void erase(Col c) { 1272 _eraseCol( cols(id(c)));1273 _eraseColId( cols(id(c)));1304 _eraseCol(_cols(id(c))); 1305 _eraseColId(_cols(id(c))); 1274 1306 } 1275 1307 ///Erase a row (i.e a constraint) from the LP … … 1277 1309 ///\param r is the row to be deleted 1278 1310 void erase(Row r) { 1279 _eraseRow( rows(id(r)));1280 _eraseRowId( rows(id(r)));1311 _eraseRow(_rows(id(r))); 1312 _eraseRowId(_rows(id(r))); 1281 1313 } 1282 1314 … … 1287 1319 std::string colName(Col c) const { 1288 1320 std::string name; 1289 _getColName( cols(id(c)), name);1321 _getColName(_cols(id(c)), name); 1290 1322 return name; 1291 1323 } … … 1296 1328 ///\param name The name to be given 1297 1329 void colName(Col c, const std::string& name) { 1298 _setColName( cols(id(c)), name);1330 _setColName(_cols(id(c)), name); 1299 1331 } 1300 1332 … … 1305 1337 Col colByName(const std::string& name) const { 1306 1338 int k = _colByName(name); 1307 return k != -1 ? Col( cols[k]) : Col(INVALID);1339 return k != -1 ? Col(_cols[k]) : Col(INVALID); 1308 1340 } 1309 1341 … … 1314 1346 std::string rowName(Row r) const { 1315 1347 std::string name; 1316 _getRowName( rows(id(r)), name);1348 _getRowName(_rows(id(r)), name); 1317 1349 return name; 1318 1350 } … … 1323 1355 ///\param name The name to be given 1324 1356 void rowName(Row r, const std::string& name) { 1325 _setRowName( rows(id(r)), name);1357 _setRowName(_rows(id(r)), name); 1326 1358 } 1327 1359 … … 1332 1364 Row rowByName(const std::string& name) const { 1333 1365 int k = _rowByName(name); 1334 return k != -1 ? Row( rows[k]) : Row(INVALID);1366 return k != -1 ? Row(_rows[k]) : Row(INVALID); 1335 1367 } 1336 1368 … … 1341 1373 ///\param val is the new value of the coefficient 1342 1374 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); 1344 1376 } 1345 1377 … … 1350 1382 ///\return the corresponding coefficient 1351 1383 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))); 1353 1385 } 1354 1386 … … 1359 1391 /// Value or -\ref INF. 1360 1392 void colLowerBound(Col c, Value value) { 1361 _setColLowerBound( cols(id(c)),value);1393 _setColLowerBound(_cols(id(c)),value); 1362 1394 } 1363 1395 … … 1368 1400 ///\return The lower bound for column \c c 1369 1401 Value colLowerBound(Col c) const { 1370 return _getColLowerBound( cols(id(c)));1402 return _getColLowerBound(_cols(id(c))); 1371 1403 } 1372 1404 … … 1414 1446 /// Value or \ref INF. 1415 1447 void colUpperBound(Col c, Value value) { 1416 _setColUpperBound( cols(id(c)),value);1448 _setColUpperBound(_cols(id(c)),value); 1417 1449 }; 1418 1450 … … 1423 1455 /// \return The upper bound for column \c c 1424 1456 Value colUpperBound(Col c) const { 1425 return _getColUpperBound( cols(id(c)));1457 return _getColUpperBound(_cols(id(c))); 1426 1458 } 1427 1459 … … 1470 1502 /// Value, -\ref INF or \ref INF. 1471 1503 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); 1474 1506 } 1475 1507 … … 1516 1548 /// Value or -\ref INF. 1517 1549 void rowLowerBound(Row r, Value value) { 1518 _setRowLowerBound( rows(id(r)),value);1550 _setRowLowerBound(_rows(id(r)),value); 1519 1551 } 1520 1552 … … 1525 1557 ///\return The lower bound for row \c r 1526 1558 Value rowLowerBound(Row r) const { 1527 return _getRowLowerBound( rows(id(r)));1559 return _getRowLowerBound(_rows(id(r))); 1528 1560 } 1529 1561 … … 1534 1566 /// Value or -\ref INF. 1535 1567 void rowUpperBound(Row r, Value value) { 1536 _setRowUpperBound( rows(id(r)),value);1568 _setRowUpperBound(_rows(id(r)),value); 1537 1569 } 1538 1570 … … 1543 1575 ///\return The upper bound for row \c r 1544 1576 Value rowUpperBound(Row r) const { 1545 return _getRowUpperBound( rows(id(r)));1577 return _getRowUpperBound(_rows(id(r))); 1546 1578 } 1547 1579 1548 1580 ///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); }; 1550 1582 1551 1583 ///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))); }; 1553 1585 1554 1586 ///Set the objective function … … 1557 1589 /// 1558 1590 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)); 1561 1593 obj_const_comp = *e; 1562 1594 } … … 1568 1600 Expr obj() const { 1569 1601 Expr e; 1570 _getObjCoeffs(InsertIterator(e.comps, cols));1602 _getObjCoeffs(InsertIterator(e.comps, _cols)); 1571 1603 *e = obj_const_comp; 1572 1604 return e; … … 1587 1619 1588 1620 ///Clear the problem 1589 void clear() { _clear(); rows.clear();cols.clear(); }1621 void clear() { _clear(); _rows.clear(); _cols.clear(); } 1590 1622 1591 1623 /// Set the message level of the solver … … 1930 1962 /// Return the primal value of the column. 1931 1963 /// \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))); } 1933 1965 1934 1966 /// Return the primal value of the expression … … 1957 1989 /// \note Some solvers does not provide primal ray calculation 1958 1990 /// functions. 1959 Value primalRay(Col c) const { return _getPrimalRay( cols(id(c))); }1991 Value primalRay(Col c) const { return _getPrimalRay(_cols(id(c))); } 1960 1992 1961 1993 /// Return the dual value of the row … … 1963 1995 /// Return the dual value of the row. 1964 1996 /// \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))); } 1966 1998 1967 1999 /// Return the dual value of the dual expression … … 1991 2023 /// \note Some solvers does not provide dual ray calculation 1992 2024 /// functions. 1993 Value dualRay(Row r) const { return _getDualRay( rows(id(r))); }2025 Value dualRay(Row r) const { return _getDualRay(_rows(id(r))); } 1994 2026 1995 2027 /// Return the basis status of the column 1996 2028 1997 2029 /// \see VarStatus 1998 VarStatus colStatus(Col c) const { return _getColStatus( cols(id(c))); }2030 VarStatus colStatus(Col c) const { return _getColStatus(_cols(id(c))); } 1999 2031 2000 2032 /// Return the basis status of the row 2001 2033 2002 2034 /// \see VarStatus 2003 VarStatus rowStatus(Row r) const { return _getRowStatus( rows(id(r))); }2035 VarStatus rowStatus(Row r) const { return _getRowStatus(_rows(id(r))); } 2004 2036 2005 2037 ///The value of the objective function … … 2081 2113 /// 2082 2114 void colType(Col c, ColTypes col_type) { 2083 _setColType( cols(id(c)),col_type);2115 _setColType(_cols(id(c)),col_type); 2084 2116 } 2085 2117 … … 2089 2121 /// 2090 2122 ColTypes colType(Col c) const { 2091 return _getColType( cols(id(c)));2123 return _getColType(_cols(id(c))); 2092 2124 } 2093 2125 ///@} … … 2106 2138 /// Return the value of the row in the solution. 2107 2139 /// \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))); } 2109 2141 2110 2142 /// Return the value of the expression in the solution
Note: See TracChangeset
for help on using the changeset viewer.