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