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

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


Author: marci
Date: Wed Jan 26 16:54:06 2005
New Revision: 1496

Added:
   hugo/trunk/src/work/marci/lp/expression.h
   hugo/trunk/src/work/marci/lp/expression_test.cc
Modified:
   hugo/trunk/src/work/marci/lp/lp_solver_wrapper_3.h
   hugo/trunk/src/work/marci/lp/makefile

Log:
A proposal or test implementation for linear expression`


Added: hugo/trunk/src/work/marci/lp/expression.h
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/marci/lp/expression.h	Wed Jan 26 16:54:06 2005
@@ -0,0 +1,91 @@
+// -*- c++ -*-
+#ifndef LEMON_EXPRESSION_H
+#define LEMON_EXPRESSION_H
+
+#include <iostream>
+#include <map>
+
+namespace lemon {
+
+  /*! \brief Linear expression
+
+    \c Expr<_Col,_Value> implements a class of linear expressions with the 
+    operations of addition and multiplication with scalar. 
+
+    \author Marton Makai
+   */
+  template <typename _Col, typename _Value>
+  class Expr;
+
+  template <typename _Col, typename _Value>
+  class Expr {
+  protected:
+    typedef 
+    typename std::map<_Col, _Value> Data; 
+    Data data;
+  public:
+    Expr() { }
+    Expr(_Col _col) { 
+      data.insert(std::make_pair(_col, 1));
+    }
+    Expr& operator*=(_Value _value) {
+      for (typename Data::iterator i=data.begin(); 
+	   i!=data.end(); ++i) {
+	(*i).second *= _value;
+      }
+      return *this;
+    }
+    Expr& operator+=(const Expr<_Col, _Value>& expr) {
+      for (typename Data::const_iterator j=expr.data.begin(); 
+	   j!=expr.data.end(); ++j) {
+	typename Data::iterator i=data.find((*j).first);
+	if (i==data.end()) {
+	  data.insert(std::make_pair((*j).first, (*j).second));
+	} else {
+	  (*i).second+=(*j).second;
+	}
+      }
+      return *this;
+    }
+    template <typename _C, typename _V> 
+    friend std::ostream& operator<<(std::ostream& os, 
+				    const Expr<_C, _V>& expr);
+  };
+
+  template <typename _Col, typename _Value>
+  Expr<_Col, _Value> operator*(_Value _value, _Col _col) {
+    Expr<_Col, _Value> tmp(_col);
+    tmp*=_value;
+    return tmp;
+  }
+
+  template <typename _Col, typename _Value>
+  Expr<_Col, _Value> operator*(_Value _value, 
+			       const Expr<_Col, _Value>& expr) {
+    Expr<_Col, _Value> tmp(expr);
+    tmp*=_value;
+    return tmp;
+  }
+
+  template <typename _Col, typename _Value>
+  Expr<_Col, _Value> operator+(const Expr<_Col, _Value>& expr1, 
+			       const Expr<_Col, _Value>& expr2) {
+    Expr<_Col, _Value> tmp(expr1);
+    tmp+=expr2;
+    return tmp;
+  }
+
+  template <typename _Col, typename _Value>
+  std::ostream& operator<<(std::ostream& os, 
+			   const Expr<_Col, _Value>& expr) {
+    for (typename Expr<_Col, _Value>::Data::const_iterator i=
+	   expr.data.begin(); 
+	 i!=expr.data.end(); ++i) {
+      os << (*i).second << "*" << (*i).first << " ";
+    }
+    return os;
+  }
+  
+} //namespace lemon
+
+#endif //LEMON_EXPRESSION_H

Added: hugo/trunk/src/work/marci/lp/expression_test.cc
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/marci/lp/expression_test.cc	Wed Jan 26 16:54:06 2005
@@ -0,0 +1,23 @@
+#include <expression.h>
+#include <iostream>
+#include <string>
+
+using std::cout;
+using std::endl;
+using std::string;
+using namespace lemon;
+
+int main() {
+  Expr<string, double> b;
+  cout << b << endl;
+  Expr<string, double> c("f");
+  cout << c << endl;
+  Expr<string, double> d=8.0*string("g");
+  cout << d << endl;
+  c*=5;
+  cout << c << endl;
+  Expr<string, double> e=c;
+  e+=8.9*9.0*string("l");
+  cout << e << endl;
+  cout << c+d << endl;
+}

Modified: 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_wrapper_3.h	Wed Jan 26 16:54:06 2005
@@ -8,6 +8,8 @@
 
 // #include <stdio.h>
 #include <stdlib.h>
+#include <iostream>
+#include <map>
 // #include <stdio>
 //#include <stdlib>
 extern "C" {
@@ -156,6 +158,105 @@
     bool valid(const ClassIt& it) const { return it.i!=-1; }
   };
 
+  template <typename _Col, typename _Value>
+  class Expr;
+
+  template <typename _Col, typename _Value>
+  class SmallExpr {
+    template <typename _C, typename _V> 
+    friend class Expr;
+  protected:
+    _Col col;
+    _Value value;
+  public:
+    SmallExpr(_Col _col) : col(_col), value(1) { 
+    }
+    SmallExpr& operator *= (_Value _value) {
+      value*=_value;
+      return *this;
+    }
+    //    template <typename _C, typename _V>
+    //    friend SmallExpr<_C, _V> operator* (_V _value, 
+    //					const SmallExpr<_C, _V>& expr);
+    template <typename _C, typename _V>
+    friend std::ostream& operator<<(std::ostream& os, 
+				    const SmallExpr<_C, _V>& expr);
+  };
+
+  template <typename _Col, typename _Value>
+  SmallExpr<_Col, _Value> 
+  operator* (_Value value, 
+	     const SmallExpr<_Col, _Value>& expr) {
+    SmallExpr<_Col, _Value> tmp;
+    tmp=expr;
+    tmp*=value;
+    return tmp;
+  }
+
+  template <typename _Col, typename _Value>
+  std::ostream& operator<<(std::ostream& os, 
+			   const SmallExpr<_Col, _Value>& expr) {
+    os << expr.value << "*" << expr.col;
+    return os;
+  }
+
+  template <typename _Col, typename _Value>
+  class Expr {
+  protected:
+    typedef 
+    typename std::map<_Col, _Value> Data; 
+    Data data;
+  public:
+    Expr() { }
+    Expr(SmallExpr<_Col, _Value> expr) { 
+      data.insert(std::make_pair(expr.col, expr.value));
+    }
+//     Expr(_Col col) { 
+//       data.insert(std::make_pair(col, 1));
+//     }
+    Expr& operator*=(_Value _value) {
+      for (typename Data::iterator i=data.begin(); 
+	   i!=data.end(); ++i) {
+	(*i).second *= _value;
+      }
+      return *this;
+    }
+    Expr& operator+=(SmallExpr<_Col, _Value> expr) {
+      typename Data::iterator i=data.find(expr.col);
+      if (i==data.end()) {
+	data.insert(std::make_pair(expr.col, expr.value));
+      } else {
+	(*i).second+=expr.value;
+      }
+      return *this;
+    }
+    //    template <typename _C, typename _V> 
+    //    friend Expr<_C, _V> operator*(_V _value, const Expr<_C, _V>& expr);
+    template <typename _C, typename _V> 
+    friend std::ostream& operator<<(std::ostream& os, 
+				    const Expr<_C, _V>& expr);
+  };
+
+  template <typename _Col, typename _Value>
+  Expr<_Col, _Value> operator*(_Value _value, 
+			       const Expr<_Col, _Value>& expr) {
+    Expr<_Col, _Value> tmp;
+    tmp=expr;
+    tmp*=_value;
+    return tmp;
+  }
+
+  template <typename _Col, typename _Value>
+  std::ostream& operator<<(std::ostream& os, 
+			   const Expr<_Col, _Value>& expr) {
+    for (typename Expr<_Col, _Value>::Data::const_iterator i=
+	   expr.data.begin(); 
+	 i!=expr.data.end(); ++i) {
+      os << (*i).second << "*" << (*i).first << " ";
+    }
+    return os;
+  }
+
   /*! \e
    */
   template <typename _Value>

Modified: hugo/trunk/src/work/marci/lp/makefile
==============================================================================
--- hugo/trunk/src/work/marci/lp/makefile	(original)
+++ hugo/trunk/src/work/marci/lp/makefile	Wed Jan 26 16:54:06 2005
@@ -5,7 +5,7 @@
 CXXFLAGS = -g -O2 -W -Wall $(INCLUDEDIRS) -ansi -pedantic
 LDFLAGS  =  -lglpk#-lcplex -lm -lpthread -lilocplex -L/usr/local/cplex/cplex75/lib/i86_linux2_glibc2.2_gcc3.0/static_mt# -L$(GLPKROOT)/lib
 
-BINARIES = cplex_1 max_flow_by_lp# sample sample2 sample11 sample15
+BINARIES = expression_test max_flow_by_lp# sample sample2 sample11 sample15
 
 #include ../makefile
 



More information about the Lemon-commits mailing list