[Lemon-commits] [lemon_svn] marci: r1421 - hugo/trunk/src/work/marci/lp

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:45:19 CET 2006


Author: marci
Date: Mon Dec  6 12:56:10 2004
New Revision: 1421

Added:
   hugo/trunk/src/work/marci/lp/lp_solver_wrapper_2.h
      - copied, changed from r1417, /hugo/trunk/src/work/marci/lp/lp_solver_wrapper.h
Modified:
   hugo/trunk/src/work/marci/lp/max_flow_by_lp.cc
   hugo/trunk/src/work/marci/lp/min_cost_gen_flow.h

Log:
:-(


Copied: hugo/trunk/src/work/marci/lp/lp_solver_wrapper_2.h (from r1417, /hugo/trunk/src/work/marci/lp/lp_solver_wrapper.h)
==============================================================================
--- /hugo/trunk/src/work/marci/lp/lp_solver_wrapper.h	(original)
+++ hugo/trunk/src/work/marci/lp/lp_solver_wrapper_2.h	Mon Dec  6 12:56:10 2004
@@ -156,6 +156,128 @@
     /// True iff the iterator is valid.
     bool valid(const ClassIt& it) const { return it.i!=-1; }
   };
+
+  /*! \e
+   */
+  class LPSolverBase {
+  public:
+    /// \e
+    typedef IterablePartition<int>::ClassIt RowIt;
+    /// \e
+    typedef IterablePartition<int>::ClassIt ColIt;
+  protected:
+    /// \e
+    IterablePartition<int> row_iter_map;
+    /// \e
+    IterablePartition<int> col_iter_map;
+    /// \e
+    const int VALID_ID;
+    /// \e
+    const int INVALID_ID;
+  public:
+    /// \e
+    LPSolverBase() : row_iter_map(2), 
+		     col_iter_map(2), 
+		     VALID_ID(0), INVALID_ID(1) { }
+    /// \e
+    virtual ~LPSolverBase() { }
+    /// \e
+    virtual void setMinimize() = 0;
+    /// \e
+    virtual void setMaximize() = 0;
+    /// \e
+    virtual RowIt addRow() = 0;
+    /// \e
+    virtual ColIt addCol() = 0;
+    /// temporally, glpk style indexing
+    virtual void setRowCoeffs(RowIt row_it, int num, 
+			      int* indices, double* doubles) = 0;
+    //pair<RowIt, double>-bol kell megadni egy std range-et
+    /// \e
+    template <typename Begin, typename End>
+    void setRowCoeffs(RowIt row_it, Begin begin, End end) {
+      int mem_length=1+colNum();
+      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;
+      }
+      setRowCoeffs(row_it, length, indices, doubles);
+      delete [] indices;
+      delete [] doubles;
+    }
+    /// temporally, glpk style indexing
+    virtual void setColCoeffs(ColIt col_it, int num, 
+			      int* indices, double* doubles) = 0;
+    //pair<ColIt, double>-bol kell megadni egy std range-et
+    /// \e
+    template <typename Begin, typename End>
+    void setColCoeffs(ColIt col_it, Begin begin, End end) {
+      int mem_length=1+rowNum();
+      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;
+      }
+      setColCoeffs(col_it, length, indices, doubles);
+      delete [] indices;
+      delete [] doubles;
+    }
+    /// \e
+    virtual void eraseCol(const ColIt& col_it) = 0;
+    /// \e
+    virtual void eraseRow(const RowIt& row_it) = 0;
+    /// \e
+    virtual void setColBounds(const ColIt& col_it, int bound_type, 
+			      double lo, double up) =0; 
+    /// \e
+    virtual double getObjCoef(const ColIt& col_it) = 0;
+    /// \e
+    virtual void setRowBounds(const RowIt& row_it, int bound_type, 
+			      double lo, double up) = 0;
+    /// \e
+    virtual void setObjCoef(const ColIt& col_it, double obj_coef) = 0;
+    /// \e
+    virtual void solveSimplex() = 0;
+    /// \e
+    virtual void solvePrimalSimplex() = 0;
+    /// \e
+    virtual void solveDualSimplex() = 0;
+    /// \e
+    virtual double getPrimal(const ColIt& col_it) = 0;
+    /// \e
+    virtual double 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
   /// 
@@ -164,76 +286,60 @@
   /// 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 {
+  class LPSolverWrapper : public LPSolverBase {
   public:
+    typedef LPSolverBase Parent;
 
-//   class Row {
-//   protected:
-//     int i;
-//   public:
-//     Row() { }
-//     Row(const Invalid&) : i(0) { }
-//     Row(const int& _i) : i(_i) { }
-//     operator int() const { return i; }
-//   };
-//   class RowIt : public Row {
-//   public:
-//     RowIt(const Row& row) : Row(row) { }
-//   };
+    //   class Row {
+    //   protected:
+    //     int i;
+    //   public:
+    //     Row() { }
+    //     Row(const Invalid&) : i(0) { }
+    //     Row(const int& _i) : i(_i) { }
+    //     operator int() const { return i; }
+    //   };
+    //   class RowIt : public Row {
+    //   public:
+    //     RowIt(const Row& row) : Row(row) { }
+    //   };
 
-//   class Col {
-//   protected:
-//     int i;
-//   public:
-//     Col() { }
-//     Col(const Invalid&) : i(0) { }
-//     Col(const int& _i) : i(_i) { }
-//     operator int() const { return i; }
-//   };
-//   class ColIt : public Col {
-//     ColIt(const Col& col) : Col(col) { }
-//   };
+    //   class Col {
+    //   protected:
+    //     int i;
+    //   public:
+    //     Col() { }
+    //     Col(const Invalid&) : i(0) { }
+    //     Col(const int& _i) : i(_i) { }
+    //     operator int() const { return i; }
+    //   };
+    //   class ColIt : public Col {
+    //     ColIt(const Col& col) : Col(col) { }
+    //   };
 
   public:
-    ///.
+    /// \e
     LPX* lp;
-    ///.
-    typedef IterablePartition<int>::ClassIt RowIt;
-    ///.
-    IterablePartition<int> row_iter_map;
-    ///.
-    typedef IterablePartition<int>::ClassIt ColIt;
-    ///.
-    IterablePartition<int> col_iter_map;
-    //std::vector<int> row_id_to_lp_row_id;
-    //std::vector<int> col_id_to_lp_col_id;
-    ///.
-    const int VALID_ID;
-    ///.
-    const int INVALID_ID;
 
   public:
-    ///.
-    LPSolverWrapper() : lp(lpx_create_prob()), 
-			row_iter_map(2), 
-			col_iter_map(2), 
-			//row_id_to_lp_row_id(), col_id_to_lp_col_id(), 
-			VALID_ID(0), INVALID_ID(1) {
+    /// \e
+    LPSolverWrapper() : LPSolverBase(), 
+			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);
     }
-    ///.
+    /// \e
     ColIt addCol() {
       int i=lpx_add_cols(lp, 1);  
       ColIt col_it;
@@ -247,12 +353,12 @@
 	//int j=col_id_to_lp_col_id.size()-1;
 	col_it=col_iter_map.push_back(i, VALID_ID);
       }
-//    edge_index_map.set(e, i);
-//    lpx_set_col_bnds(lp, i, LPX_DB, 0.0, 1.0);
-//    lpx_set_obj_coef(lp, i, cost[e]);    
+      //    edge_index_map.set(e, i);
+      //    lpx_set_col_bnds(lp, i, LPX_DB, 0.0, 1.0);
+      //    lpx_set_obj_coef(lp, i, cost[e]);    
       return col_it;
     }
-    ///.
+    /// \e
     RowIt addRow() {
       int i=lpx_add_rows(lp, 1);  
       RowIt row_it;
@@ -265,43 +371,53 @@
       }
       return row_it;
     }
-    //pair<RowIt, double>-bol kell megadni egy std range-et
-    ///.
-    template <typename Begin, typename End>
-    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<ColIt, double>-bol kell megadni egy std range-et
-    ///.
-    template <typename Begin, typename End>
-    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;
-      }
+    using Parent::setRowCoeffs;
+    void setRowCoeffs(RowIt row_it, int length, 
+		      int* indices, double* doubles) {
       lpx_set_mat_row(lp, row_iter_map[row_it], length, indices, doubles);
-      delete [] indices;
-      delete [] 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<RowIt, double>-bol kell megadni egy std range-et
+    //     /// \e
+    //     template <typename Begin, typename End>
+    //     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<ColIt, double>-bol kell megadni egy std range-et
+    //     /// \e
+    //     template <typename Begin, typename End>
+    //     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
     void eraseCol(const ColIt& col_it) {
       col_iter_map.set(col_it, VALID_ID, INVALID_ID);
       int cols[2];
@@ -314,7 +430,7 @@
 	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_ID, INVALID_ID);
       int rows[2];
@@ -327,99 +443,99 @@
 	if (row_iter_map[it]>rows[1]) --row_iter_map[it];
       }
     }
-    ///.
+    /// \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;
+      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;
+      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;
+      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;
+      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;
+      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;
       }
     }
   };

Modified: hugo/trunk/src/work/marci/lp/max_flow_by_lp.cc
==============================================================================
--- hugo/trunk/src/work/marci/lp/max_flow_by_lp.cc	(original)
+++ hugo/trunk/src/work/marci/lp/max_flow_by_lp.cc	Mon Dec  6 12:56:10 2004
@@ -10,7 +10,7 @@
 #include <lemon/preflow.h>
 #include <augmenting_flow.h>
 //#include <preflow_res.h>
-#include <lp_solver_wrapper.h>
+//#include <lp_solver_wrapper_2.h>
 #include <min_cost_gen_flow.h>
 
 // Use a DIMACS max flow file as stdin.
@@ -179,7 +179,7 @@
   MinCostGenFlow<Graph, int, Excess, LCap> 
     min_cost(g, excess, lcap, cap, flow, cost); 
   min_cost.feasible();
-  min_cost.run();
+  min_cost.runByLP();
 
   std::cout << "elapsed time: " << ts << std::endl;
   std::cout << "flow value: "<< flow[e] << std::endl;

Modified: hugo/trunk/src/work/marci/lp/min_cost_gen_flow.h
==============================================================================
--- hugo/trunk/src/work/marci/lp/min_cost_gen_flow.h	(original)
+++ hugo/trunk/src/work/marci/lp/min_cost_gen_flow.h	Mon Dec  6 12:56:10 2004
@@ -14,7 +14,7 @@
 //#include <augmenting_flow.h>
 //#include <preflow_res.h>
 #include <work/marci/merge_node_graph_wrapper.h>
-#include <work/marci/lp/lp_solver_wrapper.h>
+#include <work/marci/lp/lp_solver_wrapper_2.h>
 
 namespace lemon {
 
@@ -211,10 +211,11 @@
       return (expected>=min_cost_flow.flowValue());
     }
     void runByLP() {
-      LPSolverWrapper lp;
+      typedef LPSolverWrapper LPSolver;
+      LPSolver lp;
       lp.setMinimize();
-      typedef LPSolverWrapper::ColIt ColIt;
-      typedef LPSolverWrapper::RowIt RowIt;
+      typedef LPSolver::ColIt ColIt;
+      typedef LPSolver::RowIt RowIt;
       typedef typename Graph::template EdgeMap<ColIt> EdgeIndexMap;
       EdgeIndexMap edge_index_map(g);
       PrimalMap<typename Graph::Edge, EdgeIndexMap> lp_flow(lp, edge_index_map);



More information about the Lemon-commits mailing list