src/work/marci/lp/lp_solver_wrapper.h
changeset 765 4405b6be83bb
parent 764 615aca7091d2
child 768 a5e9303a5511
equal deleted inserted replaced
0:113c93bdf30a 1:2dc7015ac0fb
     1 // -*- c++ -*-
     1 // -*- c++ -*-
     2 #ifndef HUGO_LP_SOLVER_WRAPPER_H
     2 #ifndef HUGO_LP_SOLVER_WRAPPER_H
     3 #define HUGO_LP_SOLVER_WRAPPER
     3 #define HUGO_LP_SOLVER_WRAPPER
       
     4 
       
     5 ///\ingroup misc
       
     6 ///\file
       
     7 ///\brief Dijkstra algorithm.
     4 
     8 
     5 // #include <stdio.h>
     9 // #include <stdio.h>
     6 #include <stdlib.h>
    10 #include <stdlib.h>
     7 // #include <stdio>
    11 // #include <stdio>
     8 //#include <stdlib>
    12 //#include <stdlib>
    28 using std::cout;
    32 using std::cout;
    29 using std::cin;
    33 using std::cin;
    30 using std::endl;
    34 using std::endl;
    31 
    35 
    32 namespace hugo {
    36 namespace hugo {
       
    37 
       
    38   
       
    39   /// \addtogroup misc
       
    40   /// @{
    33 
    41 
    34   /// \brief A partitioned vector with iterable classes.
    42   /// \brief A partitioned vector with iterable classes.
    35   ///
    43   ///
    36   /// This class implements a container in which the data is stored in an 
    44   /// This class implements a container in which the data is stored in an 
    37   /// stl vector, the range is partitioned into sets and each set is 
    45   /// stl vector, the range is partitioned into sets and each set is 
   117     }
   125     }
   118     /// Returns the data pointed by \c it.
   126     /// Returns the data pointed by \c it.
   119     T& operator[](ClassIt it) { return nodes[it.i].data; }
   127     T& operator[](ClassIt it) { return nodes[it.i].data; }
   120     /// Returns the data pointed by \c it.
   128     /// Returns the data pointed by \c it.
   121     const T& operator[](ClassIt it) const { return nodes[it.i].data; }
   129     const T& operator[](ClassIt it) const { return nodes[it.i].data; }
       
   130     ///.
   122     class ClassIt {
   131     class ClassIt {
   123       friend class IterablePartition;
   132       friend class IterablePartition;
   124     protected:
   133     protected:
   125       int i;
   134       int i;
   126     public:
   135     public:
   182 //   class ColIt : public Col {
   191 //   class ColIt : public Col {
   183 //     ColIt(const Col& col) : Col(col) { }
   192 //     ColIt(const Col& col) : Col(col) { }
   184 //   };
   193 //   };
   185 
   194 
   186   public:
   195   public:
       
   196     ///.
   187     LPX* lp;
   197     LPX* lp;
       
   198     ///.
   188     typedef IterablePartition<int>::ClassIt RowIt;
   199     typedef IterablePartition<int>::ClassIt RowIt;
       
   200     ///.
   189     IterablePartition<int> row_iter_map;
   201     IterablePartition<int> row_iter_map;
       
   202     ///.
   190     typedef IterablePartition<int>::ClassIt ColIt;
   203     typedef IterablePartition<int>::ClassIt ColIt;
       
   204     ///.
   191     IterablePartition<int> col_iter_map;
   205     IterablePartition<int> col_iter_map;
   192     //std::vector<int> row_id_to_lp_row_id;
   206     //std::vector<int> row_id_to_lp_row_id;
   193     //std::vector<int> col_id_to_lp_col_id;
   207     //std::vector<int> col_id_to_lp_col_id;
       
   208     ///.
   194     const int VALID_ID;
   209     const int VALID_ID;
       
   210     ///.
   195     const int INVALID_ID;
   211     const int INVALID_ID;
   196 
   212 
   197   public:
   213   public:
       
   214     ///.
   198     LPSolverWrapper() : lp(lpx_create_prob()), 
   215     LPSolverWrapper() : lp(lpx_create_prob()), 
   199 			row_iter_map(2), 
   216 			row_iter_map(2), 
   200 			col_iter_map(2), 
   217 			col_iter_map(2), 
   201 			//row_id_to_lp_row_id(), col_id_to_lp_col_id(), 
   218 			//row_id_to_lp_row_id(), col_id_to_lp_col_id(), 
   202 			VALID_ID(0), INVALID_ID(1) {
   219 			VALID_ID(0), INVALID_ID(1) {
   203       lpx_set_int_parm(lp, LPX_K_DUAL, 1);
   220       lpx_set_int_parm(lp, LPX_K_DUAL, 1);
   204     }
   221     }
       
   222     ///.
   205     ~LPSolverWrapper() {
   223     ~LPSolverWrapper() {
   206       lpx_delete_prob(lp);
   224       lpx_delete_prob(lp);
   207     }
   225     }
       
   226     ///.
   208     void setMinimize() { 
   227     void setMinimize() { 
   209       lpx_set_obj_dir(lp, LPX_MIN);
   228       lpx_set_obj_dir(lp, LPX_MIN);
   210     }
   229     }
       
   230     ///.
   211     void setMaximize() { 
   231     void setMaximize() { 
   212       lpx_set_obj_dir(lp, LPX_MAX);
   232       lpx_set_obj_dir(lp, LPX_MAX);
   213     }
   233     }
       
   234     ///.
   214     ColIt addCol() {
   235     ColIt addCol() {
   215       int i=lpx_add_cols(lp, 1);  
   236       int i=lpx_add_cols(lp, 1);  
   216       ColIt col_it;
   237       ColIt col_it;
   217       col_iter_map.first(col_it, INVALID_ID);
   238       col_iter_map.first(col_it, INVALID_ID);
   218       if (col_iter_map.valid(col_it)) { //van hasznalhato hely
   239       if (col_iter_map.valid(col_it)) { //van hasznalhato hely
   227 //    edge_index_map.set(e, i);
   248 //    edge_index_map.set(e, i);
   228 //    lpx_set_col_bnds(lp, i, LPX_DB, 0.0, 1.0);
   249 //    lpx_set_col_bnds(lp, i, LPX_DB, 0.0, 1.0);
   229 //    lpx_set_obj_coef(lp, i, cost[e]);    
   250 //    lpx_set_obj_coef(lp, i, cost[e]);    
   230       return col_it;
   251       return col_it;
   231     }
   252     }
       
   253     ///.
   232     RowIt addRow() {
   254     RowIt addRow() {
   233       int i=lpx_add_rows(lp, 1);  
   255       int i=lpx_add_rows(lp, 1);  
   234       RowIt row_it;
   256       RowIt row_it;
   235       row_iter_map.first(row_it, INVALID_ID);
   257       row_iter_map.first(row_it, INVALID_ID);
   236       if (row_iter_map.valid(row_it)) { //van hasznalhato hely
   258       if (row_iter_map.valid(row_it)) { //van hasznalhato hely
   240 	row_it=row_iter_map.push_back(i, VALID_ID);
   262 	row_it=row_iter_map.push_back(i, VALID_ID);
   241       }
   263       }
   242       return row_it;
   264       return row_it;
   243     }
   265     }
   244     //pair<RowIt, double>-bol kell megadni egy std range-et
   266     //pair<RowIt, double>-bol kell megadni egy std range-et
       
   267     ///.
   245     template <typename Begin, typename End>
   268     template <typename Begin, typename End>
   246     void setColCoeffs(const ColIt& col_it, 
   269     void setColCoeffs(const ColIt& col_it, 
   247 		      Begin begin, End end) {
   270 		      Begin begin, End end) {
   248       int mem_length=1+lpx_get_num_rows(lp);
   271       int mem_length=1+lpx_get_num_rows(lp);
   249       int* indices = new int[mem_length];
   272       int* indices = new int[mem_length];
   257       lpx_set_mat_col(lp, col_iter_map[col_it], length, indices, doubles);
   280       lpx_set_mat_col(lp, col_iter_map[col_it], length, indices, doubles);
   258       delete [] indices;
   281       delete [] indices;
   259       delete [] doubles;
   282       delete [] doubles;
   260     }
   283     }
   261     //pair<ColIt, double>-bol kell megadni egy std range-et
   284     //pair<ColIt, double>-bol kell megadni egy std range-et
       
   285     ///.
   262     template <typename Begin, typename End>
   286     template <typename Begin, typename End>
   263     void setRowCoeffs(const RowIt& row_it, 
   287     void setRowCoeffs(const RowIt& row_it, 
   264 		      Begin begin, End end) {
   288 		      Begin begin, End end) {
   265       int mem_length=1+lpx_get_num_cols(lp);
   289       int mem_length=1+lpx_get_num_cols(lp);
   266       int* indices = new int[mem_length];
   290       int* indices = new int[mem_length];
   273       }
   297       }
   274       lpx_set_mat_row(lp, row_iter_map[row_it], length, indices, doubles);
   298       lpx_set_mat_row(lp, row_iter_map[row_it], length, indices, doubles);
   275       delete [] indices;
   299       delete [] indices;
   276       delete [] doubles;
   300       delete [] doubles;
   277     }
   301     }
       
   302     ///.
   278     void eraseCol(const ColIt& col_it) {
   303     void eraseCol(const ColIt& col_it) {
   279       col_iter_map.set(col_it, VALID_ID, INVALID_ID);
   304       col_iter_map.set(col_it, VALID_ID, INVALID_ID);
   280       int cols[2];
   305       int cols[2];
   281       cols[1]=col_iter_map[col_it];
   306       cols[1]=col_iter_map[col_it];
   282       lpx_del_cols(lp, 1, cols);
   307       lpx_del_cols(lp, 1, cols);
   285       for (col_iter_map.first(it, VALID_ID); 
   310       for (col_iter_map.first(it, VALID_ID); 
   286 	   col_iter_map.valid(it); col_iter_map.next(it)) {
   311 	   col_iter_map.valid(it); col_iter_map.next(it)) {
   287 	if (col_iter_map[it]>cols[1]) --col_iter_map[it];
   312 	if (col_iter_map[it]>cols[1]) --col_iter_map[it];
   288       }
   313       }
   289     }
   314     }
       
   315     ///.
   290     void eraseRow(const RowIt& row_it) {
   316     void eraseRow(const RowIt& row_it) {
   291       row_iter_map.set(row_it, VALID_ID, INVALID_ID);
   317       row_iter_map.set(row_it, VALID_ID, INVALID_ID);
   292       int rows[2];
   318       int rows[2];
   293       rows[1]=row_iter_map[row_it];
   319       rows[1]=row_iter_map[row_it];
   294       lpx_del_rows(lp, 1, rows);
   320       lpx_del_rows(lp, 1, rows);
   297       for (row_iter_map.first(it, VALID_ID); 
   323       for (row_iter_map.first(it, VALID_ID); 
   298 	   row_iter_map.valid(it); row_iter_map.next(it)) {
   324 	   row_iter_map.valid(it); row_iter_map.next(it)) {
   299 	if (row_iter_map[it]>rows[1]) --row_iter_map[it];
   325 	if (row_iter_map[it]>rows[1]) --row_iter_map[it];
   300       }
   326       }
   301     }
   327     }
       
   328     ///.
   302     void setColBounds(const ColIt& col_it, int bound_type, 
   329     void setColBounds(const ColIt& col_it, int bound_type, 
   303 		      double lo, double up) {
   330 		      double lo, double up) {
   304       lpx_set_col_bnds(lp, col_iter_map[col_it], bound_type, lo, up);
   331       lpx_set_col_bnds(lp, col_iter_map[col_it], bound_type, lo, up);
   305     }
   332     }
       
   333     ///.
   306     void setObjCoef(const ColIt& col_it, double obj_coef) { 
   334     void setObjCoef(const ColIt& col_it, double obj_coef) { 
   307       lpx_set_obj_coef(lp, col_iter_map[col_it], obj_coef);
   335       lpx_set_obj_coef(lp, col_iter_map[col_it], obj_coef);
   308     }
   336     }
       
   337     ///.
   309     void setRowBounds(const RowIt& row_it, int bound_type, 
   338     void setRowBounds(const RowIt& row_it, int bound_type, 
   310 		      double lo, double up) {
   339 		      double lo, double up) {
   311       lpx_set_row_bnds(lp, row_iter_map[row_it], bound_type, lo, up);
   340       lpx_set_row_bnds(lp, row_iter_map[row_it], bound_type, lo, up);
   312     }
   341     }
   313 //   void setObjCoef(const RowIt& row_it, double obj_coef) { 
   342 //   void setObjCoef(const RowIt& row_it, double obj_coef) { 
   314 //     lpx_set_obj_coef(lp, row_iter_map[row_it], obj_coef);
   343 //     lpx_set_obj_coef(lp, row_iter_map[row_it], obj_coef);
   315 //   }
   344 //   }
       
   345     ///.
   316     void solveSimplex() { lpx_simplex(lp); }
   346     void solveSimplex() { lpx_simplex(lp); }
       
   347     ///.
   317     void solvePrimalSimplex() { lpx_simplex(lp); }
   348     void solvePrimalSimplex() { lpx_simplex(lp); }
       
   349     ///.
   318     void solveDualSimplex() { lpx_simplex(lp); }
   350     void solveDualSimplex() { lpx_simplex(lp); }
       
   351     ///.
   319     double getPrimal(const ColIt& col_it) {
   352     double getPrimal(const ColIt& col_it) {
   320       return lpx_get_col_prim(lp, col_iter_map[col_it]);
   353       return lpx_get_col_prim(lp, col_iter_map[col_it]);
   321     }
   354     }
       
   355     ///.
   322     double getObjVal() { return lpx_get_obj_val(lp); }
   356     double getObjVal() { return lpx_get_obj_val(lp); }
       
   357     ///.
   323     int rowNum() const { return lpx_get_num_rows(lp); }
   358     int rowNum() const { return lpx_get_num_rows(lp); }
       
   359     ///.
   324     int colNum() const { return lpx_get_num_cols(lp); }
   360     int colNum() const { return lpx_get_num_cols(lp); }
       
   361     ///.
   325     int warmUp() { return lpx_warm_up(lp); }
   362     int warmUp() { return lpx_warm_up(lp); }
       
   363     ///.
   326     void printWarmUpStatus(int i) {
   364     void printWarmUpStatus(int i) {
   327       switch (i) {
   365       switch (i) {
   328 	case LPX_E_OK: cout << "LPX_E_OK" << endl; break;
   366 	case LPX_E_OK: cout << "LPX_E_OK" << endl; break;
   329 	case LPX_E_EMPTY: cout << "LPX_E_EMPTY" << endl; break;	
   367 	case LPX_E_EMPTY: cout << "LPX_E_EMPTY" << endl; break;	
   330 	case LPX_E_BADB: cout << "LPX_E_BADB" << endl; break;
   368 	case LPX_E_BADB: cout << "LPX_E_BADB" << endl; break;
   331 	case LPX_E_SING: cout << "LPX_E_SING" << endl; break;
   369 	case LPX_E_SING: cout << "LPX_E_SING" << endl; break;
   332       }
   370       }
   333     }
   371     }
       
   372     ///.
   334     int getPrimalStatus() { return lpx_get_prim_stat(lp); }
   373     int getPrimalStatus() { return lpx_get_prim_stat(lp); }
       
   374     ///.
   335     void printPrimalStatus(int i) {
   375     void printPrimalStatus(int i) {
   336       switch (i) {
   376       switch (i) {
   337 	case LPX_P_UNDEF: cout << "LPX_P_UNDEF" << endl; break;
   377 	case LPX_P_UNDEF: cout << "LPX_P_UNDEF" << endl; break;
   338 	case LPX_P_FEAS: cout << "LPX_P_FEAS" << endl; break;	
   378 	case LPX_P_FEAS: cout << "LPX_P_FEAS" << endl; break;	
   339 	case LPX_P_INFEAS: cout << "LPX_P_INFEAS" << endl; break;
   379 	case LPX_P_INFEAS: cout << "LPX_P_INFEAS" << endl; break;
   340 	case LPX_P_NOFEAS: cout << "LPX_P_NOFEAS" << endl; break;
   380 	case LPX_P_NOFEAS: cout << "LPX_P_NOFEAS" << endl; break;
   341       }
   381       }
   342     }
   382     }
       
   383     ///.
   343     int getDualStatus() { return lpx_get_dual_stat(lp); }
   384     int getDualStatus() { return lpx_get_dual_stat(lp); }
       
   385     ///.
   344     void printDualStatus(int i) {
   386     void printDualStatus(int i) {
   345       switch (i) {
   387       switch (i) {
   346 	case LPX_D_UNDEF: cout << "LPX_D_UNDEF" << endl; break;
   388 	case LPX_D_UNDEF: cout << "LPX_D_UNDEF" << endl; break;
   347 	case LPX_D_FEAS: cout << "LPX_D_FEAS" << endl; break;	
   389 	case LPX_D_FEAS: cout << "LPX_D_FEAS" << endl; break;	
   348 	case LPX_D_INFEAS: cout << "LPX_D_INFEAS" << endl; break;
   390 	case LPX_D_INFEAS: cout << "LPX_D_INFEAS" << endl; break;
   351     }
   393     }
   352     /// Returns the status of the slack variable assigned to row \c row_it.
   394     /// Returns the status of the slack variable assigned to row \c row_it.
   353     int getRowStat(const RowIt& row_it) { 
   395     int getRowStat(const RowIt& row_it) { 
   354       return lpx_get_row_stat(lp, row_iter_map[row_it]); 
   396       return lpx_get_row_stat(lp, row_iter_map[row_it]); 
   355     }
   397     }
       
   398     ///.
   356     void printRowStatus(int i) {
   399     void printRowStatus(int i) {
   357       switch (i) {
   400       switch (i) {
   358 	case LPX_BS: cout << "LPX_BS" << endl; break;
   401 	case LPX_BS: cout << "LPX_BS" << endl; break;
   359 	case LPX_NL: cout << "LPX_NL" << endl; break;	
   402 	case LPX_NL: cout << "LPX_NL" << endl; break;	
   360 	case LPX_NU: cout << "LPX_NU" << endl; break;
   403 	case LPX_NU: cout << "LPX_NU" << endl; break;
   364     }
   407     }
   365     /// Returns the status of the variable assigned to column \c col_it.
   408     /// Returns the status of the variable assigned to column \c col_it.
   366     int getColStat(const ColIt& col_it) { 
   409     int getColStat(const ColIt& col_it) { 
   367       return lpx_get_col_stat(lp, col_iter_map[col_it]); 
   410       return lpx_get_col_stat(lp, col_iter_map[col_it]); 
   368     }
   411     }
       
   412     ///.
   369     void printColStatus(int i) {
   413     void printColStatus(int i) {
   370       switch (i) {
   414       switch (i) {
   371 	case LPX_BS: cout << "LPX_BS" << endl; break;
   415 	case LPX_BS: cout << "LPX_BS" << endl; break;
   372 	case LPX_NL: cout << "LPX_NL" << endl; break;	
   416 	case LPX_NL: cout << "LPX_NL" << endl; break;	
   373 	case LPX_NU: cout << "LPX_NU" << endl; break;
   417 	case LPX_NU: cout << "LPX_NU" << endl; break;
   374 	case LPX_NF: cout << "LPX_NF" << endl; break;
   418 	case LPX_NF: cout << "LPX_NF" << endl; break;
   375 	case LPX_NS: cout << "LPX_NS" << endl; break;
   419 	case LPX_NS: cout << "LPX_NS" << endl; break;
   376       }
   420       }
   377     }
   421     }
   378   };
   422   };
       
   423   
       
   424   /// @}
   379 
   425 
   380 } //namespace hugo
   426 } //namespace hugo
   381 
   427 
   382 #endif //HUGO_LP_SOLVER_WRAPPER_H
   428 #endif //HUGO_LP_SOLVER_WRAPPER_H