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

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


Author: marci
Date: Tue Feb  1 13:53:30 2005
New Revision: 1510

Added:
   hugo/trunk/src/work/marci/lp/lp_solver_base.h
      - copied, changed from r1509, /hugo/trunk/src/work/marci/lp/lp_solver_wrapper_3.h
Removed:
   hugo/trunk/src/work/marci/lp/lp_solver_wrapper_2.h
   hugo/trunk/src/work/marci/lp/lp_solver_wrapper_3.h
Modified:
   hugo/trunk/src/work/marci/lp/max_flow_expression.cc

Log:
lower and upper bound handling functions for rows


Copied: hugo/trunk/src/work/marci/lp/lp_solver_base.h (from r1509, /hugo/trunk/src/work/marci/lp/lp_solver_wrapper_3.h)
==============================================================================
--- /hugo/trunk/src/work/marci/lp/lp_solver_wrapper_3.h	(original)
+++ hugo/trunk/src/work/marci/lp/lp_solver_base.h	Tue Feb  1 13:53:30 2005
@@ -24,9 +24,7 @@
 #include <memory>
 #include <utility>
 
-//#include <sage_graph.h>
 //#include <lemon/list_graph.h>
-//#include <lemon/graph_wrapper.h>
 #include <lemon/invalid.h>
 #include <expression.h>
 //#include <bfs_dfs.h>
@@ -173,12 +171,13 @@
 
 
   /*! \e
-
-  \todo A[x,y]-t cserel. Jobboldal, baloldal csere.
-  \todo LEKERDEZESEK!!!
-  \todo DOKSI!!!! Doxygen group!!!
-
-   */
+    \todo A[x,y]-t cserel. Jobboldal, baloldal csere.
+    \todo LEKERDEZESEK!!!
+    \todo DOKSI!!!! Doxygen group!!!
+    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.
+  */
   template <typename _Value>
   class LPSolverBase {
   public:
@@ -219,19 +218,19 @@
 
   protected:
     /// \e
+    virtual int _addCol() = 0;
+    /// \e
     virtual int _addRow() = 0;
     /// \e
-    virtual int _addCol() = 0;
+    virtual void _eraseCol(int i) = 0;
+    /// \e
+    virtual void _eraseRow(int i) = 0;
     /// \e
     virtual void _setRowCoeffs(int i, 
 			       const std::vector<std::pair<int, _Value> >& coeffs) = 0;
     /// \e
     virtual void _setColCoeffs(int i, 
 			       const std::vector<std::pair<int, _Value> >& coeffs) = 0;
-    /// \e
-    virtual void _eraseCol(int i) = 0;
-    /// \e
-    virtual void _eraseRow(int i) = 0;
   public:
     /// \e
     enum Bound { FREE, LOWER, UPPER, DOUBLE, FIXED };
@@ -242,26 +241,40 @@
     /// _Value or -INF.
     virtual void _setColLowerBound(int i, _Value value) = 0;
     /// \e
-    /// The upper bound of a variable (column) have to be given by an 
-    /// extended number of type _Value, i.e. a finite number of type 
-    /// _Value or INF.
-    virtual void _setColUpperBound(int i, _Value value) = 0;
-    /// \e
     /// The lower bound of a variable (column) is an 
     /// extended number of type _Value, i.e. a finite number of type 
     /// _Value or -INF.
     virtual _Value _getColLowerBound(int i) = 0;
     /// \e
+    /// The upper bound of a variable (column) have to be given by an 
+    /// extended number of type _Value, i.e. a finite number of type 
+    /// _Value or INF.
+    virtual void _setColUpperBound(int i, _Value value) = 0;
+    /// \e
     /// The upper bound of a variable (column) is an 
     /// extended number of type _Value, i.e. a finite number of type 
     /// _Value or INF.
     virtual _Value _getColUpperBound(int i) = 0;
     /// \e
-    virtual void _setColBounds(int i, Bound bound, 
-			       _Value lo, _Value up) = 0; 
+    /// The lower bound of a linear expression (row) have to be given by an 
+    /// extended number of type _Value, i.e. a finite number of type 
+    /// _Value or -INF.
+    virtual void _setRowLowerBound(int i, _Value value) = 0;
+    /// \e
+    /// The lower bound of a linear expression (row) is an 
+    /// extended number of type _Value, i.e. a finite number of type 
+    /// _Value or -INF.
+    virtual _Value _getRowLowerBound(int i) = 0;
+    /// \e
+    /// The upper bound of a linear expression (row) have to be given by an 
+    /// extended number of type _Value, i.e. a finite number of type 
+    /// _Value or INF.
+    virtual void _setRowUpperBound(int i, _Value value) = 0;
     /// \e
-    virtual void _setRowBounds(int i, Bound bound, 
-			       _Value lo, _Value up) = 0; 
+    /// The upper bound of a linear expression (row) is an 
+    /// extended number of type _Value, i.e. a finite number of type 
+    /// _Value or INF.
+    virtual _Value _getRowUpperBound(int i) = 0;
     /// \e
     virtual void _setObjCoef(int i, _Value obj_coef) = 0;
     /// \e
@@ -270,25 +283,13 @@
     //LOW LEVEL, SOLUTION RETRIEVING FUNCTIONS
 
   protected:
+    /// \e
     virtual _Value _getPrimal(int i) = 0;
 
     //HIGH LEVEL INTERFACE, MATRIX MANIPULATING FUNTIONS
 
   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;
@@ -302,24 +303,17 @@
       return col_it;
     }
     /// \e
-    template <typename Begin, typename End>
-    void setRowCoeffs(RowIt row_it, Begin begin, End end) {
-      std::vector<std::pair<int, double> > 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 <typename Begin, typename End>
-    void setColCoeffs(ColIt col_it, Begin begin, End end) {
-      std::vector<std::pair<int, double> > coeffs;
-      for ( ; begin!=end; ++begin) {
-	coeffs.push_back(std::
-			 make_pair(row_iter_map[begin->first], begin->second));
+    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);
       }
-      _setColCoeffs(col_iter_map[col_it], coeffs);
+      return row_it;
     }
     /// \e
     void eraseCol(const ColIt& col_it) {
@@ -348,30 +342,56 @@
       }
     }
     /// \e
-    void setColLowerBound(ColIt col_it, _Value lo) {
-      _setColLowerBound(col_iter_map[col_it], lo);
+    template <typename Begin, typename End>
+    void setRowCoeffs(RowIt row_it, Begin begin, End end) {
+      std::vector<std::pair<int, double> > 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
-    void setColUpperBound(ColIt col_it, _Value up) {
-      _setColUpperBound(col_iter_map[col_it], up);
+    template <typename Begin, typename End>
+    void setColCoeffs(ColIt col_it, Begin begin, End end) {
+      std::vector<std::pair<int, double> > 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);
+    }
+    /// \e
+    void setColLowerBound(ColIt col_it, _Value lo) {
+      _setColLowerBound(col_iter_map[col_it], lo);
     }
     /// \e
     _Value getColLowerBound(ColIt col_it) {
       return _getColLowerBound(col_iter_map[col_it]);
     }
     /// \e
+    void setColUpperBound(ColIt col_it, _Value up) {
+      _setColUpperBound(col_iter_map[col_it], up);
+    }
+    /// \e
     _Value getColUpperBound(ColIt col_it) {      
       return _getColUpperBound(col_iter_map[col_it]);
     }
     /// \e
-    void setColBounds(const ColIt& col_it, Bound bound, 
-		      _Value lo, _Value up) {
-      _setColBounds(col_iter_map[col_it], bound, lo, up);
+    void setRowLowerBound(RowIt row_it, _Value lo) {
+      _setRowLowerBound(row_iter_map[row_it], lo);
     }
     /// \e
-    void setRowBounds(const RowIt& row_it, Bound bound, 
-		      _Value lo, _Value up) {
-      _setRowBounds(row_iter_map[row_it], bound, lo, up);
+    _Value getRowLowerBound(RowIt row_it) {
+      return _getRowLowerBound(row_iter_map[row_it]);
+    }
+    /// \e
+    void setRowUpperBound(RowIt row_it, _Value up) {
+      _setRowUpperBound(row_iter_map[row_it], up);
+    }
+    /// \e
+    _Value getRowUpperBound(RowIt row_it) {      
+      return _getRowUpperBound(row_iter_map[row_it]);
     }
     /// \e
     void setObjCoef(const ColIt& col_it, _Value obj_coef) {
@@ -415,6 +435,7 @@
 	setObjCoef((*i).first, (*i).second);
       }
     }
+
     //SOLVER FUNCTIONS
 
     /// \e
@@ -428,6 +449,8 @@
     //HIGH LEVEL, SOLUTION RETRIEVING FUNCTIONS
 
   public:
+
+    /// \e
     _Value getPrimal(const ColIt& col_it) {
       return _getPrimal(col_iter_map[col_it]);
     }
@@ -466,13 +489,9 @@
   const _Value LPSolverBase<_Value>::INF=std::numeric_limits<_Value>::infinity();
 
 
-  /// \brief Wrappers for LP solvers
+  /// \brief Wrapper for GLPK solver
   /// 
-  /// 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.
+  /// This class implements a lemon wrapper for GLPK.
   class LPGLPK : public LPSolverBase<double> {
   public:
     typedef LPSolverBase<double> Parent;
@@ -606,6 +625,23 @@
 	}
       }
     }
+    virtual double _getColLowerBound(int i) {
+      int b=lpx_get_col_type(lp, i);
+      switch (b) {
+      case LPX_FR:
+	return -INF;
+      case LPX_LO:
+	return lpx_get_col_lb(lp, i);
+      case LPX_UP:
+	return -INF;
+      case LPX_DB:
+      case LPX_FX:
+	return lpx_get_col_lb(lp, i);
+      default: ;
+	//FIXME error
+	return 0.0;
+      }
+    }
     virtual void _setColUpperBound(int i, double up) {
       if (up==-INF) {
 	//FIXME error
@@ -652,25 +688,126 @@
 	}
       }
     }
-    virtual double _getColLowerBound(int i) {
+    virtual double _getColUpperBound(int i) {
       int b=lpx_get_col_type(lp, i);
       switch (b) {
       case LPX_FR:
+      case LPX_LO:
+	return INF;
+      case LPX_UP:
+      case LPX_DB:
+      case LPX_FX:
+	return lpx_get_col_ub(lp, i);
+      default: ;
+	//FIXME error
+	return 0.0;
+      }
+    }
+    virtual void _setRowLowerBound(int i, double lo) {
+      if (lo==INF) {
+	//FIXME error
+      }
+      int b=lpx_get_row_type(lp, i);
+      double up=lpx_get_row_ub(lp, i);	
+      if (lo==-INF) {
+	switch (b) {
+	case LPX_FR:
+	case LPX_LO:
+	  lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
+	  break;
+	case LPX_UP:
+	  break;
+	case LPX_DB:
+	case LPX_FX:
+	  lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
+	  break;
+	default: ;
+	  //FIXME error
+	}
+      } else {
+	switch (b) {
+	case LPX_FR:
+	case LPX_LO:
+	  lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
+	  break;
+	case LPX_UP:	  
+	case LPX_DB:
+	case LPX_FX:
+	  if (lo==up) 
+	    lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
+	  else 
+	    lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
+	  break;
+	default: ;
+	  //FIXME error
+	}
+      }
+    }
+    virtual double _getRowLowerBound(int i) {
+      int b=lpx_get_row_type(lp, i);
+      switch (b) {
+      case LPX_FR:
 	return -INF;
       case LPX_LO:
-	return lpx_get_col_lb(lp, i);
+	return lpx_get_row_lb(lp, i);
       case LPX_UP:
 	return -INF;
       case LPX_DB:
       case LPX_FX:
-	return lpx_get_col_lb(lp, i);
+	return lpx_get_row_lb(lp, i);
       default: ;
 	//FIXME error
 	return 0.0;
       }
     }
-    virtual double _getColUpperBound(int i) {
-      int b=lpx_get_col_type(lp, i);
+    virtual void _setRowUpperBound(int i, double up) {
+      if (up==-INF) {
+	//FIXME error
+      }
+      int b=lpx_get_row_type(lp, i);
+      double lo=lpx_get_row_lb(lp, i);
+      if (up==INF) {
+	switch (b) {
+	case LPX_FR:
+	case LPX_LO:
+	  break;
+	case LPX_UP:
+	  lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
+	  break;
+	case LPX_DB:
+	case LPX_FX:
+	  lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
+	  break;
+	default: ;
+	  //FIXME error
+	}
+      } else {
+	switch (b) {
+	case LPX_FR:
+	  lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
+	case LPX_LO:
+	  if (lo==up) 
+	    lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
+	  else
+	    lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
+	  break;
+	case LPX_UP:
+	  lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
+	  break;
+	case LPX_DB:
+	case LPX_FX:
+	  if (lo==up) 
+	    lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
+	  else 
+	    lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
+	  break;
+	default: ;
+	  //FIXME error
+	}
+      }
+    }
+    virtual double _getRowUpperBound(int i) {
+      int b=lpx_get_row_type(lp, i);
       switch (b) {
       case LPX_FR:
       case LPX_LO:
@@ -678,53 +815,12 @@
       case LPX_UP:
       case LPX_DB:
       case LPX_FX:
-	return lpx_get_col_ub(lp, i);
+	return lpx_get_row_ub(lp, i);
       default: ;
 	//FIXME error
 	return 0.0;
       }
     }
-    virtual void _setColBounds(int i, Bound bound, 
-			       double lo, double up) {
-      switch (bound) {
-      case FREE:
-	lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
-	break;
-      case LOWER:
-	lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
-	break;
-      case UPPER:
-	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
-	break;
-      case DOUBLE:
-	lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
-	break;
-      case FIXED:
-	lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
-	break;
-      }
-    } 
-    virtual void _setRowBounds(int i, Bound bound, 
-			       double lo, double up) {
-      switch (bound) {
-      case FREE:
-	lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
-	break;
-      case LOWER:
-	lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
-	break;
-      case UPPER:
-	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
-	break;
-      case DOUBLE:
-	lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
-	break;
-      case FIXED:
-	lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
-	break;
-      }
-    } 
-  protected:
     /// \e
     virtual double _getObjCoef(int i) { 
       return lpx_get_obj_coef(lp, i);

Modified: hugo/trunk/src/work/marci/lp/max_flow_expression.cc
==============================================================================
--- hugo/trunk/src/work/marci/lp/max_flow_expression.cc	(original)
+++ hugo/trunk/src/work/marci/lp/max_flow_expression.cc	Tue Feb  1 13:53:30 2005
@@ -6,7 +6,7 @@
 #include <lemon/list_graph.h>
 #include <lemon/dimacs.h>
 #include <lemon/time_measure.h>
-#include <lp_solver_wrapper_3.h>
+#include <lp_solver_base.h>
 
 using std::cout;
 using std::endl;
@@ -51,15 +51,15 @@
   EdgeIndexMap edge_index_map(g);
   PrimalMap<Edge, EdgeIndexMap> flow(lp, edge_index_map);
 
-  // capacity function
+  // nonnegativity of flow and capacity function
   for (Graph::EdgeIt e(g); e!=INVALID; ++e) {
     ColIt col_it=lp.addCol();
     edge_index_map.set(e, col_it);
     // interesting property in GLPK:
     // if you change the order of the following two lines, the 
     // two runs of GLPK are extremely different
-      lp.setColUpperBound(col_it, cap[e]);
       lp.setColLowerBound(col_it, 0);
+      lp.setColUpperBound(col_it, cap[e]);
   }
   
   for (Graph::NodeIt n(g); n!=INVALID; ++n) {
@@ -72,11 +72,12 @@
     if (n==s) {
       lp.setObjCoeffs(expr);      
     }
-    // flow conservation
+    // flow conservation constraints
     if ((n!=s) && (n!=t)) {
       RowIt row_it=lp.addRow();
       lp.setRowCoeffs(row_it, expr);
-      lp.setRowBounds(row_it, LPSolver::FIXED, 0.0, 0.0);
+      lp.setRowLowerBound(row_it, 0.0);
+      lp.setRowUpperBound(row_it, 0.0);
     }
   }
   lp.solveSimplex();



More information about the Lemon-commits mailing list