COIN-OR::LEMON - Graph Library

Changeset 1256:3bb4ed285c39 in lemon-0.x for src/work


Ignore:
Timestamp:
03/25/05 09:18:27 (19 years ago)
Author:
Alpar Juttner
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1683
Message:
  • src/lemon/utility.h: dummy<> template added
  • LpSolverBase::INF is now really defined
  • AddColSet?() to add several column at once
    • using enable_if
    • with some doxygen hack
  • More doc improvements
  • Better Makefile
Location:
src/work/athos/lp
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/work/athos/lp/Makefile

    r1254 r1256  
    22
    33all: lp_test
     4
     5clean:
     6        rm lp_test *.o
    47
    58lp_base.o: lp_base.cc lp_base.h lin_expr.h
  • src/work/athos/lp/lp_base.cc

    r1253 r1256  
    2020#include "lp_base.h"
    2121namespace lemon {
    22 
     22 
     23  const LpSolverBase::Value
     24  LpSolverBase::INF = std::numeric_limits<Value>::infinity();
    2325
    2426
  • src/work/athos/lp/lp_base.h

    r1253 r1256  
    1919
    2020#include<vector>
    21 
     21#include<limits>
     22
     23#include<lemon/utility.h>
    2224#include<lemon/error.h>
     25#include<lemon/invalid.h>
    2326
    2427#include"lin_expr.h"
     
    6669          first_free=next;
    6770        }
     71        return cross[n];
    6872      }
    6973      else throw LogicError(); //floatingId-s must form a continuous range;
     
    97101  public:
    98102
    99     /// \e
     103    ///The floating point type used by the solver
    100104    typedef double Value;
    101     /// \e
     105    ///The infinity constant
    102106    static const Value INF;
    103107   
    104     ///\e
    105     class Col { protected: int id; friend class LpSolverBase; };
    106     ///\e
    107     class Row { protected: int id; friend class LpSolverBase; };
     108    ///Refer to a column of the LP.
     109
     110    ///This type is used to refer to a column of the LP.
     111    ///
     112    ///Its value remains valid and correct even after the addition or erase of
     113    ///new column (unless the referred column itself was also deleted,
     114    ///of course).
     115    ///
     116    ///\todo Document what can one do with a Col (INVALID, comparing,
     117    ///it is similar to Node/Edge)
     118    class Col {
     119    protected:
     120      int id;
     121      friend class LpSolverBase;
     122    public:
     123      typedef True LpSolverCol;
     124      Col() {}
     125      Col(const Invalid&) : id(-1) {}
     126      bool operator<(Col c) const  {return id<c.id;}
     127      bool operator==(Col c) const  {return id==c.id;}
     128      bool operator!=(Col c) const  {return id==c.id;}
     129    };
     130
     131    ///Refer to a row of the LP.
     132
     133    ///This type is used to refer to a row of the LP.
     134    ///
     135    ///Its value remains valid and correct even after the addition or erase of
     136    ///new rows (unless the referred row itself was also deleted, of course).
     137    ///
     138    ///\todo Document what can one do with a Row (INVALID, comparing,
     139    ///it is similar to Node/Edge)
     140    class Row {
     141    protected:
     142      int id;
     143      friend class LpSolverBase;
     144    public:
     145      typedef True LpSolverRow;
     146      Row() {}
     147      Row(const Invalid&) : id(-1) {}
     148      typedef True LpSolverRow;
     149      bool operator<(Row c) const  {return id<c.id;}
     150      bool operator==(Row c) const  {return id==c.id;}
     151      bool operator!=(Row c) const  {return id==c.id;}
     152   };
    108153
    109154    typedef SparseLinExpr<Col, Value> Expr;
     
    176221    ///Add a new empty column (i.e a new variable) to the LP
    177222    Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
     223    ///\brief Fill the elements of a container with newly created columns
     224    ///(i.e a new variables)
     225    ///
     226    ///This magic function takes container as its argument
     227    ///and fills its elements
     228    ///with new columns (i.e. variables)
     229    ///\param t can be either any standard STL iterable container with
     230    ///\ref Col \c values_type or \c mapped_type
     231    ///like <tt>std::vector<LpSolverBase::Col></tt>,
     232    /// <tt>std::list<LpSolverBase::Col></tt> or
     233    /// <tt>std::map<AnyType,LpSolverBase::Col></tt> or
     234    ///it can be an iterable lemon map like
     235    /// <tt>ListGraph::NodeMap<LpSolverBase::Col></tt>.
     236    ///\return The number of the created column.
     237    ///\bug Iterable nodemap hasn't been implemented yet.
     238#ifdef DOXYGEN
     239    template<class T>
     240    int addColSet(T &t) { return 0;}
     241#else
     242    template<class T>
     243    typename enable_if<typename T::value_type::LpSolverCol,int>::type
     244    addColSet(T &t,dummy<0> = 0) {
     245      int s=0;
     246      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
     247      return s;
     248    }
     249    template<class T>
     250    typename enable_if<typename T::value_type::second_type::LpSolverCol,
     251                       int>::type
     252    addColSet(T &t,dummy<1> = 1) {
     253      int s=0;
     254      for(typename T::iterator i=t.begin();i!=t.end();++i) {
     255        i->second=addCol();
     256        s++;
     257      }
     258      return s;
     259    }
     260#endif
    178261    ///Add a new empty row (i.e a new constaint) to the LP
    179262    Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
     
    192275      indices.push_back(0);
    193276      values.push_back(0);
    194       for(Expr::iterator i=e.begin(); i!=e.end(); ++i) {
    195         indices.push_back(cols.floatingId((*i).first.id));
    196         values.push_back((*i).second);
    197       }
     277      for(Expr::iterator i=e.begin(); i!=e.end(); ++i)
     278        if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
     279          indices.push_back(cols.floatingId((*i).first.id));
     280          values.push_back((*i).second);
     281        }
    198282      _setRowCoeffs(rows.floatingId(r.id),indices.size()-1,
    199283                    &indices[0],&values[0]);
    200       _setRowLowerBound(rows.floatingId(r.id),l);
    201       _setRowUpperBound(rows.floatingId(r.id),l);
     284      _setRowLowerBound(rows.floatingId(r.id),l-e.constComp());
     285      _setRowUpperBound(rows.floatingId(r.id),u-e.constComp());
    202286      return r;
    203287    }
  • src/work/athos/lp/lp_test.cc

    r1254 r1256  
    55int main()
    66{
    7   LpSolverSkeleton lp;
     7  typedef LpSolverSkeleton LP;
     8  LP lp;
     9
     10  std::vector<LP::Col> x;
     11  for(int i=0;i<10;i++) x.push_back(lp.addCol());
     12
     13  std::vector<LP::Col> y(10);
     14  lp.addColSet(y);
     15
     16  std::map<int,LP::Col> z;
     17 
     18  z.insert(std::make_pair(12,INVALID));
     19  z.insert(std::make_pair(2,INVALID));
     20  z.insert(std::make_pair(7,INVALID));
     21  z.insert(std::make_pair(5,INVALID));
     22 
     23  lp.addColSet(z);
     24
     25
     26  LP::Expr e;
     27  e[x[3]]=2;
     28  e[x[3]]=4;
     29  e[x[3]]=1;
     30  e.constComp()=12;
     31  lp.addRow(LP::INF,e,23);
     32
    833}
Note: See TracChangeset for help on using the changeset viewer.