src/work/athos/lp/lp_base.h
author alpar
Fri, 01 Apr 2005 19:50:29 +0000
changeset 1293 8ede2a6b2594
parent 1291 16cde3e1aa9f
child 1294 2dec219d9ca2
permissions -rw-r--r--
- Make lp stuff compilable
- Some 'set's removed
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@1293
   108
    enum SolutionStatus {
alpar@1263
   109
      ///\e
alpar@1293
   110
      SOLVED = 0,
alpar@1263
   111
      ///\e
alpar@1293
   112
      UNSOLVED = 1
athos@1291
   113
    };
athos@1291
   114
      
athos@1291
   115
    ///\e
athos@1291
   116
    enum SolutionType {
athos@1291
   117
      ///\e
alpar@1293
   118
      UNDEFINED = 0,
athos@1291
   119
      ///\e
alpar@1293
   120
      INFEASIBLE = 1,
athos@1291
   121
      ///\e
alpar@1293
   122
      FEASIBLE = 2,
athos@1291
   123
      ///\e
alpar@1293
   124
      OPTIMAL = 3
alpar@1263
   125
    };
alpar@1263
   126
      
alpar@1256
   127
    ///The floating point type used by the solver
athos@1247
   128
    typedef double Value;
alpar@1256
   129
    ///The infinity constant
athos@1247
   130
    static const Value INF;
alpar@1264
   131
    ///The not a number constant
alpar@1264
   132
    static const Value NaN;
alpar@1253
   133
    
alpar@1256
   134
    ///Refer to a column of the LP.
alpar@1256
   135
alpar@1256
   136
    ///This type is used to refer to a column of the LP.
alpar@1256
   137
    ///
alpar@1256
   138
    ///Its value remains valid and correct even after the addition or erase of
alpar@1273
   139
    ///other columns.
alpar@1256
   140
    ///
alpar@1256
   141
    ///\todo Document what can one do with a Col (INVALID, comparing,
alpar@1256
   142
    ///it is similar to Node/Edge)
alpar@1256
   143
    class Col {
alpar@1256
   144
    protected:
alpar@1256
   145
      int id;
alpar@1256
   146
      friend class LpSolverBase;
alpar@1256
   147
    public:
alpar@1259
   148
      typedef Value ExprValue;
alpar@1256
   149
      typedef True LpSolverCol;
alpar@1256
   150
      Col() {}
alpar@1256
   151
      Col(const Invalid&) : id(-1) {}
alpar@1256
   152
      bool operator<(Col c) const  {return id<c.id;}
alpar@1256
   153
      bool operator==(Col c) const  {return id==c.id;}
alpar@1256
   154
      bool operator!=(Col c) const  {return id==c.id;}
alpar@1256
   155
    };
alpar@1256
   156
alpar@1256
   157
    ///Refer to a row of the LP.
alpar@1256
   158
alpar@1256
   159
    ///This type is used to refer to a row of the LP.
alpar@1256
   160
    ///
alpar@1256
   161
    ///Its value remains valid and correct even after the addition or erase of
alpar@1273
   162
    ///other rows.
alpar@1256
   163
    ///
alpar@1256
   164
    ///\todo Document what can one do with a Row (INVALID, comparing,
alpar@1256
   165
    ///it is similar to Node/Edge)
alpar@1256
   166
    class Row {
alpar@1256
   167
    protected:
alpar@1256
   168
      int id;
alpar@1256
   169
      friend class LpSolverBase;
alpar@1256
   170
    public:
alpar@1259
   171
      typedef Value ExprValue;
alpar@1256
   172
      typedef True LpSolverRow;
alpar@1256
   173
      Row() {}
alpar@1256
   174
      Row(const Invalid&) : id(-1) {}
alpar@1256
   175
      typedef True LpSolverRow;
alpar@1256
   176
      bool operator<(Row c) const  {return id<c.id;}
alpar@1256
   177
      bool operator==(Row c) const  {return id==c.id;}
alpar@1256
   178
      bool operator!=(Row c) const  {return id==c.id;} 
alpar@1256
   179
   };
alpar@1259
   180
    
alpar@1279
   181
    ///Linear expression of variables and a constant component
alpar@1279
   182
    
alpar@1279
   183
    ///This data structure strores a linear expression of the variables
alpar@1279
   184
    ///(\ref Col "Col"s) and also has a constant component.
alpar@1279
   185
    ///
alpar@1279
   186
    ///There are several ways to access and modify the contents of this
alpar@1279
   187
    ///container.
alpar@1279
   188
    ///- Its it fully compatible with \c std::map<Col,double>, so for expamle
alpar@1279
   189
    ///if \c e is an Expr and \c v and \c w are of type \ref Col then you can
alpar@1279
   190
    ///read and modify the coefficients like
alpar@1279
   191
    ///these.
alpar@1279
   192
    ///\code
alpar@1279
   193
    ///e[v]=5;
alpar@1279
   194
    ///e[v]+=12;
alpar@1279
   195
    ///e.erase(v);
alpar@1279
   196
    ///\endcode
alpar@1279
   197
    ///or you can also iterate through its elements.
alpar@1279
   198
    ///\code
alpar@1279
   199
    ///double s=0;
alpar@1279
   200
    ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i)
alpar@1279
   201
    ///  s+=i->second;
alpar@1279
   202
    ///\endcode
alpar@1279
   203
    ///(This code computes the sum of all coefficients).
alpar@1279
   204
    ///- Numbers (<tt>double</tt>'s)
alpar@1279
   205
    ///and variables (\ref Col "Col"s) directly convert to an
alpar@1279
   206
    ///\ref Expr and the usual linear operations are defined so  
alpar@1279
   207
    ///\code
alpar@1279
   208
    ///v+w
alpar@1279
   209
    ///2*v-3.12*(v-w/2)+2
alpar@1279
   210
    ///v*2.1+(3*v+(v*12+w+6)*3)/2
alpar@1279
   211
    ///\endcode
alpar@1279
   212
    ///are valid expressions. The usual assignment operations are also defined.
alpar@1279
   213
    ///\code
alpar@1279
   214
    ///e=v+w;
alpar@1279
   215
    ///e+=2*v-3.12*(v-w/2)+2;
alpar@1279
   216
    ///e*=3.4;
alpar@1279
   217
    ///e/=5;
alpar@1279
   218
    ///\endcode
alpar@1279
   219
    ///- The constant member can be set and read by \ref constComp()
alpar@1279
   220
    ///\code
alpar@1279
   221
    ///e.constComp()=12;
alpar@1279
   222
    ///double c=e.constComp();
alpar@1279
   223
    ///\endcode
alpar@1279
   224
    ///
alpar@1279
   225
    ///\note that \ref clear() not only sets all coefficients to 0 but also
alpar@1279
   226
    ///clears the constant components.
alpar@1273
   227
    class Expr : public std::map<Col,Value>
alpar@1272
   228
    {
alpar@1272
   229
    public:
alpar@1273
   230
      typedef LpSolverBase::Col Key; 
alpar@1273
   231
      typedef LpSolverBase::Value Value;
alpar@1272
   232
      
alpar@1272
   233
    protected:
alpar@1273
   234
      typedef std::map<Col,Value> Base;
alpar@1272
   235
      
alpar@1273
   236
      Value const_comp;
alpar@1272
   237
  public:
alpar@1272
   238
      typedef True IsLinExpression;
alpar@1272
   239
      ///\e
alpar@1272
   240
      Expr() : Base(), const_comp(0) { }
alpar@1272
   241
      ///\e
alpar@1273
   242
      Expr(const Key &v) : const_comp(0) {
alpar@1272
   243
	Base::insert(std::make_pair(v, 1));
alpar@1272
   244
      }
alpar@1272
   245
      ///\e
alpar@1273
   246
      Expr(const Value &v) : const_comp(v) {}
alpar@1272
   247
      ///\e
alpar@1273
   248
      void set(const Key &v,const Value &c) {
alpar@1272
   249
	Base::insert(std::make_pair(v, c));
alpar@1272
   250
      }
alpar@1272
   251
      ///\e
alpar@1273
   252
      Value &constComp() { return const_comp; }
alpar@1272
   253
      ///\e
alpar@1273
   254
      const Value &constComp() const { return const_comp; }
alpar@1272
   255
      
alpar@1272
   256
      ///Removes the components with zero coefficient.
alpar@1272
   257
      void simplify() {
alpar@1272
   258
	for (Base::iterator i=Base::begin(); i!=Base::end();) {
alpar@1272
   259
	  Base::iterator j=i;
alpar@1272
   260
	  ++j;
alpar@1272
   261
	  if ((*i).second==0) Base::erase(i);
alpar@1272
   262
	  j=i;
alpar@1272
   263
	}
alpar@1272
   264
      }
alpar@1273
   265
alpar@1273
   266
      ///Sets all coefficients and the constant component to 0.
alpar@1273
   267
      void clear() {
alpar@1273
   268
	Base::clear();
alpar@1273
   269
	const_comp=0;
alpar@1273
   270
      }
alpar@1273
   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
	///\todo it might be speeded up using "hints"
alpar@1272
   277
	const_comp+=e.const_comp;
alpar@1272
   278
	return *this;
alpar@1272
   279
      }
alpar@1272
   280
      ///\e
alpar@1272
   281
      Expr &operator-=(const Expr &e) {
alpar@1272
   282
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1272
   283
	  (*this)[j->first]-=j->second;
alpar@1272
   284
	const_comp-=e.const_comp;
alpar@1272
   285
	return *this;
alpar@1272
   286
      }
alpar@1272
   287
      ///\e
alpar@1273
   288
      Expr &operator*=(const Value &c) {
alpar@1272
   289
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1272
   290
	  j->second*=c;
alpar@1272
   291
	const_comp*=c;
alpar@1272
   292
	return *this;
alpar@1272
   293
      }
alpar@1272
   294
      ///\e
alpar@1273
   295
      Expr &operator/=(const Value &c) {
alpar@1272
   296
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1272
   297
	  j->second/=c;
alpar@1272
   298
	const_comp/=c;
alpar@1272
   299
	return *this;
alpar@1272
   300
      }
alpar@1272
   301
    };
alpar@1272
   302
    
alpar@1264
   303
    ///Linear constraint
alpar@1272
   304
    //typedef LinConstr<Expr> Constr;
alpar@1272
   305
    class Constr
alpar@1272
   306
    {
alpar@1272
   307
    public:
alpar@1272
   308
      typedef LpSolverBase::Expr Expr;
alpar@1273
   309
      typedef Expr::Key Key;
alpar@1273
   310
      typedef Expr::Value Value;
alpar@1272
   311
      
alpar@1273
   312
      static const Value INF;
alpar@1273
   313
      static const Value NaN;
alpar@1273
   314
      //     static const Value INF=0;
alpar@1273
   315
      //     static const Value NaN=1;
alpar@1272
   316
      
alpar@1273
   317
    protected:
alpar@1273
   318
      Expr _expr;
alpar@1273
   319
      Value _lb,_ub;
alpar@1273
   320
    public:
alpar@1273
   321
      ///\e
alpar@1273
   322
      Constr() : _expr(), _lb(NaN), _ub(NaN) {}
alpar@1273
   323
      ///\e
alpar@1273
   324
      Constr(Value lb,const Expr &e,Value ub) :
alpar@1273
   325
	_expr(e), _lb(lb), _ub(ub) {}
alpar@1273
   326
      ///\e
alpar@1273
   327
      Constr(const Expr &e,Value ub) : 
alpar@1273
   328
	_expr(e), _lb(NaN), _ub(ub) {}
alpar@1273
   329
      ///\e
alpar@1273
   330
      Constr(Value lb,const Expr &e) :
alpar@1273
   331
	_expr(e), _lb(lb), _ub(NaN) {}
alpar@1273
   332
      ///\e
alpar@1272
   333
      Constr(const Expr &e) : 
alpar@1273
   334
	_expr(e), _lb(NaN), _ub(NaN) {}
alpar@1273
   335
      ///\e
alpar@1273
   336
      void clear() 
alpar@1273
   337
      {
alpar@1273
   338
	_expr.clear();
alpar@1273
   339
	_lb=_ub=NaN;
alpar@1273
   340
      }
alpar@1273
   341
      ///\e
alpar@1273
   342
      Expr &expr() { return _expr; }
alpar@1273
   343
      ///\e
alpar@1273
   344
      const Expr &expr() const { return _expr; }
alpar@1273
   345
      ///\e
alpar@1273
   346
      Value &lowerBound() { return _lb; }
alpar@1273
   347
      ///\e
alpar@1273
   348
      const Value &lowerBound() const { return _lb; }
alpar@1273
   349
      ///\e
alpar@1273
   350
      Value &upperBound() { return _ub; }
alpar@1273
   351
      ///\e
alpar@1273
   352
      const Value &upperBound() const { return _ub; }
alpar@1275
   353
      ///\e
alpar@1275
   354
      bool lowerBounded() const { return std::isfinite(_lb); }
alpar@1275
   355
      ///\e
alpar@1275
   356
      bool upperBounded() const { return std::isfinite(_ub); }
alpar@1272
   357
    };
alpar@1272
   358
    
alpar@1253
   359
alpar@1253
   360
  protected:
alpar@1253
   361
    _FixId rows;
alpar@1253
   362
    _FixId cols;
athos@1246
   363
athos@1246
   364
    /// \e
athos@1246
   365
    virtual int _addCol() = 0;
athos@1246
   366
    /// \e
athos@1246
   367
    virtual int _addRow() = 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 _setRowCoeffs(int i, 
athos@1251
   373
			       int length,
athos@1247
   374
                               int  const * indices, 
athos@1247
   375
                               Value  const * values ) = 0;
athos@1246
   376
    /// \e
alpar@1253
   377
athos@1246
   378
    /// \warning Arrays are indexed from 1 (datum at index 0 is ignored)
alpar@1253
   379
    ///
athos@1246
   380
    virtual void _setColCoeffs(int i, 
athos@1251
   381
			       int length,
athos@1247
   382
                               int  const * indices, 
athos@1247
   383
                               Value  const * values ) = 0;
athos@1246
   384
    
athos@1247
   385
    /// \e
alpar@1253
   386
athos@1247
   387
    /// The lower bound of a variable (column) have to be given by an 
athos@1247
   388
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   389
    /// Value or -\ref INF.
athos@1247
   390
    virtual void _setColLowerBound(int i, Value value) = 0;
athos@1247
   391
    /// \e
alpar@1253
   392
athos@1247
   393
    /// The upper bound of a variable (column) have to be given by an 
athos@1247
   394
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   395
    /// Value or \ref INF.
athos@1247
   396
    virtual void _setColUpperBound(int i, Value value) = 0;
athos@1247
   397
    /// \e
alpar@1253
   398
athos@1247
   399
    /// The lower bound of a linear expression (row) have to be given by an 
athos@1247
   400
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   401
    /// Value or -\ref INF.
athos@1247
   402
    virtual void _setRowLowerBound(int i, Value value) = 0;
athos@1247
   403
    /// \e
alpar@1253
   404
athos@1247
   405
    /// The upper bound of a linear expression (row) have to be given by an 
athos@1247
   406
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   407
    /// Value or \ref INF.
athos@1247
   408
    virtual void _setRowUpperBound(int i, Value value) = 0;
athos@1247
   409
athos@1247
   410
    /// \e
athos@1247
   411
    virtual void _setObjCoeff(int i, Value obj_coef) = 0;
alpar@1253
   412
alpar@1253
   413
    ///\e
alpar@1263
   414
    
alpar@1263
   415
    ///\bug Wrong interface
alpar@1263
   416
    ///
alpar@1293
   417
    virtual SolutionStatus _solve() = 0;
alpar@1263
   418
alpar@1263
   419
    ///\e
alpar@1263
   420
alpar@1263
   421
    ///\bug Wrong interface
alpar@1263
   422
    ///
alpar@1293
   423
    virtual Value _getPrimal(int i) = 0;
alpar@1263
   424
    ///\e
alpar@1253
   425
alpar@1253
   426
    ///\bug unimplemented!!!!
alpar@1253
   427
    void clearObj() {}
alpar@1253
   428
  public:
alpar@1253
   429
alpar@1253
   430
alpar@1253
   431
    ///\e
alpar@1253
   432
    virtual ~LpSolverBase() {}
alpar@1253
   433
alpar@1263
   434
    ///\name Building up and modification of the LP
alpar@1263
   435
alpar@1263
   436
    ///@{
alpar@1263
   437
alpar@1253
   438
    ///Add a new empty column (i.e a new variable) to the LP
alpar@1253
   439
    Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
alpar@1263
   440
alpar@1256
   441
    ///\brief Fill the elements of a container with newly created columns
alpar@1256
   442
    ///(i.e a new variables)
alpar@1256
   443
    ///
alpar@1273
   444
    ///This magic function takes a container as its argument
alpar@1256
   445
    ///and fills its elements
alpar@1256
   446
    ///with new columns (i.e. variables)
alpar@1273
   447
    ///\param t can be
alpar@1273
   448
    ///- a standard STL compatible iterable container with
alpar@1273
   449
    ///\ref Col as its \c values_type
alpar@1273
   450
    ///like
alpar@1273
   451
    ///\code
alpar@1273
   452
    ///std::vector<LpSolverBase::Col>
alpar@1273
   453
    ///std::list<LpSolverBase::Col>
alpar@1273
   454
    ///\endcode
alpar@1273
   455
    ///- a standard STL compatible iterable container with
alpar@1273
   456
    ///\ref Col as its \c mapped_type
alpar@1273
   457
    ///like
alpar@1273
   458
    ///\code
alpar@1273
   459
    ///std::map<AnyType,LpSolverBase::Col>
alpar@1273
   460
    ///\endcode
alpar@1273
   461
    ///- an iterable lemon \ref concept::WriteMap "write map" like 
alpar@1273
   462
    ///\code
alpar@1273
   463
    ///ListGraph::NodeMap<LpSolverBase::Col>
alpar@1273
   464
    ///ListGraph::EdgeMap<LpSolverBase::Col>
alpar@1273
   465
    ///\endcode
alpar@1256
   466
    ///\return The number of the created column.
alpar@1256
   467
    ///\bug Iterable nodemap hasn't been implemented yet.
alpar@1256
   468
#ifdef DOXYGEN
alpar@1256
   469
    template<class T>
alpar@1256
   470
    int addColSet(T &t) { return 0;} 
alpar@1256
   471
#else
alpar@1256
   472
    template<class T>
alpar@1256
   473
    typename enable_if<typename T::value_type::LpSolverCol,int>::type
alpar@1256
   474
    addColSet(T &t,dummy<0> = 0) {
alpar@1256
   475
      int s=0;
alpar@1256
   476
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
alpar@1256
   477
      return s;
alpar@1256
   478
    }
alpar@1256
   479
    template<class T>
alpar@1256
   480
    typename enable_if<typename T::value_type::second_type::LpSolverCol,
alpar@1256
   481
		       int>::type
alpar@1256
   482
    addColSet(T &t,dummy<1> = 1) { 
alpar@1256
   483
      int s=0;
alpar@1256
   484
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1256
   485
	i->second=addCol();
alpar@1256
   486
	s++;
alpar@1256
   487
      }
alpar@1256
   488
      return s;
alpar@1256
   489
    }
alpar@1272
   490
    template<class T>
alpar@1272
   491
    typename enable_if<typename T::ValueSet::value_type::LpSolverCol,
alpar@1272
   492
		       int>::type
alpar@1272
   493
    addColSet(T &t,dummy<2> = 2) { 
alpar@1272
   494
      ///\bug <tt>return addColSet(t.valueSet());</tt> should also work.
alpar@1272
   495
      int s=0;
alpar@1272
   496
      for(typename T::ValueSet::iterator i=t.valueSet().begin();
alpar@1272
   497
	  i!=t.valueSet().end();
alpar@1272
   498
	  ++i)
alpar@1272
   499
	{
alpar@1272
   500
	  *i=addCol();
alpar@1272
   501
	  s++;
alpar@1272
   502
	}
alpar@1272
   503
      return s;
alpar@1272
   504
    }
alpar@1256
   505
#endif
alpar@1263
   506
alpar@1253
   507
    ///Add a new empty row (i.e a new constaint) to the LP
alpar@1258
   508
alpar@1258
   509
    ///This function adds a new empty row (i.e a new constaint) to the LP.
alpar@1258
   510
    ///\return The created row
alpar@1253
   511
    Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
alpar@1253
   512
alpar@1258
   513
    ///Set a row (i.e a constaint) of the LP
alpar@1253
   514
alpar@1258
   515
    ///\param r is the row to be modified
alpar@1259
   516
    ///\param l is lower bound (-\ref INF means no bound)
alpar@1258
   517
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   518
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1253
   519
    ///\bug This is a temportary function. The interface will change to
alpar@1253
   520
    ///a better one.
alpar@1258
   521
    void setRow(Row r, Value l,const Expr &e, Value u) {
alpar@1253
   522
      std::vector<int> indices;
alpar@1253
   523
      std::vector<Value> values;
alpar@1253
   524
      indices.push_back(0);
alpar@1253
   525
      values.push_back(0);
alpar@1258
   526
      for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i)
alpar@1256
   527
	if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
alpar@1256
   528
	  indices.push_back(cols.floatingId((*i).first.id));
alpar@1256
   529
	  values.push_back((*i).second);
alpar@1256
   530
	}
alpar@1253
   531
      _setRowCoeffs(rows.floatingId(r.id),indices.size()-1,
alpar@1253
   532
		    &indices[0],&values[0]);
alpar@1256
   533
      _setRowLowerBound(rows.floatingId(r.id),l-e.constComp());
alpar@1256
   534
      _setRowUpperBound(rows.floatingId(r.id),u-e.constComp());
alpar@1258
   535
    }
alpar@1258
   536
alpar@1264
   537
    ///Set a row (i.e a constaint) of the LP
alpar@1264
   538
alpar@1264
   539
    ///\param r is the row to be modified
alpar@1264
   540
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   541
    void setRow(Row r, const Constr &c) {
alpar@1273
   542
      setRow(r,
alpar@1275
   543
	     c.lowerBounded()?c.lowerBound():-INF,
alpar@1273
   544
	     c.expr(),
alpar@1275
   545
	     c.upperBounded()?c.upperBound():INF);
alpar@1264
   546
    }
alpar@1264
   547
alpar@1258
   548
    ///Add a new row (i.e a new constaint) to the LP
alpar@1258
   549
alpar@1259
   550
    ///\param l is the lower bound (-\ref INF means no bound)
alpar@1258
   551
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   552
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1258
   553
    ///\return The created row.
alpar@1258
   554
    ///\bug This is a temportary function. The interface will change to
alpar@1258
   555
    ///a better one.
alpar@1258
   556
    Row addRow(Value l,const Expr &e, Value u) {
alpar@1258
   557
      Row r=addRow();
alpar@1258
   558
      setRow(r,l,e,u);
alpar@1253
   559
      return r;
alpar@1253
   560
    }
alpar@1253
   561
alpar@1264
   562
    ///Add a new row (i.e a new constaint) to the LP
alpar@1264
   563
alpar@1264
   564
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   565
    ///\return The created row.
alpar@1264
   566
    Row addRow(const Constr &c) {
alpar@1264
   567
      Row r=addRow();
alpar@1264
   568
      setRow(r,c);
alpar@1264
   569
      return r;
alpar@1264
   570
    }
alpar@1264
   571
alpar@1253
   572
    /// Set the lower bound of a column (i.e a variable)
alpar@1253
   573
alpar@1293
   574
    /// The upper bound of a variable (column) has 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@1293
   577
    void colLowerBound(Col c, Value value) {
alpar@1253
   578
      _setColLowerBound(cols.floatingId(c.id),value);
alpar@1253
   579
    }
alpar@1253
   580
    /// Set the upper bound of a column (i.e a variable)
alpar@1253
   581
alpar@1293
   582
    /// The upper bound of a variable (column) has 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@1293
   585
    void colUpperBound(Col c, Value value) {
alpar@1253
   586
      _setColUpperBound(cols.floatingId(c.id),value);
alpar@1253
   587
    };
alpar@1293
   588
    /// Set the lower and the upper bounds of a column (i.e a variable)
alpar@1293
   589
alpar@1293
   590
    /// The lower and the upper bounds of
alpar@1293
   591
    /// a variable (column) have to be given by an 
alpar@1293
   592
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
   593
    /// Value, -\ref INF or \ref INF.
alpar@1293
   594
    void colBounds(Col c, Value lower, Value upper) {
alpar@1293
   595
      _setColLowerBound(cols.floatingId(c.id),lower);
alpar@1293
   596
      _setColUpperBound(cols.floatingId(c.id),upper);
alpar@1293
   597
    }
alpar@1293
   598
    
alpar@1253
   599
    /// Set the lower bound of a row (i.e a constraint)
alpar@1253
   600
alpar@1293
   601
    /// The lower bound of a linear expression (row) has to be given by an 
alpar@1253
   602
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   603
    /// Value or -\ref INF.
alpar@1293
   604
    void rowLowerBound(Row r, Value value) {
alpar@1253
   605
      _setRowLowerBound(rows.floatingId(r.id),value);
alpar@1253
   606
    };
alpar@1253
   607
    /// Set the upper bound of a row (i.e a constraint)
alpar@1253
   608
alpar@1293
   609
    /// The upper bound of a linear expression (row) has to be given by an 
alpar@1253
   610
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   611
    /// Value or \ref INF.
alpar@1293
   612
    void rowUpperBound(Row r, Value value) {
alpar@1253
   613
      _setRowUpperBound(rows.floatingId(r.id),value);
alpar@1253
   614
    };
alpar@1293
   615
    /// Set the lower and the upper bounds of a row (i.e a variable)
alpar@1293
   616
alpar@1293
   617
    /// The lower and the upper bounds of
alpar@1293
   618
    /// a constraint (row) have to be given by an 
alpar@1293
   619
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
   620
    /// Value, -\ref INF or \ref INF.
alpar@1293
   621
    void rowBounds(Row c, Value lower, Value upper) {
alpar@1293
   622
      _setRowLowerBound(rows.floatingId(c.id),lower);
alpar@1293
   623
      _setRowUpperBound(rows.floatingId(c.id),upper);
alpar@1293
   624
    }
alpar@1293
   625
    
alpar@1253
   626
    ///Set an element of the objective function
alpar@1293
   627
    void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); };
alpar@1253
   628
    ///Set the objective function
alpar@1253
   629
    
alpar@1253
   630
    ///\param e is a linear expression of type \ref Expr.
alpar@1253
   631
    ///\todo What to do with the constant component?
alpar@1253
   632
    void setObj(Expr e) {
alpar@1253
   633
      clearObj();
alpar@1253
   634
      for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
alpar@1293
   635
	objCoeff((*i).first,(*i).second);
alpar@1253
   636
    }
alpar@1263
   637
alpar@1263
   638
    ///@}
alpar@1263
   639
alpar@1263
   640
alpar@1263
   641
    ///\name Solving the LP
alpar@1263
   642
alpar@1263
   643
    ///@{
alpar@1263
   644
alpar@1263
   645
    ///\e
alpar@1293
   646
    SolutionStatus solve() { return _solve(); }
alpar@1263
   647
    
alpar@1263
   648
    ///@}
alpar@1263
   649
    
alpar@1263
   650
    ///\name Obtaining the solution LP
alpar@1263
   651
alpar@1263
   652
    ///@{
alpar@1263
   653
alpar@1263
   654
    ///\e
alpar@1293
   655
    Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); }
alpar@1263
   656
alpar@1263
   657
    ///@}
alpar@1253
   658
    
athos@1248
   659
  };  
athos@1246
   660
alpar@1272
   661
  ///\e
alpar@1272
   662
  
alpar@1272
   663
  ///\relates LpSolverBase::Expr
alpar@1272
   664
  ///
alpar@1272
   665
  inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
alpar@1272
   666
				      const LpSolverBase::Expr &b) 
alpar@1272
   667
  {
alpar@1272
   668
    LpSolverBase::Expr tmp(a);
alpar@1272
   669
    tmp+=b; ///\todo Don't STL have some special 'merge' algorithm?
alpar@1272
   670
    return tmp;
alpar@1272
   671
  }
alpar@1272
   672
  ///\e
alpar@1272
   673
  
alpar@1272
   674
  ///\relates LpSolverBase::Expr
alpar@1272
   675
  ///
alpar@1272
   676
  inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
alpar@1272
   677
				      const LpSolverBase::Expr &b) 
alpar@1272
   678
  {
alpar@1272
   679
    LpSolverBase::Expr tmp(a);
alpar@1272
   680
    tmp-=b; ///\todo Don't STL have some special 'merge' algorithm?
alpar@1272
   681
    return tmp;
alpar@1272
   682
  }
alpar@1272
   683
  ///\e
alpar@1272
   684
  
alpar@1272
   685
  ///\relates LpSolverBase::Expr
alpar@1272
   686
  ///
alpar@1272
   687
  inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
alpar@1273
   688
				      const LpSolverBase::Value &b) 
alpar@1272
   689
  {
alpar@1272
   690
    LpSolverBase::Expr tmp(a);
alpar@1272
   691
    tmp*=b; ///\todo Don't STL have some special 'merge' algorithm?
alpar@1272
   692
    return tmp;
alpar@1272
   693
  }
alpar@1272
   694
  
alpar@1272
   695
  ///\e
alpar@1272
   696
  
alpar@1272
   697
  ///\relates LpSolverBase::Expr
alpar@1272
   698
  ///
alpar@1273
   699
  inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
alpar@1272
   700
				      const LpSolverBase::Expr &b) 
alpar@1272
   701
  {
alpar@1272
   702
    LpSolverBase::Expr tmp(b);
alpar@1272
   703
    tmp*=a; ///\todo Don't STL have some special 'merge' algorithm?
alpar@1272
   704
    return tmp;
alpar@1272
   705
  }
alpar@1272
   706
  ///\e
alpar@1272
   707
  
alpar@1272
   708
  ///\relates LpSolverBase::Expr
alpar@1272
   709
  ///
alpar@1272
   710
  inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
alpar@1273
   711
				      const LpSolverBase::Value &b) 
alpar@1272
   712
  {
alpar@1272
   713
    LpSolverBase::Expr tmp(a);
alpar@1272
   714
    tmp/=b; ///\todo Don't STL have some special 'merge' algorithm?
alpar@1272
   715
    return tmp;
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,e-f,0);
alpar@1272
   726
  }
alpar@1272
   727
alpar@1272
   728
  ///\e
alpar@1272
   729
  
alpar@1272
   730
  ///\relates LpSolverBase::Constr
alpar@1272
   731
  ///
alpar@1273
   732
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
alpar@1272
   733
					 const LpSolverBase::Expr &f) 
alpar@1272
   734
  {
alpar@1272
   735
    return LpSolverBase::Constr(e,f);
alpar@1272
   736
  }
alpar@1272
   737
alpar@1272
   738
  ///\e
alpar@1272
   739
  
alpar@1272
   740
  ///\relates LpSolverBase::Constr
alpar@1272
   741
  ///
alpar@1272
   742
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1273
   743
					 const LpSolverBase::Value &f) 
alpar@1272
   744
  {
alpar@1272
   745
    return LpSolverBase::Constr(e,f);
alpar@1272
   746
  }
alpar@1272
   747
alpar@1272
   748
  ///\e
alpar@1272
   749
  
alpar@1272
   750
  ///\relates LpSolverBase::Constr
alpar@1272
   751
  ///
alpar@1272
   752
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1272
   753
					 const LpSolverBase::Expr &f) 
alpar@1272
   754
  {
alpar@1272
   755
    return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
alpar@1272
   756
  }
alpar@1272
   757
alpar@1272
   758
alpar@1272
   759
  ///\e
alpar@1272
   760
  
alpar@1272
   761
  ///\relates LpSolverBase::Constr
alpar@1272
   762
  ///
alpar@1273
   763
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
alpar@1272
   764
					 const LpSolverBase::Expr &f) 
alpar@1272
   765
  {
alpar@1272
   766
    return LpSolverBase::Constr(f,e);
alpar@1272
   767
  }
alpar@1272
   768
alpar@1272
   769
alpar@1272
   770
  ///\e
alpar@1272
   771
  
alpar@1272
   772
  ///\relates LpSolverBase::Constr
alpar@1272
   773
  ///
alpar@1272
   774
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1273
   775
					 const LpSolverBase::Value &f) 
alpar@1272
   776
  {
alpar@1272
   777
    return LpSolverBase::Constr(f,e);
alpar@1272
   778
  }
alpar@1272
   779
alpar@1272
   780
  ///\e
alpar@1272
   781
  
alpar@1272
   782
  ///\relates LpSolverBase::Constr
alpar@1272
   783
  ///
alpar@1272
   784
  inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
alpar@1272
   785
					 const LpSolverBase::Expr &f) 
alpar@1272
   786
  {
alpar@1272
   787
    return LpSolverBase::Constr(0,e-f,0);
alpar@1272
   788
  }
alpar@1272
   789
alpar@1272
   790
  ///\e
alpar@1272
   791
  
alpar@1272
   792
  ///\relates LpSolverBase::Constr
alpar@1272
   793
  ///
alpar@1273
   794
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
alpar@1272
   795
					 const LpSolverBase::Constr&c) 
alpar@1272
   796
  {
alpar@1272
   797
    LpSolverBase::Constr tmp(c);
alpar@1273
   798
    ///\todo Create an own exception type.
alpar@1273
   799
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
   800
    else tmp.lowerBound()=n;
alpar@1272
   801
    return tmp;
alpar@1272
   802
  }
alpar@1272
   803
  ///\e
alpar@1272
   804
  
alpar@1272
   805
  ///\relates LpSolverBase::Constr
alpar@1272
   806
  ///
alpar@1272
   807
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
alpar@1273
   808
					 const LpSolverBase::Value &n)
alpar@1272
   809
  {
alpar@1272
   810
    LpSolverBase::Constr tmp(c);
alpar@1273
   811
    ///\todo Create an own exception type.
alpar@1273
   812
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
   813
    else tmp.upperBound()=n;
alpar@1272
   814
    return tmp;
alpar@1272
   815
  }
alpar@1272
   816
alpar@1272
   817
  ///\e
alpar@1272
   818
  
alpar@1272
   819
  ///\relates LpSolverBase::Constr
alpar@1272
   820
  ///
alpar@1273
   821
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
alpar@1272
   822
					 const LpSolverBase::Constr&c) 
alpar@1272
   823
  {
alpar@1272
   824
    LpSolverBase::Constr tmp(c);
alpar@1273
   825
    ///\todo Create an own exception type.
alpar@1273
   826
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
   827
    else tmp.upperBound()=n;
alpar@1272
   828
    return tmp;
alpar@1272
   829
  }
alpar@1272
   830
  ///\e
alpar@1272
   831
  
alpar@1272
   832
  ///\relates LpSolverBase::Constr
alpar@1272
   833
  ///
alpar@1272
   834
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
alpar@1273
   835
					 const LpSolverBase::Value &n)
alpar@1272
   836
  {
alpar@1272
   837
    LpSolverBase::Constr tmp(c);
alpar@1273
   838
    ///\todo Create an own exception type.
alpar@1273
   839
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
   840
    else tmp.lowerBound()=n;
alpar@1272
   841
    return tmp;
alpar@1272
   842
  }
alpar@1272
   843
alpar@1272
   844
athos@1246
   845
} //namespace lemon
athos@1246
   846
athos@1246
   847
#endif //LEMON_LP_BASE_H