Changeset 1152:1765ff9fefa1 in lemon-0.x for src/work/marci/lp
- Timestamp:
- 02/16/05 22:40:16 (20 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1553
- Location:
- src/work/marci/lp
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/work/marci/lp/lp_solver_base.h
r1144 r1152 1 1 // -*- c++ -*- 2 #ifndef LEMON_LP_SOLVER_ WRAPPER_H3 #define LEMON_LP_SOLVER_ WRAPPER_H2 #ifndef LEMON_LP_SOLVER_BASE_H 3 #define LEMON_LP_SOLVER_BASE_H 4 4 5 5 ///\ingroup misc 6 6 ///\file 7 ///\brief Dijkstra algorithm.8 7 9 8 // #include <stdio.h> … … 25 24 #include <utility> 26 25 27 //#include <lemon/list_graph.h>28 26 #include <lemon/invalid.h> 29 27 #include <expression.h> 30 //#include <bfs_dfs.h>31 28 //#include <stp.h> 32 29 //#include <lemon/max_flow.h> … … 68 65 int classNum() const { return tips.size(); } 69 66 /// This lemon style iterator iterates through a class. 70 class Class It;67 class Class; 71 68 /// Constructor. The number of classes is to be given which is fixed 72 69 /// over the life of the container. … … 80 77 } 81 78 protected: 82 void befuz(Class Itit, int class_id) {79 void befuz(Class it, int class_id) { 83 80 if (tips[class_id].first==-1) { 84 81 if (tips[class_id].last==-1) { … … 93 90 } 94 91 } 95 void kifuz(Class Itit, int class_id) {92 void kifuz(Class it, int class_id) { 96 93 if (tips[class_id].first==it.i) { 97 94 if (tips[class_id].last==it.i) { … … 114 111 /// A new element with data \c t is pushed into the vector and into class 115 112 /// \c class_id. 116 Class Itpush_back(const T& t, int class_id) {113 Class push_back(const T& t, int class_id) { 117 114 Node n; 118 115 n.data=t; … … 123 120 } 124 121 /// A member is moved to an other class. 125 void set(Class Itit, int old_class_id, int new_class_id) {122 void set(Class it, int old_class_id, int new_class_id) { 126 123 kifuz(it.i, old_class_id); 127 124 befuz(it.i, new_class_id); 128 125 } 129 126 /// Returns the data pointed by \c it. 130 T& operator[](Class Itit) { return nodes[it.i].data; }127 T& operator[](Class it) { return nodes[it.i].data; } 131 128 /// Returns the data pointed by \c it. 132 const T& operator[](Class Itit) const { return nodes[it.i].data; }129 const T& operator[](Class it) const { return nodes[it.i].data; } 133 130 ///. 134 class Class It{131 class Class { 135 132 friend class IterablePartition; 136 133 protected: … … 138 135 public: 139 136 /// Default constructor. 140 Class It() { }137 Class() { } 141 138 /// This constructor constructs an iterator which points 142 139 /// to the member of th container indexed by the integer _i. 143 Class It(const int& _i) : i(_i) { }140 Class(const int& _i) : i(_i) { } 144 141 /// Invalid constructor. 145 Class It(const Invalid&) : i(-1) { }146 friend bool operator<(const Class It& x, const ClassIt& y);142 Class(const Invalid&) : i(-1) { } 143 friend bool operator<(const Class& x, const Class& y); 147 144 friend std::ostream& operator<<(std::ostream& os, 148 const ClassIt& it); 145 const Class& it); 146 bool operator==(const Class& node) const {return i == node.i;} 147 bool operator!=(const Class& node) const {return i != node.i;} 149 148 }; 150 friend bool operator<(const Class It& x, const ClassIt& y) {149 friend bool operator<(const Class& x, const Class& y) { 151 150 return (x.i < y.i); 152 151 } 153 152 friend std::ostream& operator<<(std::ostream& os, 154 const Class It& it) {153 const Class& it) { 155 154 os << it.i; 156 155 return os; 157 156 } 158 157 /// First member of class \c class_id. 159 Class It& first(ClassIt& it, int class_id) const {158 Class& first(Class& it, int class_id) const { 160 159 it.i=tips[class_id].first; 161 160 return it; 162 161 } 163 162 /// Next member. 164 Class It& next(ClassIt& it) const {163 Class& next(Class& it) const { 165 164 it.i=nodes[it.i].next; 166 165 return it; 167 166 } 168 167 /// True iff the iterator is valid. 169 bool valid(const ClassIt& it) const { return it.i!=-1; } 168 bool valid(const Class& it) const { return it.i!=-1; } 169 170 class ClassIt : public Class { 171 const IterablePartition* iterable_partition; 172 public: 173 ClassIt() { } 174 ClassIt(Invalid i) : Class(i) { } 175 ClassIt(const IterablePartition& _iterable_partition, 176 const int& i) : iterable_partition(&_iterable_partition) { 177 _iterable_partition.first(*this, i); 178 } 179 ClassIt(const IterablePartition& _iterable_partition, 180 const Class& _class) : 181 Class(_class), iterable_partition(&_iterable_partition) { } 182 ClassIt& operator++() { 183 iterable_partition->next(*this); 184 return *this; 185 } 186 }; 187 170 188 }; 171 189 … … 192 210 193 211 /// \e 212 typedef IterablePartition<int> Rows; 213 /// \e 214 typedef IterablePartition<int> Cols; 215 /// \e 194 216 typedef _Value Value; 195 217 /// \e 196 typedef IterablePartition<int>::ClassItRow;197 /// \e 198 typedef IterablePartition<int>::ClassItCol;218 typedef Rows::Class Row; 219 /// \e 220 typedef Cols::Class Col; 199 221 public: 200 222 /// \e … … 247 269 /// \e 248 270 virtual void solveDualSimplex() = 0; 249 /// \e250 271 251 272 //SOLUTION RETRIEVING … … 306 327 virtual void _getRowCoeffs(int i, 307 328 std::vector<std::pair<int, _Value> >& coeffs) = 0; 329 /// \e 308 330 virtual void _setColCoeffs(int i, 309 331 const std::vector<std::pair<int, _Value> >& coeffs) = 0; … … 312 334 virtual void _getColCoeffs(int i, 313 335 std::vector<std::pair<int, _Value> >& coeffs) = 0; 314 public: 315 /// \e 316 enum Bound { FREE, LOWER, UPPER, DOUBLE, FIXED }; 336 /// \e 337 virtual void _setCoeff(int col, int row, _Value value) = 0; 338 /// \e 339 virtual _Value _getCoeff(int col, int row) = 0; 340 // public: 341 // /// \e 342 // enum Bound { FREE, LOWER, UPPER, DOUBLE, FIXED }; 317 343 protected: 318 344 /// \e … … 357 383 virtual _Value _getRowUpperBound(int i) = 0; 358 384 /// \e 359 virtual void _setObjCoef (int i, _Value obj_coef) = 0;360 /// \e 361 virtual _Value _getObjCoef (int i) = 0;385 virtual void _setObjCoeff(int i, _Value obj_coef) = 0; 386 /// \e 387 virtual _Value _getObjCoeff(int i) = 0; 362 388 363 389 //SOLUTION RETRIEVING … … 432 458 } 433 459 /// \e 460 void setCoeff(Col col, Row row, _Value value) { 461 _setCoeff(col_iter_map[col], row_iter_map[row], value); 462 } 463 /// \e 464 _Value getCoeff(Col col, Row row) { 465 return _getCoeff(col_iter_map[col], row_iter_map[row], value); 466 } 467 /// \e 434 468 void setColLowerBound(Col col, _Value lo) { 435 469 _setColLowerBound(col_iter_map[col], lo); … … 464 498 } 465 499 /// \e 466 void setObjCoef (const Col& col, _Value obj_coef) {467 _setObjCoef (col_iter_map[col], obj_coef);468 } 469 /// \e 470 _Value getObjCoef (const Col& col) {471 return _getObjCoef (col_iter_map[col]);500 void setObjCoeff(const Col& col, _Value obj_coef) { 501 _setObjCoeff(col_iter_map[col], obj_coef); 502 } 503 /// \e 504 _Value getObjCoeff(const Col& col) { 505 return _getObjCoeff(col_iter_map[col]); 472 506 } 473 507 … … 552 586 } 553 587 /// \e 554 /// \bug ez igy nem jo555 588 void setObjCoeffs(const Expression& expr) { 589 // writing zero everywhere 590 for(Cols::ClassIt it(col_iter_map, VALID_CLASS); it!=INVALID; ++it) 591 setObjCoeff(it, 0.0); 592 // writing the data needed 556 593 for(typename Expression::Data::const_iterator i=expr.data.begin(); 557 594 i!=expr.data.end(); ++i) { 558 setObjCoef ((*i).first, (*i).second);595 setObjCoeff((*i).first, (*i).second); 559 596 } 560 597 } … … 562 599 /// This routine modifies \c expr by only adding to it. 563 600 void getObjCoeffs(Expression& expr) { 564 /// FIXME not yet implemented 601 for(Cols::ClassIt it(col_iter_map, VALID_CLASS); it!=INVALID; ++it) 602 expr+=getObjCoeff(it)*it; 603 } 604 //@} 605 606 607 /*! @name MIP functions and types (public members) 608 */ 609 //@{ 610 public: 611 /// \e 612 virtual void solveBandB() = 0; 613 /// \e 614 virtual void setLP() = 0; 615 /// \e 616 virtual void setMIP() = 0; 617 protected: 618 /// \e 619 virtual void _setColCont(int i) = 0; 620 /// \e 621 virtual void _setColInt(int i) = 0; 622 public: 623 /// \e 624 void setColCont(Col col) { 625 _setColCont(col_iter_map[col]); 626 } 627 /// \e 628 void setColInt(Col col) { 629 _setColInt(col_iter_map[col]); 565 630 } 566 631 //@} … … 691 756 rows[1]=i; 692 757 lpx_del_rows(lp, 1, rows); 758 } 759 void _setCoeff(int col, int row, double value) { 760 /// FIXME not yet implemented 761 } 762 double _getCoeff(int col, int row) { 763 /// FIXME not yet implemented 764 return 0.0; 693 765 } 694 766 virtual void _setColLowerBound(int i, double lo) { … … 929 1001 } 930 1002 /// \e 931 virtual double _getObjCoef (int i) {1003 virtual double _getObjCoeff(int i) { 932 1004 return lpx_get_obj_coef(lp, i); 933 1005 } 934 1006 /// \e 935 virtual void _setObjCoef (int i, double obj_coef) {1007 virtual void _setObjCoeff(int i, double obj_coef) { 936 1008 lpx_set_obj_coef(lp, i, obj_coef); 937 1009 } … … 943 1015 /// \e 944 1016 void solveDualSimplex() { lpx_simplex(lp); } 945 /// \e946 1017 protected: 947 1018 virtual double _getPrimal(int i) { … … 1016 1087 } 1017 1088 } 1089 1090 // MIP 1091 /// \e 1092 void solveBandB() { lpx_integer(lp); } 1093 /// \e 1094 void setLP() { lpx_set_class(lp, LPX_LP); } 1095 /// \e 1096 void setMIP() { lpx_set_class(lp, LPX_MIP); } 1097 protected: 1098 /// \e 1099 void _setColCont(int i) {lpx_set_col_kind(lp, i, LPX_CV); } 1100 /// \e 1101 void _setColInt(int i) {lpx_set_col_kind(lp, i, LPX_IV); } 1018 1102 }; 1019 1103 … … 1022 1106 } //namespace lemon 1023 1107 1024 #endif //LEMON_LP_SOLVER_ WRAPPER_H1108 #endif //LEMON_LP_SOLVER_BASE_H -
src/work/marci/lp/max_flow_expression.cc
r1144 r1152 83 83 lp.solveSimplex(); 84 84 cout << "elapsed time: " << ts << endl; 85 // cout << "rows:" << endl; 86 // for ( 87 // LPSolver::Rows::ClassIt i(lp.row_iter_map, 0); 88 // i!=INVALID; 89 // ++i) { 90 // cout << i << " "; 91 // } 92 // cout << endl; 93 // cout << "cols:" << endl; 94 // for ( 95 // LPSolver::Cols::ClassIt i(lp.col_iter_map, 0); 96 // i!=INVALID; 97 // ++i) { 98 // cout << i << " "; 99 // } 100 // cout << endl; 101 lp.setMIP(); 102 cout << "elapsed time: " << ts << endl; 103 for (LPSolver::Cols::ClassIt it(lp.col_iter_map ,1); it!=INVALID; ++it) { 104 lp.setColInt(it); 105 } 106 cout << "elapsed time: " << ts << endl; 107 lp.solveBandB(); 108 cout << "elapsed time: " << ts << endl; 85 109 }
Note: See TracChangeset
for help on using the changeset viewer.