src/work/marci/lp/lp_solver_wrapper_2.h
changeset 1068 e0b0dcee5e17
parent 1031 0b7169db694f
equal deleted inserted replaced
0:392e5ec7e679 1:17f1955a5e45
   157     bool valid(const ClassIt& it) const { return it.i!=-1; }
   157     bool valid(const ClassIt& it) const { return it.i!=-1; }
   158   };
   158   };
   159 
   159 
   160   /*! \e
   160   /*! \e
   161    */
   161    */
       
   162   template <typename _Value>
   162   class LPSolverBase {
   163   class LPSolverBase {
   163   public:
   164   public:
       
   165     /// \e
       
   166     typedef _Value Value;
   164     /// \e
   167     /// \e
   165     typedef IterablePartition<int>::ClassIt RowIt;
   168     typedef IterablePartition<int>::ClassIt RowIt;
   166     /// \e
   169     /// \e
   167     typedef IterablePartition<int>::ClassIt ColIt;
   170     typedef IterablePartition<int>::ClassIt ColIt;
   168   protected:
   171   protected:
   189     virtual RowIt addRow() = 0;
   192     virtual RowIt addRow() = 0;
   190     /// \e
   193     /// \e
   191     virtual ColIt addCol() = 0;
   194     virtual ColIt addCol() = 0;
   192     /// temporally, glpk style indexing
   195     /// temporally, glpk style indexing
   193     virtual void setRowCoeffs(RowIt row_it, int num, 
   196     virtual void setRowCoeffs(RowIt row_it, int num, 
   194 			      int* indices, double* doubles) = 0;
   197 			      int* indices, _Value* doubles) = 0;
   195     //pair<RowIt, double>-bol kell megadni egy std range-et
   198     //pair<RowIt, _Value>-bol kell megadni egy std range-et
   196     /// \e
   199     /// \e
   197     template <typename Begin, typename End>
   200     template <typename Begin, typename End>
   198     void setRowCoeffs(RowIt row_it, Begin begin, End end) {
   201     void setRowCoeffs(RowIt row_it, Begin begin, End end) {
   199       int mem_length=1+colNum();
   202       int mem_length=1+colNum();
   200       int* indices = new int[mem_length];
   203       int* indices = new int[mem_length];
   201       double* doubles = new double[mem_length];
   204       _Value* doubles = new _Value[mem_length];
   202       int length=0;
   205       int length=0;
   203       for ( ; begin!=end; ++begin) {
   206       for ( ; begin!=end; ++begin) {
   204 	++length;
   207 	++length;
   205 	indices[length]=col_iter_map[begin->first];
   208 	indices[length]=col_iter_map[begin->first];
   206 	doubles[length]=begin->second;
   209 	doubles[length]=begin->second;
   209       delete [] indices;
   212       delete [] indices;
   210       delete [] doubles;
   213       delete [] doubles;
   211     }
   214     }
   212     /// temporally, glpk style indexing
   215     /// temporally, glpk style indexing
   213     virtual void setColCoeffs(ColIt col_it, int num, 
   216     virtual void setColCoeffs(ColIt col_it, int num, 
   214 			      int* indices, double* doubles) = 0;
   217 			      int* indices, _Value* doubles) = 0;
   215     //pair<ColIt, double>-bol kell megadni egy std range-et
   218     //pair<ColIt, _Value>-bol kell megadni egy std range-et
   216     /// \e
   219     /// \e
   217     template <typename Begin, typename End>
   220     template <typename Begin, typename End>
   218     void setColCoeffs(ColIt col_it, Begin begin, End end) {
   221     void setColCoeffs(ColIt col_it, Begin begin, End end) {
   219       int mem_length=1+rowNum();
   222       int mem_length=1+rowNum();
   220       int* indices = new int[mem_length];
   223       int* indices = new int[mem_length];
   221       double* doubles = new double[mem_length];
   224       _Value* doubles = new _Value[mem_length];
   222       int length=0;
   225       int length=0;
   223       for ( ; begin!=end; ++begin) {
   226       for ( ; begin!=end; ++begin) {
   224 	++length;
   227 	++length;
   225 	indices[length]=row_iter_map[begin->first];
   228 	indices[length]=row_iter_map[begin->first];
   226 	doubles[length]=begin->second;
   229 	doubles[length]=begin->second;
   233     virtual void eraseCol(const ColIt& col_it) = 0;
   236     virtual void eraseCol(const ColIt& col_it) = 0;
   234     /// \e
   237     /// \e
   235     virtual void eraseRow(const RowIt& row_it) = 0;
   238     virtual void eraseRow(const RowIt& row_it) = 0;
   236     /// \e
   239     /// \e
   237     virtual void setColBounds(const ColIt& col_it, int bound_type, 
   240     virtual void setColBounds(const ColIt& col_it, int bound_type, 
   238 			      double lo, double up) =0; 
   241 			      _Value lo, _Value up) =0; 
   239     /// \e
   242     /// \e
   240     virtual double getObjCoef(const ColIt& col_it) = 0;
   243     virtual _Value getObjCoef(const ColIt& col_it) = 0;
   241     /// \e
   244     /// \e
   242     virtual void setRowBounds(const RowIt& row_it, int bound_type, 
   245     virtual void setRowBounds(const RowIt& row_it, int bound_type, 
   243 			      double lo, double up) = 0;
   246 			      _Value lo, _Value up) = 0;
   244     /// \e
   247     /// \e
   245     virtual void setObjCoef(const ColIt& col_it, double obj_coef) = 0;
   248     virtual void setObjCoef(const ColIt& col_it, _Value obj_coef) = 0;
   246     /// \e
   249     /// \e
   247     virtual void solveSimplex() = 0;
   250     virtual void solveSimplex() = 0;
   248     /// \e
   251     /// \e
   249     virtual void solvePrimalSimplex() = 0;
   252     virtual void solvePrimalSimplex() = 0;
   250     /// \e
   253     /// \e
   251     virtual void solveDualSimplex() = 0;
   254     virtual void solveDualSimplex() = 0;
   252     /// \e
   255     /// \e
   253     virtual double getPrimal(const ColIt& col_it) = 0;
   256     virtual _Value getPrimal(const ColIt& col_it) = 0;
   254     /// \e
   257     /// \e
   255     virtual double getObjVal() = 0;
   258     virtual _Value getObjVal() = 0;
   256     /// \e
   259     /// \e
   257     virtual int rowNum() const = 0;
   260     virtual int rowNum() const = 0;
   258     /// \e
   261     /// \e
   259     virtual int colNum() const = 0;
   262     virtual int colNum() const = 0;
   260     /// \e
   263     /// \e
   277     virtual int getColStat(const ColIt& col_it) = 0;
   280     virtual int getColStat(const ColIt& col_it) = 0;
   278     /// \e
   281     /// \e
   279     virtual void printColStatus(int i) = 0;
   282     virtual void printColStatus(int i) = 0;
   280   };
   283   };
   281   
   284   
       
   285 
   282   /// \brief Wrappers for LP solvers
   286   /// \brief Wrappers for LP solvers
   283   /// 
   287   /// 
   284   /// This class implements a lemon wrapper for glpk.
   288   /// This class implements a lemon wrapper for glpk.
   285   /// Later other LP-solvers will be wrapped into lemon.
   289   /// Later other LP-solvers will be wrapped into lemon.
   286   /// The aim of this class is to give a general surface to different 
   290   /// The aim of this class is to give a general surface to different 
   287   /// solvers, i.e. it makes possible to write algorithms using LP's, 
   291   /// solvers, i.e. it makes possible to write algorithms using LP's, 
   288   /// in which the solver can be changed to an other one easily.
   292   /// in which the solver can be changed to an other one easily.
   289   class LPSolverWrapper : public LPSolverBase {
   293   class LPSolverWrapper : public LPSolverBase<double> {
   290   public:
   294   public:
   291     typedef LPSolverBase Parent;
   295     typedef LPSolverBase<double> Parent;
   292 
   296 
   293     //   class Row {
   297     //   class Row {
   294     //   protected:
   298     //   protected:
   295     //     int i;
   299     //     int i;
   296     //   public:
   300     //   public:
   321     /// \e
   325     /// \e
   322     LPX* lp;
   326     LPX* lp;
   323 
   327 
   324   public:
   328   public:
   325     /// \e
   329     /// \e
   326     LPSolverWrapper() : LPSolverBase(), 
   330     LPSolverWrapper() : Parent(), 
   327 			lp(lpx_create_prob()) {
   331 			lp(lpx_create_prob()) {
   328       lpx_set_int_parm(lp, LPX_K_DUAL, 1);
   332       lpx_set_int_parm(lp, LPX_K_DUAL, 1);
   329     }
   333     }
   330     /// \e
   334     /// \e
   331     ~LPSolverWrapper() {
   335     ~LPSolverWrapper() {