lemon/lp_base.h
author hegyi
Tue, 03 Jan 2006 17:30:22 +0000
changeset 1871 3905d347112c
parent 1810 474d093466a5
child 1874 396831fa7012
permissions -rw-r--r--
Coding of Algorithms has begun, but code is really-really ugly yet.
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;
marci@1840
   598
    virtual bool _isBasicCol(int i) = 0;
alpar@1312
   599
    virtual SolutionStatus _getPrimalStatus() = 0;
athos@1460
   600
    virtual SolutionStatus _getDualStatus() = 0;
athos@1460
   601
    ///\todo This could be implemented here, too, using _getPrimalStatus() and
athos@1460
   602
    ///_getDualStatus()
athos@1460
   603
    virtual ProblemTypes _getProblemType() = 0;
athos@1460
   604
alpar@1312
   605
    virtual void _setMax() = 0;
alpar@1312
   606
    virtual void _setMin() = 0;
alpar@1312
   607
    
alpar@1323
   608
    //Own protected stuff
alpar@1323
   609
    
alpar@1323
   610
    //Constant component of the objective function
alpar@1323
   611
    Value obj_const_comp;
alpar@1323
   612
    
athos@1377
   613
athos@1377
   614
alpar@1323
   615
    
alpar@1253
   616
  public:
alpar@1253
   617
alpar@1323
   618
    ///\e
alpar@1323
   619
    LpSolverBase() : obj_const_comp(0) {}
alpar@1253
   620
alpar@1253
   621
    ///\e
alpar@1253
   622
    virtual ~LpSolverBase() {}
alpar@1253
   623
alpar@1364
   624
    ///Creates a new LP problem
alpar@1364
   625
    LpSolverBase &newLp() {return _newLp();}
alpar@1381
   626
    ///Makes a copy of the LP problem
alpar@1364
   627
    LpSolverBase &copyLp() {return _copyLp();}
alpar@1364
   628
    
alpar@1612
   629
    ///\name Build up and modify the LP
alpar@1263
   630
alpar@1263
   631
    ///@{
alpar@1263
   632
alpar@1253
   633
    ///Add a new empty column (i.e a new variable) to the LP
alpar@1253
   634
    Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
alpar@1263
   635
alpar@1294
   636
    ///\brief Adds several new columns
alpar@1294
   637
    ///(i.e a variables) at once
alpar@1256
   638
    ///
alpar@1273
   639
    ///This magic function takes a container as its argument
alpar@1256
   640
    ///and fills its elements
alpar@1256
   641
    ///with new columns (i.e. variables)
alpar@1273
   642
    ///\param t can be
alpar@1273
   643
    ///- a standard STL compatible iterable container with
alpar@1273
   644
    ///\ref Col as its \c values_type
alpar@1273
   645
    ///like
alpar@1273
   646
    ///\code
alpar@1273
   647
    ///std::vector<LpSolverBase::Col>
alpar@1273
   648
    ///std::list<LpSolverBase::Col>
alpar@1273
   649
    ///\endcode
alpar@1273
   650
    ///- a standard STL compatible iterable container with
alpar@1273
   651
    ///\ref Col as its \c mapped_type
alpar@1273
   652
    ///like
alpar@1273
   653
    ///\code
alpar@1364
   654
    ///std::map<AnyType,LpSolverBase::Col>
alpar@1273
   655
    ///\endcode
alpar@1273
   656
    ///- an iterable lemon \ref concept::WriteMap "write map" like 
alpar@1273
   657
    ///\code
alpar@1273
   658
    ///ListGraph::NodeMap<LpSolverBase::Col>
alpar@1273
   659
    ///ListGraph::EdgeMap<LpSolverBase::Col>
alpar@1273
   660
    ///\endcode
alpar@1256
   661
    ///\return The number of the created column.
alpar@1256
   662
#ifdef DOXYGEN
alpar@1256
   663
    template<class T>
alpar@1256
   664
    int addColSet(T &t) { return 0;} 
alpar@1256
   665
#else
alpar@1256
   666
    template<class T>
alpar@1256
   667
    typename enable_if<typename T::value_type::LpSolverCol,int>::type
alpar@1256
   668
    addColSet(T &t,dummy<0> = 0) {
alpar@1256
   669
      int s=0;
alpar@1256
   670
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
alpar@1256
   671
      return s;
alpar@1256
   672
    }
alpar@1256
   673
    template<class T>
alpar@1256
   674
    typename enable_if<typename T::value_type::second_type::LpSolverCol,
alpar@1256
   675
		       int>::type
alpar@1256
   676
    addColSet(T &t,dummy<1> = 1) { 
alpar@1256
   677
      int s=0;
alpar@1256
   678
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1256
   679
	i->second=addCol();
alpar@1256
   680
	s++;
alpar@1256
   681
      }
alpar@1256
   682
      return s;
alpar@1256
   683
    }
alpar@1272
   684
    template<class T>
deba@1810
   685
    typename enable_if<typename T::MapIt::Value::LpSolverCol,
alpar@1272
   686
		       int>::type
alpar@1272
   687
    addColSet(T &t,dummy<2> = 2) { 
alpar@1272
   688
      int s=0;
deba@1810
   689
      for(typename T::MapIt i(t); i!=INVALID; ++i)
alpar@1272
   690
	{
deba@1810
   691
	  i.set(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!!!
marci@1787
   711
	  indices.push_back(rows.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>
deba@1810
   788
    typename enable_if<typename T::MapIt::Value::LpSolverRow,
alpar@1445
   789
		       int>::type
alpar@1445
   790
    addRowSet(T &t,dummy<2> = 2) { 
alpar@1445
   791
      int s=0;
deba@1810
   792
      for(typename T::MapIt i(t); i!=INVALID; ++i)
alpar@1445
   793
	{
deba@1810
   794
	  i.set(addRow());
alpar@1445
   795
	  s++;
alpar@1445
   796
	}
alpar@1445
   797
      return s;
alpar@1445
   798
    }
alpar@1445
   799
#endif
alpar@1445
   800
alpar@1445
   801
    ///Set a row (i.e a constraint) of the LP
alpar@1253
   802
alpar@1258
   803
    ///\param r is the row to be modified
alpar@1259
   804
    ///\param l is lower bound (-\ref INF means no bound)
alpar@1258
   805
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   806
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1253
   807
    ///\bug This is a temportary function. The interface will change to
alpar@1253
   808
    ///a better one.
alpar@1328
   809
    ///\todo Option to control whether a constraint with a single variable is
alpar@1328
   810
    ///added or not.
alpar@1258
   811
    void setRow(Row r, Value l,const Expr &e, Value u) {
alpar@1253
   812
      std::vector<int> indices;
alpar@1253
   813
      std::vector<Value> values;
alpar@1253
   814
      indices.push_back(0);
alpar@1253
   815
      values.push_back(0);
alpar@1258
   816
      for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i)
alpar@1256
   817
	if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
alpar@1256
   818
	  indices.push_back(cols.floatingId((*i).first.id));
alpar@1256
   819
	  values.push_back((*i).second);
alpar@1256
   820
	}
alpar@1253
   821
      _setRowCoeffs(rows.floatingId(r.id),indices.size()-1,
alpar@1253
   822
		    &indices[0],&values[0]);
athos@1405
   823
//       _setRowLowerBound(rows.floatingId(r.id),l-e.constComp());
athos@1405
   824
//       _setRowUpperBound(rows.floatingId(r.id),u-e.constComp());
athos@1405
   825
       _setRowBounds(rows.floatingId(r.id),l-e.constComp(),u-e.constComp());
alpar@1258
   826
    }
alpar@1258
   827
alpar@1445
   828
    ///Set a row (i.e a constraint) of the LP
alpar@1264
   829
alpar@1264
   830
    ///\param r is the row to be modified
alpar@1264
   831
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   832
    void setRow(Row r, const Constr &c) {
alpar@1273
   833
      setRow(r,
alpar@1275
   834
	     c.lowerBounded()?c.lowerBound():-INF,
alpar@1273
   835
	     c.expr(),
alpar@1275
   836
	     c.upperBounded()?c.upperBound():INF);
alpar@1264
   837
    }
alpar@1264
   838
alpar@1445
   839
    ///Add a new row (i.e a new constraint) to the LP
alpar@1258
   840
alpar@1259
   841
    ///\param l is the lower bound (-\ref INF means no bound)
alpar@1258
   842
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   843
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1258
   844
    ///\return The created row.
alpar@1258
   845
    ///\bug This is a temportary function. The interface will change to
alpar@1258
   846
    ///a better one.
alpar@1258
   847
    Row addRow(Value l,const Expr &e, Value u) {
alpar@1258
   848
      Row r=addRow();
alpar@1258
   849
      setRow(r,l,e,u);
alpar@1253
   850
      return r;
alpar@1253
   851
    }
alpar@1253
   852
alpar@1445
   853
    ///Add a new row (i.e a new constraint) to the LP
alpar@1264
   854
alpar@1264
   855
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   856
    ///\return The created row.
alpar@1264
   857
    Row addRow(const Constr &c) {
alpar@1264
   858
      Row r=addRow();
alpar@1264
   859
      setRow(r,c);
alpar@1264
   860
      return r;
alpar@1264
   861
    }
athos@1542
   862
    ///Erase a coloumn (i.e a variable) from the LP
athos@1542
   863
athos@1542
   864
    ///\param c is the coloumn to be deleted
athos@1542
   865
    ///\todo Please check this
athos@1542
   866
    void eraseCol(Col c) {
athos@1542
   867
      _eraseCol(cols.floatingId(c.id));
athos@1542
   868
      cols.erase(c.id);
athos@1542
   869
    }
athos@1542
   870
    ///Erase a  row (i.e a constraint) from the LP
athos@1542
   871
athos@1542
   872
    ///\param r is the row to be deleted
athos@1542
   873
    ///\todo Please check this
athos@1542
   874
    void eraseRow(Row r) {
athos@1542
   875
      _eraseRow(rows.floatingId(r.id));
athos@1542
   876
      rows.erase(r.id);
athos@1542
   877
    }
alpar@1264
   878
athos@1436
   879
    ///Set an element of the coefficient matrix of the LP
athos@1436
   880
athos@1436
   881
    ///\param r is the row of the element to be modified
athos@1436
   882
    ///\param c is the coloumn of the element to be modified
athos@1436
   883
    ///\param val is the new value of the coefficient
athos@1436
   884
    void setCoeff(Row r, Col c, Value val){
athos@1436
   885
      _setCoeff(rows.floatingId(r.id),cols.floatingId(c.id), val);
athos@1436
   886
    }
athos@1436
   887
alpar@1253
   888
    /// Set the lower bound of a column (i.e a variable)
alpar@1253
   889
alpar@1293
   890
    /// The upper bound of a variable (column) has to be given by an 
alpar@1253
   891
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   892
    /// Value or -\ref INF.
alpar@1293
   893
    void colLowerBound(Col c, Value value) {
alpar@1253
   894
      _setColLowerBound(cols.floatingId(c.id),value);
alpar@1253
   895
    }
alpar@1253
   896
    /// Set the upper bound of a column (i.e a variable)
alpar@1253
   897
alpar@1293
   898
    /// The upper bound of a variable (column) has to be given by an 
alpar@1253
   899
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   900
    /// Value or \ref INF.
alpar@1293
   901
    void colUpperBound(Col c, Value value) {
alpar@1253
   902
      _setColUpperBound(cols.floatingId(c.id),value);
alpar@1253
   903
    };
alpar@1293
   904
    /// Set the lower and the upper bounds of a column (i.e a variable)
alpar@1293
   905
alpar@1293
   906
    /// The lower and the upper bounds of
alpar@1293
   907
    /// a variable (column) have to be given by an 
alpar@1293
   908
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
   909
    /// Value, -\ref INF or \ref INF.
alpar@1293
   910
    void colBounds(Col c, Value lower, Value upper) {
alpar@1293
   911
      _setColLowerBound(cols.floatingId(c.id),lower);
alpar@1293
   912
      _setColUpperBound(cols.floatingId(c.id),upper);
alpar@1293
   913
    }
alpar@1293
   914
    
athos@1405
   915
//     /// Set the lower bound of a row (i.e a constraint)
alpar@1253
   916
athos@1405
   917
//     /// The lower bound of a linear expression (row) has to be given by an 
athos@1405
   918
//     /// extended number of type Value, i.e. a finite number of type 
athos@1405
   919
//     /// Value or -\ref INF.
athos@1405
   920
//     void rowLowerBound(Row r, Value value) {
athos@1405
   921
//       _setRowLowerBound(rows.floatingId(r.id),value);
athos@1405
   922
//     };
athos@1405
   923
//     /// Set the upper bound of a row (i.e a constraint)
alpar@1253
   924
athos@1405
   925
//     /// The upper bound of a linear expression (row) has to be given by an 
athos@1405
   926
//     /// extended number of type Value, i.e. a finite number of type 
athos@1405
   927
//     /// Value or \ref INF.
athos@1405
   928
//     void rowUpperBound(Row r, Value value) {
athos@1405
   929
//       _setRowUpperBound(rows.floatingId(r.id),value);
athos@1405
   930
//     };
athos@1405
   931
athos@1405
   932
    /// Set the lower and the upper bounds of a row (i.e a constraint)
alpar@1293
   933
alpar@1293
   934
    /// The lower and the upper bounds of
alpar@1293
   935
    /// a constraint (row) have to be given by an 
alpar@1293
   936
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
   937
    /// Value, -\ref INF or \ref INF.
alpar@1293
   938
    void rowBounds(Row c, Value lower, Value upper) {
athos@1379
   939
      _setRowBounds(rows.floatingId(c.id),lower, upper);
athos@1379
   940
      // _setRowUpperBound(rows.floatingId(c.id),upper);
alpar@1293
   941
    }
alpar@1293
   942
    
alpar@1253
   943
    ///Set an element of the objective function
alpar@1293
   944
    void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); };
alpar@1253
   945
    ///Set the objective function
alpar@1253
   946
    
alpar@1253
   947
    ///\param e is a linear expression of type \ref Expr.
alpar@1323
   948
    ///\bug The previous objective function is not cleared!
alpar@1253
   949
    void setObj(Expr e) {
athos@1377
   950
      _clearObj();
alpar@1253
   951
      for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
alpar@1293
   952
	objCoeff((*i).first,(*i).second);
alpar@1323
   953
      obj_const_comp=e.constComp();
alpar@1253
   954
    }
alpar@1263
   955
alpar@1312
   956
    ///Maximize
alpar@1312
   957
    void max() { _setMax(); }
alpar@1312
   958
    ///Minimize
alpar@1312
   959
    void min() { _setMin(); }
alpar@1312
   960
alpar@1312
   961
    
alpar@1263
   962
    ///@}
alpar@1263
   963
alpar@1263
   964
alpar@1294
   965
    ///\name Solve the LP
alpar@1263
   966
alpar@1263
   967
    ///@{
alpar@1263
   968
athos@1458
   969
    ///\e Solve the LP problem at hand
athos@1458
   970
    ///
athos@1458
   971
    ///\return The result of the optimization procedure. Possible values and their meanings can be found in the documentation of \ref SolveExitStatus.
athos@1458
   972
    ///
athos@1458
   973
    ///\todo Which method is used to solve the problem
alpar@1303
   974
    SolveExitStatus solve() { return _solve(); }
alpar@1263
   975
    
alpar@1263
   976
    ///@}
alpar@1263
   977
    
alpar@1294
   978
    ///\name Obtain the solution
alpar@1263
   979
alpar@1263
   980
    ///@{
alpar@1263
   981
athos@1460
   982
    /// The status of the primal problem (the original LP problem)
alpar@1312
   983
    SolutionStatus primalStatus() {
alpar@1312
   984
      return _getPrimalStatus();
alpar@1294
   985
    }
alpar@1294
   986
athos@1460
   987
    /// The status of the dual (of the original LP) problem 
athos@1460
   988
    SolutionStatus dualStatus() {
athos@1460
   989
      return _getDualStatus();
athos@1460
   990
    }
athos@1460
   991
athos@1460
   992
    ///The type of the original LP problem
athos@1462
   993
    ProblemTypes problemType() {
athos@1460
   994
      return _getProblemType();
athos@1460
   995
    }
athos@1460
   996
alpar@1294
   997
    ///\e
alpar@1293
   998
    Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); }
alpar@1263
   999
alpar@1312
  1000
    ///\e
marci@1787
  1001
    Value dual(Row r) { return _getDual(rows.floatingId(r.id)); }
marci@1787
  1002
marci@1787
  1003
    ///\e
marci@1840
  1004
    bool isBasicCol(Col c) { return _isBasicCol(cols.floatingId(c.id)); }
marci@1840
  1005
marci@1840
  1006
    ///\e
alpar@1312
  1007
alpar@1312
  1008
    ///\return
alpar@1312
  1009
    ///- \ref INF or -\ref INF means either infeasibility or unboundedness
alpar@1312
  1010
    /// of the primal problem, depending on whether we minimize or maximize.
alpar@1364
  1011
    ///- \ref NaN if no primal solution is found.
alpar@1312
  1012
    ///- The (finite) objective value if an optimal solution is found.
alpar@1323
  1013
    Value primalValue() { return _getPrimalValue()+obj_const_comp;}
alpar@1263
  1014
    ///@}
alpar@1253
  1015
    
athos@1248
  1016
  };  
athos@1246
  1017
alpar@1272
  1018
  ///\e
alpar@1272
  1019
  
alpar@1272
  1020
  ///\relates LpSolverBase::Expr
alpar@1272
  1021
  ///
alpar@1272
  1022
  inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
alpar@1272
  1023
				      const LpSolverBase::Expr &b) 
alpar@1272
  1024
  {
alpar@1272
  1025
    LpSolverBase::Expr tmp(a);
alpar@1766
  1026
    tmp+=b;
alpar@1272
  1027
    return tmp;
alpar@1272
  1028
  }
alpar@1272
  1029
  ///\e
alpar@1272
  1030
  
alpar@1272
  1031
  ///\relates LpSolverBase::Expr
alpar@1272
  1032
  ///
alpar@1272
  1033
  inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
alpar@1272
  1034
				      const LpSolverBase::Expr &b) 
alpar@1272
  1035
  {
alpar@1272
  1036
    LpSolverBase::Expr tmp(a);
alpar@1766
  1037
    tmp-=b;
alpar@1272
  1038
    return tmp;
alpar@1272
  1039
  }
alpar@1272
  1040
  ///\e
alpar@1272
  1041
  
alpar@1272
  1042
  ///\relates LpSolverBase::Expr
alpar@1272
  1043
  ///
alpar@1272
  1044
  inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
alpar@1273
  1045
				      const LpSolverBase::Value &b) 
alpar@1272
  1046
  {
alpar@1272
  1047
    LpSolverBase::Expr tmp(a);
alpar@1766
  1048
    tmp*=b;
alpar@1272
  1049
    return tmp;
alpar@1272
  1050
  }
alpar@1272
  1051
  
alpar@1272
  1052
  ///\e
alpar@1272
  1053
  
alpar@1272
  1054
  ///\relates LpSolverBase::Expr
alpar@1272
  1055
  ///
alpar@1273
  1056
  inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
alpar@1272
  1057
				      const LpSolverBase::Expr &b) 
alpar@1272
  1058
  {
alpar@1272
  1059
    LpSolverBase::Expr tmp(b);
alpar@1766
  1060
    tmp*=a;
alpar@1272
  1061
    return tmp;
alpar@1272
  1062
  }
alpar@1272
  1063
  ///\e
alpar@1272
  1064
  
alpar@1272
  1065
  ///\relates LpSolverBase::Expr
alpar@1272
  1066
  ///
alpar@1272
  1067
  inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
alpar@1273
  1068
				      const LpSolverBase::Value &b) 
alpar@1272
  1069
  {
alpar@1272
  1070
    LpSolverBase::Expr tmp(a);
alpar@1766
  1071
    tmp/=b;
alpar@1272
  1072
    return tmp;
alpar@1272
  1073
  }
alpar@1272
  1074
  
alpar@1272
  1075
  ///\e
alpar@1272
  1076
  
alpar@1272
  1077
  ///\relates LpSolverBase::Constr
alpar@1272
  1078
  ///
alpar@1272
  1079
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1272
  1080
					 const LpSolverBase::Expr &f) 
alpar@1272
  1081
  {
alpar@1272
  1082
    return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
alpar@1272
  1083
  }
alpar@1272
  1084
alpar@1272
  1085
  ///\e
alpar@1272
  1086
  
alpar@1272
  1087
  ///\relates LpSolverBase::Constr
alpar@1272
  1088
  ///
alpar@1273
  1089
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
alpar@1272
  1090
					 const LpSolverBase::Expr &f) 
alpar@1272
  1091
  {
alpar@1272
  1092
    return LpSolverBase::Constr(e,f);
alpar@1272
  1093
  }
alpar@1272
  1094
alpar@1272
  1095
  ///\e
alpar@1272
  1096
  
alpar@1272
  1097
  ///\relates LpSolverBase::Constr
alpar@1272
  1098
  ///
alpar@1272
  1099
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1273
  1100
					 const LpSolverBase::Value &f) 
alpar@1272
  1101
  {
alpar@1272
  1102
    return LpSolverBase::Constr(e,f);
alpar@1272
  1103
  }
alpar@1272
  1104
alpar@1272
  1105
  ///\e
alpar@1272
  1106
  
alpar@1272
  1107
  ///\relates LpSolverBase::Constr
alpar@1272
  1108
  ///
alpar@1272
  1109
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1272
  1110
					 const LpSolverBase::Expr &f) 
alpar@1272
  1111
  {
alpar@1272
  1112
    return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
alpar@1272
  1113
  }
alpar@1272
  1114
alpar@1272
  1115
alpar@1272
  1116
  ///\e
alpar@1272
  1117
  
alpar@1272
  1118
  ///\relates LpSolverBase::Constr
alpar@1272
  1119
  ///
alpar@1273
  1120
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
alpar@1272
  1121
					 const LpSolverBase::Expr &f) 
alpar@1272
  1122
  {
alpar@1272
  1123
    return LpSolverBase::Constr(f,e);
alpar@1272
  1124
  }
alpar@1272
  1125
alpar@1272
  1126
alpar@1272
  1127
  ///\e
alpar@1272
  1128
  
alpar@1272
  1129
  ///\relates LpSolverBase::Constr
alpar@1272
  1130
  ///
alpar@1272
  1131
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1273
  1132
					 const LpSolverBase::Value &f) 
alpar@1272
  1133
  {
alpar@1272
  1134
    return LpSolverBase::Constr(f,e);
alpar@1272
  1135
  }
alpar@1272
  1136
alpar@1272
  1137
  ///\e
alpar@1272
  1138
  
alpar@1272
  1139
  ///\relates LpSolverBase::Constr
alpar@1272
  1140
  ///
alpar@1272
  1141
  inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
alpar@1272
  1142
					 const LpSolverBase::Expr &f) 
alpar@1272
  1143
  {
alpar@1272
  1144
    return LpSolverBase::Constr(0,e-f,0);
alpar@1272
  1145
  }
alpar@1272
  1146
alpar@1272
  1147
  ///\e
alpar@1272
  1148
  
alpar@1272
  1149
  ///\relates LpSolverBase::Constr
alpar@1272
  1150
  ///
alpar@1273
  1151
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
alpar@1272
  1152
					 const LpSolverBase::Constr&c) 
alpar@1272
  1153
  {
alpar@1272
  1154
    LpSolverBase::Constr tmp(c);
alpar@1273
  1155
    ///\todo Create an own exception type.
alpar@1273
  1156
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
  1157
    else tmp.lowerBound()=n;
alpar@1272
  1158
    return tmp;
alpar@1272
  1159
  }
alpar@1272
  1160
  ///\e
alpar@1272
  1161
  
alpar@1272
  1162
  ///\relates LpSolverBase::Constr
alpar@1272
  1163
  ///
alpar@1272
  1164
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
alpar@1273
  1165
					 const LpSolverBase::Value &n)
alpar@1272
  1166
  {
alpar@1272
  1167
    LpSolverBase::Constr tmp(c);
alpar@1273
  1168
    ///\todo Create an own exception type.
alpar@1273
  1169
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
  1170
    else tmp.upperBound()=n;
alpar@1272
  1171
    return tmp;
alpar@1272
  1172
  }
alpar@1272
  1173
alpar@1272
  1174
  ///\e
alpar@1272
  1175
  
alpar@1272
  1176
  ///\relates LpSolverBase::Constr
alpar@1272
  1177
  ///
alpar@1273
  1178
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
alpar@1272
  1179
					 const LpSolverBase::Constr&c) 
alpar@1272
  1180
  {
alpar@1272
  1181
    LpSolverBase::Constr tmp(c);
alpar@1273
  1182
    ///\todo Create an own exception type.
alpar@1273
  1183
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
  1184
    else tmp.upperBound()=n;
alpar@1272
  1185
    return tmp;
alpar@1272
  1186
  }
alpar@1272
  1187
  ///\e
alpar@1272
  1188
  
alpar@1272
  1189
  ///\relates LpSolverBase::Constr
alpar@1272
  1190
  ///
alpar@1272
  1191
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
alpar@1273
  1192
					 const LpSolverBase::Value &n)
alpar@1272
  1193
  {
alpar@1272
  1194
    LpSolverBase::Constr tmp(c);
alpar@1273
  1195
    ///\todo Create an own exception type.
alpar@1273
  1196
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
  1197
    else tmp.lowerBound()=n;
alpar@1272
  1198
    return tmp;
alpar@1272
  1199
  }
alpar@1272
  1200
alpar@1445
  1201
  ///\e
alpar@1445
  1202
  
alpar@1445
  1203
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1204
  ///
alpar@1445
  1205
  inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a,
alpar@1445
  1206
				      const LpSolverBase::DualExpr &b) 
alpar@1445
  1207
  {
alpar@1445
  1208
    LpSolverBase::DualExpr tmp(a);
alpar@1766
  1209
    tmp+=b;
alpar@1445
  1210
    return tmp;
alpar@1445
  1211
  }
alpar@1445
  1212
  ///\e
alpar@1445
  1213
  
alpar@1445
  1214
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1215
  ///
alpar@1445
  1216
  inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a,
alpar@1445
  1217
				      const LpSolverBase::DualExpr &b) 
alpar@1445
  1218
  {
alpar@1445
  1219
    LpSolverBase::DualExpr tmp(a);
alpar@1766
  1220
    tmp-=b;
alpar@1445
  1221
    return tmp;
alpar@1445
  1222
  }
alpar@1445
  1223
  ///\e
alpar@1445
  1224
  
alpar@1445
  1225
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1226
  ///
alpar@1445
  1227
  inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a,
alpar@1445
  1228
				      const LpSolverBase::Value &b) 
alpar@1445
  1229
  {
alpar@1445
  1230
    LpSolverBase::DualExpr tmp(a);
alpar@1766
  1231
    tmp*=b;
alpar@1445
  1232
    return tmp;
alpar@1445
  1233
  }
alpar@1445
  1234
  
alpar@1445
  1235
  ///\e
alpar@1445
  1236
  
alpar@1445
  1237
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1238
  ///
alpar@1445
  1239
  inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a,
alpar@1445
  1240
				      const LpSolverBase::DualExpr &b) 
alpar@1445
  1241
  {
alpar@1445
  1242
    LpSolverBase::DualExpr tmp(b);
alpar@1766
  1243
    tmp*=a;
alpar@1445
  1244
    return tmp;
alpar@1445
  1245
  }
alpar@1445
  1246
  ///\e
alpar@1445
  1247
  
alpar@1445
  1248
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1249
  ///
alpar@1445
  1250
  inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a,
alpar@1445
  1251
				      const LpSolverBase::Value &b) 
alpar@1445
  1252
  {
alpar@1445
  1253
    LpSolverBase::DualExpr tmp(a);
alpar@1766
  1254
    tmp/=b;
alpar@1445
  1255
    return tmp;
alpar@1445
  1256
  }
alpar@1445
  1257
  
alpar@1272
  1258
athos@1246
  1259
} //namespace lemon
athos@1246
  1260
athos@1246
  1261
#endif //LEMON_LP_BASE_H