src/work/athos/lp/lp_base.h
author alpar
Thu, 31 Mar 2005 08:34:55 +0000
changeset 1279 7caed393608e
parent 1275 16980bf77bd3
child 1291 16cde3e1aa9f
permissions -rw-r--r--
LpSolverBase::Expr is documented
athos@1247
     1
/* -*- C++ -*-
alpar@1253
     2
 * src/lemon/lp_base.h - Part of LEMON, a generic C++ optimization library
athos@1247
     3
 *
athos@1247
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
athos@1247
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
athos@1247
     6
 *
athos@1247
     7
 * Permission to use, modify and distribute this software is granted
athos@1247
     8
 * provided that this copyright notice appears in all copies. For
athos@1247
     9
 * precise terms see the accompanying LICENSE file.
athos@1247
    10
 *
athos@1247
    11
 * This software is provided "AS IS" with no warranty of any kind,
athos@1247
    12
 * express or implied, and with no claim as to its suitability for any
athos@1247
    13
 * purpose.
athos@1247
    14
 *
athos@1247
    15
 */
athos@1247
    16
athos@1246
    17
#ifndef LEMON_LP_BASE_H
athos@1246
    18
#define LEMON_LP_BASE_H
athos@1246
    19
alpar@1253
    20
#include<vector>
alpar@1272
    21
#include<map>
alpar@1256
    22
#include<limits>
alpar@1273
    23
#include<math.h>
alpar@1253
    24
alpar@1256
    25
#include<lemon/utility.h>
alpar@1253
    26
#include<lemon/error.h>
alpar@1256
    27
#include<lemon/invalid.h>
alpar@1253
    28
alpar@1272
    29
//#include"lin_expr.h"
alpar@1272
    30
athos@1246
    31
///\file
athos@1246
    32
///\brief The interface of the LP solver interface.
athos@1246
    33
namespace lemon {
alpar@1253
    34
  
alpar@1253
    35
  ///Internal data structure to convert floating id's to fix one's
alpar@1253
    36
    
alpar@1279
    37
  ///\todo This might be implemented to be also usable in other places.
alpar@1253
    38
  class _FixId 
alpar@1253
    39
  {
alpar@1253
    40
    std::vector<int> index;
alpar@1253
    41
    std::vector<int> cross;
alpar@1253
    42
    int first_free;
alpar@1253
    43
  public:
alpar@1253
    44
    _FixId() : first_free(-1) {};
alpar@1253
    45
    ///Convert a floating id to a fix one
alpar@1253
    46
alpar@1253
    47
    ///\param n is a floating id
alpar@1253
    48
    ///\return the corresponding fix id
alpar@1253
    49
    int fixId(int n) {return cross[n];}
alpar@1253
    50
    ///Convert a fix id to a floating one
alpar@1253
    51
alpar@1253
    52
    ///\param n is a fix id
alpar@1253
    53
    ///\return the corresponding floating id
alpar@1253
    54
    int floatingId(int n) { return index[n];}
alpar@1253
    55
    ///Add a new floating id.
alpar@1253
    56
alpar@1253
    57
    ///\param n is a floating id
alpar@1253
    58
    ///\return the fix id of the new value
alpar@1253
    59
    ///\todo Multiple additions should also be handled.
alpar@1253
    60
    int insert(int n)
alpar@1253
    61
    {
alpar@1253
    62
      if(n>=int(cross.size())) {
alpar@1253
    63
	cross.resize(n+1);
alpar@1253
    64
	if(first_free==-1) {
alpar@1253
    65
	  cross[n]=index.size();
alpar@1253
    66
	  index.push_back(n);
alpar@1253
    67
	}
alpar@1253
    68
	else {
alpar@1253
    69
	  cross[n]=first_free;
alpar@1253
    70
	  int next=index[first_free];
alpar@1253
    71
	  index[first_free]=n;
alpar@1253
    72
	  first_free=next;
alpar@1253
    73
	}
alpar@1256
    74
	return cross[n];
alpar@1253
    75
      }
alpar@1273
    76
      ///\todo Create an own exception type.
alpar@1253
    77
      else throw LogicError(); //floatingId-s must form a continuous range;
alpar@1253
    78
    }
alpar@1253
    79
    ///Remove a fix id.
alpar@1253
    80
alpar@1253
    81
    ///\param n is a fix id
alpar@1253
    82
    ///
alpar@1253
    83
    void erase(int n) 
alpar@1253
    84
    {
alpar@1253
    85
      int fl=index[n];
alpar@1253
    86
      index[n]=first_free;
alpar@1253
    87
      first_free=n;
alpar@1253
    88
      for(int i=fl+1;i<int(cross.size());++i) {
alpar@1253
    89
	cross[i-1]=cross[i];
alpar@1253
    90
	index[cross[i]]--;
alpar@1253
    91
      }
alpar@1253
    92
      cross.pop_back();
alpar@1253
    93
    }
alpar@1253
    94
    ///An upper bound on the largest fix id.
alpar@1253
    95
alpar@1253
    96
    ///\todo Do we need this?
alpar@1253
    97
    ///
alpar@1253
    98
    std::size_t maxFixId() { return cross.size()-1; }
alpar@1253
    99
  
alpar@1253
   100
  };
alpar@1253
   101
    
alpar@1253
   102
  ///Common base class for LP solvers
athos@1246
   103
  class LpSolverBase {
alpar@1253
   104
    
athos@1247
   105
  public:
athos@1247
   106
alpar@1263
   107
    ///\e
alpar@1263
   108
    enum SolutionType {
alpar@1263
   109
      ///\e
alpar@1263
   110
      INFEASIBLE = 0,
alpar@1263
   111
      ///\e
alpar@1263
   112
      UNBOUNDED = 1,
alpar@1263
   113
      ///\e
alpar@1263
   114
      OPTIMAL = 2,
alpar@1263
   115
      ///\e
alpar@1263
   116
      FEASIBLE = 3,
alpar@1263
   117
    };
alpar@1263
   118
      
alpar@1256
   119
    ///The floating point type used by the solver
athos@1247
   120
    typedef double Value;
alpar@1256
   121
    ///The infinity constant
athos@1247
   122
    static const Value INF;
alpar@1264
   123
    ///The not a number constant
alpar@1264
   124
    static const Value NaN;
alpar@1253
   125
    
alpar@1256
   126
    ///Refer to a column of the LP.
alpar@1256
   127
alpar@1256
   128
    ///This type is used to refer to a column of the LP.
alpar@1256
   129
    ///
alpar@1256
   130
    ///Its value remains valid and correct even after the addition or erase of
alpar@1273
   131
    ///other columns.
alpar@1256
   132
    ///
alpar@1256
   133
    ///\todo Document what can one do with a Col (INVALID, comparing,
alpar@1256
   134
    ///it is similar to Node/Edge)
alpar@1256
   135
    class Col {
alpar@1256
   136
    protected:
alpar@1256
   137
      int id;
alpar@1256
   138
      friend class LpSolverBase;
alpar@1256
   139
    public:
alpar@1259
   140
      typedef Value ExprValue;
alpar@1256
   141
      typedef True LpSolverCol;
alpar@1256
   142
      Col() {}
alpar@1256
   143
      Col(const Invalid&) : id(-1) {}
alpar@1256
   144
      bool operator<(Col c) const  {return id<c.id;}
alpar@1256
   145
      bool operator==(Col c) const  {return id==c.id;}
alpar@1256
   146
      bool operator!=(Col c) const  {return id==c.id;}
alpar@1256
   147
    };
alpar@1256
   148
alpar@1256
   149
    ///Refer to a row of the LP.
alpar@1256
   150
alpar@1256
   151
    ///This type is used to refer to a row of the LP.
alpar@1256
   152
    ///
alpar@1256
   153
    ///Its value remains valid and correct even after the addition or erase of
alpar@1273
   154
    ///other rows.
alpar@1256
   155
    ///
alpar@1256
   156
    ///\todo Document what can one do with a Row (INVALID, comparing,
alpar@1256
   157
    ///it is similar to Node/Edge)
alpar@1256
   158
    class Row {
alpar@1256
   159
    protected:
alpar@1256
   160
      int id;
alpar@1256
   161
      friend class LpSolverBase;
alpar@1256
   162
    public:
alpar@1259
   163
      typedef Value ExprValue;
alpar@1256
   164
      typedef True LpSolverRow;
alpar@1256
   165
      Row() {}
alpar@1256
   166
      Row(const Invalid&) : id(-1) {}
alpar@1256
   167
      typedef True LpSolverRow;
alpar@1256
   168
      bool operator<(Row c) const  {return id<c.id;}
alpar@1256
   169
      bool operator==(Row c) const  {return id==c.id;}
alpar@1256
   170
      bool operator!=(Row c) const  {return id==c.id;} 
alpar@1256
   171
   };
alpar@1259
   172
    
alpar@1279
   173
    ///Linear expression of variables and a constant component
alpar@1279
   174
    
alpar@1279
   175
    ///This data structure strores a linear expression of the variables
alpar@1279
   176
    ///(\ref Col "Col"s) and also has a constant component.
alpar@1279
   177
    ///
alpar@1279
   178
    ///There are several ways to access and modify the contents of this
alpar@1279
   179
    ///container.
alpar@1279
   180
    ///- Its it fully compatible with \c std::map<Col,double>, so for expamle
alpar@1279
   181
    ///if \c e is an Expr and \c v and \c w are of type \ref Col then you can
alpar@1279
   182
    ///read and modify the coefficients like
alpar@1279
   183
    ///these.
alpar@1279
   184
    ///\code
alpar@1279
   185
    ///e[v]=5;
alpar@1279
   186
    ///e[v]+=12;
alpar@1279
   187
    ///e.erase(v);
alpar@1279
   188
    ///\endcode
alpar@1279
   189
    ///or you can also iterate through its elements.
alpar@1279
   190
    ///\code
alpar@1279
   191
    ///double s=0;
alpar@1279
   192
    ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i)
alpar@1279
   193
    ///  s+=i->second;
alpar@1279
   194
    ///\endcode
alpar@1279
   195
    ///(This code computes the sum of all coefficients).
alpar@1279
   196
    ///- Numbers (<tt>double</tt>'s)
alpar@1279
   197
    ///and variables (\ref Col "Col"s) directly convert to an
alpar@1279
   198
    ///\ref Expr and the usual linear operations are defined so  
alpar@1279
   199
    ///\code
alpar@1279
   200
    ///v+w
alpar@1279
   201
    ///2*v-3.12*(v-w/2)+2
alpar@1279
   202
    ///v*2.1+(3*v+(v*12+w+6)*3)/2
alpar@1279
   203
    ///\endcode
alpar@1279
   204
    ///are valid expressions. The usual assignment operations are also defined.
alpar@1279
   205
    ///\code
alpar@1279
   206
    ///e=v+w;
alpar@1279
   207
    ///e+=2*v-3.12*(v-w/2)+2;
alpar@1279
   208
    ///e*=3.4;
alpar@1279
   209
    ///e/=5;
alpar@1279
   210
    ///\endcode
alpar@1279
   211
    ///- The constant member can be set and read by \ref constComp()
alpar@1279
   212
    ///\code
alpar@1279
   213
    ///e.constComp()=12;
alpar@1279
   214
    ///double c=e.constComp();
alpar@1279
   215
    ///\endcode
alpar@1279
   216
    ///
alpar@1279
   217
    ///\note that \ref clear() not only sets all coefficients to 0 but also
alpar@1279
   218
    ///clears the constant components.
alpar@1273
   219
    class Expr : public std::map<Col,Value>
alpar@1272
   220
    {
alpar@1272
   221
    public:
alpar@1273
   222
      typedef LpSolverBase::Col Key; 
alpar@1273
   223
      typedef LpSolverBase::Value Value;
alpar@1272
   224
      
alpar@1272
   225
    protected:
alpar@1273
   226
      typedef std::map<Col,Value> Base;
alpar@1272
   227
      
alpar@1273
   228
      Value const_comp;
alpar@1272
   229
  public:
alpar@1272
   230
      typedef True IsLinExpression;
alpar@1272
   231
      ///\e
alpar@1272
   232
      Expr() : Base(), const_comp(0) { }
alpar@1272
   233
      ///\e
alpar@1273
   234
      Expr(const Key &v) : const_comp(0) {
alpar@1272
   235
	Base::insert(std::make_pair(v, 1));
alpar@1272
   236
      }
alpar@1272
   237
      ///\e
alpar@1273
   238
      Expr(const Value &v) : const_comp(v) {}
alpar@1272
   239
      ///\e
alpar@1273
   240
      void set(const Key &v,const Value &c) {
alpar@1272
   241
	Base::insert(std::make_pair(v, c));
alpar@1272
   242
      }
alpar@1272
   243
      ///\e
alpar@1273
   244
      Value &constComp() { return const_comp; }
alpar@1272
   245
      ///\e
alpar@1273
   246
      const Value &constComp() const { return const_comp; }
alpar@1272
   247
      
alpar@1272
   248
      ///Removes the components with zero coefficient.
alpar@1272
   249
      void simplify() {
alpar@1272
   250
	for (Base::iterator i=Base::begin(); i!=Base::end();) {
alpar@1272
   251
	  Base::iterator j=i;
alpar@1272
   252
	  ++j;
alpar@1272
   253
	  if ((*i).second==0) Base::erase(i);
alpar@1272
   254
	  j=i;
alpar@1272
   255
	}
alpar@1272
   256
      }
alpar@1273
   257
alpar@1273
   258
      ///Sets all coefficients and the constant component to 0.
alpar@1273
   259
      void clear() {
alpar@1273
   260
	Base::clear();
alpar@1273
   261
	const_comp=0;
alpar@1273
   262
      }
alpar@1273
   263
alpar@1272
   264
      ///\e
alpar@1272
   265
      Expr &operator+=(const Expr &e) {
alpar@1272
   266
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1272
   267
	  (*this)[j->first]+=j->second;
alpar@1272
   268
	///\todo it might be speeded up using "hints"
alpar@1272
   269
	const_comp+=e.const_comp;
alpar@1272
   270
	return *this;
alpar@1272
   271
      }
alpar@1272
   272
      ///\e
alpar@1272
   273
      Expr &operator-=(const Expr &e) {
alpar@1272
   274
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1272
   275
	  (*this)[j->first]-=j->second;
alpar@1272
   276
	const_comp-=e.const_comp;
alpar@1272
   277
	return *this;
alpar@1272
   278
      }
alpar@1272
   279
      ///\e
alpar@1273
   280
      Expr &operator*=(const Value &c) {
alpar@1272
   281
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1272
   282
	  j->second*=c;
alpar@1272
   283
	const_comp*=c;
alpar@1272
   284
	return *this;
alpar@1272
   285
      }
alpar@1272
   286
      ///\e
alpar@1273
   287
      Expr &operator/=(const Value &c) {
alpar@1272
   288
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1272
   289
	  j->second/=c;
alpar@1272
   290
	const_comp/=c;
alpar@1272
   291
	return *this;
alpar@1272
   292
      }
alpar@1272
   293
    };
alpar@1272
   294
    
alpar@1264
   295
    ///Linear constraint
alpar@1272
   296
    //typedef LinConstr<Expr> Constr;
alpar@1272
   297
    class Constr
alpar@1272
   298
    {
alpar@1272
   299
    public:
alpar@1272
   300
      typedef LpSolverBase::Expr Expr;
alpar@1273
   301
      typedef Expr::Key Key;
alpar@1273
   302
      typedef Expr::Value Value;
alpar@1272
   303
      
alpar@1273
   304
      static const Value INF;
alpar@1273
   305
      static const Value NaN;
alpar@1273
   306
      //     static const Value INF=0;
alpar@1273
   307
      //     static const Value NaN=1;
alpar@1272
   308
      
alpar@1273
   309
    protected:
alpar@1273
   310
      Expr _expr;
alpar@1273
   311
      Value _lb,_ub;
alpar@1273
   312
    public:
alpar@1273
   313
      ///\e
alpar@1273
   314
      Constr() : _expr(), _lb(NaN), _ub(NaN) {}
alpar@1273
   315
      ///\e
alpar@1273
   316
      Constr(Value lb,const Expr &e,Value ub) :
alpar@1273
   317
	_expr(e), _lb(lb), _ub(ub) {}
alpar@1273
   318
      ///\e
alpar@1273
   319
      Constr(const Expr &e,Value ub) : 
alpar@1273
   320
	_expr(e), _lb(NaN), _ub(ub) {}
alpar@1273
   321
      ///\e
alpar@1273
   322
      Constr(Value lb,const Expr &e) :
alpar@1273
   323
	_expr(e), _lb(lb), _ub(NaN) {}
alpar@1273
   324
      ///\e
alpar@1272
   325
      Constr(const Expr &e) : 
alpar@1273
   326
	_expr(e), _lb(NaN), _ub(NaN) {}
alpar@1273
   327
      ///\e
alpar@1273
   328
      void clear() 
alpar@1273
   329
      {
alpar@1273
   330
	_expr.clear();
alpar@1273
   331
	_lb=_ub=NaN;
alpar@1273
   332
      }
alpar@1273
   333
      ///\e
alpar@1273
   334
      Expr &expr() { return _expr; }
alpar@1273
   335
      ///\e
alpar@1273
   336
      const Expr &expr() const { return _expr; }
alpar@1273
   337
      ///\e
alpar@1273
   338
      Value &lowerBound() { return _lb; }
alpar@1273
   339
      ///\e
alpar@1273
   340
      const Value &lowerBound() const { return _lb; }
alpar@1273
   341
      ///\e
alpar@1273
   342
      Value &upperBound() { return _ub; }
alpar@1273
   343
      ///\e
alpar@1273
   344
      const Value &upperBound() const { return _ub; }
alpar@1275
   345
      ///\e
alpar@1275
   346
      bool lowerBounded() const { return std::isfinite(_lb); }
alpar@1275
   347
      ///\e
alpar@1275
   348
      bool upperBounded() const { return std::isfinite(_ub); }
alpar@1272
   349
    };
alpar@1272
   350
    
alpar@1253
   351
alpar@1253
   352
  protected:
alpar@1253
   353
    _FixId rows;
alpar@1253
   354
    _FixId cols;
athos@1246
   355
athos@1246
   356
    /// \e
athos@1246
   357
    virtual int _addCol() = 0;
athos@1246
   358
    /// \e
athos@1246
   359
    virtual int _addRow() = 0;
athos@1246
   360
    /// \e
alpar@1253
   361
athos@1246
   362
    /// \warning Arrays are indexed from 1 (datum at index 0 is ignored)
alpar@1253
   363
    ///
athos@1246
   364
    virtual void _setRowCoeffs(int i, 
athos@1251
   365
			       int length,
athos@1247
   366
                               int  const * indices, 
athos@1247
   367
                               Value  const * values ) = 0;
athos@1246
   368
    /// \e
alpar@1253
   369
athos@1246
   370
    /// \warning Arrays are indexed from 1 (datum at index 0 is ignored)
alpar@1253
   371
    ///
athos@1246
   372
    virtual void _setColCoeffs(int i, 
athos@1251
   373
			       int length,
athos@1247
   374
                               int  const * indices, 
athos@1247
   375
                               Value  const * values ) = 0;
athos@1246
   376
    
athos@1247
   377
    /// \e
alpar@1253
   378
athos@1247
   379
    /// The lower bound of a variable (column) have to be given by an 
athos@1247
   380
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   381
    /// Value or -\ref INF.
athos@1247
   382
    virtual void _setColLowerBound(int i, Value value) = 0;
athos@1247
   383
    /// \e
alpar@1253
   384
athos@1247
   385
    /// The upper bound of a variable (column) have to be given by an 
athos@1247
   386
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   387
    /// Value or \ref INF.
athos@1247
   388
    virtual void _setColUpperBound(int i, Value value) = 0;
athos@1247
   389
    /// \e
alpar@1253
   390
athos@1247
   391
    /// The lower bound of a linear expression (row) have to be given by an 
athos@1247
   392
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   393
    /// Value or -\ref INF.
athos@1247
   394
    virtual void _setRowLowerBound(int i, Value value) = 0;
athos@1247
   395
    /// \e
alpar@1253
   396
athos@1247
   397
    /// The upper bound of a linear expression (row) have to be given by an 
athos@1247
   398
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   399
    /// Value or \ref INF.
athos@1247
   400
    virtual void _setRowUpperBound(int i, Value value) = 0;
athos@1247
   401
athos@1247
   402
    /// \e
athos@1247
   403
    virtual void _setObjCoeff(int i, Value obj_coef) = 0;
alpar@1253
   404
alpar@1253
   405
    ///\e
alpar@1263
   406
    
alpar@1263
   407
    ///\bug Wrong interface
alpar@1263
   408
    ///
alpar@1263
   409
    virtual SolutionType _solve() = 0;
alpar@1263
   410
alpar@1263
   411
    ///\e
alpar@1263
   412
alpar@1263
   413
    ///\bug Wrong interface
alpar@1263
   414
    ///
alpar@1263
   415
    virtual Value _getSolution(int i) = 0;
alpar@1263
   416
    ///\e
alpar@1253
   417
alpar@1253
   418
    ///\bug unimplemented!!!!
alpar@1253
   419
    void clearObj() {}
alpar@1253
   420
  public:
alpar@1253
   421
alpar@1253
   422
alpar@1253
   423
    ///\e
alpar@1253
   424
    virtual ~LpSolverBase() {}
alpar@1253
   425
alpar@1263
   426
    ///\name Building up and modification of the LP
alpar@1263
   427
alpar@1263
   428
    ///@{
alpar@1263
   429
alpar@1253
   430
    ///Add a new empty column (i.e a new variable) to the LP
alpar@1253
   431
    Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
alpar@1263
   432
alpar@1256
   433
    ///\brief Fill the elements of a container with newly created columns
alpar@1256
   434
    ///(i.e a new variables)
alpar@1256
   435
    ///
alpar@1273
   436
    ///This magic function takes a container as its argument
alpar@1256
   437
    ///and fills its elements
alpar@1256
   438
    ///with new columns (i.e. variables)
alpar@1273
   439
    ///\param t can be
alpar@1273
   440
    ///- a standard STL compatible iterable container with
alpar@1273
   441
    ///\ref Col as its \c values_type
alpar@1273
   442
    ///like
alpar@1273
   443
    ///\code
alpar@1273
   444
    ///std::vector<LpSolverBase::Col>
alpar@1273
   445
    ///std::list<LpSolverBase::Col>
alpar@1273
   446
    ///\endcode
alpar@1273
   447
    ///- a standard STL compatible iterable container with
alpar@1273
   448
    ///\ref Col as its \c mapped_type
alpar@1273
   449
    ///like
alpar@1273
   450
    ///\code
alpar@1273
   451
    ///std::map<AnyType,LpSolverBase::Col>
alpar@1273
   452
    ///\endcode
alpar@1273
   453
    ///- an iterable lemon \ref concept::WriteMap "write map" like 
alpar@1273
   454
    ///\code
alpar@1273
   455
    ///ListGraph::NodeMap<LpSolverBase::Col>
alpar@1273
   456
    ///ListGraph::EdgeMap<LpSolverBase::Col>
alpar@1273
   457
    ///\endcode
alpar@1256
   458
    ///\return The number of the created column.
alpar@1256
   459
    ///\bug Iterable nodemap hasn't been implemented yet.
alpar@1256
   460
#ifdef DOXYGEN
alpar@1256
   461
    template<class T>
alpar@1256
   462
    int addColSet(T &t) { return 0;} 
alpar@1256
   463
#else
alpar@1256
   464
    template<class T>
alpar@1256
   465
    typename enable_if<typename T::value_type::LpSolverCol,int>::type
alpar@1256
   466
    addColSet(T &t,dummy<0> = 0) {
alpar@1256
   467
      int s=0;
alpar@1256
   468
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
alpar@1256
   469
      return s;
alpar@1256
   470
    }
alpar@1256
   471
    template<class T>
alpar@1256
   472
    typename enable_if<typename T::value_type::second_type::LpSolverCol,
alpar@1256
   473
		       int>::type
alpar@1256
   474
    addColSet(T &t,dummy<1> = 1) { 
alpar@1256
   475
      int s=0;
alpar@1256
   476
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1256
   477
	i->second=addCol();
alpar@1256
   478
	s++;
alpar@1256
   479
      }
alpar@1256
   480
      return s;
alpar@1256
   481
    }
alpar@1272
   482
    template<class T>
alpar@1272
   483
    typename enable_if<typename T::ValueSet::value_type::LpSolverCol,
alpar@1272
   484
		       int>::type
alpar@1272
   485
    addColSet(T &t,dummy<2> = 2) { 
alpar@1272
   486
      ///\bug <tt>return addColSet(t.valueSet());</tt> should also work.
alpar@1272
   487
      int s=0;
alpar@1272
   488
      for(typename T::ValueSet::iterator i=t.valueSet().begin();
alpar@1272
   489
	  i!=t.valueSet().end();
alpar@1272
   490
	  ++i)
alpar@1272
   491
	{
alpar@1272
   492
	  *i=addCol();
alpar@1272
   493
	  s++;
alpar@1272
   494
	}
alpar@1272
   495
      return s;
alpar@1272
   496
    }
alpar@1256
   497
#endif
alpar@1263
   498
alpar@1253
   499
    ///Add a new empty row (i.e a new constaint) to the LP
alpar@1258
   500
alpar@1258
   501
    ///This function adds a new empty row (i.e a new constaint) to the LP.
alpar@1258
   502
    ///\return The created row
alpar@1253
   503
    Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
alpar@1253
   504
alpar@1258
   505
    ///Set a row (i.e a constaint) of the LP
alpar@1253
   506
alpar@1258
   507
    ///\param r is the row to be modified
alpar@1259
   508
    ///\param l is lower bound (-\ref INF means no bound)
alpar@1258
   509
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   510
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1253
   511
    ///\bug This is a temportary function. The interface will change to
alpar@1253
   512
    ///a better one.
alpar@1258
   513
    void setRow(Row r, Value l,const Expr &e, Value u) {
alpar@1253
   514
      std::vector<int> indices;
alpar@1253
   515
      std::vector<Value> values;
alpar@1253
   516
      indices.push_back(0);
alpar@1253
   517
      values.push_back(0);
alpar@1258
   518
      for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i)
alpar@1256
   519
	if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
alpar@1256
   520
	  indices.push_back(cols.floatingId((*i).first.id));
alpar@1256
   521
	  values.push_back((*i).second);
alpar@1256
   522
	}
alpar@1253
   523
      _setRowCoeffs(rows.floatingId(r.id),indices.size()-1,
alpar@1253
   524
		    &indices[0],&values[0]);
alpar@1256
   525
      _setRowLowerBound(rows.floatingId(r.id),l-e.constComp());
alpar@1256
   526
      _setRowUpperBound(rows.floatingId(r.id),u-e.constComp());
alpar@1258
   527
    }
alpar@1258
   528
alpar@1264
   529
    ///Set a row (i.e a constaint) of the LP
alpar@1264
   530
alpar@1264
   531
    ///\param r is the row to be modified
alpar@1264
   532
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   533
    void setRow(Row r, const Constr &c) {
alpar@1273
   534
      setRow(r,
alpar@1275
   535
	     c.lowerBounded()?c.lowerBound():-INF,
alpar@1273
   536
	     c.expr(),
alpar@1275
   537
	     c.upperBounded()?c.upperBound():INF);
alpar@1264
   538
    }
alpar@1264
   539
alpar@1258
   540
    ///Add a new row (i.e a new constaint) to the LP
alpar@1258
   541
alpar@1259
   542
    ///\param l is the lower bound (-\ref INF means no bound)
alpar@1258
   543
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   544
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1258
   545
    ///\return The created row.
alpar@1258
   546
    ///\bug This is a temportary function. The interface will change to
alpar@1258
   547
    ///a better one.
alpar@1258
   548
    Row addRow(Value l,const Expr &e, Value u) {
alpar@1258
   549
      Row r=addRow();
alpar@1258
   550
      setRow(r,l,e,u);
alpar@1253
   551
      return r;
alpar@1253
   552
    }
alpar@1253
   553
alpar@1264
   554
    ///Add a new row (i.e a new constaint) to the LP
alpar@1264
   555
alpar@1264
   556
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   557
    ///\return The created row.
alpar@1264
   558
    Row addRow(const Constr &c) {
alpar@1264
   559
      Row r=addRow();
alpar@1264
   560
      setRow(r,c);
alpar@1264
   561
      return r;
alpar@1264
   562
    }
alpar@1264
   563
alpar@1253
   564
    /// Set the lower bound of a column (i.e a variable)
alpar@1253
   565
alpar@1253
   566
    /// The upper bound of a variable (column) have to be given by an 
alpar@1253
   567
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   568
    /// Value or -\ref INF.
alpar@1253
   569
    virtual void setColLowerBound(Col c, Value value) {
alpar@1253
   570
      _setColLowerBound(cols.floatingId(c.id),value);
alpar@1253
   571
    }
alpar@1253
   572
    /// Set the upper bound of a column (i.e a variable)
alpar@1253
   573
alpar@1253
   574
    /// The upper bound of a variable (column) have to be given by an 
alpar@1253
   575
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   576
    /// Value or \ref INF.
alpar@1253
   577
    virtual void setColUpperBound(Col c, Value value) {
alpar@1253
   578
      _setColUpperBound(cols.floatingId(c.id),value);
alpar@1253
   579
    };
alpar@1253
   580
    /// Set the lower bound of a row (i.e a constraint)
alpar@1253
   581
alpar@1253
   582
    /// The lower bound of a linear expression (row) have to be given by an 
alpar@1253
   583
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   584
    /// Value or -\ref INF.
alpar@1253
   585
    virtual void setRowLowerBound(Row r, Value value) {
alpar@1253
   586
      _setRowLowerBound(rows.floatingId(r.id),value);
alpar@1253
   587
    };
alpar@1253
   588
    /// Set the upper bound of a row (i.e a constraint)
alpar@1253
   589
alpar@1253
   590
    /// The upper bound of a linear expression (row) have to be given by an 
alpar@1253
   591
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   592
    /// Value or \ref INF.
alpar@1253
   593
    virtual void setRowUpperBound(Row r, Value value) {
alpar@1253
   594
      _setRowUpperBound(rows.floatingId(r.id),value);
alpar@1253
   595
    };
alpar@1253
   596
    ///Set an element of the objective function
alpar@1253
   597
    void setObjCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); };
alpar@1253
   598
    ///Set the objective function
alpar@1253
   599
    
alpar@1253
   600
    ///\param e is a linear expression of type \ref Expr.
alpar@1253
   601
    ///\todo What to do with the constant component?
alpar@1253
   602
    void setObj(Expr e) {
alpar@1253
   603
      clearObj();
alpar@1253
   604
      for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
alpar@1253
   605
	setObjCoeff((*i).first,(*i).second);
alpar@1253
   606
    }
alpar@1263
   607
alpar@1263
   608
    ///@}
alpar@1263
   609
alpar@1263
   610
alpar@1263
   611
    ///\name Solving the LP
alpar@1263
   612
alpar@1263
   613
    ///@{
alpar@1263
   614
alpar@1263
   615
    ///\e
alpar@1263
   616
    SolutionType solve() { return _solve(); }
alpar@1263
   617
    
alpar@1263
   618
    ///@}
alpar@1263
   619
    
alpar@1263
   620
    ///\name Obtaining the solution LP
alpar@1263
   621
alpar@1263
   622
    ///@{
alpar@1263
   623
alpar@1263
   624
    ///\e
alpar@1263
   625
    Value solution(Col c) { return _getSolution(cols.floatingId(c.id)); }
alpar@1263
   626
alpar@1263
   627
    ///@}
alpar@1253
   628
    
athos@1248
   629
  };  
athos@1246
   630
alpar@1272
   631
  ///\e
alpar@1272
   632
  
alpar@1272
   633
  ///\relates LpSolverBase::Expr
alpar@1272
   634
  ///
alpar@1272
   635
  inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
alpar@1272
   636
				      const LpSolverBase::Expr &b) 
alpar@1272
   637
  {
alpar@1272
   638
    LpSolverBase::Expr tmp(a);
alpar@1272
   639
    tmp+=b; ///\todo Don't STL have some special 'merge' algorithm?
alpar@1272
   640
    return tmp;
alpar@1272
   641
  }
alpar@1272
   642
  ///\e
alpar@1272
   643
  
alpar@1272
   644
  ///\relates LpSolverBase::Expr
alpar@1272
   645
  ///
alpar@1272
   646
  inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
alpar@1272
   647
				      const LpSolverBase::Expr &b) 
alpar@1272
   648
  {
alpar@1272
   649
    LpSolverBase::Expr tmp(a);
alpar@1272
   650
    tmp-=b; ///\todo Don't STL have some special 'merge' algorithm?
alpar@1272
   651
    return tmp;
alpar@1272
   652
  }
alpar@1272
   653
  ///\e
alpar@1272
   654
  
alpar@1272
   655
  ///\relates LpSolverBase::Expr
alpar@1272
   656
  ///
alpar@1272
   657
  inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
alpar@1273
   658
				      const LpSolverBase::Value &b) 
alpar@1272
   659
  {
alpar@1272
   660
    LpSolverBase::Expr tmp(a);
alpar@1272
   661
    tmp*=b; ///\todo Don't STL have some special 'merge' algorithm?
alpar@1272
   662
    return tmp;
alpar@1272
   663
  }
alpar@1272
   664
  
alpar@1272
   665
  ///\e
alpar@1272
   666
  
alpar@1272
   667
  ///\relates LpSolverBase::Expr
alpar@1272
   668
  ///
alpar@1273
   669
  inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
alpar@1272
   670
				      const LpSolverBase::Expr &b) 
alpar@1272
   671
  {
alpar@1272
   672
    LpSolverBase::Expr tmp(b);
alpar@1272
   673
    tmp*=a; ///\todo Don't STL have some special 'merge' algorithm?
alpar@1272
   674
    return tmp;
alpar@1272
   675
  }
alpar@1272
   676
  ///\e
alpar@1272
   677
  
alpar@1272
   678
  ///\relates LpSolverBase::Expr
alpar@1272
   679
  ///
alpar@1272
   680
  inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
alpar@1273
   681
				      const LpSolverBase::Value &b) 
alpar@1272
   682
  {
alpar@1272
   683
    LpSolverBase::Expr tmp(a);
alpar@1272
   684
    tmp/=b; ///\todo Don't STL have some special 'merge' algorithm?
alpar@1272
   685
    return tmp;
alpar@1272
   686
  }
alpar@1272
   687
  
alpar@1272
   688
  ///\e
alpar@1272
   689
  
alpar@1272
   690
  ///\relates LpSolverBase::Constr
alpar@1272
   691
  ///
alpar@1272
   692
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1272
   693
					 const LpSolverBase::Expr &f) 
alpar@1272
   694
  {
alpar@1272
   695
    return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
alpar@1272
   696
  }
alpar@1272
   697
alpar@1272
   698
  ///\e
alpar@1272
   699
  
alpar@1272
   700
  ///\relates LpSolverBase::Constr
alpar@1272
   701
  ///
alpar@1273
   702
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
alpar@1272
   703
					 const LpSolverBase::Expr &f) 
alpar@1272
   704
  {
alpar@1272
   705
    return LpSolverBase::Constr(e,f);
alpar@1272
   706
  }
alpar@1272
   707
alpar@1272
   708
  ///\e
alpar@1272
   709
  
alpar@1272
   710
  ///\relates LpSolverBase::Constr
alpar@1272
   711
  ///
alpar@1272
   712
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1273
   713
					 const LpSolverBase::Value &f) 
alpar@1272
   714
  {
alpar@1272
   715
    return LpSolverBase::Constr(e,f);
alpar@1272
   716
  }
alpar@1272
   717
alpar@1272
   718
  ///\e
alpar@1272
   719
  
alpar@1272
   720
  ///\relates LpSolverBase::Constr
alpar@1272
   721
  ///
alpar@1272
   722
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1272
   723
					 const LpSolverBase::Expr &f) 
alpar@1272
   724
  {
alpar@1272
   725
    return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
alpar@1272
   726
  }
alpar@1272
   727
alpar@1272
   728
alpar@1272
   729
  ///\e
alpar@1272
   730
  
alpar@1272
   731
  ///\relates LpSolverBase::Constr
alpar@1272
   732
  ///
alpar@1273
   733
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
alpar@1272
   734
					 const LpSolverBase::Expr &f) 
alpar@1272
   735
  {
alpar@1272
   736
    return LpSolverBase::Constr(f,e);
alpar@1272
   737
  }
alpar@1272
   738
alpar@1272
   739
alpar@1272
   740
  ///\e
alpar@1272
   741
  
alpar@1272
   742
  ///\relates LpSolverBase::Constr
alpar@1272
   743
  ///
alpar@1272
   744
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1273
   745
					 const LpSolverBase::Value &f) 
alpar@1272
   746
  {
alpar@1272
   747
    return LpSolverBase::Constr(f,e);
alpar@1272
   748
  }
alpar@1272
   749
alpar@1272
   750
  ///\e
alpar@1272
   751
  
alpar@1272
   752
  ///\relates LpSolverBase::Constr
alpar@1272
   753
  ///
alpar@1272
   754
  inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
alpar@1272
   755
					 const LpSolverBase::Expr &f) 
alpar@1272
   756
  {
alpar@1272
   757
    return LpSolverBase::Constr(0,e-f,0);
alpar@1272
   758
  }
alpar@1272
   759
alpar@1272
   760
  ///\e
alpar@1272
   761
  
alpar@1272
   762
  ///\relates LpSolverBase::Constr
alpar@1272
   763
  ///
alpar@1273
   764
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
alpar@1272
   765
					 const LpSolverBase::Constr&c) 
alpar@1272
   766
  {
alpar@1272
   767
    LpSolverBase::Constr tmp(c);
alpar@1273
   768
    ///\todo Create an own exception type.
alpar@1273
   769
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
   770
    else tmp.lowerBound()=n;
alpar@1272
   771
    return tmp;
alpar@1272
   772
  }
alpar@1272
   773
  ///\e
alpar@1272
   774
  
alpar@1272
   775
  ///\relates LpSolverBase::Constr
alpar@1272
   776
  ///
alpar@1272
   777
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
alpar@1273
   778
					 const LpSolverBase::Value &n)
alpar@1272
   779
  {
alpar@1272
   780
    LpSolverBase::Constr tmp(c);
alpar@1273
   781
    ///\todo Create an own exception type.
alpar@1273
   782
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
   783
    else tmp.upperBound()=n;
alpar@1272
   784
    return tmp;
alpar@1272
   785
  }
alpar@1272
   786
alpar@1272
   787
  ///\e
alpar@1272
   788
  
alpar@1272
   789
  ///\relates LpSolverBase::Constr
alpar@1272
   790
  ///
alpar@1273
   791
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
alpar@1272
   792
					 const LpSolverBase::Constr&c) 
alpar@1272
   793
  {
alpar@1272
   794
    LpSolverBase::Constr tmp(c);
alpar@1273
   795
    ///\todo Create an own exception type.
alpar@1273
   796
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
   797
    else tmp.upperBound()=n;
alpar@1272
   798
    return tmp;
alpar@1272
   799
  }
alpar@1272
   800
  ///\e
alpar@1272
   801
  
alpar@1272
   802
  ///\relates LpSolverBase::Constr
alpar@1272
   803
  ///
alpar@1272
   804
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
alpar@1273
   805
					 const LpSolverBase::Value &n)
alpar@1272
   806
  {
alpar@1272
   807
    LpSolverBase::Constr tmp(c);
alpar@1273
   808
    ///\todo Create an own exception type.
alpar@1273
   809
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
   810
    else tmp.lowerBound()=n;
alpar@1272
   811
    return tmp;
alpar@1272
   812
  }
alpar@1272
   813
alpar@1272
   814
athos@1246
   815
} //namespace lemon
athos@1246
   816
athos@1246
   817
#endif //LEMON_LP_BASE_H