# HG changeset patch # User marci # Date 1105708636 0 # Node ID c0ad2673b11f7d2a133f4ee1616781fff1377948 # Parent 568ce2be7fe0b13e54f9093ba49cf480729cacbf more precise distinction 'tween low and high level lp interfaces diff -r 568ce2be7fe0 -r c0ad2673b11f src/work/marci/lp/lp_solver_wrapper_3.h --- a/src/work/marci/lp/lp_solver_wrapper_3.h Fri Jan 14 08:02:10 2005 +0000 +++ b/src/work/marci/lp/lp_solver_wrapper_3.h Fri Jan 14 13:17:16 2005 +0000 @@ -36,7 +36,6 @@ using std::endl; namespace lemon { - /// \addtogroup misc /// @{ @@ -184,15 +183,54 @@ VALID_CLASS(0), INVALID_CLASS(1) { } /// \e virtual ~LPSolverBase() { } + + //MATRIX INDEPEDENT MANIPULATING FUNCTIONS + + public: /// \e virtual void setMinimize() = 0; /// \e virtual void setMaximize() = 0; + + //LOW LEVEL INTERFACE, MATRIX MANIPULATING FUNCTIONS + protected: /// \e virtual int _addRow() = 0; /// \e virtual int _addCol() = 0; + /// \e + virtual void _setRowCoeffs(int i, + std::vector > coeffs) = 0; + /// \e + virtual void _setColCoeffs(int i, + std::vector > coeffs) = 0; + /// \e + virtual void _eraseCol(int i) = 0; + /// \e + virtual void _eraseRow(int i) = 0; + public: + /// \e + enum Bound { FREE, LOWER, UPPER, DOUBLE, FIXED }; + protected: + /// \e + virtual void _setColBounds(int i, Bound bound, + _Value lo, _Value up) = 0; + /// \e + virtual void _setRowBounds(int i, Bound bound, + _Value lo, _Value up) = 0; + /// \e + virtual void _setObjCoef(int i, _Value obj_coef) = 0; + /// \e + virtual _Value _getObjCoef(int i) = 0; + + //LOW LEVEL, SOLUTION RETRIEVING FUNCTIONS + + protected: + virtual _Value _getPrimal(int i) = 0; + + //HIGH LEVEL INTERFACE, MATRIX MANIPULATING FUNTIONS + public: /// \e RowIt addRow() { @@ -221,12 +259,6 @@ return col_it; } /// \e - virtual void setRowCoeffs(int i, - std::vector > coeffs) = 0; - /// \e - virtual void setColCoeffs(int i, - std::vector > coeffs) = 0; - /// \e template void setRowCoeffs(RowIt row_it, Begin begin, End end) { std::vector > coeffs; @@ -234,7 +266,7 @@ coeffs.push_back(std:: make_pair(col_iter_map[begin->first], begin->second)); } - setRowCoeffs(row_iter_map[row_it], coeffs); + _setRowCoeffs(row_iter_map[row_it], coeffs); } /// \e template @@ -244,57 +276,8 @@ coeffs.push_back(std:: make_pair(row_iter_map[begin->first], begin->second)); } - setColCoeffs(col_iter_map[col_it], coeffs); + _setColCoeffs(col_iter_map[col_it], coeffs); } - /// temporally, glpk style indexing - //virtual void setRowCoeffs(RowIt row_it, int num, - // int* indices, _Value* doubles) = 0; - //pair-bol kell megadni egy std range-et - /// \e - // virtual void seColCoeffs(int i, - // std::vector > coeffs) = 0; - /// \e -// template -// void setRowCoeffs(RowIt row_it, Begin begin, End end) { -// int mem_length=1+colNum(); -// int* indices = new int[mem_length]; -// _Value* doubles = new _Value[mem_length]; -// int length=0; -// for ( ; begin!=end; ++begin) { -// ++length; -// indices[length]=col_iter_map[begin->first]; -// doubles[length]=begin->second; -// } -// setRowCoeffs(row_it, length, indices, doubles); -// delete [] indices; -// delete [] doubles; -// } - /// temporally, glpk style indexing - //virtual void setColCoeffs(ColIt col_it, int num, - // int* indices, _Value* doubles) = 0; - //pair-bol kell megadni egy std range-et - /// \e -// template -// void setColCoeffs(ColIt col_it, Begin begin, End end) { -// int mem_length=1+rowNum(); -// int* indices = new int[mem_length]; -// _Value* doubles = new _Value[mem_length]; -// int length=0; -// for ( ; begin!=end; ++begin) { -// ++length; -// indices[length]=row_iter_map[begin->first]; -// doubles[length]=begin->second; -// } -// setColCoeffs(col_it, length, indices, doubles); -// delete [] indices; -// delete [] doubles; -// } - protected: - /// \e - virtual void _eraseCol(int i) = 0; - /// \e - virtual void _eraseRow(int i) = 0; - public: /// \e void eraseCol(const ColIt& col_it) { col_iter_map.set(col_it, VALID_CLASS, INVALID_CLASS); @@ -322,15 +305,26 @@ } } /// \e - virtual void setColBounds(const ColIt& col_it, int bound_type, - _Value lo, _Value up) =0; + void setColBounds(const ColIt& col_it, Bound bound, + _Value lo, _Value up) { + _setColBounds(col_iter_map[col_it], bound, lo, up); + } /// \e - virtual _Value getObjCoef(const ColIt& col_it) = 0; + void setRowBounds(const RowIt& row_it, Bound bound, + _Value lo, _Value up) { + _setRowBounds(row_iter_map[row_it], bound, lo, up); + } /// \e - virtual void setRowBounds(const RowIt& row_it, int bound_type, - _Value lo, _Value up) = 0; + void setObjCoef(const ColIt& col_it, _Value obj_coef) { + _setObjCoef(col_iter_map[col_it], obj_coef); + } /// \e - virtual void setObjCoef(const ColIt& col_it, _Value obj_coef) = 0; + _Value getObjCoef(const ColIt& col_it) { + return _getObjCoef(col_iter_map[col_it]); + } + + //SOLVER FUNCTIONS + /// \e virtual void solveSimplex() = 0; /// \e @@ -338,9 +332,18 @@ /// \e virtual void solveDualSimplex() = 0; /// \e - virtual _Value getPrimal(const ColIt& col_it) = 0; + + //HIGH LEVEL, SOLUTION RETRIEVING FUNCTIONS + + public: + _Value getPrimal(const ColIt& col_it) { + return _getPrimal(col_iter_map[col_it]); + } /// \e virtual _Value getObjVal() = 0; + + //OTHER FUNCTIONS + /// \e virtual int rowNum() const = 0; /// \e @@ -375,7 +378,7 @@ /// The aim of this class is to give a general surface to different /// solvers, i.e. it makes possible to write algorithms using LP's, /// in which the solver can be changed to an other one easily. - class LPSolverWrapper : public LPSolverBase { + class LPGLPK : public LPSolverBase { public: typedef LPSolverBase Parent; @@ -385,14 +388,17 @@ public: /// \e - LPSolverWrapper() : Parent(), + LPGLPK() : Parent(), lp(lpx_create_prob()) { lpx_set_int_parm(lp, LPX_K_DUAL, 1); } /// \e - ~LPSolverWrapper() { + ~LPGLPK() { lpx_delete_prob(lp); } + + //MATRIX INDEPEDENT MANIPULATING FUNCTIONS + /// \e void setMinimize() { lpx_set_obj_dir(lp, LPX_MIN); @@ -401,6 +407,9 @@ void setMaximize() { lpx_set_obj_dir(lp, LPX_MAX); } + + //LOW LEVEL INTERFACE, MATRIX MANIPULATING FUNCTIONS + protected: /// \e int _addCol() { @@ -410,11 +419,9 @@ int _addRow() { return lpx_add_rows(lp, 1); } - public: - using Parent::setRowCoeffs; /// \e - virtual void setRowCoeffs(int i, - std::vector > coeffs) { + virtual void _setRowCoeffs(int i, + std::vector > coeffs) { int mem_length=1+colNum(); int* indices = new int[mem_length]; double* doubles = new double[mem_length]; @@ -424,17 +431,17 @@ ++length; indices[length]=it->first; doubles[length]=it->second; - std::cout << " " << indices[length] << " " - << doubles[length] << std::endl; +// std::cout << " " << indices[length] << " " +// << doubles[length] << std::endl; } - std::cout << i << " " << length << std::endl; +// std::cout << i << " " << length << std::endl; lpx_set_mat_row(lp, i, length, indices, doubles); delete [] indices; delete [] doubles; } /// \e - virtual void setColCoeffs(int i, - std::vector > coeffs) { + virtual void _setColCoeffs(int i, + std::vector > coeffs) { int mem_length=1+rowNum(); int* indices = new int[mem_length]; double* doubles = new double[mem_length]; @@ -449,74 +456,7 @@ delete [] indices; delete [] doubles; } -// /// \e -// /// temporally, glpk style indexing -// virtual void setRowCoeffs(RowIt row_it, int num, -// int* indices, _Value* doubles) = 0; -// //pair-bol kell megadni egy std range-et -// /// \e -// template -// void setRowCoeffs(RowIt row_it, Begin begin, End end) { -// int mem_length=1+colNum(); -// int* indices = new int[mem_length]; -// _Value* doubles = new _Value[mem_length]; -// int length=0; -// for ( ; begin!=end; ++begin) { -// ++length; -// indices[length]=col_iter_map[begin->first]; -// doubles[length]=begin->second; -// } -// setRowCoeffs(row_it, length, indices, doubles); -// delete [] indices; -// delete [] doubles; -// } -// void setRowCoeffs(RowIt row_it, int length, -// int* indices, double* doubles) { -// lpx_set_mat_row(lp, row_iter_map[row_it], length, indices, doubles); -// } -// using Parent::setColCoeffs; -// void setColCoeffs(ColIt col_it, int length, -// int* indices, double* doubles) { -// lpx_set_mat_col(lp, col_iter_map[col_it], length, indices, doubles); -// } - // //pair-bol kell megadni egy std range-et - // /// \e - // template - // void setColCoeffs(const ColIt& col_it, - // Begin begin, End end) { - // int mem_length=1+lpx_get_num_rows(lp); - // int* indices = new int[mem_length]; - // double* doubles = new double[mem_length]; - // int length=0; - // for ( ; begin!=end; ++begin) { - // ++length; - // indices[length]=row_iter_map[begin->first]; - // doubles[length]=begin->second; - // } - // lpx_set_mat_col(lp, col_iter_map[col_it], length, indices, doubles); - // delete [] indices; - // delete [] doubles; - // } - // //pair-bol kell megadni egy std range-et - // /// \e - // template - // void setRowCoeffs(const RowIt& row_it, - // Begin begin, End end) { - // int mem_length=1+lpx_get_num_cols(lp); - // int* indices = new int[mem_length]; - // double* doubles = new double[mem_length]; - // int length=0; - // for ( ; begin!=end; ++begin) { - // ++length; - // indices[length]=col_iter_map[begin->first]; - // doubles[length]=begin->second; - // } - // lpx_set_mat_row(lp, row_iter_map[row_it], length, indices, doubles); - // delete [] indices; - // delete [] doubles; - // } /// \e - protected: virtual void _eraseCol(int i) { int cols[2]; cols[1]=i; @@ -527,25 +467,56 @@ rows[1]=i; lpx_del_rows(lp, 1, rows); } - public: + virtual void _setColBounds(int i, Bound bound, + double lo, double up) { + switch (bound) { + case FREE: + lpx_set_col_bnds(lp, i, LPX_FR, lo, up); + break; + case LOWER: + lpx_set_col_bnds(lp, i, LPX_LO, lo, up); + break; + case UPPER: + lpx_set_col_bnds(lp, i, LPX_UP, lo, up); + break; + case DOUBLE: + lpx_set_col_bnds(lp, i, LPX_DB, lo, up); + break; + case FIXED: + lpx_set_col_bnds(lp, i, LPX_FX, lo, up); + break; + } + } + virtual void _setRowBounds(int i, Bound bound, + double lo, double up) { + switch (bound) { + case FREE: + lpx_set_row_bnds(lp, i, LPX_FR, lo, up); + break; + case LOWER: + lpx_set_row_bnds(lp, i, LPX_LO, lo, up); + break; + case UPPER: + lpx_set_row_bnds(lp, i, LPX_UP, lo, up); + break; + case DOUBLE: + lpx_set_row_bnds(lp, i, LPX_DB, lo, up); + break; + case FIXED: + lpx_set_row_bnds(lp, i, LPX_FX, lo, up); + break; + } + } + protected: /// \e - void setColBounds(const ColIt& col_it, int bound_type, - double lo, double up) { - lpx_set_col_bnds(lp, col_iter_map[col_it], bound_type, lo, up); + virtual double _getObjCoef(int i) { + return lpx_get_obj_coef(lp, i); } /// \e - double getObjCoef(const ColIt& col_it) { - return lpx_get_obj_coef(lp, col_iter_map[col_it]); + virtual void _setObjCoef(int i, double obj_coef) { + lpx_set_obj_coef(lp, i, obj_coef); } - /// \e - void setRowBounds(const RowIt& row_it, int bound_type, - double lo, double up) { - lpx_set_row_bnds(lp, row_iter_map[row_it], bound_type, lo, up); - } - /// \e - void setObjCoef(const ColIt& col_it, double obj_coef) { - lpx_set_obj_coef(lp, col_iter_map[col_it], obj_coef); - } + public: /// \e void solveSimplex() { lpx_simplex(lp); } /// \e @@ -553,9 +524,11 @@ /// \e void solveDualSimplex() { lpx_simplex(lp); } /// \e - double getPrimal(const ColIt& col_it) { - return lpx_get_col_prim(lp, col_iter_map[col_it]); + protected: + virtual double _getPrimal(int i) { + return lpx_get_col_prim(lp, i); } + public: /// \e double getObjVal() { return lpx_get_obj_val(lp); } /// \e diff -r 568ce2be7fe0 -r c0ad2673b11f src/work/marci/lp/min_cost_gen_flow.h --- a/src/work/marci/lp/min_cost_gen_flow.h Fri Jan 14 08:02:10 2005 +0000 +++ b/src/work/marci/lp/min_cost_gen_flow.h Fri Jan 14 13:17:16 2005 +0000 @@ -21,10 +21,10 @@ template class PrimalMap { protected: - LPSolverWrapper* lp; + LPGLPK* lp; EdgeIndexMap* edge_index_map; public: - PrimalMap(LPSolverWrapper& _lp, EdgeIndexMap& _edge_index_map) : + PrimalMap(LPGLPK& _lp, EdgeIndexMap& _edge_index_map) : lp(&_lp), edge_index_map(&_edge_index_map) { } double operator[](Edge e) const { return lp->getPrimal((*edge_index_map)[e]); @@ -211,7 +211,7 @@ return (min_cost_flow.flowValue()>=expected); } void runByLP() { - typedef LPSolverWrapper LPSolver; + typedef LPGLPK LPSolver; LPSolver lp; lp.setMinimize(); typedef LPSolver::ColIt ColIt; @@ -223,16 +223,16 @@ ColIt col_it=lp.addCol(); edge_index_map.set(e, col_it); if (lcapacity[e]==capacity[e]) - lp.setColBounds(col_it, LPX_FX, lcapacity[e], capacity[e]); + lp.setColBounds(col_it, LPSolver::FIXED, lcapacity[e], capacity[e]); else - lp.setColBounds(col_it, LPX_DB, lcapacity[e], capacity[e]); + lp.setColBounds(col_it, LPSolver::DOUBLE, lcapacity[e], capacity[e]); lp.setObjCoef(col_it, cost[e]); } LPSolver::ColIt col_it; for (lp.col_iter_map.first(col_it, lp.VALID_CLASS); lp.col_iter_map.valid(col_it); lp.col_iter_map.next(col_it)) { - std::cout << "ize " << lp.col_iter_map[col_it] << std::endl; +// std::cout << "ize " << lp.col_iter_map[col_it] << std::endl; } for (typename Graph::NodeIt n(g); n!=INVALID; ++n) { typename Graph::template EdgeMap coeffs(g, 0); @@ -250,9 +250,9 @@ } } //std::cout << std::endl; - std::cout << " " << g.id(n) << " " << row.size() << std::endl; + //std::cout << " " << g.id(n) << " " << row.size() << std::endl; lp.setRowCoeffs(row_it, row.begin(), row.end()); - lp.setRowBounds(row_it, LPX_FX, 0.0, 0.0); + lp.setRowBounds(row_it, LPSolver::FIXED, 0.0, 0.0); } lp.solveSimplex(); //std::cout << lp.colNum() << std::endl;