lemon/lp_base.h
author deba
Thu, 10 Nov 2005 12:41:33 +0000
changeset 1786 a263c131e999
parent 1766 6c59b1386fe8
child 1787 932b8490caf0
permissions -rw-r--r--
Doc bug fix
athos@1247
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * 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
athos@1246
    29
///\file
athos@1246
    30
///\brief The interface of the LP solver interface.
alpar@1328
    31
///\ingroup gen_opt_group
athos@1246
    32
namespace lemon {
alpar@1253
    33
  
alpar@1253
    34
  ///Internal data structure to convert floating id's to fix one's
alpar@1253
    35
    
alpar@1279
    36
  ///\todo This might be implemented to be also usable in other places.
alpar@1253
    37
  class _FixId 
alpar@1253
    38
  {
alpar@1253
    39
    std::vector<int> index;
alpar@1253
    40
    std::vector<int> cross;
alpar@1253
    41
    int first_free;
alpar@1253
    42
  public:
alpar@1253
    43
    _FixId() : first_free(-1) {};
alpar@1253
    44
    ///Convert a floating id to a fix one
alpar@1253
    45
alpar@1253
    46
    ///\param n is a floating id
alpar@1253
    47
    ///\return the corresponding fix id
alpar@1484
    48
    int fixId(int n) const {return cross[n];}
alpar@1253
    49
    ///Convert a fix id to a floating one
alpar@1253
    50
alpar@1253
    51
    ///\param n is a fix id
alpar@1253
    52
    ///\return the corresponding floating id
alpar@1484
    53
    int floatingId(int n) const { return index[n];}
alpar@1253
    54
    ///Add a new floating id.
alpar@1253
    55
alpar@1253
    56
    ///\param n is a floating id
alpar@1253
    57
    ///\return the fix id of the new value
alpar@1253
    58
    ///\todo Multiple additions should also be handled.
alpar@1253
    59
    int insert(int n)
alpar@1253
    60
    {
alpar@1253
    61
      if(n>=int(cross.size())) {
alpar@1253
    62
	cross.resize(n+1);
alpar@1253
    63
	if(first_free==-1) {
alpar@1253
    64
	  cross[n]=index.size();
alpar@1253
    65
	  index.push_back(n);
alpar@1253
    66
	}
alpar@1253
    67
	else {
alpar@1253
    68
	  cross[n]=first_free;
alpar@1253
    69
	  int next=index[first_free];
alpar@1253
    70
	  index[first_free]=n;
alpar@1253
    71
	  first_free=next;
alpar@1253
    72
	}
alpar@1256
    73
	return cross[n];
alpar@1253
    74
      }
alpar@1273
    75
      ///\todo Create an own exception type.
alpar@1253
    76
      else throw LogicError(); //floatingId-s must form a continuous range;
alpar@1253
    77
    }
alpar@1253
    78
    ///Remove a fix id.
alpar@1253
    79
alpar@1253
    80
    ///\param n is a fix id
alpar@1253
    81
    ///
alpar@1253
    82
    void erase(int n) 
alpar@1253
    83
    {
alpar@1253
    84
      int fl=index[n];
alpar@1253
    85
      index[n]=first_free;
alpar@1253
    86
      first_free=n;
alpar@1253
    87
      for(int i=fl+1;i<int(cross.size());++i) {
alpar@1253
    88
	cross[i-1]=cross[i];
alpar@1253
    89
	index[cross[i]]--;
alpar@1253
    90
      }
alpar@1253
    91
      cross.pop_back();
alpar@1253
    92
    }
alpar@1253
    93
    ///An upper bound on the largest fix id.
alpar@1253
    94
alpar@1253
    95
    ///\todo Do we need this?
alpar@1253
    96
    ///
alpar@1253
    97
    std::size_t maxFixId() { return cross.size()-1; }
alpar@1253
    98
  
alpar@1253
    99
  };
alpar@1253
   100
    
alpar@1253
   101
  ///Common base class for LP solvers
alpar@1328
   102
  
alpar@1328
   103
  ///\todo Much more docs
alpar@1328
   104
  ///\ingroup gen_opt_group
athos@1246
   105
  class LpSolverBase {
alpar@1323
   106
athos@1247
   107
  public:
athos@1247
   108
athos@1458
   109
    ///Possible outcomes of an LP solving procedure
alpar@1303
   110
    enum SolveExitStatus {
athos@1458
   111
      ///This means that the problem has been successfully solved: either
athos@1458
   112
      ///an optimal solution has been found or infeasibility/unboundedness
athos@1458
   113
      ///has been proved.
alpar@1293
   114
      SOLVED = 0,
athos@1458
   115
      ///Any other case (including the case when some user specified limit has been exceeded)
alpar@1293
   116
      UNSOLVED = 1
athos@1291
   117
    };
athos@1291
   118
      
athos@1460
   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
    };
athos@1460
   138
athos@1542
   139
    ///\e The type of the investigated LP problem
athos@1542
   140
    enum ProblemTypes {
athos@1542
   141
      ///Primal-dual feasible
athos@1542
   142
      PRIMAL_DUAL_FEASIBLE = 0,
athos@1542
   143
      ///Primal feasible dual infeasible
athos@1542
   144
      PRIMAL_FEASIBLE_DUAL_INFEASIBLE = 1,
athos@1542
   145
      ///Primal infeasible dual feasible
athos@1542
   146
      PRIMAL_INFEASIBLE_DUAL_FEASIBLE = 2,
athos@1542
   147
      ///Primal-dual infeasible
athos@1542
   148
      PRIMAL_DUAL_INFEASIBLE = 3,
athos@1542
   149
      ///Could not determine so far
athos@1542
   150
      UNKNOWN = 4
athos@1542
   151
    };
athos@1508
   152
alpar@1256
   153
    ///The floating point type used by the solver
athos@1247
   154
    typedef double Value;
alpar@1256
   155
    ///The infinity constant
athos@1247
   156
    static const Value INF;
alpar@1264
   157
    ///The not a number constant
alpar@1264
   158
    static const Value NaN;
alpar@1253
   159
    
alpar@1256
   160
    ///Refer to a column of the LP.
alpar@1256
   161
alpar@1256
   162
    ///This type is used to refer to a column of the LP.
alpar@1256
   163
    ///
alpar@1256
   164
    ///Its value remains valid and correct even after the addition or erase of
alpar@1273
   165
    ///other columns.
alpar@1256
   166
    ///
alpar@1256
   167
    ///\todo Document what can one do with a Col (INVALID, comparing,
alpar@1256
   168
    ///it is similar to Node/Edge)
alpar@1256
   169
    class Col {
alpar@1256
   170
    protected:
alpar@1256
   171
      int id;
alpar@1256
   172
      friend class LpSolverBase;
alpar@1256
   173
    public:
alpar@1259
   174
      typedef Value ExprValue;
alpar@1256
   175
      typedef True LpSolverCol;
alpar@1256
   176
      Col() {}
alpar@1256
   177
      Col(const Invalid&) : id(-1) {}
alpar@1256
   178
      bool operator<(Col c) const  {return id<c.id;}
alpar@1256
   179
      bool operator==(Col c) const  {return id==c.id;}
alpar@1256
   180
      bool operator!=(Col c) const  {return id==c.id;}
alpar@1256
   181
    };
alpar@1256
   182
alpar@1256
   183
    ///Refer to a row of the LP.
alpar@1256
   184
alpar@1256
   185
    ///This type is used to refer to a row of the LP.
alpar@1256
   186
    ///
alpar@1256
   187
    ///Its value remains valid and correct even after the addition or erase of
alpar@1273
   188
    ///other rows.
alpar@1256
   189
    ///
alpar@1256
   190
    ///\todo Document what can one do with a Row (INVALID, comparing,
alpar@1256
   191
    ///it is similar to Node/Edge)
alpar@1256
   192
    class Row {
alpar@1256
   193
    protected:
alpar@1256
   194
      int id;
alpar@1256
   195
      friend class LpSolverBase;
alpar@1256
   196
    public:
alpar@1259
   197
      typedef Value ExprValue;
alpar@1256
   198
      typedef True LpSolverRow;
alpar@1256
   199
      Row() {}
alpar@1256
   200
      Row(const Invalid&) : id(-1) {}
alpar@1439
   201
alpar@1256
   202
      bool operator<(Row c) const  {return id<c.id;}
alpar@1256
   203
      bool operator==(Row c) const  {return id==c.id;}
alpar@1256
   204
      bool operator!=(Row c) const  {return id==c.id;} 
alpar@1256
   205
   };
alpar@1259
   206
    
alpar@1279
   207
    ///Linear expression of variables and a constant component
alpar@1279
   208
    
alpar@1279
   209
    ///This data structure strores a linear expression of the variables
alpar@1279
   210
    ///(\ref Col "Col"s) and also has a constant component.
alpar@1279
   211
    ///
alpar@1279
   212
    ///There are several ways to access and modify the contents of this
alpar@1279
   213
    ///container.
alpar@1279
   214
    ///- Its it fully compatible with \c std::map<Col,double>, so for expamle
alpar@1364
   215
    ///if \c e is an Expr and \c v and \c w are of type \ref Col, then you can
alpar@1279
   216
    ///read and modify the coefficients like
alpar@1279
   217
    ///these.
alpar@1279
   218
    ///\code
alpar@1279
   219
    ///e[v]=5;
alpar@1279
   220
    ///e[v]+=12;
alpar@1279
   221
    ///e.erase(v);
alpar@1279
   222
    ///\endcode
alpar@1279
   223
    ///or you can also iterate through its elements.
alpar@1279
   224
    ///\code
alpar@1279
   225
    ///double s=0;
alpar@1279
   226
    ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i)
alpar@1279
   227
    ///  s+=i->second;
alpar@1279
   228
    ///\endcode
alpar@1279
   229
    ///(This code computes the sum of all coefficients).
alpar@1279
   230
    ///- Numbers (<tt>double</tt>'s)
alpar@1279
   231
    ///and variables (\ref Col "Col"s) directly convert to an
alpar@1279
   232
    ///\ref Expr and the usual linear operations are defined so  
alpar@1279
   233
    ///\code
alpar@1279
   234
    ///v+w
alpar@1279
   235
    ///2*v-3.12*(v-w/2)+2
alpar@1279
   236
    ///v*2.1+(3*v+(v*12+w+6)*3)/2
alpar@1279
   237
    ///\endcode
alpar@1328
   238
    ///are valid \ref Expr "Expr"essions.
alpar@1328
   239
    ///The usual assignment operations are also defined.
alpar@1279
   240
    ///\code
alpar@1279
   241
    ///e=v+w;
alpar@1279
   242
    ///e+=2*v-3.12*(v-w/2)+2;
alpar@1279
   243
    ///e*=3.4;
alpar@1279
   244
    ///e/=5;
alpar@1279
   245
    ///\endcode
alpar@1279
   246
    ///- The constant member can be set and read by \ref constComp()
alpar@1279
   247
    ///\code
alpar@1279
   248
    ///e.constComp()=12;
alpar@1279
   249
    ///double c=e.constComp();
alpar@1279
   250
    ///\endcode
alpar@1279
   251
    ///
alpar@1328
   252
    ///\note \ref clear() not only sets all coefficients to 0 but also
alpar@1279
   253
    ///clears the constant components.
alpar@1328
   254
    ///
alpar@1328
   255
    ///\sa Constr
alpar@1328
   256
    ///
alpar@1273
   257
    class Expr : public std::map<Col,Value>
alpar@1272
   258
    {
alpar@1272
   259
    public:
alpar@1273
   260
      typedef LpSolverBase::Col Key; 
alpar@1273
   261
      typedef LpSolverBase::Value Value;
alpar@1272
   262
      
alpar@1272
   263
    protected:
alpar@1273
   264
      typedef std::map<Col,Value> Base;
alpar@1272
   265
      
alpar@1273
   266
      Value const_comp;
alpar@1272
   267
  public:
alpar@1272
   268
      typedef True IsLinExpression;
alpar@1272
   269
      ///\e
alpar@1272
   270
      Expr() : Base(), const_comp(0) { }
alpar@1272
   271
      ///\e
alpar@1273
   272
      Expr(const Key &v) : const_comp(0) {
alpar@1272
   273
	Base::insert(std::make_pair(v, 1));
alpar@1272
   274
      }
alpar@1272
   275
      ///\e
alpar@1273
   276
      Expr(const Value &v) : const_comp(v) {}
alpar@1272
   277
      ///\e
alpar@1273
   278
      void set(const Key &v,const Value &c) {
alpar@1272
   279
	Base::insert(std::make_pair(v, c));
alpar@1272
   280
      }
alpar@1272
   281
      ///\e
alpar@1273
   282
      Value &constComp() { return const_comp; }
alpar@1272
   283
      ///\e
alpar@1273
   284
      const Value &constComp() const { return const_comp; }
alpar@1272
   285
      
alpar@1272
   286
      ///Removes the components with zero coefficient.
alpar@1272
   287
      void simplify() {
alpar@1272
   288
	for (Base::iterator i=Base::begin(); i!=Base::end();) {
alpar@1272
   289
	  Base::iterator j=i;
alpar@1272
   290
	  ++j;
alpar@1272
   291
	  if ((*i).second==0) Base::erase(i);
alpar@1272
   292
	  j=i;
alpar@1272
   293
	}
alpar@1272
   294
      }
alpar@1273
   295
alpar@1771
   296
      ///Removes the coefficients closer to zero than \c tolerance.
alpar@1771
   297
      void simplify(double &tolerance) {
alpar@1771
   298
	for (Base::iterator i=Base::begin(); i!=Base::end();) {
alpar@1771
   299
	  Base::iterator j=i;
alpar@1771
   300
	  ++j;
alpar@1771
   301
	  if (std::fabs((*i).second)<tolerance) Base::erase(i);
alpar@1771
   302
	  j=i;
alpar@1771
   303
	}
alpar@1771
   304
      }
alpar@1771
   305
alpar@1273
   306
      ///Sets all coefficients and the constant component to 0.
alpar@1273
   307
      void clear() {
alpar@1273
   308
	Base::clear();
alpar@1273
   309
	const_comp=0;
alpar@1273
   310
      }
alpar@1273
   311
alpar@1272
   312
      ///\e
alpar@1272
   313
      Expr &operator+=(const Expr &e) {
alpar@1272
   314
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1272
   315
	  (*this)[j->first]+=j->second;
alpar@1272
   316
	const_comp+=e.const_comp;
alpar@1272
   317
	return *this;
alpar@1272
   318
      }
alpar@1272
   319
      ///\e
alpar@1272
   320
      Expr &operator-=(const Expr &e) {
alpar@1272
   321
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1272
   322
	  (*this)[j->first]-=j->second;
alpar@1272
   323
	const_comp-=e.const_comp;
alpar@1272
   324
	return *this;
alpar@1272
   325
      }
alpar@1272
   326
      ///\e
alpar@1273
   327
      Expr &operator*=(const Value &c) {
alpar@1272
   328
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1272
   329
	  j->second*=c;
alpar@1272
   330
	const_comp*=c;
alpar@1272
   331
	return *this;
alpar@1272
   332
      }
alpar@1272
   333
      ///\e
alpar@1273
   334
      Expr &operator/=(const Value &c) {
alpar@1272
   335
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1272
   336
	  j->second/=c;
alpar@1272
   337
	const_comp/=c;
alpar@1272
   338
	return *this;
alpar@1272
   339
      }
alpar@1272
   340
    };
alpar@1272
   341
    
alpar@1264
   342
    ///Linear constraint
alpar@1328
   343
alpar@1364
   344
    ///This data stucture represents a linear constraint in the LP.
alpar@1364
   345
    ///Basically it is a linear expression with a lower or an upper bound
alpar@1364
   346
    ///(or both). These parts of the constraint can be obtained by the member
alpar@1364
   347
    ///functions \ref expr(), \ref lowerBound() and \ref upperBound(),
alpar@1364
   348
    ///respectively.
alpar@1364
   349
    ///There are two ways to construct a constraint.
alpar@1364
   350
    ///- You can set the linear expression and the bounds directly
alpar@1364
   351
    ///  by the functions above.
alpar@1364
   352
    ///- The operators <tt>\<=</tt>, <tt>==</tt> and  <tt>\>=</tt>
alpar@1364
   353
    ///  are defined between expressions, or even between constraints whenever
alpar@1364
   354
    ///  it makes sense. Therefore if \c e and \c f are linear expressions and
alpar@1364
   355
    ///  \c s and \c t are numbers, then the followings are valid expressions
alpar@1364
   356
    ///  and thus they can be used directly e.g. in \ref addRow() whenever
alpar@1364
   357
    ///  it makes sense.
alpar@1364
   358
    ///  \code
alpar@1364
   359
    ///  e<=s
alpar@1364
   360
    ///  e<=f
alpar@1364
   361
    ///  s<=e<=t
alpar@1364
   362
    ///  e>=t
alpar@1364
   363
    ///  \endcode
alpar@1364
   364
    ///\warning The validity of a constraint is checked only at run time, so
alpar@1364
   365
    ///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw a
alpar@1364
   366
    ///\ref LogicError exception.
alpar@1272
   367
    class Constr
alpar@1272
   368
    {
alpar@1272
   369
    public:
alpar@1272
   370
      typedef LpSolverBase::Expr Expr;
alpar@1273
   371
      typedef Expr::Key Key;
alpar@1273
   372
      typedef Expr::Value Value;
alpar@1272
   373
      
alpar@1364
   374
//       static const Value INF;
alpar@1364
   375
//       static const Value NaN;
alpar@1364
   376
alpar@1273
   377
    protected:
alpar@1273
   378
      Expr _expr;
alpar@1273
   379
      Value _lb,_ub;
alpar@1273
   380
    public:
alpar@1273
   381
      ///\e
alpar@1273
   382
      Constr() : _expr(), _lb(NaN), _ub(NaN) {}
alpar@1273
   383
      ///\e
alpar@1273
   384
      Constr(Value lb,const Expr &e,Value ub) :
alpar@1273
   385
	_expr(e), _lb(lb), _ub(ub) {}
alpar@1273
   386
      ///\e
alpar@1273
   387
      Constr(const Expr &e,Value ub) : 
alpar@1273
   388
	_expr(e), _lb(NaN), _ub(ub) {}
alpar@1273
   389
      ///\e
alpar@1273
   390
      Constr(Value lb,const Expr &e) :
alpar@1273
   391
	_expr(e), _lb(lb), _ub(NaN) {}
alpar@1273
   392
      ///\e
alpar@1272
   393
      Constr(const Expr &e) : 
alpar@1273
   394
	_expr(e), _lb(NaN), _ub(NaN) {}
alpar@1273
   395
      ///\e
alpar@1273
   396
      void clear() 
alpar@1273
   397
      {
alpar@1273
   398
	_expr.clear();
alpar@1273
   399
	_lb=_ub=NaN;
alpar@1273
   400
      }
alpar@1364
   401
alpar@1364
   402
      ///Reference to the linear expression 
alpar@1273
   403
      Expr &expr() { return _expr; }
alpar@1364
   404
      ///Cont reference to the linear expression 
alpar@1273
   405
      const Expr &expr() const { return _expr; }
alpar@1364
   406
      ///Reference to the lower bound.
alpar@1364
   407
alpar@1364
   408
      ///\return
alpar@1536
   409
      ///- \ref INF "INF": the constraint is lower unbounded.
alpar@1536
   410
      ///- \ref NaN "NaN": lower bound has not been set.
alpar@1364
   411
      ///- finite number: the lower bound
alpar@1273
   412
      Value &lowerBound() { return _lb; }
alpar@1364
   413
      ///The const version of \ref lowerBound()
alpar@1273
   414
      const Value &lowerBound() const { return _lb; }
alpar@1364
   415
      ///Reference to the upper bound.
alpar@1364
   416
alpar@1364
   417
      ///\return
alpar@1536
   418
      ///- \ref INF "INF": the constraint is upper unbounded.
alpar@1536
   419
      ///- \ref NaN "NaN": upper bound has not been set.
alpar@1364
   420
      ///- finite number: the upper bound
alpar@1273
   421
      Value &upperBound() { return _ub; }
alpar@1364
   422
      ///The const version of \ref upperBound()
alpar@1273
   423
      const Value &upperBound() const { return _ub; }
alpar@1364
   424
      ///Is the constraint lower bounded?
alpar@1295
   425
      bool lowerBounded() const { 
alpar@1295
   426
	using namespace std;
alpar@1397
   427
	return finite(_lb);
alpar@1295
   428
      }
alpar@1364
   429
      ///Is the constraint upper bounded?
alpar@1295
   430
      bool upperBounded() const {
alpar@1295
   431
	using namespace std;
alpar@1397
   432
	return finite(_ub);
alpar@1295
   433
      }
alpar@1272
   434
    };
alpar@1272
   435
    
alpar@1445
   436
    ///Linear expression of rows
alpar@1445
   437
    
alpar@1445
   438
    ///This data structure represents a column of the matrix,
alpar@1445
   439
    ///thas is it strores a linear expression of the dual variables
alpar@1445
   440
    ///(\ref Row "Row"s).
alpar@1445
   441
    ///
alpar@1445
   442
    ///There are several ways to access and modify the contents of this
alpar@1445
   443
    ///container.
alpar@1445
   444
    ///- Its it fully compatible with \c std::map<Row,double>, so for expamle
alpar@1445
   445
    ///if \c e is an DualExpr and \c v
alpar@1445
   446
    ///and \c w are of type \ref Row, then you can
alpar@1445
   447
    ///read and modify the coefficients like
alpar@1445
   448
    ///these.
alpar@1445
   449
    ///\code
alpar@1445
   450
    ///e[v]=5;
alpar@1445
   451
    ///e[v]+=12;
alpar@1445
   452
    ///e.erase(v);
alpar@1445
   453
    ///\endcode
alpar@1445
   454
    ///or you can also iterate through its elements.
alpar@1445
   455
    ///\code
alpar@1445
   456
    ///double s=0;
alpar@1445
   457
    ///for(LpSolverBase::DualExpr::iterator i=e.begin();i!=e.end();++i)
alpar@1445
   458
    ///  s+=i->second;
alpar@1445
   459
    ///\endcode
alpar@1445
   460
    ///(This code computes the sum of all coefficients).
alpar@1445
   461
    ///- Numbers (<tt>double</tt>'s)
alpar@1445
   462
    ///and variables (\ref Row "Row"s) directly convert to an
alpar@1445
   463
    ///\ref DualExpr and the usual linear operations are defined so  
alpar@1445
   464
    ///\code
alpar@1445
   465
    ///v+w
alpar@1445
   466
    ///2*v-3.12*(v-w/2)
alpar@1445
   467
    ///v*2.1+(3*v+(v*12+w)*3)/2
alpar@1445
   468
    ///\endcode
alpar@1445
   469
    ///are valid \ref DualExpr "DualExpr"essions.
alpar@1445
   470
    ///The usual assignment operations are also defined.
alpar@1445
   471
    ///\code
alpar@1445
   472
    ///e=v+w;
alpar@1445
   473
    ///e+=2*v-3.12*(v-w/2);
alpar@1445
   474
    ///e*=3.4;
alpar@1445
   475
    ///e/=5;
alpar@1445
   476
    ///\endcode
alpar@1445
   477
    ///
alpar@1445
   478
    ///\sa Expr
alpar@1445
   479
    ///
alpar@1445
   480
    class DualExpr : public std::map<Row,Value>
alpar@1445
   481
    {
alpar@1445
   482
    public:
alpar@1445
   483
      typedef LpSolverBase::Row Key; 
alpar@1445
   484
      typedef LpSolverBase::Value Value;
alpar@1445
   485
      
alpar@1445
   486
    protected:
alpar@1445
   487
      typedef std::map<Row,Value> Base;
alpar@1445
   488
      
alpar@1445
   489
    public:
alpar@1445
   490
      typedef True IsLinExpression;
alpar@1445
   491
      ///\e
alpar@1445
   492
      DualExpr() : Base() { }
alpar@1445
   493
      ///\e
alpar@1445
   494
      DualExpr(const Key &v) {
alpar@1445
   495
	Base::insert(std::make_pair(v, 1));
alpar@1445
   496
      }
alpar@1445
   497
      ///\e
alpar@1445
   498
      void set(const Key &v,const Value &c) {
alpar@1445
   499
	Base::insert(std::make_pair(v, c));
alpar@1445
   500
      }
alpar@1445
   501
      
alpar@1445
   502
      ///Removes the components with zero coefficient.
alpar@1445
   503
      void simplify() {
alpar@1445
   504
	for (Base::iterator i=Base::begin(); i!=Base::end();) {
alpar@1445
   505
	  Base::iterator j=i;
alpar@1445
   506
	  ++j;
alpar@1445
   507
	  if ((*i).second==0) Base::erase(i);
alpar@1445
   508
	  j=i;
alpar@1445
   509
	}
alpar@1445
   510
      }
alpar@1445
   511
alpar@1771
   512
      ///Removes the coefficients closer to zero than \c tolerance.
alpar@1771
   513
      void simplify(double &tolerance) {
alpar@1771
   514
	for (Base::iterator i=Base::begin(); i!=Base::end();) {
alpar@1771
   515
	  Base::iterator j=i;
alpar@1771
   516
	  ++j;
alpar@1771
   517
	  if (std::fabs((*i).second)<tolerance) Base::erase(i);
alpar@1771
   518
	  j=i;
alpar@1771
   519
	}
alpar@1771
   520
      }
alpar@1771
   521
alpar@1771
   522
alpar@1445
   523
      ///Sets all coefficients to 0.
alpar@1445
   524
      void clear() {
alpar@1445
   525
	Base::clear();
alpar@1445
   526
      }
alpar@1445
   527
alpar@1445
   528
      ///\e
alpar@1445
   529
      DualExpr &operator+=(const DualExpr &e) {
alpar@1445
   530
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1445
   531
	  (*this)[j->first]+=j->second;
alpar@1445
   532
	return *this;
alpar@1445
   533
      }
alpar@1445
   534
      ///\e
alpar@1445
   535
      DualExpr &operator-=(const DualExpr &e) {
alpar@1445
   536
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1445
   537
	  (*this)[j->first]-=j->second;
alpar@1445
   538
	return *this;
alpar@1445
   539
      }
alpar@1445
   540
      ///\e
alpar@1445
   541
      DualExpr &operator*=(const Value &c) {
alpar@1445
   542
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1445
   543
	  j->second*=c;
alpar@1445
   544
	return *this;
alpar@1445
   545
      }
alpar@1445
   546
      ///\e
alpar@1445
   547
      DualExpr &operator/=(const Value &c) {
alpar@1445
   548
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1445
   549
	  j->second/=c;
alpar@1445
   550
	return *this;
alpar@1445
   551
      }
alpar@1445
   552
    };
alpar@1445
   553
    
alpar@1253
   554
alpar@1253
   555
  protected:
alpar@1253
   556
    _FixId rows;
alpar@1253
   557
    _FixId cols;
athos@1246
   558
alpar@1323
   559
    //Abstract virtual functions
alpar@1364
   560
    virtual LpSolverBase &_newLp() = 0;
athos@1436
   561
    virtual LpSolverBase &_copyLp(){
athos@1436
   562
      ///\todo This should be implemented here, too,  when we have problem retrieving routines. It can be overriden.
athos@1436
   563
athos@1436
   564
      //Starting:
athos@1436
   565
      LpSolverBase & newlp(_newLp());
athos@1436
   566
      return newlp;
athos@1436
   567
      //return *(LpSolverBase*)0;
athos@1436
   568
    };
alpar@1364
   569
athos@1246
   570
    virtual int _addCol() = 0;
athos@1246
   571
    virtual int _addRow() = 0;
athos@1542
   572
    virtual void _eraseCol(int col) = 0;
athos@1542
   573
    virtual void _eraseRow(int row) = 0;
athos@1246
   574
    virtual void _setRowCoeffs(int i, 
athos@1251
   575
			       int length,
athos@1247
   576
                               int  const * indices, 
athos@1247
   577
                               Value  const * values ) = 0;
athos@1246
   578
    virtual void _setColCoeffs(int i, 
athos@1251
   579
			       int length,
athos@1247
   580
                               int  const * indices, 
athos@1247
   581
                               Value  const * values ) = 0;
athos@1431
   582
    virtual void _setCoeff(int row, int col, Value value) = 0;
alpar@1294
   583
    virtual void _setColLowerBound(int i, Value value) = 0;
alpar@1294
   584
    virtual void _setColUpperBound(int i, Value value) = 0;
athos@1405
   585
//     virtual void _setRowLowerBound(int i, Value value) = 0;
athos@1405
   586
//     virtual void _setRowUpperBound(int i, Value value) = 0;
athos@1379
   587
    virtual void _setRowBounds(int i, Value lower, Value upper) = 0;
alpar@1294
   588
    virtual void _setObjCoeff(int i, Value obj_coef) = 0;
athos@1377
   589
    virtual void _clearObj()=0;
athos@1377
   590
//     virtual void _setObj(int length,
athos@1377
   591
//                          int  const * indices, 
athos@1377
   592
//                          Value  const * values ) = 0;
alpar@1303
   593
    virtual SolveExitStatus _solve() = 0;
alpar@1294
   594
    virtual Value _getPrimal(int i) = 0;
alpar@1312
   595
    virtual Value _getPrimalValue() = 0;
alpar@1312
   596
    virtual SolutionStatus _getPrimalStatus() = 0;
athos@1460
   597
    virtual SolutionStatus _getDualStatus() = 0;
athos@1460
   598
    ///\todo This could be implemented here, too, using _getPrimalStatus() and
athos@1460
   599
    ///_getDualStatus()
athos@1460
   600
    virtual ProblemTypes _getProblemType() = 0;
athos@1460
   601
alpar@1312
   602
    virtual void _setMax() = 0;
alpar@1312
   603
    virtual void _setMin() = 0;
alpar@1312
   604
    
alpar@1323
   605
    //Own protected stuff
alpar@1323
   606
    
alpar@1323
   607
    //Constant component of the objective function
alpar@1323
   608
    Value obj_const_comp;
alpar@1323
   609
    
athos@1377
   610
athos@1377
   611
alpar@1323
   612
    
alpar@1253
   613
  public:
alpar@1253
   614
alpar@1323
   615
    ///\e
alpar@1323
   616
    LpSolverBase() : obj_const_comp(0) {}
alpar@1253
   617
alpar@1253
   618
    ///\e
alpar@1253
   619
    virtual ~LpSolverBase() {}
alpar@1253
   620
alpar@1364
   621
    ///Creates a new LP problem
alpar@1364
   622
    LpSolverBase &newLp() {return _newLp();}
alpar@1381
   623
    ///Makes a copy of the LP problem
alpar@1364
   624
    LpSolverBase &copyLp() {return _copyLp();}
alpar@1364
   625
    
alpar@1612
   626
    ///\name Build up and modify the LP
alpar@1263
   627
alpar@1263
   628
    ///@{
alpar@1263
   629
alpar@1253
   630
    ///Add a new empty column (i.e a new variable) to the LP
alpar@1253
   631
    Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
alpar@1263
   632
alpar@1294
   633
    ///\brief Adds several new columns
alpar@1294
   634
    ///(i.e a variables) at once
alpar@1256
   635
    ///
alpar@1273
   636
    ///This magic function takes a container as its argument
alpar@1256
   637
    ///and fills its elements
alpar@1256
   638
    ///with new columns (i.e. variables)
alpar@1273
   639
    ///\param t can be
alpar@1273
   640
    ///- a standard STL compatible iterable container with
alpar@1273
   641
    ///\ref Col as its \c values_type
alpar@1273
   642
    ///like
alpar@1273
   643
    ///\code
alpar@1273
   644
    ///std::vector<LpSolverBase::Col>
alpar@1273
   645
    ///std::list<LpSolverBase::Col>
alpar@1273
   646
    ///\endcode
alpar@1273
   647
    ///- a standard STL compatible iterable container with
alpar@1273
   648
    ///\ref Col as its \c mapped_type
alpar@1273
   649
    ///like
alpar@1273
   650
    ///\code
alpar@1364
   651
    ///std::map<AnyType,LpSolverBase::Col>
alpar@1273
   652
    ///\endcode
alpar@1273
   653
    ///- an iterable lemon \ref concept::WriteMap "write map" like 
alpar@1273
   654
    ///\code
alpar@1273
   655
    ///ListGraph::NodeMap<LpSolverBase::Col>
alpar@1273
   656
    ///ListGraph::EdgeMap<LpSolverBase::Col>
alpar@1273
   657
    ///\endcode
alpar@1256
   658
    ///\return The number of the created column.
alpar@1256
   659
#ifdef DOXYGEN
alpar@1256
   660
    template<class T>
alpar@1256
   661
    int addColSet(T &t) { return 0;} 
alpar@1256
   662
#else
alpar@1256
   663
    template<class T>
alpar@1256
   664
    typename enable_if<typename T::value_type::LpSolverCol,int>::type
alpar@1256
   665
    addColSet(T &t,dummy<0> = 0) {
alpar@1256
   666
      int s=0;
alpar@1256
   667
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
alpar@1256
   668
      return s;
alpar@1256
   669
    }
alpar@1256
   670
    template<class T>
alpar@1256
   671
    typename enable_if<typename T::value_type::second_type::LpSolverCol,
alpar@1256
   672
		       int>::type
alpar@1256
   673
    addColSet(T &t,dummy<1> = 1) { 
alpar@1256
   674
      int s=0;
alpar@1256
   675
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1256
   676
	i->second=addCol();
alpar@1256
   677
	s++;
alpar@1256
   678
      }
alpar@1256
   679
      return s;
alpar@1256
   680
    }
alpar@1272
   681
    template<class T>
alpar@1272
   682
    typename enable_if<typename T::ValueSet::value_type::LpSolverCol,
alpar@1272
   683
		       int>::type
alpar@1272
   684
    addColSet(T &t,dummy<2> = 2) { 
alpar@1272
   685
      ///\bug <tt>return addColSet(t.valueSet());</tt> should also work.
alpar@1272
   686
      int s=0;
alpar@1272
   687
      for(typename T::ValueSet::iterator i=t.valueSet().begin();
alpar@1272
   688
	  i!=t.valueSet().end();
alpar@1272
   689
	  ++i)
alpar@1272
   690
	{
alpar@1272
   691
	  *i=addCol();
alpar@1272
   692
	  s++;
alpar@1272
   693
	}
alpar@1272
   694
      return s;
alpar@1272
   695
    }
alpar@1256
   696
#endif
alpar@1263
   697
alpar@1445
   698
    ///Set a column (i.e a dual constraint) of the LP
alpar@1258
   699
alpar@1445
   700
    ///\param c is the column to be modified
alpar@1445
   701
    ///\param e is a dual linear expression (see \ref DualExpr)
athos@1542
   702
    ///\bug This is a temporary function. The interface will change to
alpar@1445
   703
    ///a better one.
alpar@1445
   704
    void setCol(Col c,const DualExpr &e) {
alpar@1445
   705
      std::vector<int> indices;
alpar@1445
   706
      std::vector<Value> values;
alpar@1445
   707
      indices.push_back(0);
alpar@1445
   708
      values.push_back(0);
alpar@1445
   709
      for(DualExpr::const_iterator i=e.begin(); i!=e.end(); ++i)
alpar@1445
   710
	if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
alpar@1445
   711
	  indices.push_back(cols.floatingId((*i).first.id));
alpar@1445
   712
	  values.push_back((*i).second);
alpar@1445
   713
	}
alpar@1445
   714
      _setColCoeffs(cols.floatingId(c.id),indices.size()-1,
alpar@1445
   715
		    &indices[0],&values[0]);
alpar@1445
   716
    }
alpar@1445
   717
alpar@1445
   718
    ///Add a new column to the LP
alpar@1445
   719
alpar@1445
   720
    ///\param e is a dual linear expression (see \ref DualExpr)
alpar@1445
   721
    ///\param obj is the corresponding component of the objective
alpar@1445
   722
    ///function. It is 0 by default.
alpar@1445
   723
    ///\return The created column.
alpar@1445
   724
    ///\bug This is a temportary function. The interface will change to
alpar@1445
   725
    ///a better one.
alpar@1493
   726
    Col addCol(const DualExpr &e, Value obj=0) {
alpar@1445
   727
      Col c=addCol();
alpar@1445
   728
      setCol(c,e);
alpar@1493
   729
      objCoeff(c,obj);
alpar@1445
   730
      return c;
alpar@1445
   731
    }
alpar@1445
   732
alpar@1445
   733
    ///Add a new empty row (i.e a new constraint) to the LP
alpar@1445
   734
alpar@1445
   735
    ///This function adds a new empty row (i.e a new constraint) to the LP.
alpar@1258
   736
    ///\return The created row
alpar@1253
   737
    Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
alpar@1253
   738
athos@1542
   739
    ///\brief Add several new rows
athos@1542
   740
    ///(i.e a constraints) at once
alpar@1445
   741
    ///
alpar@1445
   742
    ///This magic function takes a container as its argument
alpar@1445
   743
    ///and fills its elements
alpar@1445
   744
    ///with new row (i.e. variables)
alpar@1445
   745
    ///\param t can be
alpar@1445
   746
    ///- a standard STL compatible iterable container with
alpar@1445
   747
    ///\ref Row as its \c values_type
alpar@1445
   748
    ///like
alpar@1445
   749
    ///\code
alpar@1445
   750
    ///std::vector<LpSolverBase::Row>
alpar@1445
   751
    ///std::list<LpSolverBase::Row>
alpar@1445
   752
    ///\endcode
alpar@1445
   753
    ///- a standard STL compatible iterable container with
alpar@1445
   754
    ///\ref Row as its \c mapped_type
alpar@1445
   755
    ///like
alpar@1445
   756
    ///\code
alpar@1445
   757
    ///std::map<AnyType,LpSolverBase::Row>
alpar@1445
   758
    ///\endcode
alpar@1445
   759
    ///- an iterable lemon \ref concept::WriteMap "write map" like 
alpar@1445
   760
    ///\code
alpar@1445
   761
    ///ListGraph::NodeMap<LpSolverBase::Row>
alpar@1445
   762
    ///ListGraph::EdgeMap<LpSolverBase::Row>
alpar@1445
   763
    ///\endcode
alpar@1445
   764
    ///\return The number of rows created.
alpar@1445
   765
#ifdef DOXYGEN
alpar@1445
   766
    template<class T>
alpar@1445
   767
    int addRowSet(T &t) { return 0;} 
alpar@1445
   768
#else
alpar@1445
   769
    template<class T>
alpar@1445
   770
    typename enable_if<typename T::value_type::LpSolverRow,int>::type
alpar@1445
   771
    addRowSet(T &t,dummy<0> = 0) {
alpar@1445
   772
      int s=0;
alpar@1445
   773
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;}
alpar@1445
   774
      return s;
alpar@1445
   775
    }
alpar@1445
   776
    template<class T>
alpar@1445
   777
    typename enable_if<typename T::value_type::second_type::LpSolverRow,
alpar@1445
   778
		       int>::type
alpar@1445
   779
    addRowSet(T &t,dummy<1> = 1) { 
alpar@1445
   780
      int s=0;
alpar@1445
   781
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1445
   782
	i->second=addRow();
alpar@1445
   783
	s++;
alpar@1445
   784
      }
alpar@1445
   785
      return s;
alpar@1445
   786
    }
alpar@1445
   787
    template<class T>
alpar@1445
   788
    typename enable_if<typename T::ValueSet::value_type::LpSolverRow,
alpar@1445
   789
		       int>::type
alpar@1445
   790
    addRowSet(T &t,dummy<2> = 2) { 
alpar@1445
   791
      ///\bug <tt>return addRowSet(t.valueSet());</tt> should also work.
alpar@1445
   792
      int s=0;
alpar@1445
   793
      for(typename T::ValueSet::iterator i=t.valueSet().begin();
alpar@1445
   794
	  i!=t.valueSet().end();
alpar@1445
   795
	  ++i)
alpar@1445
   796
	{
alpar@1445
   797
	  *i=addRow();
alpar@1445
   798
	  s++;
alpar@1445
   799
	}
alpar@1445
   800
      return s;
alpar@1445
   801
    }
alpar@1445
   802
#endif
alpar@1445
   803
alpar@1445
   804
    ///Set a row (i.e a constraint) of the LP
alpar@1253
   805
alpar@1258
   806
    ///\param r is the row to be modified
alpar@1259
   807
    ///\param l is lower bound (-\ref INF means no bound)
alpar@1258
   808
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   809
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1253
   810
    ///\bug This is a temportary function. The interface will change to
alpar@1253
   811
    ///a better one.
alpar@1328
   812
    ///\todo Option to control whether a constraint with a single variable is
alpar@1328
   813
    ///added or not.
alpar@1258
   814
    void setRow(Row r, Value l,const Expr &e, Value u) {
alpar@1253
   815
      std::vector<int> indices;
alpar@1253
   816
      std::vector<Value> values;
alpar@1253
   817
      indices.push_back(0);
alpar@1253
   818
      values.push_back(0);
alpar@1258
   819
      for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i)
alpar@1256
   820
	if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
alpar@1256
   821
	  indices.push_back(cols.floatingId((*i).first.id));
alpar@1256
   822
	  values.push_back((*i).second);
alpar@1256
   823
	}
alpar@1253
   824
      _setRowCoeffs(rows.floatingId(r.id),indices.size()-1,
alpar@1253
   825
		    &indices[0],&values[0]);
athos@1405
   826
//       _setRowLowerBound(rows.floatingId(r.id),l-e.constComp());
athos@1405
   827
//       _setRowUpperBound(rows.floatingId(r.id),u-e.constComp());
athos@1405
   828
       _setRowBounds(rows.floatingId(r.id),l-e.constComp(),u-e.constComp());
alpar@1258
   829
    }
alpar@1258
   830
alpar@1445
   831
    ///Set a row (i.e a constraint) of the LP
alpar@1264
   832
alpar@1264
   833
    ///\param r is the row to be modified
alpar@1264
   834
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   835
    void setRow(Row r, const Constr &c) {
alpar@1273
   836
      setRow(r,
alpar@1275
   837
	     c.lowerBounded()?c.lowerBound():-INF,
alpar@1273
   838
	     c.expr(),
alpar@1275
   839
	     c.upperBounded()?c.upperBound():INF);
alpar@1264
   840
    }
alpar@1264
   841
alpar@1445
   842
    ///Add a new row (i.e a new constraint) to the LP
alpar@1258
   843
alpar@1259
   844
    ///\param l is the lower bound (-\ref INF means no bound)
alpar@1258
   845
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   846
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1258
   847
    ///\return The created row.
alpar@1258
   848
    ///\bug This is a temportary function. The interface will change to
alpar@1258
   849
    ///a better one.
alpar@1258
   850
    Row addRow(Value l,const Expr &e, Value u) {
alpar@1258
   851
      Row r=addRow();
alpar@1258
   852
      setRow(r,l,e,u);
alpar@1253
   853
      return r;
alpar@1253
   854
    }
alpar@1253
   855
alpar@1445
   856
    ///Add a new row (i.e a new constraint) to the LP
alpar@1264
   857
alpar@1264
   858
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   859
    ///\return The created row.
alpar@1264
   860
    Row addRow(const Constr &c) {
alpar@1264
   861
      Row r=addRow();
alpar@1264
   862
      setRow(r,c);
alpar@1264
   863
      return r;
alpar@1264
   864
    }
athos@1542
   865
    ///Erase a coloumn (i.e a variable) from the LP
athos@1542
   866
athos@1542
   867
    ///\param c is the coloumn to be deleted
athos@1542
   868
    ///\todo Please check this
athos@1542
   869
    void eraseCol(Col c) {
athos@1542
   870
      _eraseCol(cols.floatingId(c.id));
athos@1542
   871
      cols.erase(c.id);
athos@1542
   872
    }
athos@1542
   873
    ///Erase a  row (i.e a constraint) from the LP
athos@1542
   874
athos@1542
   875
    ///\param r is the row to be deleted
athos@1542
   876
    ///\todo Please check this
athos@1542
   877
    void eraseRow(Row r) {
athos@1542
   878
      _eraseRow(rows.floatingId(r.id));
athos@1542
   879
      rows.erase(r.id);
athos@1542
   880
    }
alpar@1264
   881
athos@1436
   882
    ///Set an element of the coefficient matrix of the LP
athos@1436
   883
athos@1436
   884
    ///\param r is the row of the element to be modified
athos@1436
   885
    ///\param c is the coloumn of the element to be modified
athos@1436
   886
    ///\param val is the new value of the coefficient
athos@1436
   887
    void setCoeff(Row r, Col c, Value val){
athos@1436
   888
      _setCoeff(rows.floatingId(r.id),cols.floatingId(c.id), val);
athos@1436
   889
    }
athos@1436
   890
alpar@1253
   891
    /// Set the lower bound of a column (i.e a variable)
alpar@1253
   892
alpar@1293
   893
    /// The upper bound of a variable (column) has to be given by an 
alpar@1253
   894
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   895
    /// Value or -\ref INF.
alpar@1293
   896
    void colLowerBound(Col c, Value value) {
alpar@1253
   897
      _setColLowerBound(cols.floatingId(c.id),value);
alpar@1253
   898
    }
alpar@1253
   899
    /// Set the upper bound of a column (i.e a variable)
alpar@1253
   900
alpar@1293
   901
    /// The upper bound of a variable (column) has to be given by an 
alpar@1253
   902
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   903
    /// Value or \ref INF.
alpar@1293
   904
    void colUpperBound(Col c, Value value) {
alpar@1253
   905
      _setColUpperBound(cols.floatingId(c.id),value);
alpar@1253
   906
    };
alpar@1293
   907
    /// Set the lower and the upper bounds of a column (i.e a variable)
alpar@1293
   908
alpar@1293
   909
    /// The lower and the upper bounds of
alpar@1293
   910
    /// a variable (column) have to be given by an 
alpar@1293
   911
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
   912
    /// Value, -\ref INF or \ref INF.
alpar@1293
   913
    void colBounds(Col c, Value lower, Value upper) {
alpar@1293
   914
      _setColLowerBound(cols.floatingId(c.id),lower);
alpar@1293
   915
      _setColUpperBound(cols.floatingId(c.id),upper);
alpar@1293
   916
    }
alpar@1293
   917
    
athos@1405
   918
//     /// Set the lower bound of a row (i.e a constraint)
alpar@1253
   919
athos@1405
   920
//     /// The lower bound of a linear expression (row) has to be given by an 
athos@1405
   921
//     /// extended number of type Value, i.e. a finite number of type 
athos@1405
   922
//     /// Value or -\ref INF.
athos@1405
   923
//     void rowLowerBound(Row r, Value value) {
athos@1405
   924
//       _setRowLowerBound(rows.floatingId(r.id),value);
athos@1405
   925
//     };
athos@1405
   926
//     /// Set the upper bound of a row (i.e a constraint)
alpar@1253
   927
athos@1405
   928
//     /// The upper bound of a linear expression (row) has to be given by an 
athos@1405
   929
//     /// extended number of type Value, i.e. a finite number of type 
athos@1405
   930
//     /// Value or \ref INF.
athos@1405
   931
//     void rowUpperBound(Row r, Value value) {
athos@1405
   932
//       _setRowUpperBound(rows.floatingId(r.id),value);
athos@1405
   933
//     };
athos@1405
   934
athos@1405
   935
    /// Set the lower and the upper bounds of a row (i.e a constraint)
alpar@1293
   936
alpar@1293
   937
    /// The lower and the upper bounds of
alpar@1293
   938
    /// a constraint (row) have to be given by an 
alpar@1293
   939
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
   940
    /// Value, -\ref INF or \ref INF.
alpar@1293
   941
    void rowBounds(Row c, Value lower, Value upper) {
athos@1379
   942
      _setRowBounds(rows.floatingId(c.id),lower, upper);
athos@1379
   943
      // _setRowUpperBound(rows.floatingId(c.id),upper);
alpar@1293
   944
    }
alpar@1293
   945
    
alpar@1253
   946
    ///Set an element of the objective function
alpar@1293
   947
    void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); };
alpar@1253
   948
    ///Set the objective function
alpar@1253
   949
    
alpar@1253
   950
    ///\param e is a linear expression of type \ref Expr.
alpar@1323
   951
    ///\bug The previous objective function is not cleared!
alpar@1253
   952
    void setObj(Expr e) {
athos@1377
   953
      _clearObj();
alpar@1253
   954
      for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
alpar@1293
   955
	objCoeff((*i).first,(*i).second);
alpar@1323
   956
      obj_const_comp=e.constComp();
alpar@1253
   957
    }
alpar@1263
   958
alpar@1312
   959
    ///Maximize
alpar@1312
   960
    void max() { _setMax(); }
alpar@1312
   961
    ///Minimize
alpar@1312
   962
    void min() { _setMin(); }
alpar@1312
   963
alpar@1312
   964
    
alpar@1263
   965
    ///@}
alpar@1263
   966
alpar@1263
   967
alpar@1294
   968
    ///\name Solve the LP
alpar@1263
   969
alpar@1263
   970
    ///@{
alpar@1263
   971
athos@1458
   972
    ///\e Solve the LP problem at hand
athos@1458
   973
    ///
athos@1458
   974
    ///\return The result of the optimization procedure. Possible values and their meanings can be found in the documentation of \ref SolveExitStatus.
athos@1458
   975
    ///
athos@1458
   976
    ///\todo Which method is used to solve the problem
alpar@1303
   977
    SolveExitStatus solve() { return _solve(); }
alpar@1263
   978
    
alpar@1263
   979
    ///@}
alpar@1263
   980
    
alpar@1294
   981
    ///\name Obtain the solution
alpar@1263
   982
alpar@1263
   983
    ///@{
alpar@1263
   984
athos@1460
   985
    /// The status of the primal problem (the original LP problem)
alpar@1312
   986
    SolutionStatus primalStatus() {
alpar@1312
   987
      return _getPrimalStatus();
alpar@1294
   988
    }
alpar@1294
   989
athos@1460
   990
    /// The status of the dual (of the original LP) problem 
athos@1460
   991
    SolutionStatus dualStatus() {
athos@1460
   992
      return _getDualStatus();
athos@1460
   993
    }
athos@1460
   994
athos@1460
   995
    ///The type of the original LP problem
athos@1462
   996
    ProblemTypes problemType() {
athos@1460
   997
      return _getProblemType();
athos@1460
   998
    }
athos@1460
   999
alpar@1294
  1000
    ///\e
alpar@1293
  1001
    Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); }
alpar@1263
  1002
alpar@1312
  1003
    ///\e
alpar@1312
  1004
alpar@1312
  1005
    ///\return
alpar@1312
  1006
    ///- \ref INF or -\ref INF means either infeasibility or unboundedness
alpar@1312
  1007
    /// of the primal problem, depending on whether we minimize or maximize.
alpar@1364
  1008
    ///- \ref NaN if no primal solution is found.
alpar@1312
  1009
    ///- The (finite) objective value if an optimal solution is found.
alpar@1323
  1010
    Value primalValue() { return _getPrimalValue()+obj_const_comp;}
alpar@1263
  1011
    ///@}
alpar@1253
  1012
    
athos@1248
  1013
  };  
athos@1246
  1014
alpar@1272
  1015
  ///\e
alpar@1272
  1016
  
alpar@1272
  1017
  ///\relates LpSolverBase::Expr
alpar@1272
  1018
  ///
alpar@1272
  1019
  inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
alpar@1272
  1020
				      const LpSolverBase::Expr &b) 
alpar@1272
  1021
  {
alpar@1272
  1022
    LpSolverBase::Expr tmp(a);
alpar@1766
  1023
    tmp+=b;
alpar@1272
  1024
    return tmp;
alpar@1272
  1025
  }
alpar@1272
  1026
  ///\e
alpar@1272
  1027
  
alpar@1272
  1028
  ///\relates LpSolverBase::Expr
alpar@1272
  1029
  ///
alpar@1272
  1030
  inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
alpar@1272
  1031
				      const LpSolverBase::Expr &b) 
alpar@1272
  1032
  {
alpar@1272
  1033
    LpSolverBase::Expr tmp(a);
alpar@1766
  1034
    tmp-=b;
alpar@1272
  1035
    return tmp;
alpar@1272
  1036
  }
alpar@1272
  1037
  ///\e
alpar@1272
  1038
  
alpar@1272
  1039
  ///\relates LpSolverBase::Expr
alpar@1272
  1040
  ///
alpar@1272
  1041
  inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
alpar@1273
  1042
				      const LpSolverBase::Value &b) 
alpar@1272
  1043
  {
alpar@1272
  1044
    LpSolverBase::Expr tmp(a);
alpar@1766
  1045
    tmp*=b;
alpar@1272
  1046
    return tmp;
alpar@1272
  1047
  }
alpar@1272
  1048
  
alpar@1272
  1049
  ///\e
alpar@1272
  1050
  
alpar@1272
  1051
  ///\relates LpSolverBase::Expr
alpar@1272
  1052
  ///
alpar@1273
  1053
  inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
alpar@1272
  1054
				      const LpSolverBase::Expr &b) 
alpar@1272
  1055
  {
alpar@1272
  1056
    LpSolverBase::Expr tmp(b);
alpar@1766
  1057
    tmp*=a;
alpar@1272
  1058
    return tmp;
alpar@1272
  1059
  }
alpar@1272
  1060
  ///\e
alpar@1272
  1061
  
alpar@1272
  1062
  ///\relates LpSolverBase::Expr
alpar@1272
  1063
  ///
alpar@1272
  1064
  inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
alpar@1273
  1065
				      const LpSolverBase::Value &b) 
alpar@1272
  1066
  {
alpar@1272
  1067
    LpSolverBase::Expr tmp(a);
alpar@1766
  1068
    tmp/=b;
alpar@1272
  1069
    return tmp;
alpar@1272
  1070
  }
alpar@1272
  1071
  
alpar@1272
  1072
  ///\e
alpar@1272
  1073
  
alpar@1272
  1074
  ///\relates LpSolverBase::Constr
alpar@1272
  1075
  ///
alpar@1272
  1076
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1272
  1077
					 const LpSolverBase::Expr &f) 
alpar@1272
  1078
  {
alpar@1272
  1079
    return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
alpar@1272
  1080
  }
alpar@1272
  1081
alpar@1272
  1082
  ///\e
alpar@1272
  1083
  
alpar@1272
  1084
  ///\relates LpSolverBase::Constr
alpar@1272
  1085
  ///
alpar@1273
  1086
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
alpar@1272
  1087
					 const LpSolverBase::Expr &f) 
alpar@1272
  1088
  {
alpar@1272
  1089
    return LpSolverBase::Constr(e,f);
alpar@1272
  1090
  }
alpar@1272
  1091
alpar@1272
  1092
  ///\e
alpar@1272
  1093
  
alpar@1272
  1094
  ///\relates LpSolverBase::Constr
alpar@1272
  1095
  ///
alpar@1272
  1096
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1273
  1097
					 const LpSolverBase::Value &f) 
alpar@1272
  1098
  {
alpar@1272
  1099
    return LpSolverBase::Constr(e,f);
alpar@1272
  1100
  }
alpar@1272
  1101
alpar@1272
  1102
  ///\e
alpar@1272
  1103
  
alpar@1272
  1104
  ///\relates LpSolverBase::Constr
alpar@1272
  1105
  ///
alpar@1272
  1106
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1272
  1107
					 const LpSolverBase::Expr &f) 
alpar@1272
  1108
  {
alpar@1272
  1109
    return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
alpar@1272
  1110
  }
alpar@1272
  1111
alpar@1272
  1112
alpar@1272
  1113
  ///\e
alpar@1272
  1114
  
alpar@1272
  1115
  ///\relates LpSolverBase::Constr
alpar@1272
  1116
  ///
alpar@1273
  1117
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
alpar@1272
  1118
					 const LpSolverBase::Expr &f) 
alpar@1272
  1119
  {
alpar@1272
  1120
    return LpSolverBase::Constr(f,e);
alpar@1272
  1121
  }
alpar@1272
  1122
alpar@1272
  1123
alpar@1272
  1124
  ///\e
alpar@1272
  1125
  
alpar@1272
  1126
  ///\relates LpSolverBase::Constr
alpar@1272
  1127
  ///
alpar@1272
  1128
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1273
  1129
					 const LpSolverBase::Value &f) 
alpar@1272
  1130
  {
alpar@1272
  1131
    return LpSolverBase::Constr(f,e);
alpar@1272
  1132
  }
alpar@1272
  1133
alpar@1272
  1134
  ///\e
alpar@1272
  1135
  
alpar@1272
  1136
  ///\relates LpSolverBase::Constr
alpar@1272
  1137
  ///
alpar@1272
  1138
  inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
alpar@1272
  1139
					 const LpSolverBase::Expr &f) 
alpar@1272
  1140
  {
alpar@1272
  1141
    return LpSolverBase::Constr(0,e-f,0);
alpar@1272
  1142
  }
alpar@1272
  1143
alpar@1272
  1144
  ///\e
alpar@1272
  1145
  
alpar@1272
  1146
  ///\relates LpSolverBase::Constr
alpar@1272
  1147
  ///
alpar@1273
  1148
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
alpar@1272
  1149
					 const LpSolverBase::Constr&c) 
alpar@1272
  1150
  {
alpar@1272
  1151
    LpSolverBase::Constr tmp(c);
alpar@1273
  1152
    ///\todo Create an own exception type.
alpar@1273
  1153
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
  1154
    else tmp.lowerBound()=n;
alpar@1272
  1155
    return tmp;
alpar@1272
  1156
  }
alpar@1272
  1157
  ///\e
alpar@1272
  1158
  
alpar@1272
  1159
  ///\relates LpSolverBase::Constr
alpar@1272
  1160
  ///
alpar@1272
  1161
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
alpar@1273
  1162
					 const LpSolverBase::Value &n)
alpar@1272
  1163
  {
alpar@1272
  1164
    LpSolverBase::Constr tmp(c);
alpar@1273
  1165
    ///\todo Create an own exception type.
alpar@1273
  1166
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
  1167
    else tmp.upperBound()=n;
alpar@1272
  1168
    return tmp;
alpar@1272
  1169
  }
alpar@1272
  1170
alpar@1272
  1171
  ///\e
alpar@1272
  1172
  
alpar@1272
  1173
  ///\relates LpSolverBase::Constr
alpar@1272
  1174
  ///
alpar@1273
  1175
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
alpar@1272
  1176
					 const LpSolverBase::Constr&c) 
alpar@1272
  1177
  {
alpar@1272
  1178
    LpSolverBase::Constr tmp(c);
alpar@1273
  1179
    ///\todo Create an own exception type.
alpar@1273
  1180
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
  1181
    else tmp.upperBound()=n;
alpar@1272
  1182
    return tmp;
alpar@1272
  1183
  }
alpar@1272
  1184
  ///\e
alpar@1272
  1185
  
alpar@1272
  1186
  ///\relates LpSolverBase::Constr
alpar@1272
  1187
  ///
alpar@1272
  1188
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
alpar@1273
  1189
					 const LpSolverBase::Value &n)
alpar@1272
  1190
  {
alpar@1272
  1191
    LpSolverBase::Constr tmp(c);
alpar@1273
  1192
    ///\todo Create an own exception type.
alpar@1273
  1193
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
  1194
    else tmp.lowerBound()=n;
alpar@1272
  1195
    return tmp;
alpar@1272
  1196
  }
alpar@1272
  1197
alpar@1445
  1198
  ///\e
alpar@1445
  1199
  
alpar@1445
  1200
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1201
  ///
alpar@1445
  1202
  inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a,
alpar@1445
  1203
				      const LpSolverBase::DualExpr &b) 
alpar@1445
  1204
  {
alpar@1445
  1205
    LpSolverBase::DualExpr tmp(a);
alpar@1766
  1206
    tmp+=b;
alpar@1445
  1207
    return tmp;
alpar@1445
  1208
  }
alpar@1445
  1209
  ///\e
alpar@1445
  1210
  
alpar@1445
  1211
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1212
  ///
alpar@1445
  1213
  inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a,
alpar@1445
  1214
				      const LpSolverBase::DualExpr &b) 
alpar@1445
  1215
  {
alpar@1445
  1216
    LpSolverBase::DualExpr tmp(a);
alpar@1766
  1217
    tmp-=b;
alpar@1445
  1218
    return tmp;
alpar@1445
  1219
  }
alpar@1445
  1220
  ///\e
alpar@1445
  1221
  
alpar@1445
  1222
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1223
  ///
alpar@1445
  1224
  inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a,
alpar@1445
  1225
				      const LpSolverBase::Value &b) 
alpar@1445
  1226
  {
alpar@1445
  1227
    LpSolverBase::DualExpr tmp(a);
alpar@1766
  1228
    tmp*=b;
alpar@1445
  1229
    return tmp;
alpar@1445
  1230
  }
alpar@1445
  1231
  
alpar@1445
  1232
  ///\e
alpar@1445
  1233
  
alpar@1445
  1234
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1235
  ///
alpar@1445
  1236
  inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a,
alpar@1445
  1237
				      const LpSolverBase::DualExpr &b) 
alpar@1445
  1238
  {
alpar@1445
  1239
    LpSolverBase::DualExpr tmp(b);
alpar@1766
  1240
    tmp*=a;
alpar@1445
  1241
    return tmp;
alpar@1445
  1242
  }
alpar@1445
  1243
  ///\e
alpar@1445
  1244
  
alpar@1445
  1245
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1246
  ///
alpar@1445
  1247
  inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a,
alpar@1445
  1248
				      const LpSolverBase::Value &b) 
alpar@1445
  1249
  {
alpar@1445
  1250
    LpSolverBase::DualExpr tmp(a);
alpar@1766
  1251
    tmp/=b;
alpar@1445
  1252
    return tmp;
alpar@1445
  1253
  }
alpar@1445
  1254
  
alpar@1272
  1255
athos@1246
  1256
} //namespace lemon
athos@1246
  1257
athos@1246
  1258
#endif //LEMON_LP_BASE_H