src/lemon/lp_base.h
author alpar
Mon, 18 Apr 2005 14:59:24 +0000
changeset 1367 a490662291b9
parent 1359 1581f961cfaa
child 1376 8de0c1aeeb32
permissions -rw-r--r--
More steps toward gcc-3.4 compatibility
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
alpar@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, 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@1364
   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@1364
   321
    ///This data stucture represents a linear constraint in the LP.
alpar@1364
   322
    ///Basically it is a linear expression with a lower or an upper bound
alpar@1364
   323
    ///(or both). These parts of the constraint can be obtained by the member
alpar@1364
   324
    ///functions \ref expr(), \ref lowerBound() and \ref upperBound(),
alpar@1364
   325
    ///respectively.
alpar@1364
   326
    ///There are two ways to construct a constraint.
alpar@1364
   327
    ///- You can set the linear expression and the bounds directly
alpar@1364
   328
    ///  by the functions above.
alpar@1364
   329
    ///- The operators <tt>\<=</tt>, <tt>==</tt> and  <tt>\>=</tt>
alpar@1364
   330
    ///  are defined between expressions, or even between constraints whenever
alpar@1364
   331
    ///  it makes sense. Therefore if \c e and \c f are linear expressions and
alpar@1364
   332
    ///  \c s and \c t are numbers, then the followings are valid expressions
alpar@1364
   333
    ///  and thus they can be used directly e.g. in \ref addRow() whenever
alpar@1364
   334
    ///  it makes sense.
alpar@1364
   335
    ///  \code
alpar@1364
   336
    ///  e<=s
alpar@1364
   337
    ///  e<=f
alpar@1364
   338
    ///  s<=e<=t
alpar@1364
   339
    ///  e>=t
alpar@1364
   340
    ///  \endcode
alpar@1364
   341
    ///\warning The validity of a constraint is checked only at run time, so
alpar@1364
   342
    ///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw a
alpar@1364
   343
    ///\ref LogicError exception.
alpar@1272
   344
    class Constr
alpar@1272
   345
    {
alpar@1272
   346
    public:
alpar@1272
   347
      typedef LpSolverBase::Expr Expr;
alpar@1273
   348
      typedef Expr::Key Key;
alpar@1273
   349
      typedef Expr::Value Value;
alpar@1272
   350
      
alpar@1364
   351
//       static const Value INF;
alpar@1364
   352
//       static const Value NaN;
alpar@1364
   353
alpar@1273
   354
    protected:
alpar@1273
   355
      Expr _expr;
alpar@1273
   356
      Value _lb,_ub;
alpar@1273
   357
    public:
alpar@1273
   358
      ///\e
alpar@1273
   359
      Constr() : _expr(), _lb(NaN), _ub(NaN) {}
alpar@1273
   360
      ///\e
alpar@1273
   361
      Constr(Value lb,const Expr &e,Value ub) :
alpar@1273
   362
	_expr(e), _lb(lb), _ub(ub) {}
alpar@1273
   363
      ///\e
alpar@1273
   364
      Constr(const Expr &e,Value ub) : 
alpar@1273
   365
	_expr(e), _lb(NaN), _ub(ub) {}
alpar@1273
   366
      ///\e
alpar@1273
   367
      Constr(Value lb,const Expr &e) :
alpar@1273
   368
	_expr(e), _lb(lb), _ub(NaN) {}
alpar@1273
   369
      ///\e
alpar@1272
   370
      Constr(const Expr &e) : 
alpar@1273
   371
	_expr(e), _lb(NaN), _ub(NaN) {}
alpar@1273
   372
      ///\e
alpar@1273
   373
      void clear() 
alpar@1273
   374
      {
alpar@1273
   375
	_expr.clear();
alpar@1273
   376
	_lb=_ub=NaN;
alpar@1273
   377
      }
alpar@1364
   378
alpar@1364
   379
      ///Reference to the linear expression 
alpar@1273
   380
      Expr &expr() { return _expr; }
alpar@1364
   381
      ///Cont reference to the linear expression 
alpar@1273
   382
      const Expr &expr() const { return _expr; }
alpar@1364
   383
      ///Reference to the lower bound.
alpar@1364
   384
alpar@1364
   385
      ///\return
alpar@1364
   386
      ///- -\ref INF: the constraint is lower unbounded.
alpar@1364
   387
      ///- -\ref NaN: lower bound has not been set.
alpar@1364
   388
      ///- finite number: the lower bound
alpar@1273
   389
      Value &lowerBound() { return _lb; }
alpar@1364
   390
      ///The const version of \ref lowerBound()
alpar@1273
   391
      const Value &lowerBound() const { return _lb; }
alpar@1364
   392
      ///Reference to the upper bound.
alpar@1364
   393
alpar@1364
   394
      ///\return
alpar@1364
   395
      ///- -\ref INF: the constraint is upper unbounded.
alpar@1364
   396
      ///- -\ref NaN: upper bound has not been set.
alpar@1364
   397
      ///- finite number: the upper bound
alpar@1273
   398
      Value &upperBound() { return _ub; }
alpar@1364
   399
      ///The const version of \ref upperBound()
alpar@1273
   400
      const Value &upperBound() const { return _ub; }
alpar@1364
   401
      ///Is the constraint lower bounded?
alpar@1295
   402
      bool lowerBounded() const { 
alpar@1295
   403
	using namespace std;
alpar@1295
   404
	return isfinite(_lb);
alpar@1295
   405
      }
alpar@1364
   406
      ///Is the constraint upper bounded?
alpar@1295
   407
      bool upperBounded() const {
alpar@1295
   408
	using namespace std;
alpar@1295
   409
	return isfinite(_ub);
alpar@1295
   410
      }
alpar@1272
   411
    };
alpar@1272
   412
    
alpar@1253
   413
alpar@1253
   414
  protected:
alpar@1253
   415
    _FixId rows;
alpar@1253
   416
    _FixId cols;
athos@1246
   417
alpar@1323
   418
    //Abstract virtual functions
alpar@1364
   419
    virtual LpSolverBase &_newLp() = 0;
alpar@1364
   420
    virtual LpSolverBase &_copyLp() = 0;
alpar@1364
   421
athos@1246
   422
    virtual int _addCol() = 0;
athos@1246
   423
    virtual int _addRow() = 0;
athos@1246
   424
    virtual void _setRowCoeffs(int i, 
athos@1251
   425
			       int length,
athos@1247
   426
                               int  const * indices, 
athos@1247
   427
                               Value  const * values ) = 0;
athos@1246
   428
    virtual void _setColCoeffs(int i, 
athos@1251
   429
			       int length,
athos@1247
   430
                               int  const * indices, 
athos@1247
   431
                               Value  const * values ) = 0;
alpar@1294
   432
    virtual void _setColLowerBound(int i, Value value) = 0;
alpar@1294
   433
    virtual void _setColUpperBound(int i, Value value) = 0;
alpar@1294
   434
    virtual void _setRowLowerBound(int i, Value value) = 0;
alpar@1294
   435
    virtual void _setRowUpperBound(int i, Value value) = 0;
alpar@1294
   436
    virtual void _setObjCoeff(int i, Value obj_coef) = 0;
alpar@1303
   437
    virtual SolveExitStatus _solve() = 0;
alpar@1294
   438
    virtual Value _getPrimal(int i) = 0;
alpar@1312
   439
    virtual Value _getPrimalValue() = 0;
alpar@1312
   440
    virtual SolutionStatus _getPrimalStatus() = 0;
alpar@1312
   441
    virtual void _setMax() = 0;
alpar@1312
   442
    virtual void _setMin() = 0;
alpar@1312
   443
    
alpar@1323
   444
    //Own protected stuff
alpar@1323
   445
    
alpar@1323
   446
    //Constant component of the objective function
alpar@1323
   447
    Value obj_const_comp;
alpar@1323
   448
    
alpar@1323
   449
    ///\e
alpar@1323
   450
    
alpar@1323
   451
    ///\bug Unimplemented
alpar@1253
   452
    void clearObj() {}
alpar@1323
   453
    
alpar@1253
   454
  public:
alpar@1253
   455
alpar@1323
   456
    ///\e
alpar@1323
   457
    LpSolverBase() : obj_const_comp(0) {}
alpar@1253
   458
alpar@1253
   459
    ///\e
alpar@1253
   460
    virtual ~LpSolverBase() {}
alpar@1253
   461
alpar@1364
   462
    ///Creates a new LP problem
alpar@1364
   463
    LpSolverBase &newLp() {return _newLp();}
alpar@1364
   464
    ///Make a copy of the LP problem
alpar@1364
   465
    LpSolverBase &copyLp() {return _copyLp();}
alpar@1364
   466
    
alpar@1294
   467
    ///\name Build up and modify of the LP
alpar@1263
   468
alpar@1263
   469
    ///@{
alpar@1263
   470
alpar@1253
   471
    ///Add a new empty column (i.e a new variable) to the LP
alpar@1253
   472
    Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
alpar@1263
   473
alpar@1294
   474
    ///\brief Adds several new columns
alpar@1294
   475
    ///(i.e a variables) at once
alpar@1256
   476
    ///
alpar@1273
   477
    ///This magic function takes a container as its argument
alpar@1256
   478
    ///and fills its elements
alpar@1256
   479
    ///with new columns (i.e. variables)
alpar@1273
   480
    ///\param t can be
alpar@1273
   481
    ///- a standard STL compatible iterable container with
alpar@1273
   482
    ///\ref Col as its \c values_type
alpar@1273
   483
    ///like
alpar@1273
   484
    ///\code
alpar@1273
   485
    ///std::vector<LpSolverBase::Col>
alpar@1273
   486
    ///std::list<LpSolverBase::Col>
alpar@1273
   487
    ///\endcode
alpar@1273
   488
    ///- a standard STL compatible iterable container with
alpar@1273
   489
    ///\ref Col as its \c mapped_type
alpar@1273
   490
    ///like
alpar@1273
   491
    ///\code
alpar@1364
   492
    ///std::map<AnyType,LpSolverBase::Col>
alpar@1273
   493
    ///\endcode
alpar@1273
   494
    ///- an iterable lemon \ref concept::WriteMap "write map" like 
alpar@1273
   495
    ///\code
alpar@1273
   496
    ///ListGraph::NodeMap<LpSolverBase::Col>
alpar@1273
   497
    ///ListGraph::EdgeMap<LpSolverBase::Col>
alpar@1273
   498
    ///\endcode
alpar@1256
   499
    ///\return The number of the created column.
alpar@1256
   500
#ifdef DOXYGEN
alpar@1256
   501
    template<class T>
alpar@1256
   502
    int addColSet(T &t) { return 0;} 
alpar@1256
   503
#else
alpar@1256
   504
    template<class T>
alpar@1256
   505
    typename enable_if<typename T::value_type::LpSolverCol,int>::type
alpar@1256
   506
    addColSet(T &t,dummy<0> = 0) {
alpar@1256
   507
      int s=0;
alpar@1256
   508
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
alpar@1256
   509
      return s;
alpar@1256
   510
    }
alpar@1256
   511
    template<class T>
alpar@1256
   512
    typename enable_if<typename T::value_type::second_type::LpSolverCol,
alpar@1256
   513
		       int>::type
alpar@1256
   514
    addColSet(T &t,dummy<1> = 1) { 
alpar@1256
   515
      int s=0;
alpar@1256
   516
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1256
   517
	i->second=addCol();
alpar@1256
   518
	s++;
alpar@1256
   519
      }
alpar@1256
   520
      return s;
alpar@1256
   521
    }
alpar@1272
   522
    template<class T>
alpar@1272
   523
    typename enable_if<typename T::ValueSet::value_type::LpSolverCol,
alpar@1272
   524
		       int>::type
alpar@1272
   525
    addColSet(T &t,dummy<2> = 2) { 
alpar@1272
   526
      ///\bug <tt>return addColSet(t.valueSet());</tt> should also work.
alpar@1272
   527
      int s=0;
alpar@1272
   528
      for(typename T::ValueSet::iterator i=t.valueSet().begin();
alpar@1272
   529
	  i!=t.valueSet().end();
alpar@1272
   530
	  ++i)
alpar@1272
   531
	{
alpar@1272
   532
	  *i=addCol();
alpar@1272
   533
	  s++;
alpar@1272
   534
	}
alpar@1272
   535
      return s;
alpar@1272
   536
    }
alpar@1256
   537
#endif
alpar@1263
   538
alpar@1253
   539
    ///Add a new empty row (i.e a new constaint) to the LP
alpar@1258
   540
alpar@1258
   541
    ///This function adds a new empty row (i.e a new constaint) to the LP.
alpar@1258
   542
    ///\return The created row
alpar@1253
   543
    Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
alpar@1253
   544
alpar@1258
   545
    ///Set a row (i.e a constaint) of the LP
alpar@1253
   546
alpar@1258
   547
    ///\param r is the row to be modified
alpar@1259
   548
    ///\param l is lower bound (-\ref INF means no bound)
alpar@1258
   549
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   550
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1253
   551
    ///\bug This is a temportary function. The interface will change to
alpar@1253
   552
    ///a better one.
alpar@1328
   553
    ///\todo Option to control whether a constraint with a single variable is
alpar@1328
   554
    ///added or not.
alpar@1258
   555
    void setRow(Row r, Value l,const Expr &e, Value u) {
alpar@1253
   556
      std::vector<int> indices;
alpar@1253
   557
      std::vector<Value> values;
alpar@1253
   558
      indices.push_back(0);
alpar@1253
   559
      values.push_back(0);
alpar@1258
   560
      for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i)
alpar@1256
   561
	if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
alpar@1256
   562
	  indices.push_back(cols.floatingId((*i).first.id));
alpar@1256
   563
	  values.push_back((*i).second);
alpar@1256
   564
	}
alpar@1253
   565
      _setRowCoeffs(rows.floatingId(r.id),indices.size()-1,
alpar@1253
   566
		    &indices[0],&values[0]);
alpar@1256
   567
      _setRowLowerBound(rows.floatingId(r.id),l-e.constComp());
alpar@1256
   568
      _setRowUpperBound(rows.floatingId(r.id),u-e.constComp());
alpar@1258
   569
    }
alpar@1258
   570
alpar@1264
   571
    ///Set a row (i.e a constaint) of the LP
alpar@1264
   572
alpar@1264
   573
    ///\param r is the row to be modified
alpar@1264
   574
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   575
    void setRow(Row r, const Constr &c) {
alpar@1273
   576
      setRow(r,
alpar@1275
   577
	     c.lowerBounded()?c.lowerBound():-INF,
alpar@1273
   578
	     c.expr(),
alpar@1275
   579
	     c.upperBounded()?c.upperBound():INF);
alpar@1264
   580
    }
alpar@1264
   581
alpar@1258
   582
    ///Add a new row (i.e a new constaint) to the LP
alpar@1258
   583
alpar@1259
   584
    ///\param l is the lower bound (-\ref INF means no bound)
alpar@1258
   585
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   586
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1258
   587
    ///\return The created row.
alpar@1258
   588
    ///\bug This is a temportary function. The interface will change to
alpar@1258
   589
    ///a better one.
alpar@1258
   590
    Row addRow(Value l,const Expr &e, Value u) {
alpar@1258
   591
      Row r=addRow();
alpar@1258
   592
      setRow(r,l,e,u);
alpar@1253
   593
      return r;
alpar@1253
   594
    }
alpar@1253
   595
alpar@1264
   596
    ///Add a new row (i.e a new constaint) to the LP
alpar@1264
   597
alpar@1264
   598
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   599
    ///\return The created row.
alpar@1264
   600
    Row addRow(const Constr &c) {
alpar@1264
   601
      Row r=addRow();
alpar@1264
   602
      setRow(r,c);
alpar@1264
   603
      return r;
alpar@1264
   604
    }
alpar@1264
   605
alpar@1253
   606
    /// Set the lower bound of a column (i.e a variable)
alpar@1253
   607
alpar@1293
   608
    /// The upper bound of a variable (column) has to be given by an 
alpar@1253
   609
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   610
    /// Value or -\ref INF.
alpar@1293
   611
    void colLowerBound(Col c, Value value) {
alpar@1253
   612
      _setColLowerBound(cols.floatingId(c.id),value);
alpar@1253
   613
    }
alpar@1253
   614
    /// Set the upper bound of a column (i.e a variable)
alpar@1253
   615
alpar@1293
   616
    /// The upper bound of a variable (column) has to be given by an 
alpar@1253
   617
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   618
    /// Value or \ref INF.
alpar@1293
   619
    void colUpperBound(Col c, Value value) {
alpar@1253
   620
      _setColUpperBound(cols.floatingId(c.id),value);
alpar@1253
   621
    };
alpar@1293
   622
    /// Set the lower and the upper bounds of a column (i.e a variable)
alpar@1293
   623
alpar@1293
   624
    /// The lower and the upper bounds of
alpar@1293
   625
    /// a variable (column) have to be given by an 
alpar@1293
   626
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
   627
    /// Value, -\ref INF or \ref INF.
alpar@1293
   628
    void colBounds(Col c, Value lower, Value upper) {
alpar@1293
   629
      _setColLowerBound(cols.floatingId(c.id),lower);
alpar@1293
   630
      _setColUpperBound(cols.floatingId(c.id),upper);
alpar@1293
   631
    }
alpar@1293
   632
    
alpar@1253
   633
    /// Set the lower bound of a row (i.e a constraint)
alpar@1253
   634
alpar@1293
   635
    /// The lower bound of a linear expression (row) has to be given by an 
alpar@1253
   636
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   637
    /// Value or -\ref INF.
alpar@1293
   638
    void rowLowerBound(Row r, Value value) {
alpar@1253
   639
      _setRowLowerBound(rows.floatingId(r.id),value);
alpar@1253
   640
    };
alpar@1253
   641
    /// Set the upper bound of a row (i.e a constraint)
alpar@1253
   642
alpar@1293
   643
    /// The upper bound of a linear expression (row) has to be given by an 
alpar@1253
   644
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   645
    /// Value or \ref INF.
alpar@1293
   646
    void rowUpperBound(Row r, Value value) {
alpar@1253
   647
      _setRowUpperBound(rows.floatingId(r.id),value);
alpar@1253
   648
    };
alpar@1293
   649
    /// Set the lower and the upper bounds of a row (i.e a variable)
alpar@1293
   650
alpar@1293
   651
    /// The lower and the upper bounds of
alpar@1293
   652
    /// a constraint (row) have to be given by an 
alpar@1293
   653
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
   654
    /// Value, -\ref INF or \ref INF.
alpar@1293
   655
    void rowBounds(Row c, Value lower, Value upper) {
alpar@1293
   656
      _setRowLowerBound(rows.floatingId(c.id),lower);
alpar@1293
   657
      _setRowUpperBound(rows.floatingId(c.id),upper);
alpar@1293
   658
    }
alpar@1293
   659
    
alpar@1253
   660
    ///Set an element of the objective function
alpar@1293
   661
    void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); };
alpar@1253
   662
    ///Set the objective function
alpar@1253
   663
    
alpar@1253
   664
    ///\param e is a linear expression of type \ref Expr.
alpar@1323
   665
    ///\bug The previous objective function is not cleared!
alpar@1253
   666
    void setObj(Expr e) {
alpar@1253
   667
      clearObj();
alpar@1253
   668
      for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
alpar@1293
   669
	objCoeff((*i).first,(*i).second);
alpar@1323
   670
      obj_const_comp=e.constComp();
alpar@1253
   671
    }
alpar@1263
   672
alpar@1312
   673
    ///Maximize
alpar@1312
   674
    void max() { _setMax(); }
alpar@1312
   675
    ///Minimize
alpar@1312
   676
    void min() { _setMin(); }
alpar@1312
   677
alpar@1312
   678
    
alpar@1263
   679
    ///@}
alpar@1263
   680
alpar@1263
   681
alpar@1294
   682
    ///\name Solve the LP
alpar@1263
   683
alpar@1263
   684
    ///@{
alpar@1263
   685
alpar@1263
   686
    ///\e
alpar@1303
   687
    SolveExitStatus solve() { return _solve(); }
alpar@1263
   688
    
alpar@1263
   689
    ///@}
alpar@1263
   690
    
alpar@1294
   691
    ///\name Obtain the solution
alpar@1263
   692
alpar@1263
   693
    ///@{
alpar@1263
   694
alpar@1263
   695
    ///\e
alpar@1312
   696
    SolutionStatus primalStatus() {
alpar@1312
   697
      return _getPrimalStatus();
alpar@1294
   698
    }
alpar@1294
   699
alpar@1294
   700
    ///\e
alpar@1293
   701
    Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); }
alpar@1263
   702
alpar@1312
   703
    ///\e
alpar@1312
   704
alpar@1312
   705
    ///\return
alpar@1312
   706
    ///- \ref INF or -\ref INF means either infeasibility or unboundedness
alpar@1312
   707
    /// of the primal problem, depending on whether we minimize or maximize.
alpar@1364
   708
    ///- \ref NaN if no primal solution is found.
alpar@1312
   709
    ///- The (finite) objective value if an optimal solution is found.
alpar@1323
   710
    Value primalValue() { return _getPrimalValue()+obj_const_comp;}
alpar@1263
   711
    ///@}
alpar@1253
   712
    
athos@1248
   713
  };  
athos@1246
   714
alpar@1272
   715
  ///\e
alpar@1272
   716
  
alpar@1272
   717
  ///\relates LpSolverBase::Expr
alpar@1272
   718
  ///
alpar@1272
   719
  inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
alpar@1272
   720
				      const LpSolverBase::Expr &b) 
alpar@1272
   721
  {
alpar@1272
   722
    LpSolverBase::Expr tmp(a);
alpar@1364
   723
    tmp+=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
   724
    return tmp;
alpar@1272
   725
  }
alpar@1272
   726
  ///\e
alpar@1272
   727
  
alpar@1272
   728
  ///\relates LpSolverBase::Expr
alpar@1272
   729
  ///
alpar@1272
   730
  inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
alpar@1272
   731
				      const LpSolverBase::Expr &b) 
alpar@1272
   732
  {
alpar@1272
   733
    LpSolverBase::Expr tmp(a);
alpar@1364
   734
    tmp-=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
   735
    return tmp;
alpar@1272
   736
  }
alpar@1272
   737
  ///\e
alpar@1272
   738
  
alpar@1272
   739
  ///\relates LpSolverBase::Expr
alpar@1272
   740
  ///
alpar@1272
   741
  inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
alpar@1273
   742
				      const LpSolverBase::Value &b) 
alpar@1272
   743
  {
alpar@1272
   744
    LpSolverBase::Expr tmp(a);
alpar@1364
   745
    tmp*=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
   746
    return tmp;
alpar@1272
   747
  }
alpar@1272
   748
  
alpar@1272
   749
  ///\e
alpar@1272
   750
  
alpar@1272
   751
  ///\relates LpSolverBase::Expr
alpar@1272
   752
  ///
alpar@1273
   753
  inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
alpar@1272
   754
				      const LpSolverBase::Expr &b) 
alpar@1272
   755
  {
alpar@1272
   756
    LpSolverBase::Expr tmp(b);
alpar@1364
   757
    tmp*=a; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
   758
    return tmp;
alpar@1272
   759
  }
alpar@1272
   760
  ///\e
alpar@1272
   761
  
alpar@1272
   762
  ///\relates LpSolverBase::Expr
alpar@1272
   763
  ///
alpar@1272
   764
  inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
alpar@1273
   765
				      const LpSolverBase::Value &b) 
alpar@1272
   766
  {
alpar@1272
   767
    LpSolverBase::Expr tmp(a);
alpar@1364
   768
    tmp/=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
   769
    return tmp;
alpar@1272
   770
  }
alpar@1272
   771
  
alpar@1272
   772
  ///\e
alpar@1272
   773
  
alpar@1272
   774
  ///\relates LpSolverBase::Constr
alpar@1272
   775
  ///
alpar@1272
   776
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1272
   777
					 const LpSolverBase::Expr &f) 
alpar@1272
   778
  {
alpar@1272
   779
    return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
alpar@1272
   780
  }
alpar@1272
   781
alpar@1272
   782
  ///\e
alpar@1272
   783
  
alpar@1272
   784
  ///\relates LpSolverBase::Constr
alpar@1272
   785
  ///
alpar@1273
   786
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
alpar@1272
   787
					 const LpSolverBase::Expr &f) 
alpar@1272
   788
  {
alpar@1272
   789
    return LpSolverBase::Constr(e,f);
alpar@1272
   790
  }
alpar@1272
   791
alpar@1272
   792
  ///\e
alpar@1272
   793
  
alpar@1272
   794
  ///\relates LpSolverBase::Constr
alpar@1272
   795
  ///
alpar@1272
   796
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1273
   797
					 const LpSolverBase::Value &f) 
alpar@1272
   798
  {
alpar@1272
   799
    return LpSolverBase::Constr(e,f);
alpar@1272
   800
  }
alpar@1272
   801
alpar@1272
   802
  ///\e
alpar@1272
   803
  
alpar@1272
   804
  ///\relates LpSolverBase::Constr
alpar@1272
   805
  ///
alpar@1272
   806
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1272
   807
					 const LpSolverBase::Expr &f) 
alpar@1272
   808
  {
alpar@1272
   809
    return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
alpar@1272
   810
  }
alpar@1272
   811
alpar@1272
   812
alpar@1272
   813
  ///\e
alpar@1272
   814
  
alpar@1272
   815
  ///\relates LpSolverBase::Constr
alpar@1272
   816
  ///
alpar@1273
   817
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
alpar@1272
   818
					 const LpSolverBase::Expr &f) 
alpar@1272
   819
  {
alpar@1272
   820
    return LpSolverBase::Constr(f,e);
alpar@1272
   821
  }
alpar@1272
   822
alpar@1272
   823
alpar@1272
   824
  ///\e
alpar@1272
   825
  
alpar@1272
   826
  ///\relates LpSolverBase::Constr
alpar@1272
   827
  ///
alpar@1272
   828
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1273
   829
					 const LpSolverBase::Value &f) 
alpar@1272
   830
  {
alpar@1272
   831
    return LpSolverBase::Constr(f,e);
alpar@1272
   832
  }
alpar@1272
   833
alpar@1272
   834
  ///\e
alpar@1272
   835
  
alpar@1272
   836
  ///\relates LpSolverBase::Constr
alpar@1272
   837
  ///
alpar@1272
   838
  inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
alpar@1272
   839
					 const LpSolverBase::Expr &f) 
alpar@1272
   840
  {
alpar@1272
   841
    return LpSolverBase::Constr(0,e-f,0);
alpar@1272
   842
  }
alpar@1272
   843
alpar@1272
   844
  ///\e
alpar@1272
   845
  
alpar@1272
   846
  ///\relates LpSolverBase::Constr
alpar@1272
   847
  ///
alpar@1273
   848
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
alpar@1272
   849
					 const LpSolverBase::Constr&c) 
alpar@1272
   850
  {
alpar@1272
   851
    LpSolverBase::Constr tmp(c);
alpar@1273
   852
    ///\todo Create an own exception type.
alpar@1273
   853
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
   854
    else tmp.lowerBound()=n;
alpar@1272
   855
    return tmp;
alpar@1272
   856
  }
alpar@1272
   857
  ///\e
alpar@1272
   858
  
alpar@1272
   859
  ///\relates LpSolverBase::Constr
alpar@1272
   860
  ///
alpar@1272
   861
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
alpar@1273
   862
					 const LpSolverBase::Value &n)
alpar@1272
   863
  {
alpar@1272
   864
    LpSolverBase::Constr tmp(c);
alpar@1273
   865
    ///\todo Create an own exception type.
alpar@1273
   866
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
   867
    else tmp.upperBound()=n;
alpar@1272
   868
    return tmp;
alpar@1272
   869
  }
alpar@1272
   870
alpar@1272
   871
  ///\e
alpar@1272
   872
  
alpar@1272
   873
  ///\relates LpSolverBase::Constr
alpar@1272
   874
  ///
alpar@1273
   875
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
alpar@1272
   876
					 const LpSolverBase::Constr&c) 
alpar@1272
   877
  {
alpar@1272
   878
    LpSolverBase::Constr tmp(c);
alpar@1273
   879
    ///\todo Create an own exception type.
alpar@1273
   880
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
   881
    else tmp.upperBound()=n;
alpar@1272
   882
    return tmp;
alpar@1272
   883
  }
alpar@1272
   884
  ///\e
alpar@1272
   885
  
alpar@1272
   886
  ///\relates LpSolverBase::Constr
alpar@1272
   887
  ///
alpar@1272
   888
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
alpar@1273
   889
					 const LpSolverBase::Value &n)
alpar@1272
   890
  {
alpar@1272
   891
    LpSolverBase::Constr tmp(c);
alpar@1273
   892
    ///\todo Create an own exception type.
alpar@1273
   893
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
   894
    else tmp.lowerBound()=n;
alpar@1272
   895
    return tmp;
alpar@1272
   896
  }
alpar@1272
   897
alpar@1272
   898
athos@1246
   899
} //namespace lemon
athos@1246
   900
athos@1246
   901
#endif //LEMON_LP_BASE_H