src/lemon/lp_base.h
author alpar
Thu, 19 May 2005 11:46:42 +0000
changeset 1428 9ba88ddc629c
parent 1397 30828157ae80
child 1431 ad44b1dd8013
permissions -rw-r--r--
A very simple xml parser
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@1397
    23
#include<cmath>
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@1397
   404
	return finite(_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@1397
   409
	return finite(_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;
athos@1405
   434
//     virtual void _setRowLowerBound(int i, Value value) = 0;
athos@1405
   435
//     virtual void _setRowUpperBound(int i, Value value) = 0;
athos@1379
   436
    virtual void _setRowBounds(int i, Value lower, Value upper) = 0;
alpar@1294
   437
    virtual void _setObjCoeff(int i, Value obj_coef) = 0;
athos@1377
   438
    virtual void _clearObj()=0;
athos@1377
   439
//     virtual void _setObj(int length,
athos@1377
   440
//                          int  const * indices, 
athos@1377
   441
//                          Value  const * values ) = 0;
alpar@1303
   442
    virtual SolveExitStatus _solve() = 0;
alpar@1294
   443
    virtual Value _getPrimal(int i) = 0;
alpar@1312
   444
    virtual Value _getPrimalValue() = 0;
alpar@1312
   445
    virtual SolutionStatus _getPrimalStatus() = 0;
alpar@1312
   446
    virtual void _setMax() = 0;
alpar@1312
   447
    virtual void _setMin() = 0;
alpar@1312
   448
    
alpar@1323
   449
    //Own protected stuff
alpar@1323
   450
    
alpar@1323
   451
    //Constant component of the objective function
alpar@1323
   452
    Value obj_const_comp;
alpar@1323
   453
    
athos@1377
   454
athos@1377
   455
alpar@1323
   456
    
alpar@1253
   457
  public:
alpar@1253
   458
alpar@1323
   459
    ///\e
alpar@1323
   460
    LpSolverBase() : obj_const_comp(0) {}
alpar@1253
   461
alpar@1253
   462
    ///\e
alpar@1253
   463
    virtual ~LpSolverBase() {}
alpar@1253
   464
alpar@1364
   465
    ///Creates a new LP problem
alpar@1364
   466
    LpSolverBase &newLp() {return _newLp();}
alpar@1381
   467
    ///Makes a copy of the LP problem
alpar@1364
   468
    LpSolverBase &copyLp() {return _copyLp();}
alpar@1364
   469
    
alpar@1294
   470
    ///\name Build up and modify of the LP
alpar@1263
   471
alpar@1263
   472
    ///@{
alpar@1263
   473
alpar@1253
   474
    ///Add a new empty column (i.e a new variable) to the LP
alpar@1253
   475
    Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
alpar@1263
   476
alpar@1294
   477
    ///\brief Adds several new columns
alpar@1294
   478
    ///(i.e a variables) at once
alpar@1256
   479
    ///
alpar@1273
   480
    ///This magic function takes a container as its argument
alpar@1256
   481
    ///and fills its elements
alpar@1256
   482
    ///with new columns (i.e. variables)
alpar@1273
   483
    ///\param t can be
alpar@1273
   484
    ///- a standard STL compatible iterable container with
alpar@1273
   485
    ///\ref Col as its \c values_type
alpar@1273
   486
    ///like
alpar@1273
   487
    ///\code
alpar@1273
   488
    ///std::vector<LpSolverBase::Col>
alpar@1273
   489
    ///std::list<LpSolverBase::Col>
alpar@1273
   490
    ///\endcode
alpar@1273
   491
    ///- a standard STL compatible iterable container with
alpar@1273
   492
    ///\ref Col as its \c mapped_type
alpar@1273
   493
    ///like
alpar@1273
   494
    ///\code
alpar@1364
   495
    ///std::map<AnyType,LpSolverBase::Col>
alpar@1273
   496
    ///\endcode
alpar@1273
   497
    ///- an iterable lemon \ref concept::WriteMap "write map" like 
alpar@1273
   498
    ///\code
alpar@1273
   499
    ///ListGraph::NodeMap<LpSolverBase::Col>
alpar@1273
   500
    ///ListGraph::EdgeMap<LpSolverBase::Col>
alpar@1273
   501
    ///\endcode
alpar@1256
   502
    ///\return The number of the created column.
alpar@1256
   503
#ifdef DOXYGEN
alpar@1256
   504
    template<class T>
alpar@1256
   505
    int addColSet(T &t) { return 0;} 
alpar@1256
   506
#else
alpar@1256
   507
    template<class T>
alpar@1256
   508
    typename enable_if<typename T::value_type::LpSolverCol,int>::type
alpar@1256
   509
    addColSet(T &t,dummy<0> = 0) {
alpar@1256
   510
      int s=0;
alpar@1256
   511
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
alpar@1256
   512
      return s;
alpar@1256
   513
    }
alpar@1256
   514
    template<class T>
alpar@1256
   515
    typename enable_if<typename T::value_type::second_type::LpSolverCol,
alpar@1256
   516
		       int>::type
alpar@1256
   517
    addColSet(T &t,dummy<1> = 1) { 
alpar@1256
   518
      int s=0;
alpar@1256
   519
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1256
   520
	i->second=addCol();
alpar@1256
   521
	s++;
alpar@1256
   522
      }
alpar@1256
   523
      return s;
alpar@1256
   524
    }
alpar@1272
   525
    template<class T>
alpar@1272
   526
    typename enable_if<typename T::ValueSet::value_type::LpSolverCol,
alpar@1272
   527
		       int>::type
alpar@1272
   528
    addColSet(T &t,dummy<2> = 2) { 
alpar@1272
   529
      ///\bug <tt>return addColSet(t.valueSet());</tt> should also work.
alpar@1272
   530
      int s=0;
alpar@1272
   531
      for(typename T::ValueSet::iterator i=t.valueSet().begin();
alpar@1272
   532
	  i!=t.valueSet().end();
alpar@1272
   533
	  ++i)
alpar@1272
   534
	{
alpar@1272
   535
	  *i=addCol();
alpar@1272
   536
	  s++;
alpar@1272
   537
	}
alpar@1272
   538
      return s;
alpar@1272
   539
    }
alpar@1256
   540
#endif
alpar@1263
   541
alpar@1253
   542
    ///Add a new empty row (i.e a new constaint) to the LP
alpar@1258
   543
alpar@1258
   544
    ///This function adds a new empty row (i.e a new constaint) to the LP.
alpar@1258
   545
    ///\return The created row
alpar@1253
   546
    Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
alpar@1253
   547
alpar@1258
   548
    ///Set a row (i.e a constaint) of the LP
alpar@1253
   549
alpar@1258
   550
    ///\param r is the row to be modified
alpar@1259
   551
    ///\param l is lower bound (-\ref INF means no bound)
alpar@1258
   552
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   553
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1253
   554
    ///\bug This is a temportary function. The interface will change to
alpar@1253
   555
    ///a better one.
alpar@1328
   556
    ///\todo Option to control whether a constraint with a single variable is
alpar@1328
   557
    ///added or not.
alpar@1258
   558
    void setRow(Row r, Value l,const Expr &e, Value u) {
alpar@1253
   559
      std::vector<int> indices;
alpar@1253
   560
      std::vector<Value> values;
alpar@1253
   561
      indices.push_back(0);
alpar@1253
   562
      values.push_back(0);
alpar@1258
   563
      for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i)
alpar@1256
   564
	if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
alpar@1256
   565
	  indices.push_back(cols.floatingId((*i).first.id));
alpar@1256
   566
	  values.push_back((*i).second);
alpar@1256
   567
	}
alpar@1253
   568
      _setRowCoeffs(rows.floatingId(r.id),indices.size()-1,
alpar@1253
   569
		    &indices[0],&values[0]);
athos@1405
   570
//       _setRowLowerBound(rows.floatingId(r.id),l-e.constComp());
athos@1405
   571
//       _setRowUpperBound(rows.floatingId(r.id),u-e.constComp());
athos@1405
   572
       _setRowBounds(rows.floatingId(r.id),l-e.constComp(),u-e.constComp());
alpar@1258
   573
    }
alpar@1258
   574
alpar@1264
   575
    ///Set a row (i.e a constaint) of the LP
alpar@1264
   576
alpar@1264
   577
    ///\param r is the row to be modified
alpar@1264
   578
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   579
    void setRow(Row r, const Constr &c) {
alpar@1273
   580
      setRow(r,
alpar@1275
   581
	     c.lowerBounded()?c.lowerBound():-INF,
alpar@1273
   582
	     c.expr(),
alpar@1275
   583
	     c.upperBounded()?c.upperBound():INF);
alpar@1264
   584
    }
alpar@1264
   585
alpar@1258
   586
    ///Add a new row (i.e a new constaint) to the LP
alpar@1258
   587
alpar@1259
   588
    ///\param l is the lower bound (-\ref INF means no bound)
alpar@1258
   589
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   590
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1258
   591
    ///\return The created row.
alpar@1258
   592
    ///\bug This is a temportary function. The interface will change to
alpar@1258
   593
    ///a better one.
alpar@1258
   594
    Row addRow(Value l,const Expr &e, Value u) {
alpar@1258
   595
      Row r=addRow();
alpar@1258
   596
      setRow(r,l,e,u);
alpar@1253
   597
      return r;
alpar@1253
   598
    }
alpar@1253
   599
alpar@1264
   600
    ///Add a new row (i.e a new constaint) to the LP
alpar@1264
   601
alpar@1264
   602
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   603
    ///\return The created row.
alpar@1264
   604
    Row addRow(const Constr &c) {
alpar@1264
   605
      Row r=addRow();
alpar@1264
   606
      setRow(r,c);
alpar@1264
   607
      return r;
alpar@1264
   608
    }
alpar@1264
   609
alpar@1253
   610
    /// Set the lower bound of a column (i.e a variable)
alpar@1253
   611
alpar@1293
   612
    /// The upper bound of a variable (column) has to be given by an 
alpar@1253
   613
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   614
    /// Value or -\ref INF.
alpar@1293
   615
    void colLowerBound(Col c, Value value) {
alpar@1253
   616
      _setColLowerBound(cols.floatingId(c.id),value);
alpar@1253
   617
    }
alpar@1253
   618
    /// Set the upper bound of a column (i.e a variable)
alpar@1253
   619
alpar@1293
   620
    /// The upper bound of a variable (column) has to be given by an 
alpar@1253
   621
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   622
    /// Value or \ref INF.
alpar@1293
   623
    void colUpperBound(Col c, Value value) {
alpar@1253
   624
      _setColUpperBound(cols.floatingId(c.id),value);
alpar@1253
   625
    };
alpar@1293
   626
    /// Set the lower and the upper bounds of a column (i.e a variable)
alpar@1293
   627
alpar@1293
   628
    /// The lower and the upper bounds of
alpar@1293
   629
    /// a variable (column) have to be given by an 
alpar@1293
   630
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
   631
    /// Value, -\ref INF or \ref INF.
alpar@1293
   632
    void colBounds(Col c, Value lower, Value upper) {
alpar@1293
   633
      _setColLowerBound(cols.floatingId(c.id),lower);
alpar@1293
   634
      _setColUpperBound(cols.floatingId(c.id),upper);
alpar@1293
   635
    }
alpar@1293
   636
    
athos@1405
   637
//     /// Set the lower bound of a row (i.e a constraint)
alpar@1253
   638
athos@1405
   639
//     /// The lower bound of a linear expression (row) has to be given by an 
athos@1405
   640
//     /// extended number of type Value, i.e. a finite number of type 
athos@1405
   641
//     /// Value or -\ref INF.
athos@1405
   642
//     void rowLowerBound(Row r, Value value) {
athos@1405
   643
//       _setRowLowerBound(rows.floatingId(r.id),value);
athos@1405
   644
//     };
athos@1405
   645
//     /// Set the upper bound of a row (i.e a constraint)
alpar@1253
   646
athos@1405
   647
//     /// The upper bound of a linear expression (row) has to be given by an 
athos@1405
   648
//     /// extended number of type Value, i.e. a finite number of type 
athos@1405
   649
//     /// Value or \ref INF.
athos@1405
   650
//     void rowUpperBound(Row r, Value value) {
athos@1405
   651
//       _setRowUpperBound(rows.floatingId(r.id),value);
athos@1405
   652
//     };
athos@1405
   653
athos@1405
   654
    /// Set the lower and the upper bounds of a row (i.e a constraint)
alpar@1293
   655
alpar@1293
   656
    /// The lower and the upper bounds of
alpar@1293
   657
    /// a constraint (row) have to be given by an 
alpar@1293
   658
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
   659
    /// Value, -\ref INF or \ref INF.
alpar@1293
   660
    void rowBounds(Row c, Value lower, Value upper) {
athos@1379
   661
      _setRowBounds(rows.floatingId(c.id),lower, upper);
athos@1379
   662
      // _setRowUpperBound(rows.floatingId(c.id),upper);
alpar@1293
   663
    }
alpar@1293
   664
    
alpar@1253
   665
    ///Set an element of the objective function
alpar@1293
   666
    void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); };
alpar@1253
   667
    ///Set the objective function
alpar@1253
   668
    
alpar@1253
   669
    ///\param e is a linear expression of type \ref Expr.
alpar@1323
   670
    ///\bug The previous objective function is not cleared!
alpar@1253
   671
    void setObj(Expr e) {
athos@1377
   672
      _clearObj();
alpar@1253
   673
      for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
alpar@1293
   674
	objCoeff((*i).first,(*i).second);
alpar@1323
   675
      obj_const_comp=e.constComp();
alpar@1253
   676
    }
alpar@1263
   677
alpar@1312
   678
    ///Maximize
alpar@1312
   679
    void max() { _setMax(); }
alpar@1312
   680
    ///Minimize
alpar@1312
   681
    void min() { _setMin(); }
alpar@1312
   682
alpar@1312
   683
    
alpar@1263
   684
    ///@}
alpar@1263
   685
alpar@1263
   686
alpar@1294
   687
    ///\name Solve the LP
alpar@1263
   688
alpar@1263
   689
    ///@{
alpar@1263
   690
alpar@1263
   691
    ///\e
alpar@1303
   692
    SolveExitStatus solve() { return _solve(); }
alpar@1263
   693
    
alpar@1263
   694
    ///@}
alpar@1263
   695
    
alpar@1294
   696
    ///\name Obtain the solution
alpar@1263
   697
alpar@1263
   698
    ///@{
alpar@1263
   699
alpar@1263
   700
    ///\e
alpar@1312
   701
    SolutionStatus primalStatus() {
alpar@1312
   702
      return _getPrimalStatus();
alpar@1294
   703
    }
alpar@1294
   704
alpar@1294
   705
    ///\e
alpar@1293
   706
    Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); }
alpar@1263
   707
alpar@1312
   708
    ///\e
alpar@1312
   709
alpar@1312
   710
    ///\return
alpar@1312
   711
    ///- \ref INF or -\ref INF means either infeasibility or unboundedness
alpar@1312
   712
    /// of the primal problem, depending on whether we minimize or maximize.
alpar@1364
   713
    ///- \ref NaN if no primal solution is found.
alpar@1312
   714
    ///- The (finite) objective value if an optimal solution is found.
alpar@1323
   715
    Value primalValue() { return _getPrimalValue()+obj_const_comp;}
alpar@1263
   716
    ///@}
alpar@1253
   717
    
athos@1248
   718
  };  
athos@1246
   719
alpar@1272
   720
  ///\e
alpar@1272
   721
  
alpar@1272
   722
  ///\relates LpSolverBase::Expr
alpar@1272
   723
  ///
alpar@1272
   724
  inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
alpar@1272
   725
				      const LpSolverBase::Expr &b) 
alpar@1272
   726
  {
alpar@1272
   727
    LpSolverBase::Expr tmp(a);
alpar@1364
   728
    tmp+=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
   729
    return tmp;
alpar@1272
   730
  }
alpar@1272
   731
  ///\e
alpar@1272
   732
  
alpar@1272
   733
  ///\relates LpSolverBase::Expr
alpar@1272
   734
  ///
alpar@1272
   735
  inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
alpar@1272
   736
				      const LpSolverBase::Expr &b) 
alpar@1272
   737
  {
alpar@1272
   738
    LpSolverBase::Expr tmp(a);
alpar@1364
   739
    tmp-=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
   740
    return tmp;
alpar@1272
   741
  }
alpar@1272
   742
  ///\e
alpar@1272
   743
  
alpar@1272
   744
  ///\relates LpSolverBase::Expr
alpar@1272
   745
  ///
alpar@1272
   746
  inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
alpar@1273
   747
				      const LpSolverBase::Value &b) 
alpar@1272
   748
  {
alpar@1272
   749
    LpSolverBase::Expr tmp(a);
alpar@1364
   750
    tmp*=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
   751
    return tmp;
alpar@1272
   752
  }
alpar@1272
   753
  
alpar@1272
   754
  ///\e
alpar@1272
   755
  
alpar@1272
   756
  ///\relates LpSolverBase::Expr
alpar@1272
   757
  ///
alpar@1273
   758
  inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
alpar@1272
   759
				      const LpSolverBase::Expr &b) 
alpar@1272
   760
  {
alpar@1272
   761
    LpSolverBase::Expr tmp(b);
alpar@1364
   762
    tmp*=a; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
   763
    return tmp;
alpar@1272
   764
  }
alpar@1272
   765
  ///\e
alpar@1272
   766
  
alpar@1272
   767
  ///\relates LpSolverBase::Expr
alpar@1272
   768
  ///
alpar@1272
   769
  inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
alpar@1273
   770
				      const LpSolverBase::Value &b) 
alpar@1272
   771
  {
alpar@1272
   772
    LpSolverBase::Expr tmp(a);
alpar@1364
   773
    tmp/=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
   774
    return tmp;
alpar@1272
   775
  }
alpar@1272
   776
  
alpar@1272
   777
  ///\e
alpar@1272
   778
  
alpar@1272
   779
  ///\relates LpSolverBase::Constr
alpar@1272
   780
  ///
alpar@1272
   781
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1272
   782
					 const LpSolverBase::Expr &f) 
alpar@1272
   783
  {
alpar@1272
   784
    return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
alpar@1272
   785
  }
alpar@1272
   786
alpar@1272
   787
  ///\e
alpar@1272
   788
  
alpar@1272
   789
  ///\relates LpSolverBase::Constr
alpar@1272
   790
  ///
alpar@1273
   791
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
alpar@1272
   792
					 const LpSolverBase::Expr &f) 
alpar@1272
   793
  {
alpar@1272
   794
    return LpSolverBase::Constr(e,f);
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@1273
   802
					 const LpSolverBase::Value &f) 
alpar@1272
   803
  {
alpar@1272
   804
    return LpSolverBase::Constr(e,f);
alpar@1272
   805
  }
alpar@1272
   806
alpar@1272
   807
  ///\e
alpar@1272
   808
  
alpar@1272
   809
  ///\relates LpSolverBase::Constr
alpar@1272
   810
  ///
alpar@1272
   811
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1272
   812
					 const LpSolverBase::Expr &f) 
alpar@1272
   813
  {
alpar@1272
   814
    return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
alpar@1272
   815
  }
alpar@1272
   816
alpar@1272
   817
alpar@1272
   818
  ///\e
alpar@1272
   819
  
alpar@1272
   820
  ///\relates LpSolverBase::Constr
alpar@1272
   821
  ///
alpar@1273
   822
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
alpar@1272
   823
					 const LpSolverBase::Expr &f) 
alpar@1272
   824
  {
alpar@1272
   825
    return LpSolverBase::Constr(f,e);
alpar@1272
   826
  }
alpar@1272
   827
alpar@1272
   828
alpar@1272
   829
  ///\e
alpar@1272
   830
  
alpar@1272
   831
  ///\relates LpSolverBase::Constr
alpar@1272
   832
  ///
alpar@1272
   833
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1273
   834
					 const LpSolverBase::Value &f) 
alpar@1272
   835
  {
alpar@1272
   836
    return LpSolverBase::Constr(f,e);
alpar@1272
   837
  }
alpar@1272
   838
alpar@1272
   839
  ///\e
alpar@1272
   840
  
alpar@1272
   841
  ///\relates LpSolverBase::Constr
alpar@1272
   842
  ///
alpar@1272
   843
  inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
alpar@1272
   844
					 const LpSolverBase::Expr &f) 
alpar@1272
   845
  {
alpar@1272
   846
    return LpSolverBase::Constr(0,e-f,0);
alpar@1272
   847
  }
alpar@1272
   848
alpar@1272
   849
  ///\e
alpar@1272
   850
  
alpar@1272
   851
  ///\relates LpSolverBase::Constr
alpar@1272
   852
  ///
alpar@1273
   853
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
alpar@1272
   854
					 const LpSolverBase::Constr&c) 
alpar@1272
   855
  {
alpar@1272
   856
    LpSolverBase::Constr tmp(c);
alpar@1273
   857
    ///\todo Create an own exception type.
alpar@1273
   858
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
   859
    else tmp.lowerBound()=n;
alpar@1272
   860
    return tmp;
alpar@1272
   861
  }
alpar@1272
   862
  ///\e
alpar@1272
   863
  
alpar@1272
   864
  ///\relates LpSolverBase::Constr
alpar@1272
   865
  ///
alpar@1272
   866
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
alpar@1273
   867
					 const LpSolverBase::Value &n)
alpar@1272
   868
  {
alpar@1272
   869
    LpSolverBase::Constr tmp(c);
alpar@1273
   870
    ///\todo Create an own exception type.
alpar@1273
   871
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
   872
    else tmp.upperBound()=n;
alpar@1272
   873
    return tmp;
alpar@1272
   874
  }
alpar@1272
   875
alpar@1272
   876
  ///\e
alpar@1272
   877
  
alpar@1272
   878
  ///\relates LpSolverBase::Constr
alpar@1272
   879
  ///
alpar@1273
   880
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
alpar@1272
   881
					 const LpSolverBase::Constr&c) 
alpar@1272
   882
  {
alpar@1272
   883
    LpSolverBase::Constr tmp(c);
alpar@1273
   884
    ///\todo Create an own exception type.
alpar@1273
   885
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
   886
    else tmp.upperBound()=n;
alpar@1272
   887
    return tmp;
alpar@1272
   888
  }
alpar@1272
   889
  ///\e
alpar@1272
   890
  
alpar@1272
   891
  ///\relates LpSolverBase::Constr
alpar@1272
   892
  ///
alpar@1272
   893
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
alpar@1273
   894
					 const LpSolverBase::Value &n)
alpar@1272
   895
  {
alpar@1272
   896
    LpSolverBase::Constr tmp(c);
alpar@1273
   897
    ///\todo Create an own exception type.
alpar@1273
   898
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
   899
    else tmp.lowerBound()=n;
alpar@1272
   900
    return tmp;
alpar@1272
   901
  }
alpar@1272
   902
alpar@1272
   903
athos@1246
   904
} //namespace lemon
athos@1246
   905
athos@1246
   906
#endif //LEMON_LP_BASE_H