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