Changeset 1256:3bb4ed285c39 in lemon-0.x for src/work/athos/lp
- Timestamp:
- 03/25/05 09:18:27 (20 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1683
- Location:
- src/work/athos/lp
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/work/athos/lp/Makefile
r1254 r1256 2 2 3 3 all: lp_test 4 5 clean: 6 rm lp_test *.o 4 7 5 8 lp_base.o: lp_base.cc lp_base.h lin_expr.h -
src/work/athos/lp/lp_base.cc
r1253 r1256 20 20 #include "lp_base.h" 21 21 namespace lemon { 22 22 23 const LpSolverBase::Value 24 LpSolverBase::INF = std::numeric_limits<Value>::infinity(); 23 25 24 26 -
src/work/athos/lp/lp_base.h
r1253 r1256 19 19 20 20 #include<vector> 21 21 #include<limits> 22 23 #include<lemon/utility.h> 22 24 #include<lemon/error.h> 25 #include<lemon/invalid.h> 23 26 24 27 #include"lin_expr.h" … … 66 69 first_free=next; 67 70 } 71 return cross[n]; 68 72 } 69 73 else throw LogicError(); //floatingId-s must form a continuous range; … … 97 101 public: 98 102 99 /// \e103 ///The floating point type used by the solver 100 104 typedef double Value; 101 /// \e105 ///The infinity constant 102 106 static const Value INF; 103 107 104 ///\e 105 class Col { protected: int id; friend class LpSolverBase; }; 106 ///\e 107 class Row { protected: int id; friend class LpSolverBase; }; 108 ///Refer to a column of the LP. 109 110 ///This type is used to refer to a column of the LP. 111 /// 112 ///Its value remains valid and correct even after the addition or erase of 113 ///new column (unless the referred column itself was also deleted, 114 ///of course). 115 /// 116 ///\todo Document what can one do with a Col (INVALID, comparing, 117 ///it is similar to Node/Edge) 118 class Col { 119 protected: 120 int id; 121 friend class LpSolverBase; 122 public: 123 typedef True LpSolverCol; 124 Col() {} 125 Col(const Invalid&) : id(-1) {} 126 bool operator<(Col c) const {return id<c.id;} 127 bool operator==(Col c) const {return id==c.id;} 128 bool operator!=(Col c) const {return id==c.id;} 129 }; 130 131 ///Refer to a row of the LP. 132 133 ///This type is used to refer to a row of the LP. 134 /// 135 ///Its value remains valid and correct even after the addition or erase of 136 ///new rows (unless the referred row itself was also deleted, of course). 137 /// 138 ///\todo Document what can one do with a Row (INVALID, comparing, 139 ///it is similar to Node/Edge) 140 class Row { 141 protected: 142 int id; 143 friend class LpSolverBase; 144 public: 145 typedef True LpSolverRow; 146 Row() {} 147 Row(const Invalid&) : id(-1) {} 148 typedef True LpSolverRow; 149 bool operator<(Row c) const {return id<c.id;} 150 bool operator==(Row c) const {return id==c.id;} 151 bool operator!=(Row c) const {return id==c.id;} 152 }; 108 153 109 154 typedef SparseLinExpr<Col, Value> Expr; … … 176 221 ///Add a new empty column (i.e a new variable) to the LP 177 222 Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;} 223 ///\brief Fill the elements of a container with newly created columns 224 ///(i.e a new variables) 225 /// 226 ///This magic function takes container as its argument 227 ///and fills its elements 228 ///with new columns (i.e. variables) 229 ///\param t can be either any standard STL iterable container with 230 ///\ref Col \c values_type or \c mapped_type 231 ///like <tt>std::vector<LpSolverBase::Col></tt>, 232 /// <tt>std::list<LpSolverBase::Col></tt> or 233 /// <tt>std::map<AnyType,LpSolverBase::Col></tt> or 234 ///it can be an iterable lemon map like 235 /// <tt>ListGraph::NodeMap<LpSolverBase::Col></tt>. 236 ///\return The number of the created column. 237 ///\bug Iterable nodemap hasn't been implemented yet. 238 #ifdef DOXYGEN 239 template<class T> 240 int addColSet(T &t) { return 0;} 241 #else 242 template<class T> 243 typename enable_if<typename T::value_type::LpSolverCol,int>::type 244 addColSet(T &t,dummy<0> = 0) { 245 int s=0; 246 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;} 247 return s; 248 } 249 template<class T> 250 typename enable_if<typename T::value_type::second_type::LpSolverCol, 251 int>::type 252 addColSet(T &t,dummy<1> = 1) { 253 int s=0; 254 for(typename T::iterator i=t.begin();i!=t.end();++i) { 255 i->second=addCol(); 256 s++; 257 } 258 return s; 259 } 260 #endif 178 261 ///Add a new empty row (i.e a new constaint) to the LP 179 262 Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;} … … 192 275 indices.push_back(0); 193 276 values.push_back(0); 194 for(Expr::iterator i=e.begin(); i!=e.end(); ++i) { 195 indices.push_back(cols.floatingId((*i).first.id)); 196 values.push_back((*i).second); 197 } 277 for(Expr::iterator i=e.begin(); i!=e.end(); ++i) 278 if((*i).second!=0) { ///\bug EPSILON would be necessary here!!! 279 indices.push_back(cols.floatingId((*i).first.id)); 280 values.push_back((*i).second); 281 } 198 282 _setRowCoeffs(rows.floatingId(r.id),indices.size()-1, 199 283 &indices[0],&values[0]); 200 _setRowLowerBound(rows.floatingId(r.id),l );201 _setRowUpperBound(rows.floatingId(r.id), l);284 _setRowLowerBound(rows.floatingId(r.id),l-e.constComp()); 285 _setRowUpperBound(rows.floatingId(r.id),u-e.constComp()); 202 286 return r; 203 287 } -
src/work/athos/lp/lp_test.cc
r1254 r1256 5 5 int main() 6 6 { 7 LpSolverSkeleton lp; 7 typedef LpSolverSkeleton LP; 8 LP lp; 9 10 std::vector<LP::Col> x; 11 for(int i=0;i<10;i++) x.push_back(lp.addCol()); 12 13 std::vector<LP::Col> y(10); 14 lp.addColSet(y); 15 16 std::map<int,LP::Col> z; 17 18 z.insert(std::make_pair(12,INVALID)); 19 z.insert(std::make_pair(2,INVALID)); 20 z.insert(std::make_pair(7,INVALID)); 21 z.insert(std::make_pair(5,INVALID)); 22 23 lp.addColSet(z); 24 25 26 LP::Expr e; 27 e[x[3]]=2; 28 e[x[3]]=4; 29 e[x[3]]=1; 30 e.constComp()=12; 31 lp.addRow(LP::INF,e,23); 32 8 33 }
Note: See TracChangeset
for help on using the changeset viewer.