marci@1031: // -*- c++ -*- marci@1031: #ifndef LEMON_LP_SOLVER_WRAPPER_H marci@1031: #define LEMON_LP_SOLVER_WRAPPER_H marci@1031: marci@1031: ///\ingroup misc marci@1031: ///\file marci@1031: ///\brief Dijkstra algorithm. marci@1031: marci@1031: // #include marci@1031: #include marci@1031: // #include marci@1031: //#include marci@1031: extern "C" { marci@1031: #include "glpk.h" marci@1031: } marci@1031: marci@1031: #include marci@1031: #include marci@1031: #include marci@1031: #include marci@1031: #include marci@1031: #include marci@1031: marci@1031: //#include marci@1031: //#include marci@1031: //#include marci@1031: #include marci@1031: //#include marci@1031: //#include marci@1031: //#include marci@1031: //#include marci@1031: //#include marci@1031: marci@1031: using std::cout; marci@1031: using std::cin; marci@1031: using std::endl; marci@1031: marci@1031: namespace lemon { marci@1031: marci@1031: marci@1031: /// \addtogroup misc marci@1031: /// @{ marci@1031: marci@1031: /// \brief A partitioned vector with iterable classes. marci@1031: /// marci@1031: /// This class implements a container in which the data is stored in an marci@1031: /// stl vector, the range is partitioned into sets and each set is marci@1031: /// doubly linked in a list. marci@1031: /// That is, each class is iterable by lemon iterators, and any member of marci@1031: /// the vector can bo moved to an other class. marci@1031: template marci@1031: class IterablePartition { marci@1031: protected: marci@1031: struct Node { marci@1031: T data; marci@1031: int prev; //invalid az -1 marci@1031: int next; marci@1031: }; marci@1031: std::vector nodes; marci@1031: struct Tip { marci@1031: int first; marci@1031: int last; marci@1031: }; marci@1031: std::vector tips; marci@1031: public: marci@1031: /// The classes are indexed by integers from \c 0 to \c classNum()-1. marci@1031: int classNum() const { return tips.size(); } marci@1031: /// This lemon style iterator iterates through a class. marci@1031: class ClassIt; marci@1031: /// Constructor. The number of classes is to be given which is fixed marci@1031: /// over the life of the container. marci@1031: /// The partition classes are indexed from 0 to class_num-1. marci@1031: IterablePartition(int class_num) { marci@1031: for (int i=0; i marci@1031: class LPSolverBase { marci@1031: public: marci@1031: /// \e marci@1048: typedef _Value Value; marci@1048: /// \e marci@1031: typedef IterablePartition::ClassIt RowIt; marci@1031: /// \e marci@1031: typedef IterablePartition::ClassIt ColIt; marci@1074: public: marci@1031: /// \e marci@1031: IterablePartition row_iter_map; marci@1031: /// \e marci@1031: IterablePartition col_iter_map; marci@1031: /// \e marci@1074: const int VALID_CLASS; marci@1031: /// \e marci@1074: const int INVALID_CLASS; marci@1031: public: marci@1031: /// \e marci@1031: LPSolverBase() : row_iter_map(2), marci@1031: col_iter_map(2), marci@1074: VALID_CLASS(0), INVALID_CLASS(1) { } marci@1031: /// \e marci@1031: virtual ~LPSolverBase() { } marci@1031: /// \e marci@1031: virtual void setMinimize() = 0; marci@1031: /// \e marci@1031: virtual void setMaximize() = 0; marci@1074: protected: marci@1031: /// \e marci@1074: virtual int _addRow() = 0; marci@1031: /// \e marci@1074: virtual int _addCol() = 0; marci@1074: public: marci@1074: /// \e marci@1074: RowIt addRow() { marci@1074: int i=_addRow(); marci@1074: RowIt row_it; marci@1074: row_iter_map.first(row_it, INVALID_CLASS); marci@1074: if (row_iter_map.valid(row_it)) { //van hasznalhato hely marci@1074: row_iter_map.set(row_it, INVALID_CLASS, VALID_CLASS); marci@1074: row_iter_map[row_it]=i; marci@1074: } else { //a cucc vegere kell inzertalni mert nincs szabad hely marci@1074: row_it=row_iter_map.push_back(i, VALID_CLASS); marci@1074: } marci@1074: return row_it; marci@1074: } marci@1074: /// \e marci@1074: ColIt addCol() { marci@1074: int i=_addCol(); marci@1074: ColIt col_it; marci@1074: col_iter_map.first(col_it, INVALID_CLASS); marci@1074: if (col_iter_map.valid(col_it)) { //van hasznalhato hely marci@1074: col_iter_map.set(col_it, INVALID_CLASS, VALID_CLASS); marci@1074: col_iter_map[col_it]=i; marci@1074: } else { //a cucc vegere kell inzertalni mert nincs szabad hely marci@1074: col_it=col_iter_map.push_back(i, VALID_CLASS); marci@1074: } marci@1074: return col_it; marci@1074: } marci@1074: /// \e marci@1074: virtual void setRowCoeffs(int i, marci@1074: std::vector > coeffs) = 0; marci@1074: /// \e marci@1074: virtual void setColCoeffs(int i, marci@1074: std::vector > coeffs) = 0; marci@1031: /// \e marci@1031: template marci@1031: void setRowCoeffs(RowIt row_it, Begin begin, End end) { marci@1074: std::vector > coeffs; marci@1031: for ( ; begin!=end; ++begin) { marci@1074: coeffs.push_back(std:: marci@1074: make_pair(col_iter_map[begin->first], begin->second)); marci@1031: } marci@1074: setRowCoeffs(row_iter_map[row_it], coeffs); marci@1031: } marci@1031: /// \e marci@1031: template marci@1031: void setColCoeffs(ColIt col_it, Begin begin, End end) { marci@1074: std::vector > coeffs; marci@1031: for ( ; begin!=end; ++begin) { marci@1074: coeffs.push_back(std:: marci@1074: make_pair(row_iter_map[begin->first], begin->second)); marci@1031: } marci@1074: setColCoeffs(col_iter_map[col_it], coeffs); marci@1074: } marci@1074: /// temporally, glpk style indexing marci@1074: //virtual void setRowCoeffs(RowIt row_it, int num, marci@1074: // int* indices, _Value* doubles) = 0; marci@1074: //pair-bol kell megadni egy std range-et marci@1074: /// \e marci@1074: // virtual void seColCoeffs(int i, marci@1074: // std::vector > coeffs) = 0; marci@1074: /// \e marci@1074: // template marci@1074: // void setRowCoeffs(RowIt row_it, Begin begin, End end) { marci@1074: // int mem_length=1+colNum(); marci@1074: // int* indices = new int[mem_length]; marci@1074: // _Value* doubles = new _Value[mem_length]; marci@1074: // int length=0; marci@1074: // for ( ; begin!=end; ++begin) { marci@1074: // ++length; marci@1074: // indices[length]=col_iter_map[begin->first]; marci@1074: // doubles[length]=begin->second; marci@1074: // } marci@1074: // setRowCoeffs(row_it, length, indices, doubles); marci@1074: // delete [] indices; marci@1074: // delete [] doubles; marci@1074: // } marci@1074: /// temporally, glpk style indexing marci@1074: //virtual void setColCoeffs(ColIt col_it, int num, marci@1074: // int* indices, _Value* doubles) = 0; marci@1074: //pair-bol kell megadni egy std range-et marci@1074: /// \e marci@1074: // template marci@1074: // void setColCoeffs(ColIt col_it, Begin begin, End end) { marci@1074: // int mem_length=1+rowNum(); marci@1074: // int* indices = new int[mem_length]; marci@1074: // _Value* doubles = new _Value[mem_length]; marci@1074: // int length=0; marci@1074: // for ( ; begin!=end; ++begin) { marci@1074: // ++length; marci@1074: // indices[length]=row_iter_map[begin->first]; marci@1074: // doubles[length]=begin->second; marci@1074: // } marci@1074: // setColCoeffs(col_it, length, indices, doubles); marci@1074: // delete [] indices; marci@1074: // delete [] doubles; marci@1074: // } marci@1074: protected: marci@1074: /// \e marci@1074: virtual void _eraseCol(int i) = 0; marci@1074: /// \e marci@1074: virtual void _eraseRow(int i) = 0; marci@1074: public: marci@1074: /// \e marci@1074: void eraseCol(const ColIt& col_it) { marci@1074: col_iter_map.set(col_it, VALID_CLASS, INVALID_CLASS); marci@1074: int cols[2]; marci@1074: cols[1]=col_iter_map[col_it]; marci@1074: _eraseCol(cols[1]); marci@1074: col_iter_map[col_it]=0; //glpk specifikus, de kell ez?? marci@1074: ColIt it; marci@1074: for (col_iter_map.first(it, VALID_CLASS); marci@1074: col_iter_map.valid(it); col_iter_map.next(it)) { marci@1074: if (col_iter_map[it]>cols[1]) --col_iter_map[it]; marci@1074: } marci@1031: } marci@1031: /// \e marci@1074: void eraseRow(const RowIt& row_it) { marci@1074: row_iter_map.set(row_it, VALID_CLASS, INVALID_CLASS); marci@1074: int rows[2]; marci@1074: rows[1]=row_iter_map[row_it]; marci@1074: _eraseRow(rows[1]); marci@1074: row_iter_map[row_it]=0; //glpk specifikus, de kell ez?? marci@1074: RowIt it; marci@1074: for (row_iter_map.first(it, VALID_CLASS); marci@1074: row_iter_map.valid(it); row_iter_map.next(it)) { marci@1074: if (row_iter_map[it]>rows[1]) --row_iter_map[it]; marci@1074: } marci@1074: } marci@1031: /// \e marci@1031: virtual void setColBounds(const ColIt& col_it, int bound_type, marci@1048: _Value lo, _Value up) =0; marci@1031: /// \e marci@1048: virtual _Value getObjCoef(const ColIt& col_it) = 0; marci@1031: /// \e marci@1031: virtual void setRowBounds(const RowIt& row_it, int bound_type, marci@1048: _Value lo, _Value up) = 0; marci@1031: /// \e marci@1048: virtual void setObjCoef(const ColIt& col_it, _Value obj_coef) = 0; marci@1031: /// \e marci@1031: virtual void solveSimplex() = 0; marci@1031: /// \e marci@1031: virtual void solvePrimalSimplex() = 0; marci@1031: /// \e marci@1031: virtual void solveDualSimplex() = 0; marci@1031: /// \e marci@1048: virtual _Value getPrimal(const ColIt& col_it) = 0; marci@1031: /// \e marci@1048: virtual _Value getObjVal() = 0; marci@1031: /// \e marci@1031: virtual int rowNum() const = 0; marci@1031: /// \e marci@1031: virtual int colNum() const = 0; marci@1031: /// \e marci@1031: virtual int warmUp() = 0; marci@1031: /// \e marci@1031: virtual void printWarmUpStatus(int i) = 0; marci@1031: /// \e marci@1031: virtual int getPrimalStatus() = 0; marci@1031: /// \e marci@1031: virtual void printPrimalStatus(int i) = 0; marci@1031: /// \e marci@1031: virtual int getDualStatus() = 0; marci@1031: /// \e marci@1031: virtual void printDualStatus(int i) = 0; marci@1031: /// Returns the status of the slack variable assigned to row \c row_it. marci@1031: virtual int getRowStat(const RowIt& row_it) = 0; marci@1031: /// \e marci@1031: virtual void printRowStatus(int i) = 0; marci@1031: /// Returns the status of the variable assigned to column \c col_it. marci@1031: virtual int getColStat(const ColIt& col_it) = 0; marci@1031: /// \e marci@1031: virtual void printColStatus(int i) = 0; marci@1031: }; marci@1031: marci@1048: marci@1031: /// \brief Wrappers for LP solvers marci@1031: /// marci@1031: /// This class implements a lemon wrapper for glpk. marci@1031: /// Later other LP-solvers will be wrapped into lemon. marci@1031: /// The aim of this class is to give a general surface to different marci@1031: /// solvers, i.e. it makes possible to write algorithms using LP's, marci@1031: /// in which the solver can be changed to an other one easily. marci@1048: class LPSolverWrapper : public LPSolverBase { marci@1031: public: marci@1048: typedef LPSolverBase Parent; marci@1031: marci@1031: public: marci@1031: /// \e marci@1031: LPX* lp; marci@1031: marci@1031: public: marci@1031: /// \e marci@1048: LPSolverWrapper() : Parent(), marci@1031: lp(lpx_create_prob()) { marci@1031: lpx_set_int_parm(lp, LPX_K_DUAL, 1); marci@1031: } marci@1031: /// \e marci@1031: ~LPSolverWrapper() { marci@1031: lpx_delete_prob(lp); marci@1031: } marci@1031: /// \e marci@1031: void setMinimize() { marci@1031: lpx_set_obj_dir(lp, LPX_MIN); marci@1031: } marci@1031: /// \e marci@1031: void setMaximize() { marci@1031: lpx_set_obj_dir(lp, LPX_MAX); marci@1031: } marci@1074: protected: marci@1031: /// \e marci@1074: int _addCol() { marci@1074: return lpx_add_cols(lp, 1); marci@1031: } marci@1031: /// \e marci@1074: int _addRow() { marci@1074: return lpx_add_rows(lp, 1); marci@1074: } marci@1074: public: marci@1074: using Parent::setRowCoeffs; marci@1074: /// \e marci@1074: virtual void setRowCoeffs(int i, marci@1074: std::vector > coeffs) { marci@1074: int mem_length=1+colNum(); marci@1074: int* indices = new int[mem_length]; marci@1074: double* doubles = new double[mem_length]; marci@1074: int length=0; marci@1074: for (std::vector >:: marci@1074: const_iterator it=coeffs.begin(); it!=coeffs.end(); ++it) { marci@1074: ++length; marci@1074: indices[length]=it->first; marci@1074: doubles[length]=it->second; marci@1074: std::cout << " " << indices[length] << " " marci@1074: << doubles[length] << std::endl; marci@1031: } marci@1074: std::cout << i << " " << length << std::endl; marci@1074: lpx_set_mat_row(lp, i, length, indices, doubles); marci@1074: delete [] indices; marci@1074: delete [] doubles; marci@1031: } marci@1074: /// \e marci@1074: virtual void setColCoeffs(int i, marci@1074: std::vector > coeffs) { marci@1074: int mem_length=1+rowNum(); marci@1074: int* indices = new int[mem_length]; marci@1074: double* doubles = new double[mem_length]; marci@1074: int length=0; marci@1074: for (std::vector >:: marci@1074: const_iterator it=coeffs.begin(); it!=coeffs.end(); ++it) { marci@1074: ++length; marci@1074: indices[length]=it->first; marci@1074: doubles[length]=it->second; marci@1074: } marci@1074: lpx_set_mat_col(lp, i, length, indices, doubles); marci@1074: delete [] indices; marci@1074: delete [] doubles; marci@1031: } marci@1074: // /// \e marci@1074: // /// temporally, glpk style indexing marci@1074: // virtual void setRowCoeffs(RowIt row_it, int num, marci@1074: // int* indices, _Value* doubles) = 0; marci@1074: // //pair-bol kell megadni egy std range-et marci@1074: // /// \e marci@1074: // template marci@1074: // void setRowCoeffs(RowIt row_it, Begin begin, End end) { marci@1074: // int mem_length=1+colNum(); marci@1074: // int* indices = new int[mem_length]; marci@1074: // _Value* doubles = new _Value[mem_length]; marci@1074: // int length=0; marci@1074: // for ( ; begin!=end; ++begin) { marci@1074: // ++length; marci@1074: // indices[length]=col_iter_map[begin->first]; marci@1074: // doubles[length]=begin->second; marci@1074: // } marci@1074: // setRowCoeffs(row_it, length, indices, doubles); marci@1074: // delete [] indices; marci@1074: // delete [] doubles; marci@1074: // } marci@1074: // void setRowCoeffs(RowIt row_it, int length, marci@1074: // int* indices, double* doubles) { marci@1074: // lpx_set_mat_row(lp, row_iter_map[row_it], length, indices, doubles); marci@1074: // } marci@1074: // using Parent::setColCoeffs; marci@1074: // void setColCoeffs(ColIt col_it, int length, marci@1074: // int* indices, double* doubles) { marci@1074: // lpx_set_mat_col(lp, col_iter_map[col_it], length, indices, doubles); marci@1074: // } marci@1031: // //pair-bol kell megadni egy std range-et marci@1031: // /// \e marci@1031: // template marci@1031: // void setColCoeffs(const ColIt& col_it, marci@1031: // Begin begin, End end) { marci@1031: // int mem_length=1+lpx_get_num_rows(lp); marci@1031: // int* indices = new int[mem_length]; marci@1031: // double* doubles = new double[mem_length]; marci@1031: // int length=0; marci@1031: // for ( ; begin!=end; ++begin) { marci@1031: // ++length; marci@1031: // indices[length]=row_iter_map[begin->first]; marci@1031: // doubles[length]=begin->second; marci@1031: // } marci@1031: // lpx_set_mat_col(lp, col_iter_map[col_it], length, indices, doubles); marci@1031: // delete [] indices; marci@1031: // delete [] doubles; marci@1031: // } marci@1031: // //pair-bol kell megadni egy std range-et marci@1031: // /// \e marci@1031: // template marci@1031: // void setRowCoeffs(const RowIt& row_it, marci@1031: // Begin begin, End end) { marci@1031: // int mem_length=1+lpx_get_num_cols(lp); marci@1031: // int* indices = new int[mem_length]; marci@1031: // double* doubles = new double[mem_length]; marci@1031: // int length=0; marci@1031: // for ( ; begin!=end; ++begin) { marci@1031: // ++length; marci@1031: // indices[length]=col_iter_map[begin->first]; marci@1031: // doubles[length]=begin->second; marci@1031: // } marci@1031: // lpx_set_mat_row(lp, row_iter_map[row_it], length, indices, doubles); marci@1031: // delete [] indices; marci@1031: // delete [] doubles; marci@1031: // } marci@1031: /// \e marci@1074: protected: marci@1074: virtual void _eraseCol(int i) { marci@1031: int cols[2]; marci@1074: cols[1]=i; marci@1031: lpx_del_cols(lp, 1, cols); marci@1031: } marci@1074: virtual void _eraseRow(int i) { marci@1031: int rows[2]; marci@1074: rows[1]=i; marci@1031: lpx_del_rows(lp, 1, rows); marci@1031: } marci@1074: public: marci@1031: /// \e marci@1031: void setColBounds(const ColIt& col_it, int bound_type, marci@1031: double lo, double up) { marci@1031: lpx_set_col_bnds(lp, col_iter_map[col_it], bound_type, lo, up); marci@1031: } marci@1031: /// \e marci@1031: double getObjCoef(const ColIt& col_it) { marci@1031: return lpx_get_obj_coef(lp, col_iter_map[col_it]); marci@1031: } marci@1031: /// \e marci@1031: void setRowBounds(const RowIt& row_it, int bound_type, marci@1031: double lo, double up) { marci@1031: lpx_set_row_bnds(lp, row_iter_map[row_it], bound_type, lo, up); marci@1031: } marci@1031: /// \e marci@1031: void setObjCoef(const ColIt& col_it, double obj_coef) { marci@1031: lpx_set_obj_coef(lp, col_iter_map[col_it], obj_coef); marci@1031: } marci@1031: /// \e marci@1031: void solveSimplex() { lpx_simplex(lp); } marci@1031: /// \e marci@1031: void solvePrimalSimplex() { lpx_simplex(lp); } marci@1031: /// \e marci@1031: void solveDualSimplex() { lpx_simplex(lp); } marci@1031: /// \e marci@1031: double getPrimal(const ColIt& col_it) { marci@1031: return lpx_get_col_prim(lp, col_iter_map[col_it]); marci@1031: } marci@1031: /// \e marci@1031: double getObjVal() { return lpx_get_obj_val(lp); } marci@1031: /// \e marci@1031: int rowNum() const { return lpx_get_num_rows(lp); } marci@1031: /// \e marci@1031: int colNum() const { return lpx_get_num_cols(lp); } marci@1031: /// \e marci@1031: int warmUp() { return lpx_warm_up(lp); } marci@1031: /// \e marci@1031: void printWarmUpStatus(int i) { marci@1031: switch (i) { marci@1031: case LPX_E_OK: cout << "LPX_E_OK" << endl; break; marci@1031: case LPX_E_EMPTY: cout << "LPX_E_EMPTY" << endl; break; marci@1031: case LPX_E_BADB: cout << "LPX_E_BADB" << endl; break; marci@1031: case LPX_E_SING: cout << "LPX_E_SING" << endl; break; marci@1031: } marci@1031: } marci@1031: /// \e marci@1031: int getPrimalStatus() { return lpx_get_prim_stat(lp); } marci@1031: /// \e marci@1031: void printPrimalStatus(int i) { marci@1031: switch (i) { marci@1031: case LPX_P_UNDEF: cout << "LPX_P_UNDEF" << endl; break; marci@1031: case LPX_P_FEAS: cout << "LPX_P_FEAS" << endl; break; marci@1031: case LPX_P_INFEAS: cout << "LPX_P_INFEAS" << endl; break; marci@1031: case LPX_P_NOFEAS: cout << "LPX_P_NOFEAS" << endl; break; marci@1031: } marci@1031: } marci@1031: /// \e marci@1031: int getDualStatus() { return lpx_get_dual_stat(lp); } marci@1031: /// \e marci@1031: void printDualStatus(int i) { marci@1031: switch (i) { marci@1031: case LPX_D_UNDEF: cout << "LPX_D_UNDEF" << endl; break; marci@1031: case LPX_D_FEAS: cout << "LPX_D_FEAS" << endl; break; marci@1031: case LPX_D_INFEAS: cout << "LPX_D_INFEAS" << endl; break; marci@1031: case LPX_D_NOFEAS: cout << "LPX_D_NOFEAS" << endl; break; marci@1031: } marci@1031: } marci@1031: /// Returns the status of the slack variable assigned to row \c row_it. marci@1031: int getRowStat(const RowIt& row_it) { marci@1031: return lpx_get_row_stat(lp, row_iter_map[row_it]); marci@1031: } marci@1031: /// \e marci@1031: void printRowStatus(int i) { marci@1031: switch (i) { marci@1031: case LPX_BS: cout << "LPX_BS" << endl; break; marci@1031: case LPX_NL: cout << "LPX_NL" << endl; break; marci@1031: case LPX_NU: cout << "LPX_NU" << endl; break; marci@1031: case LPX_NF: cout << "LPX_NF" << endl; break; marci@1031: case LPX_NS: cout << "LPX_NS" << endl; break; marci@1031: } marci@1031: } marci@1031: /// Returns the status of the variable assigned to column \c col_it. marci@1031: int getColStat(const ColIt& col_it) { marci@1031: return lpx_get_col_stat(lp, col_iter_map[col_it]); marci@1031: } marci@1031: /// \e marci@1031: void printColStatus(int i) { marci@1031: switch (i) { marci@1031: case LPX_BS: cout << "LPX_BS" << endl; break; marci@1031: case LPX_NL: cout << "LPX_NL" << endl; break; marci@1031: case LPX_NU: cout << "LPX_NU" << endl; break; marci@1031: case LPX_NF: cout << "LPX_NF" << endl; break; marci@1031: case LPX_NS: cout << "LPX_NS" << endl; break; marci@1031: } marci@1031: } marci@1031: }; marci@1031: marci@1031: /// @} marci@1031: marci@1031: } //namespace lemon marci@1031: marci@1031: #endif //LEMON_LP_SOLVER_WRAPPER_H