- LpGlpk added to the makefile
authoralpar
Fri, 25 Mar 2005 16:19:03 +0000
changeset 1263a490938ad0aa
parent 1262 61f989e3e525
child 1264 92ba3e62825d
- LpGlpk added to the makefile
- missing const_cast<>() added
- prop for two new functions (solve() and solution())
src/work/athos/lp/Makefile
src/work/athos/lp/lp_base.h
src/work/athos/lp/lp_glpk.cc
src/work/athos/lp/lp_glpk.h
src/work/athos/lp/lp_solver_skeleton.cc
src/work/athos/lp/lp_solver_skeleton.h
src/work/athos/lp/lp_test.cc
     1.1 --- a/src/work/athos/lp/Makefile	Fri Mar 25 15:32:05 2005 +0000
     1.2 +++ b/src/work/athos/lp/Makefile	Fri Mar 25 16:19:03 2005 +0000
     1.3 @@ -8,8 +8,10 @@
     1.4  lp_base.o: lp_base.cc lp_base.h lin_expr.h
     1.5  lp_solver_skeleton.o: lp_solver_skeleton.cc lp_solver_skeleton.h lp_base.h \
     1.6  		lin_expr.h
     1.7 +lp_glpk.o: lp_glpk.cc lp_glpk.h lp_base.h \
     1.8 +		lin_expr.h
     1.9  lp_test.o: lp_test.cc lp_base.h lin_expr.h lp_solver_skeleton.h lp_base.h \
    1.10  		lin_expr.h
    1.11  
    1.12 -lp_test: lp_test.o lp_base.o lp_solver_skeleton.o 
    1.13 -	g++ -o $@ $^
    1.14 \ No newline at end of file
    1.15 +lp_test: lp_test.o lp_base.o lp_solver_skeleton.o lp_glpk.o
    1.16 +	g++ -o $@ $^ -lglpk
    1.17 \ No newline at end of file
     2.1 --- a/src/work/athos/lp/lp_base.h	Fri Mar 25 15:32:05 2005 +0000
     2.2 +++ b/src/work/athos/lp/lp_base.h	Fri Mar 25 16:19:03 2005 +0000
     2.3 @@ -100,6 +100,18 @@
     2.4      
     2.5    public:
     2.6  
     2.7 +    ///\e
     2.8 +    enum SolutionType {
     2.9 +      ///\e
    2.10 +      INFEASIBLE = 0,
    2.11 +      ///\e
    2.12 +      UNBOUNDED = 1,
    2.13 +      ///\e
    2.14 +      OPTIMAL = 2,
    2.15 +      ///\e
    2.16 +      FEASIBLE = 3,
    2.17 +    };
    2.18 +      
    2.19      ///The floating point type used by the solver
    2.20      typedef double Value;
    2.21      ///The infinity constant
    2.22 @@ -160,8 +172,6 @@
    2.23      _FixId rows;
    2.24      _FixId cols;
    2.25  
    2.26 -    //MATRIX MANIPULATING FUNCTIONS
    2.27 -
    2.28      /// \e
    2.29      virtual int _addCol() = 0;
    2.30      /// \e
    2.31 @@ -212,6 +222,17 @@
    2.32      virtual void _setObjCoeff(int i, Value obj_coef) = 0;
    2.33  
    2.34      ///\e
    2.35 +    
    2.36 +    ///\bug Wrong interface
    2.37 +    ///
    2.38 +    virtual SolutionType _solve() = 0;
    2.39 +
    2.40 +    ///\e
    2.41 +
    2.42 +    ///\bug Wrong interface
    2.43 +    ///
    2.44 +    virtual Value _getSolution(int i) = 0;
    2.45 +    ///\e
    2.46  
    2.47      ///\bug unimplemented!!!!
    2.48      void clearObj() {}
    2.49 @@ -221,8 +242,13 @@
    2.50      ///\e
    2.51      virtual ~LpSolverBase() {}
    2.52  
    2.53 +    ///\name Building up and modification of the LP
    2.54 +
    2.55 +    ///@{
    2.56 +
    2.57      ///Add a new empty column (i.e a new variable) to the LP
    2.58      Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
    2.59 +
    2.60      ///\brief Fill the elements of a container with newly created columns
    2.61      ///(i.e a new variables)
    2.62      ///
    2.63 @@ -261,6 +287,7 @@
    2.64        return s;
    2.65      }
    2.66  #endif
    2.67 +
    2.68      ///Add a new empty row (i.e a new constaint) to the LP
    2.69  
    2.70      ///This function adds a new empty row (i.e a new constaint) to the LP.
    2.71 @@ -348,6 +375,27 @@
    2.72        for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
    2.73  	setObjCoeff((*i).first,(*i).second);
    2.74      }
    2.75 +
    2.76 +    ///@}
    2.77 +
    2.78 +
    2.79 +    ///\name Solving the LP
    2.80 +
    2.81 +    ///@{
    2.82 +
    2.83 +    ///\e
    2.84 +    SolutionType solve() { return _solve(); }
    2.85 +    
    2.86 +    ///@}
    2.87 +    
    2.88 +    ///\name Obtaining the solution LP
    2.89 +
    2.90 +    ///@{
    2.91 +
    2.92 +    ///\e
    2.93 +    Value solution(Col c) { return _getSolution(cols.floatingId(c.id)); }
    2.94 +
    2.95 +    ///@}
    2.96      
    2.97    };  
    2.98  
     3.1 --- a/src/work/athos/lp/lp_glpk.cc	Fri Mar 25 15:32:05 2005 +0000
     3.2 +++ b/src/work/athos/lp/lp_glpk.cc	Fri Mar 25 16:19:03 2005 +0000
     3.3 @@ -41,18 +41,22 @@
     3.4    
     3.5      void LpGlpk::_setRowCoeffs(int i, 
     3.6  			       int length,
     3.7 -			       int   * indices, 
     3.8 -			       Value   * values )
     3.9 +			       const int   * indices, 
    3.10 +			       const Value   * values )
    3.11      {
    3.12 -	lpx_set_mat_row(lp, i, length, indices, values);
    3.13 +      lpx_set_mat_row(lp, i, length,
    3.14 +		      const_cast<int * >(indices) ,
    3.15 +		      const_cast<Value * >(values));
    3.16      }
    3.17    
    3.18      void LpGlpk::_setColCoeffs(int i, 
    3.19  			       int length,
    3.20 -			       int   * indices, 
    3.21 -			       Value   * values)
    3.22 +			       const int   * indices, 
    3.23 +			       const Value   * values)
    3.24      {
    3.25 -	lpx_set_mat_col(lp, i, length, indices, values);
    3.26 +      lpx_set_mat_col(lp, i, length,
    3.27 +		      const_cast<int * >(indices),
    3.28 +		      const_cast<Value * >(values));
    3.29      }
    3.30    
    3.31      void LpGlpk::_setColLowerBound(int i, Value lo)
    3.32 @@ -233,6 +237,19 @@
    3.33        lpx_set_obj_coef(lp, i, obj_coef);
    3.34      }
    3.35  
    3.36 +
    3.37 +  LpGlpk::SolutionType LpGlpk::_solve()
    3.38 +  {
    3.39 +    return OPTIMAL;
    3.40 +  }
    3.41 +
    3.42 +  LpGlpk::Value LpGlpk::_getSolution(int i)
    3.43 +  {
    3.44 +    return 0;
    3.45 +  }
    3.46 +  
    3.47 +
    3.48 +
    3.49  } //END OF NAMESPACE LEMON
    3.50  
    3.51  #endif //LEMON_LP_GLPK_CC
     4.1 --- a/src/work/athos/lp/lp_glpk.h	Fri Mar 25 15:32:05 2005 +0000
     4.2 +++ b/src/work/athos/lp/lp_glpk.h	Fri Mar 25 16:19:03 2005 +0000
     4.3 @@ -55,17 +55,27 @@
     4.4      virtual int _addRow();
     4.5      virtual void _setRowCoeffs(int i, 
     4.6  			       int length,
     4.7 -                               int   * indices, 
     4.8 -                               Value   * values );
     4.9 +                               const int   * indices, 
    4.10 +                               const Value   * values );
    4.11      virtual void _setColCoeffs(int i, 
    4.12  			       int length,
    4.13 -                               int   * indices, 
    4.14 -                               Value   * values);
    4.15 +                               const int   * indices, 
    4.16 +                               const Value   * values);
    4.17      virtual void _setColLowerBound(int i, Value value);
    4.18      virtual void _setColUpperBound(int i, Value value);
    4.19      virtual void _setRowLowerBound(int i, Value value);
    4.20      virtual void _setRowUpperBound(int i, Value value);
    4.21      virtual void _setObjCoeff(int i, Value obj_coef);
    4.22 +    ///\e
    4.23 +    
    4.24 +    ///\bug Unimplemented
    4.25 +    ///
    4.26 +    virtual SolutionType _solve();
    4.27 +    ///\e
    4.28 +    
    4.29 +    ///\bug Unimplemented
    4.30 +    ///
    4.31 +    virtual Value _getSolution(int i);
    4.32  
    4.33    };
    4.34  } //END OF NAMESPACE LEMON
     5.1 --- a/src/work/athos/lp/lp_solver_skeleton.cc	Fri Mar 25 15:32:05 2005 +0000
     5.2 +++ b/src/work/athos/lp/lp_solver_skeleton.cc	Fri Mar 25 16:19:03 2005 +0000
     5.3 @@ -64,6 +64,16 @@
     5.4    void LpSolverSkeleton::_setObjCoeff(int i, Value obj_coef)
     5.5    {
     5.6    }
     5.7 +
     5.8 +  LpSolverSkeleton::SolutionType LpSolverSkeleton::_solve()
     5.9 +  {
    5.10 +    return OPTIMAL;
    5.11 +  }
    5.12 +
    5.13 +  LpSolverSkeleton::Value LpSolverSkeleton::_getSolution(int i)
    5.14 +  {
    5.15 +    return 0;
    5.16 +  }
    5.17    
    5.18  } //namespace lemon
    5.19  
     6.1 --- a/src/work/athos/lp/lp_solver_skeleton.h	Fri Mar 25 15:32:05 2005 +0000
     6.2 +++ b/src/work/athos/lp/lp_solver_skeleton.h	Fri Mar 25 16:19:03 2005 +0000
     6.3 @@ -43,6 +43,9 @@
     6.4      virtual void _setRowLowerBound(int i, Value value);
     6.5      virtual void _setRowUpperBound(int i, Value value);
     6.6      virtual void _setObjCoeff(int i, Value obj_coef);
     6.7 +    virtual SolutionType _solve();
     6.8 +    virtual Value _getSolution(int i);
     6.9 +
    6.10    };  
    6.11  
    6.12  } //namespace lemon
     7.1 --- a/src/work/athos/lp/lp_test.cc	Fri Mar 25 15:32:05 2005 +0000
     7.2 +++ b/src/work/athos/lp/lp_test.cc	Fri Mar 25 16:19:03 2005 +0000
     7.3 @@ -1,11 +1,11 @@
     7.4  #include"lp_solver_skeleton.h"
     7.5 +#include"lp_glpk.h"
     7.6  
     7.7  using namespace lemon;
     7.8  
     7.9 -int main()
    7.10 +void lpTest(LpSolverBase & lp)
    7.11  {
    7.12 -  typedef LpSolverSkeleton LP;
    7.13 -  LP lp;
    7.14 +  typedef LpSolverBase LP;
    7.15  
    7.16    std::vector<LP::Col> x;
    7.17    for(int i=0;i<10;i++) x.push_back(lp.addCol());
    7.18 @@ -37,3 +37,13 @@
    7.19    lp.addRow(LP::INF,3.0*(p1+p2*2-5*p3+12-p4/3)+2*p4-4,23);
    7.20    lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
    7.21  }
    7.22 +
    7.23 +
    7.24 +int main() 
    7.25 +{
    7.26 +  LpSolverSkeleton lp_skel;
    7.27 +  LpGlpk lp_glpk;
    7.28 +
    7.29 +  lpTest(lp_skel);
    7.30 +  lpTest(lp_glpk);
    7.31 +}