A proposal or test implementation for linear expression`
authormarci
Wed, 26 Jan 2005 15:54:06 +0000
changeset 1097c91e765266d7
parent 1096 1cfb25ef14d2
child 1098 e3b3667c6857
A proposal or test implementation for linear expression`
src/work/marci/lp/expression.h
src/work/marci/lp/expression_test.cc
src/work/marci/lp/lp_solver_wrapper_3.h
src/work/marci/lp/makefile
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/marci/lp/expression.h	Wed Jan 26 15:54:06 2005 +0000
     1.3 @@ -0,0 +1,91 @@
     1.4 +// -*- c++ -*-
     1.5 +#ifndef LEMON_EXPRESSION_H
     1.6 +#define LEMON_EXPRESSION_H
     1.7 +
     1.8 +#include <iostream>
     1.9 +#include <map>
    1.10 +
    1.11 +namespace lemon {
    1.12 +
    1.13 +  /*! \brief Linear expression
    1.14 +
    1.15 +    \c Expr<_Col,_Value> implements a class of linear expressions with the 
    1.16 +    operations of addition and multiplication with scalar. 
    1.17 +
    1.18 +    \author Marton Makai
    1.19 +   */
    1.20 +  template <typename _Col, typename _Value>
    1.21 +  class Expr;
    1.22 +
    1.23 +  template <typename _Col, typename _Value>
    1.24 +  class Expr {
    1.25 +  protected:
    1.26 +    typedef 
    1.27 +    typename std::map<_Col, _Value> Data; 
    1.28 +    Data data;
    1.29 +  public:
    1.30 +    Expr() { }
    1.31 +    Expr(_Col _col) { 
    1.32 +      data.insert(std::make_pair(_col, 1));
    1.33 +    }
    1.34 +    Expr& operator*=(_Value _value) {
    1.35 +      for (typename Data::iterator i=data.begin(); 
    1.36 +	   i!=data.end(); ++i) {
    1.37 +	(*i).second *= _value;
    1.38 +      }
    1.39 +      return *this;
    1.40 +    }
    1.41 +    Expr& operator+=(const Expr<_Col, _Value>& expr) {
    1.42 +      for (typename Data::const_iterator j=expr.data.begin(); 
    1.43 +	   j!=expr.data.end(); ++j) {
    1.44 +	typename Data::iterator i=data.find((*j).first);
    1.45 +	if (i==data.end()) {
    1.46 +	  data.insert(std::make_pair((*j).first, (*j).second));
    1.47 +	} else {
    1.48 +	  (*i).second+=(*j).second;
    1.49 +	}
    1.50 +      }
    1.51 +      return *this;
    1.52 +    }
    1.53 +    template <typename _C, typename _V> 
    1.54 +    friend std::ostream& operator<<(std::ostream& os, 
    1.55 +				    const Expr<_C, _V>& expr);
    1.56 +  };
    1.57 +
    1.58 +  template <typename _Col, typename _Value>
    1.59 +  Expr<_Col, _Value> operator*(_Value _value, _Col _col) {
    1.60 +    Expr<_Col, _Value> tmp(_col);
    1.61 +    tmp*=_value;
    1.62 +    return tmp;
    1.63 +  }
    1.64 +
    1.65 +  template <typename _Col, typename _Value>
    1.66 +  Expr<_Col, _Value> operator*(_Value _value, 
    1.67 +			       const Expr<_Col, _Value>& expr) {
    1.68 +    Expr<_Col, _Value> tmp(expr);
    1.69 +    tmp*=_value;
    1.70 +    return tmp;
    1.71 +  }
    1.72 +
    1.73 +  template <typename _Col, typename _Value>
    1.74 +  Expr<_Col, _Value> operator+(const Expr<_Col, _Value>& expr1, 
    1.75 +			       const Expr<_Col, _Value>& expr2) {
    1.76 +    Expr<_Col, _Value> tmp(expr1);
    1.77 +    tmp+=expr2;
    1.78 +    return tmp;
    1.79 +  }
    1.80 +
    1.81 +  template <typename _Col, typename _Value>
    1.82 +  std::ostream& operator<<(std::ostream& os, 
    1.83 +			   const Expr<_Col, _Value>& expr) {
    1.84 +    for (typename Expr<_Col, _Value>::Data::const_iterator i=
    1.85 +	   expr.data.begin(); 
    1.86 +	 i!=expr.data.end(); ++i) {
    1.87 +      os << (*i).second << "*" << (*i).first << " ";
    1.88 +    }
    1.89 +    return os;
    1.90 +  }
    1.91 +  
    1.92 +} //namespace lemon
    1.93 +
    1.94 +#endif //LEMON_EXPRESSION_H
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/work/marci/lp/expression_test.cc	Wed Jan 26 15:54:06 2005 +0000
     2.3 @@ -0,0 +1,23 @@
     2.4 +#include <expression.h>
     2.5 +#include <iostream>
     2.6 +#include <string>
     2.7 +
     2.8 +using std::cout;
     2.9 +using std::endl;
    2.10 +using std::string;
    2.11 +using namespace lemon;
    2.12 +
    2.13 +int main() {
    2.14 +  Expr<string, double> b;
    2.15 +  cout << b << endl;
    2.16 +  Expr<string, double> c("f");
    2.17 +  cout << c << endl;
    2.18 +  Expr<string, double> d=8.0*string("g");
    2.19 +  cout << d << endl;
    2.20 +  c*=5;
    2.21 +  cout << c << endl;
    2.22 +  Expr<string, double> e=c;
    2.23 +  e+=8.9*9.0*string("l");
    2.24 +  cout << e << endl;
    2.25 +  cout << c+d << endl;
    2.26 +}
     3.1 --- a/src/work/marci/lp/lp_solver_wrapper_3.h	Wed Jan 26 09:00:40 2005 +0000
     3.2 +++ b/src/work/marci/lp/lp_solver_wrapper_3.h	Wed Jan 26 15:54:06 2005 +0000
     3.3 @@ -8,6 +8,8 @@
     3.4  
     3.5  // #include <stdio.h>
     3.6  #include <stdlib.h>
     3.7 +#include <iostream>
     3.8 +#include <map>
     3.9  // #include <stdio>
    3.10  //#include <stdlib>
    3.11  extern "C" {
    3.12 @@ -156,6 +158,105 @@
    3.13      bool valid(const ClassIt& it) const { return it.i!=-1; }
    3.14    };
    3.15  
    3.16 +  template <typename _Col, typename _Value>
    3.17 +  class Expr;
    3.18 +
    3.19 +  template <typename _Col, typename _Value>
    3.20 +  class SmallExpr {
    3.21 +    template <typename _C, typename _V> 
    3.22 +    friend class Expr;
    3.23 +  protected:
    3.24 +    _Col col;
    3.25 +    _Value value;
    3.26 +  public:
    3.27 +    SmallExpr(_Col _col) : col(_col), value(1) { 
    3.28 +    }
    3.29 +    SmallExpr& operator *= (_Value _value) {
    3.30 +      value*=_value;
    3.31 +      return *this;
    3.32 +    }
    3.33 +    //    template <typename _C, typename _V>
    3.34 +    //    friend SmallExpr<_C, _V> operator* (_V _value, 
    3.35 +    //					const SmallExpr<_C, _V>& expr);
    3.36 +    template <typename _C, typename _V>
    3.37 +    friend std::ostream& operator<<(std::ostream& os, 
    3.38 +				    const SmallExpr<_C, _V>& expr);
    3.39 +  };
    3.40 +
    3.41 +  template <typename _Col, typename _Value>
    3.42 +  SmallExpr<_Col, _Value> 
    3.43 +  operator* (_Value value, 
    3.44 +	     const SmallExpr<_Col, _Value>& expr) {
    3.45 +    SmallExpr<_Col, _Value> tmp;
    3.46 +    tmp=expr;
    3.47 +    tmp*=value;
    3.48 +    return tmp;
    3.49 +  }
    3.50 +
    3.51 +  template <typename _Col, typename _Value>
    3.52 +  std::ostream& operator<<(std::ostream& os, 
    3.53 +			   const SmallExpr<_Col, _Value>& expr) {
    3.54 +    os << expr.value << "*" << expr.col;
    3.55 +    return os;
    3.56 +  }
    3.57 +
    3.58 +  template <typename _Col, typename _Value>
    3.59 +  class Expr {
    3.60 +  protected:
    3.61 +    typedef 
    3.62 +    typename std::map<_Col, _Value> Data; 
    3.63 +    Data data;
    3.64 +  public:
    3.65 +    Expr() { }
    3.66 +    Expr(SmallExpr<_Col, _Value> expr) { 
    3.67 +      data.insert(std::make_pair(expr.col, expr.value));
    3.68 +    }
    3.69 +//     Expr(_Col col) { 
    3.70 +//       data.insert(std::make_pair(col, 1));
    3.71 +//     }
    3.72 +    Expr& operator*=(_Value _value) {
    3.73 +      for (typename Data::iterator i=data.begin(); 
    3.74 +	   i!=data.end(); ++i) {
    3.75 +	(*i).second *= _value;
    3.76 +      }
    3.77 +      return *this;
    3.78 +    }
    3.79 +    Expr& operator+=(SmallExpr<_Col, _Value> expr) {
    3.80 +      typename Data::iterator i=data.find(expr.col);
    3.81 +      if (i==data.end()) {
    3.82 +	data.insert(std::make_pair(expr.col, expr.value));
    3.83 +      } else {
    3.84 +	(*i).second+=expr.value;
    3.85 +      }
    3.86 +      return *this;
    3.87 +    }
    3.88 +    //    template <typename _C, typename _V> 
    3.89 +    //    friend Expr<_C, _V> operator*(_V _value, const Expr<_C, _V>& expr);
    3.90 +    template <typename _C, typename _V> 
    3.91 +    friend std::ostream& operator<<(std::ostream& os, 
    3.92 +				    const Expr<_C, _V>& expr);
    3.93 +  };
    3.94 +
    3.95 +  template <typename _Col, typename _Value>
    3.96 +  Expr<_Col, _Value> operator*(_Value _value, 
    3.97 +			       const Expr<_Col, _Value>& expr) {
    3.98 +    Expr<_Col, _Value> tmp;
    3.99 +    tmp=expr;
   3.100 +    tmp*=_value;
   3.101 +    return tmp;
   3.102 +  }
   3.103 +
   3.104 +  template <typename _Col, typename _Value>
   3.105 +  std::ostream& operator<<(std::ostream& os, 
   3.106 +			   const Expr<_Col, _Value>& expr) {
   3.107 +    for (typename Expr<_Col, _Value>::Data::const_iterator i=
   3.108 +	   expr.data.begin(); 
   3.109 +	 i!=expr.data.end(); ++i) {
   3.110 +      os << (*i).second << "*" << (*i).first << " ";
   3.111 +    }
   3.112 +    return os;
   3.113 +  }
   3.114 +
   3.115    /*! \e
   3.116     */
   3.117    template <typename _Value>
     4.1 --- a/src/work/marci/lp/makefile	Wed Jan 26 09:00:40 2005 +0000
     4.2 +++ b/src/work/marci/lp/makefile	Wed Jan 26 15:54:06 2005 +0000
     4.3 @@ -5,7 +5,7 @@
     4.4  CXXFLAGS = -g -O2 -W -Wall $(INCLUDEDIRS) -ansi -pedantic
     4.5  LDFLAGS  =  -lglpk#-lcplex -lm -lpthread -lilocplex -L/usr/local/cplex/cplex75/lib/i86_linux2_glibc2.2_gcc3.0/static_mt# -L$(GLPKROOT)/lib
     4.6  
     4.7 -BINARIES = cplex_1 max_flow_by_lp# sample sample2 sample11 sample15
     4.8 +BINARIES = expression_test max_flow_by_lp# sample sample2 sample11 sample15
     4.9  
    4.10  #include ../makefile
    4.11