COIN-OR::LEMON - Graph Library

Changeset 1099:91a8ee9d088d in lemon-0.x for src/work/marci


Ignore:
Timestamp:
01/27/05 18:44:04 (19 years ago)
Author:
marci
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1498
Message:

-=, - operators in expressions

Location:
src/work/marci/lp
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/work/marci/lp/expression.h

    r1097 r1099  
    2020  template <typename _Col, typename _Value>
    2121  class Expr {
    22   protected:
     22//  protected:
     23  public:
    2324    typedef
    2425    typename std::map<_Col, _Value> Data;
    2526    Data data;
    2627  public:
     28    void simplify() {
     29      for (typename Data::iterator i=data.begin();
     30           i!=data.end(); ++i) {
     31        if ((*i).second==0) data.erase(i);
     32      }
     33    }
    2734    Expr() { }
    2835    Expr(_Col _col) {
     
    3441        (*i).second *= _value;
    3542      }
     43      simplify();
    3644      return *this;
    3745    }
     
    4654        }
    4755      }
     56      simplify();
     57      return *this;
     58    }
     59    Expr& operator-=(const Expr<_Col, _Value>& expr) {
     60      for (typename Data::const_iterator j=expr.data.begin();
     61           j!=expr.data.end(); ++j) {
     62        typename Data::iterator i=data.find((*j).first);
     63        if (i==data.end()) {
     64          data.insert(std::make_pair((*j).first, -(*j).second));
     65        } else {
     66          (*i).second+=-(*j).second;
     67        }
     68      }
     69      simplify();
    4870      return *this;
    4971    }
     
    5779    Expr<_Col, _Value> tmp(_col);
    5880    tmp*=_value;
     81    tmp.simplify();
    5982    return tmp;
    6083  }
     
    6588    Expr<_Col, _Value> tmp(expr);
    6689    tmp*=_value;
     90    tmp.simplify();
    6791    return tmp;
    6892  }
     
    7397    Expr<_Col, _Value> tmp(expr1);
    7498    tmp+=expr2;
     99    tmp.simplify();
     100    return tmp;
     101  }
     102
     103  template <typename _Col, typename _Value>
     104  Expr<_Col, _Value> operator-(const Expr<_Col, _Value>& expr1,
     105                               const Expr<_Col, _Value>& expr2) {
     106    Expr<_Col, _Value> tmp(expr1);
     107    tmp-=expr2;
     108    tmp.simplify();
    75109    return tmp;
    76110  }
  • src/work/marci/lp/lp_solver_wrapper_3.h

    r1097 r1099  
    2828//#include <lemon/graph_wrapper.h>
    2929#include <lemon/invalid.h>
     30#include <expression.h>
    3031//#include <bfs_dfs.h>
    3132//#include <stp.h>
     
    144145      /// Invalid constructor.
    145146      ClassIt(const Invalid&) : i(-1) { }
     147      friend bool operator<(const ClassIt& x, const ClassIt& y);
     148      friend std::ostream& operator<<(std::ostream& os,
     149                                      const ClassIt& it);
    146150    };
     151    friend bool operator<(const ClassIt& x, const ClassIt& y) {
     152      return (x.i < y.i);
     153    }
     154    friend std::ostream& operator<<(std::ostream& os,
     155                                    const ClassIt& it) {
     156      os << it.i;
     157      return os;
     158    }
    147159    /// First member of class \c class_id.
    148160    ClassIt& first(ClassIt& it, int class_id) const {
     
    159171  };
    160172
    161   template <typename _Col, typename _Value>
    162   class Expr;
    163 
    164   template <typename _Col, typename _Value>
    165   class SmallExpr {
    166     template <typename _C, typename _V>
    167     friend class Expr;
    168   protected:
    169     _Col col;
    170     _Value value;
    171   public:
    172     SmallExpr(_Col _col) : col(_col), value(1) {
    173     }
    174     SmallExpr& operator *= (_Value _value) {
    175       value*=_value;
    176       return *this;
    177     }
    178     //    template <typename _C, typename _V>
    179     //    friend SmallExpr<_C, _V> operator* (_V _value,
    180     //                                  const SmallExpr<_C, _V>& expr);
    181     template <typename _C, typename _V>
    182     friend std::ostream& operator<<(std::ostream& os,
    183                                     const SmallExpr<_C, _V>& expr);
    184   };
    185 
    186   template <typename _Col, typename _Value>
    187   SmallExpr<_Col, _Value>
    188   operator* (_Value value,
    189              const SmallExpr<_Col, _Value>& expr) {
    190     SmallExpr<_Col, _Value> tmp;
    191     tmp=expr;
    192     tmp*=value;
    193     return tmp;
    194   }
    195 
    196   template <typename _Col, typename _Value>
    197   std::ostream& operator<<(std::ostream& os,
    198                            const SmallExpr<_Col, _Value>& expr) {
    199     os << expr.value << "*" << expr.col;
    200     return os;
    201   }
    202 
    203   template <typename _Col, typename _Value>
    204   class Expr {
    205   protected:
    206     typedef
    207     typename std::map<_Col, _Value> Data;
    208     Data data;
    209   public:
    210     Expr() { }
    211     Expr(SmallExpr<_Col, _Value> expr) {
    212       data.insert(std::make_pair(expr.col, expr.value));
    213     }
    214 //     Expr(_Col col) {
    215 //       data.insert(std::make_pair(col, 1));
    216 //     }
    217     Expr& operator*=(_Value _value) {
    218       for (typename Data::iterator i=data.begin();
    219            i!=data.end(); ++i) {
    220         (*i).second *= _value;
    221       }
    222       return *this;
    223     }
    224     Expr& operator+=(SmallExpr<_Col, _Value> expr) {
    225       typename Data::iterator i=data.find(expr.col);
    226       if (i==data.end()) {
    227         data.insert(std::make_pair(expr.col, expr.value));
    228       } else {
    229         (*i).second+=expr.value;
    230       }
    231       return *this;
    232     }
    233     //    template <typename _C, typename _V>
    234     //    friend Expr<_C, _V> operator*(_V _value, const Expr<_C, _V>& expr);
    235     template <typename _C, typename _V>
    236     friend std::ostream& operator<<(std::ostream& os,
    237                                     const Expr<_C, _V>& expr);
    238   };
    239 
    240   template <typename _Col, typename _Value>
    241   Expr<_Col, _Value> operator*(_Value _value,
    242                                const Expr<_Col, _Value>& expr) {
    243     Expr<_Col, _Value> tmp;
    244     tmp=expr;
    245     tmp*=_value;
    246     return tmp;
    247   }
    248 
    249   template <typename _Col, typename _Value>
    250   std::ostream& operator<<(std::ostream& os,
    251                            const Expr<_Col, _Value>& expr) {
    252     for (typename Expr<_Col, _Value>::Data::const_iterator i=
    253            expr.data.begin();
    254          i!=expr.data.end(); ++i) {
    255       os << (*i).second << "*" << (*i).first << " ";
    256     }
    257     return os;
    258   }
    259173
    260174  /*! \e
     
    425339    }
    426340
     341    //MOST HIGH LEVEL, USER FRIEND FUNCTIONS
     342
     343    /// \e
     344    typedef Expr<ColIt, _Value> Expression;
     345    /// \e
     346    typedef Expr<RowIt, _Value> DualExpression;
     347    /// \e
     348    void setRowCoeffs(RowIt row_it, const Expression& expr) {
     349      std::vector<std::pair<int, _Value> > row_coeffs;
     350      for(typename Expression::Data::const_iterator i=expr.data.begin();
     351          i!=expr.data.end(); ++i) {
     352        row_coeffs.push_back(std::make_pair
     353                             (col_iter_map[(*i).first], (*i).second));
     354      }
     355      _setRowCoeffs(row_iter_map[row_it], row_coeffs);
     356    }
     357    /// \e
     358    void setColCoeffs(ColIt col_it, const DualExpression& expr) {
     359      std::vector<std::pair<int, _Value> > col_coeffs;
     360      for(typename DualExpression::Data::const_iterator i=expr.data.begin();
     361          i!=expr.data.end(); ++i) {
     362        col_coeffs.push_back(std::make_pair
     363                             (row_iter_map[(*i).first], (*i).second));
     364      }
     365      _setColCoeffs(col_iter_map[col_it], col_coeffs);
     366    }
     367    /// \e
     368    void setObjCoeffs(const Expression& expr) {
     369      for(typename Expression::Data::const_iterator i=expr.data.begin();
     370          i!=expr.data.end(); ++i) {
     371        setObjCoef((*i).first, (*i).second);
     372      }
     373    }
    427374    //SOLVER FUNCTIONS
    428375
Note: See TracChangeset for help on using the changeset viewer.