# HG changeset patch # User marci # Date 1105463746 0 # Node ID 4a24a46407db79d11236f330e2f7d68812a767bb # Parent bedab8bd915f0d776ba911e85926aac2d02951ee :-} diff -r bedab8bd915f -r 4a24a46407db src/work/marci/lp/lp_solver_wrapper_3.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/marci/lp/lp_solver_wrapper_3.h Tue Jan 11 17:15:46 2005 +0000 @@ -0,0 +1,632 @@ +// -*- c++ -*- +#ifndef LEMON_LP_SOLVER_WRAPPER_H +#define LEMON_LP_SOLVER_WRAPPER_H + +///\ingroup misc +///\file +///\brief Dijkstra algorithm. + +// #include +#include +// #include +//#include +extern "C" { +#include "glpk.h" +} + +#include +#include +#include +#include +#include +#include + +//#include +//#include +//#include +#include +//#include +//#include +//#include +//#include +//#include + +using std::cout; +using std::cin; +using std::endl; + +namespace lemon { + + + /// \addtogroup misc + /// @{ + + /// \brief A partitioned vector with iterable classes. + /// + /// This class implements a container in which the data is stored in an + /// stl vector, the range is partitioned into sets and each set is + /// doubly linked in a list. + /// That is, each class is iterable by lemon iterators, and any member of + /// the vector can bo moved to an other class. + template + class IterablePartition { + protected: + struct Node { + T data; + int prev; //invalid az -1 + int next; + }; + std::vector nodes; + struct Tip { + int first; + int last; + }; + std::vector tips; + public: + /// The classes are indexed by integers from \c 0 to \c classNum()-1. + int classNum() const { return tips.size(); } + /// This lemon style iterator iterates through a class. + class ClassIt; + /// Constructor. The number of classes is to be given which is fixed + /// over the life of the container. + /// The partition classes are indexed from 0 to class_num-1. + IterablePartition(int class_num) { + for (int i=0; i + class LPSolverBase { + public: + /// \e + typedef _Value Value; + /// \e + typedef IterablePartition::ClassIt RowIt; + /// \e + typedef IterablePartition::ClassIt ColIt; + public: + /// \e + IterablePartition row_iter_map; + /// \e + IterablePartition col_iter_map; + /// \e + const int VALID_CLASS; + /// \e + const int INVALID_CLASS; + public: + /// \e + LPSolverBase() : row_iter_map(2), + col_iter_map(2), + VALID_CLASS(0), INVALID_CLASS(1) { } + /// \e + virtual ~LPSolverBase() { } + /// \e + virtual void setMinimize() = 0; + /// \e + virtual void setMaximize() = 0; + protected: + /// \e + virtual int _addRow() = 0; + /// \e + virtual int _addCol() = 0; + public: + /// \e + RowIt addRow() { + int i=_addRow(); + RowIt row_it; + row_iter_map.first(row_it, INVALID_CLASS); + if (row_iter_map.valid(row_it)) { //van hasznalhato hely + row_iter_map.set(row_it, INVALID_CLASS, VALID_CLASS); + row_iter_map[row_it]=i; + } else { //a cucc vegere kell inzertalni mert nincs szabad hely + row_it=row_iter_map.push_back(i, VALID_CLASS); + } + return row_it; + } + /// \e + ColIt addCol() { + int i=_addCol(); + ColIt col_it; + col_iter_map.first(col_it, INVALID_CLASS); + if (col_iter_map.valid(col_it)) { //van hasznalhato hely + col_iter_map.set(col_it, INVALID_CLASS, VALID_CLASS); + col_iter_map[col_it]=i; + } else { //a cucc vegere kell inzertalni mert nincs szabad hely + col_it=col_iter_map.push_back(i, VALID_CLASS); + } + 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; + for ( ; begin!=end; ++begin) { + coeffs.push_back(std:: + make_pair(col_iter_map[begin->first], begin->second)); + } + setRowCoeffs(row_iter_map[row_it], coeffs); + } + /// \e + template + void setColCoeffs(ColIt col_it, Begin begin, End end) { + std::vector > coeffs; + for ( ; begin!=end; ++begin) { + coeffs.push_back(std:: + make_pair(row_iter_map[begin->first], begin->second)); + } + 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); + int cols[2]; + cols[1]=col_iter_map[col_it]; + _eraseCol(cols[1]); + col_iter_map[col_it]=0; //glpk specifikus, de kell ez?? + ColIt it; + for (col_iter_map.first(it, VALID_CLASS); + col_iter_map.valid(it); col_iter_map.next(it)) { + if (col_iter_map[it]>cols[1]) --col_iter_map[it]; + } + } + /// \e + void eraseRow(const RowIt& row_it) { + row_iter_map.set(row_it, VALID_CLASS, INVALID_CLASS); + int rows[2]; + rows[1]=row_iter_map[row_it]; + _eraseRow(rows[1]); + row_iter_map[row_it]=0; //glpk specifikus, de kell ez?? + RowIt it; + for (row_iter_map.first(it, VALID_CLASS); + row_iter_map.valid(it); row_iter_map.next(it)) { + if (row_iter_map[it]>rows[1]) --row_iter_map[it]; + } + } + /// \e + virtual void setColBounds(const ColIt& col_it, int bound_type, + _Value lo, _Value up) =0; + /// \e + virtual _Value getObjCoef(const ColIt& col_it) = 0; + /// \e + virtual void setRowBounds(const RowIt& row_it, int bound_type, + _Value lo, _Value up) = 0; + /// \e + virtual void setObjCoef(const ColIt& col_it, _Value obj_coef) = 0; + /// \e + virtual void solveSimplex() = 0; + /// \e + virtual void solvePrimalSimplex() = 0; + /// \e + virtual void solveDualSimplex() = 0; + /// \e + virtual _Value getPrimal(const ColIt& col_it) = 0; + /// \e + virtual _Value getObjVal() = 0; + /// \e + virtual int rowNum() const = 0; + /// \e + virtual int colNum() const = 0; + /// \e + virtual int warmUp() = 0; + /// \e + virtual void printWarmUpStatus(int i) = 0; + /// \e + virtual int getPrimalStatus() = 0; + /// \e + virtual void printPrimalStatus(int i) = 0; + /// \e + virtual int getDualStatus() = 0; + /// \e + virtual void printDualStatus(int i) = 0; + /// Returns the status of the slack variable assigned to row \c row_it. + virtual int getRowStat(const RowIt& row_it) = 0; + /// \e + virtual void printRowStatus(int i) = 0; + /// Returns the status of the variable assigned to column \c col_it. + virtual int getColStat(const ColIt& col_it) = 0; + /// \e + virtual void printColStatus(int i) = 0; + }; + + + /// \brief Wrappers for LP solvers + /// + /// This class implements a lemon wrapper for glpk. + /// Later other LP-solvers will be wrapped into lemon. + /// 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 { + public: + typedef LPSolverBase Parent; + + public: + /// \e + LPX* lp; + + public: + /// \e + LPSolverWrapper() : Parent(), + lp(lpx_create_prob()) { + lpx_set_int_parm(lp, LPX_K_DUAL, 1); + } + /// \e + ~LPSolverWrapper() { + lpx_delete_prob(lp); + } + /// \e + void setMinimize() { + lpx_set_obj_dir(lp, LPX_MIN); + } + /// \e + void setMaximize() { + lpx_set_obj_dir(lp, LPX_MAX); + } + protected: + /// \e + int _addCol() { + return lpx_add_cols(lp, 1); + } + /// \e + int _addRow() { + return lpx_add_rows(lp, 1); + } + public: + using Parent::setRowCoeffs; + /// \e + 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]; + int length=0; + for (std::vector >:: + const_iterator it=coeffs.begin(); it!=coeffs.end(); ++it) { + ++length; + indices[length]=it->first; + doubles[length]=it->second; + std::cout << " " << indices[length] << " " + << doubles[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) { + int mem_length=1+rowNum(); + int* indices = new int[mem_length]; + double* doubles = new double[mem_length]; + int length=0; + for (std::vector >:: + const_iterator it=coeffs.begin(); it!=coeffs.end(); ++it) { + ++length; + indices[length]=it->first; + doubles[length]=it->second; + } + lpx_set_mat_col(lp, i, length, indices, doubles); + 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; + lpx_del_cols(lp, 1, cols); + } + virtual void _eraseRow(int i) { + int rows[2]; + rows[1]=i; + lpx_del_rows(lp, 1, rows); + } + public: + /// \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); + } + /// \e + double getObjCoef(const ColIt& col_it) { + return lpx_get_obj_coef(lp, col_iter_map[col_it]); + } + /// \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); + } + /// \e + void solveSimplex() { lpx_simplex(lp); } + /// \e + void solvePrimalSimplex() { lpx_simplex(lp); } + /// \e + void solveDualSimplex() { lpx_simplex(lp); } + /// \e + double getPrimal(const ColIt& col_it) { + return lpx_get_col_prim(lp, col_iter_map[col_it]); + } + /// \e + double getObjVal() { return lpx_get_obj_val(lp); } + /// \e + int rowNum() const { return lpx_get_num_rows(lp); } + /// \e + int colNum() const { return lpx_get_num_cols(lp); } + /// \e + int warmUp() { return lpx_warm_up(lp); } + /// \e + void printWarmUpStatus(int i) { + switch (i) { + case LPX_E_OK: cout << "LPX_E_OK" << endl; break; + case LPX_E_EMPTY: cout << "LPX_E_EMPTY" << endl; break; + case LPX_E_BADB: cout << "LPX_E_BADB" << endl; break; + case LPX_E_SING: cout << "LPX_E_SING" << endl; break; + } + } + /// \e + int getPrimalStatus() { return lpx_get_prim_stat(lp); } + /// \e + void printPrimalStatus(int i) { + switch (i) { + case LPX_P_UNDEF: cout << "LPX_P_UNDEF" << endl; break; + case LPX_P_FEAS: cout << "LPX_P_FEAS" << endl; break; + case LPX_P_INFEAS: cout << "LPX_P_INFEAS" << endl; break; + case LPX_P_NOFEAS: cout << "LPX_P_NOFEAS" << endl; break; + } + } + /// \e + int getDualStatus() { return lpx_get_dual_stat(lp); } + /// \e + void printDualStatus(int i) { + switch (i) { + case LPX_D_UNDEF: cout << "LPX_D_UNDEF" << endl; break; + case LPX_D_FEAS: cout << "LPX_D_FEAS" << endl; break; + case LPX_D_INFEAS: cout << "LPX_D_INFEAS" << endl; break; + case LPX_D_NOFEAS: cout << "LPX_D_NOFEAS" << endl; break; + } + } + /// Returns the status of the slack variable assigned to row \c row_it. + int getRowStat(const RowIt& row_it) { + return lpx_get_row_stat(lp, row_iter_map[row_it]); + } + /// \e + void printRowStatus(int i) { + switch (i) { + case LPX_BS: cout << "LPX_BS" << endl; break; + case LPX_NL: cout << "LPX_NL" << endl; break; + case LPX_NU: cout << "LPX_NU" << endl; break; + case LPX_NF: cout << "LPX_NF" << endl; break; + case LPX_NS: cout << "LPX_NS" << endl; break; + } + } + /// Returns the status of the variable assigned to column \c col_it. + int getColStat(const ColIt& col_it) { + return lpx_get_col_stat(lp, col_iter_map[col_it]); + } + /// \e + void printColStatus(int i) { + switch (i) { + case LPX_BS: cout << "LPX_BS" << endl; break; + case LPX_NL: cout << "LPX_NL" << endl; break; + case LPX_NU: cout << "LPX_NU" << endl; break; + case LPX_NF: cout << "LPX_NF" << endl; break; + case LPX_NS: cout << "LPX_NS" << endl; break; + } + } + }; + + /// @} + +} //namespace lemon + +#endif //LEMON_LP_SOLVER_WRAPPER_H diff -r bedab8bd915f -r 4a24a46407db src/work/marci/lp/min_cost_gen_flow.h --- a/src/work/marci/lp/min_cost_gen_flow.h Tue Jan 11 09:15:25 2005 +0000 +++ b/src/work/marci/lp/min_cost_gen_flow.h Tue Jan 11 17:15:46 2005 +0000 @@ -14,7 +14,7 @@ //#include //#include #include -#include +#include namespace lemon { @@ -228,6 +228,12 @@ lp.setColBounds(col_it, LPX_DB, 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; + } for (typename Graph::NodeIt n(g); n!=INVALID; ++n) { typename Graph::template EdgeMap coeffs(g, 0); for (typename Graph::InEdgeIt e(g, n); e!=INVALID; ++e) @@ -244,6 +250,7 @@ } } //std::cout << 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); }