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