lemon/lp_base.h
author alpar
Fri, 15 Jul 2005 16:12:35 +0000
changeset 1561 be178ff88711
parent 1536 308150155bb5
child 1610 893dacc1866c
permissions -rw-r--r--
Wrap long lines
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@1542
   141
    ///\e The type of the investigated LP problem
athos@1542
   142
    enum ProblemTypes {
athos@1542
   143
      ///Primal-dual feasible
athos@1542
   144
      PRIMAL_DUAL_FEASIBLE = 0,
athos@1542
   145
      ///Primal feasible dual infeasible
athos@1542
   146
      PRIMAL_FEASIBLE_DUAL_INFEASIBLE = 1,
athos@1542
   147
      ///Primal infeasible dual feasible
athos@1542
   148
      PRIMAL_INFEASIBLE_DUAL_FEASIBLE = 2,
athos@1542
   149
      ///Primal-dual infeasible
athos@1542
   150
      PRIMAL_DUAL_INFEASIBLE = 3,
athos@1542
   151
      ///Could not determine so far
athos@1542
   152
      UNKNOWN = 4
athos@1542
   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@1536
   402
      ///- \ref INF "INF": the constraint is lower unbounded.
alpar@1536
   403
      ///- \ref NaN "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@1536
   411
      ///- \ref INF "INF": the constraint is upper unbounded.
alpar@1536
   412
      ///- \ref NaN "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@1542
   555
    virtual void _eraseCol(int col) = 0;
athos@1542
   556
    virtual void _eraseRow(int row) = 0;
athos@1246
   557
    virtual void _setRowCoeffs(int i, 
athos@1251
   558
			       int length,
athos@1247
   559
                               int  const * indices, 
athos@1247
   560
                               Value  const * values ) = 0;
athos@1246
   561
    virtual void _setColCoeffs(int i, 
athos@1251
   562
			       int length,
athos@1247
   563
                               int  const * indices, 
athos@1247
   564
                               Value  const * values ) = 0;
athos@1431
   565
    virtual void _setCoeff(int row, int col, Value value) = 0;
alpar@1294
   566
    virtual void _setColLowerBound(int i, Value value) = 0;
alpar@1294
   567
    virtual void _setColUpperBound(int i, Value value) = 0;
athos@1405
   568
//     virtual void _setRowLowerBound(int i, Value value) = 0;
athos@1405
   569
//     virtual void _setRowUpperBound(int i, Value value) = 0;
athos@1379
   570
    virtual void _setRowBounds(int i, Value lower, Value upper) = 0;
alpar@1294
   571
    virtual void _setObjCoeff(int i, Value obj_coef) = 0;
athos@1377
   572
    virtual void _clearObj()=0;
athos@1377
   573
//     virtual void _setObj(int length,
athos@1377
   574
//                          int  const * indices, 
athos@1377
   575
//                          Value  const * values ) = 0;
alpar@1303
   576
    virtual SolveExitStatus _solve() = 0;
alpar@1294
   577
    virtual Value _getPrimal(int i) = 0;
alpar@1312
   578
    virtual Value _getPrimalValue() = 0;
alpar@1312
   579
    virtual SolutionStatus _getPrimalStatus() = 0;
athos@1460
   580
    virtual SolutionStatus _getDualStatus() = 0;
athos@1460
   581
    ///\todo This could be implemented here, too, using _getPrimalStatus() and
athos@1460
   582
    ///_getDualStatus()
athos@1460
   583
    virtual ProblemTypes _getProblemType() = 0;
athos@1460
   584
alpar@1312
   585
    virtual void _setMax() = 0;
alpar@1312
   586
    virtual void _setMin() = 0;
alpar@1312
   587
    
alpar@1323
   588
    //Own protected stuff
alpar@1323
   589
    
alpar@1323
   590
    //Constant component of the objective function
alpar@1323
   591
    Value obj_const_comp;
alpar@1323
   592
    
athos@1377
   593
athos@1377
   594
alpar@1323
   595
    
alpar@1253
   596
  public:
alpar@1253
   597
alpar@1323
   598
    ///\e
alpar@1323
   599
    LpSolverBase() : obj_const_comp(0) {}
alpar@1253
   600
alpar@1253
   601
    ///\e
alpar@1253
   602
    virtual ~LpSolverBase() {}
alpar@1253
   603
alpar@1364
   604
    ///Creates a new LP problem
alpar@1364
   605
    LpSolverBase &newLp() {return _newLp();}
alpar@1381
   606
    ///Makes a copy of the LP problem
alpar@1364
   607
    LpSolverBase &copyLp() {return _copyLp();}
alpar@1364
   608
    
alpar@1294
   609
    ///\name Build up and modify of the LP
alpar@1263
   610
alpar@1263
   611
    ///@{
alpar@1263
   612
alpar@1253
   613
    ///Add a new empty column (i.e a new variable) to the LP
alpar@1253
   614
    Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
alpar@1263
   615
alpar@1294
   616
    ///\brief Adds several new columns
alpar@1294
   617
    ///(i.e a variables) at once
alpar@1256
   618
    ///
alpar@1273
   619
    ///This magic function takes a container as its argument
alpar@1256
   620
    ///and fills its elements
alpar@1256
   621
    ///with new columns (i.e. variables)
alpar@1273
   622
    ///\param t can be
alpar@1273
   623
    ///- a standard STL compatible iterable container with
alpar@1273
   624
    ///\ref Col as its \c values_type
alpar@1273
   625
    ///like
alpar@1273
   626
    ///\code
alpar@1273
   627
    ///std::vector<LpSolverBase::Col>
alpar@1273
   628
    ///std::list<LpSolverBase::Col>
alpar@1273
   629
    ///\endcode
alpar@1273
   630
    ///- a standard STL compatible iterable container with
alpar@1273
   631
    ///\ref Col as its \c mapped_type
alpar@1273
   632
    ///like
alpar@1273
   633
    ///\code
alpar@1364
   634
    ///std::map<AnyType,LpSolverBase::Col>
alpar@1273
   635
    ///\endcode
alpar@1273
   636
    ///- an iterable lemon \ref concept::WriteMap "write map" like 
alpar@1273
   637
    ///\code
alpar@1273
   638
    ///ListGraph::NodeMap<LpSolverBase::Col>
alpar@1273
   639
    ///ListGraph::EdgeMap<LpSolverBase::Col>
alpar@1273
   640
    ///\endcode
alpar@1256
   641
    ///\return The number of the created column.
alpar@1256
   642
#ifdef DOXYGEN
alpar@1256
   643
    template<class T>
alpar@1256
   644
    int addColSet(T &t) { return 0;} 
alpar@1256
   645
#else
alpar@1256
   646
    template<class T>
alpar@1256
   647
    typename enable_if<typename T::value_type::LpSolverCol,int>::type
alpar@1256
   648
    addColSet(T &t,dummy<0> = 0) {
alpar@1256
   649
      int s=0;
alpar@1256
   650
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
alpar@1256
   651
      return s;
alpar@1256
   652
    }
alpar@1256
   653
    template<class T>
alpar@1256
   654
    typename enable_if<typename T::value_type::second_type::LpSolverCol,
alpar@1256
   655
		       int>::type
alpar@1256
   656
    addColSet(T &t,dummy<1> = 1) { 
alpar@1256
   657
      int s=0;
alpar@1256
   658
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1256
   659
	i->second=addCol();
alpar@1256
   660
	s++;
alpar@1256
   661
      }
alpar@1256
   662
      return s;
alpar@1256
   663
    }
alpar@1272
   664
    template<class T>
alpar@1272
   665
    typename enable_if<typename T::ValueSet::value_type::LpSolverCol,
alpar@1272
   666
		       int>::type
alpar@1272
   667
    addColSet(T &t,dummy<2> = 2) { 
alpar@1272
   668
      ///\bug <tt>return addColSet(t.valueSet());</tt> should also work.
alpar@1272
   669
      int s=0;
alpar@1272
   670
      for(typename T::ValueSet::iterator i=t.valueSet().begin();
alpar@1272
   671
	  i!=t.valueSet().end();
alpar@1272
   672
	  ++i)
alpar@1272
   673
	{
alpar@1272
   674
	  *i=addCol();
alpar@1272
   675
	  s++;
alpar@1272
   676
	}
alpar@1272
   677
      return s;
alpar@1272
   678
    }
alpar@1256
   679
#endif
alpar@1263
   680
alpar@1445
   681
    ///Set a column (i.e a dual constraint) of the LP
alpar@1258
   682
alpar@1445
   683
    ///\param c is the column to be modified
alpar@1445
   684
    ///\param e is a dual linear expression (see \ref DualExpr)
athos@1542
   685
    ///\bug This is a temporary function. The interface will change to
alpar@1445
   686
    ///a better one.
alpar@1445
   687
    void setCol(Col c,const DualExpr &e) {
alpar@1445
   688
      std::vector<int> indices;
alpar@1445
   689
      std::vector<Value> values;
alpar@1445
   690
      indices.push_back(0);
alpar@1445
   691
      values.push_back(0);
alpar@1445
   692
      for(DualExpr::const_iterator i=e.begin(); i!=e.end(); ++i)
alpar@1445
   693
	if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
alpar@1445
   694
	  indices.push_back(cols.floatingId((*i).first.id));
alpar@1445
   695
	  values.push_back((*i).second);
alpar@1445
   696
	}
alpar@1445
   697
      _setColCoeffs(cols.floatingId(c.id),indices.size()-1,
alpar@1445
   698
		    &indices[0],&values[0]);
alpar@1445
   699
    }
alpar@1445
   700
alpar@1445
   701
    ///Add a new column to the LP
alpar@1445
   702
alpar@1445
   703
    ///\param e is a dual linear expression (see \ref DualExpr)
alpar@1445
   704
    ///\param obj is the corresponding component of the objective
alpar@1445
   705
    ///function. It is 0 by default.
alpar@1445
   706
    ///\return The created column.
alpar@1445
   707
    ///\bug This is a temportary function. The interface will change to
alpar@1445
   708
    ///a better one.
alpar@1493
   709
    Col addCol(const DualExpr &e, Value obj=0) {
alpar@1445
   710
      Col c=addCol();
alpar@1445
   711
      setCol(c,e);
alpar@1493
   712
      objCoeff(c,obj);
alpar@1445
   713
      return c;
alpar@1445
   714
    }
alpar@1445
   715
alpar@1445
   716
    ///Add a new empty row (i.e a new constraint) to the LP
alpar@1445
   717
alpar@1445
   718
    ///This function adds a new empty row (i.e a new constraint) to the LP.
alpar@1258
   719
    ///\return The created row
alpar@1253
   720
    Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
alpar@1253
   721
athos@1542
   722
    ///\brief Add several new rows
athos@1542
   723
    ///(i.e a constraints) at once
alpar@1445
   724
    ///
alpar@1445
   725
    ///This magic function takes a container as its argument
alpar@1445
   726
    ///and fills its elements
alpar@1445
   727
    ///with new row (i.e. variables)
alpar@1445
   728
    ///\param t can be
alpar@1445
   729
    ///- a standard STL compatible iterable container with
alpar@1445
   730
    ///\ref Row as its \c values_type
alpar@1445
   731
    ///like
alpar@1445
   732
    ///\code
alpar@1445
   733
    ///std::vector<LpSolverBase::Row>
alpar@1445
   734
    ///std::list<LpSolverBase::Row>
alpar@1445
   735
    ///\endcode
alpar@1445
   736
    ///- a standard STL compatible iterable container with
alpar@1445
   737
    ///\ref Row as its \c mapped_type
alpar@1445
   738
    ///like
alpar@1445
   739
    ///\code
alpar@1445
   740
    ///std::map<AnyType,LpSolverBase::Row>
alpar@1445
   741
    ///\endcode
alpar@1445
   742
    ///- an iterable lemon \ref concept::WriteMap "write map" like 
alpar@1445
   743
    ///\code
alpar@1445
   744
    ///ListGraph::NodeMap<LpSolverBase::Row>
alpar@1445
   745
    ///ListGraph::EdgeMap<LpSolverBase::Row>
alpar@1445
   746
    ///\endcode
alpar@1445
   747
    ///\return The number of rows created.
alpar@1445
   748
#ifdef DOXYGEN
alpar@1445
   749
    template<class T>
alpar@1445
   750
    int addRowSet(T &t) { return 0;} 
alpar@1445
   751
#else
alpar@1445
   752
    template<class T>
alpar@1445
   753
    typename enable_if<typename T::value_type::LpSolverRow,int>::type
alpar@1445
   754
    addRowSet(T &t,dummy<0> = 0) {
alpar@1445
   755
      int s=0;
alpar@1445
   756
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;}
alpar@1445
   757
      return s;
alpar@1445
   758
    }
alpar@1445
   759
    template<class T>
alpar@1445
   760
    typename enable_if<typename T::value_type::second_type::LpSolverRow,
alpar@1445
   761
		       int>::type
alpar@1445
   762
    addRowSet(T &t,dummy<1> = 1) { 
alpar@1445
   763
      int s=0;
alpar@1445
   764
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1445
   765
	i->second=addRow();
alpar@1445
   766
	s++;
alpar@1445
   767
      }
alpar@1445
   768
      return s;
alpar@1445
   769
    }
alpar@1445
   770
    template<class T>
alpar@1445
   771
    typename enable_if<typename T::ValueSet::value_type::LpSolverRow,
alpar@1445
   772
		       int>::type
alpar@1445
   773
    addRowSet(T &t,dummy<2> = 2) { 
alpar@1445
   774
      ///\bug <tt>return addRowSet(t.valueSet());</tt> should also work.
alpar@1445
   775
      int s=0;
alpar@1445
   776
      for(typename T::ValueSet::iterator i=t.valueSet().begin();
alpar@1445
   777
	  i!=t.valueSet().end();
alpar@1445
   778
	  ++i)
alpar@1445
   779
	{
alpar@1445
   780
	  *i=addRow();
alpar@1445
   781
	  s++;
alpar@1445
   782
	}
alpar@1445
   783
      return s;
alpar@1445
   784
    }
alpar@1445
   785
#endif
alpar@1445
   786
alpar@1445
   787
    ///Set a row (i.e a constraint) of the LP
alpar@1253
   788
alpar@1258
   789
    ///\param r is the row to be modified
alpar@1259
   790
    ///\param l is lower bound (-\ref INF means no bound)
alpar@1258
   791
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   792
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1253
   793
    ///\bug This is a temportary function. The interface will change to
alpar@1253
   794
    ///a better one.
alpar@1328
   795
    ///\todo Option to control whether a constraint with a single variable is
alpar@1328
   796
    ///added or not.
alpar@1258
   797
    void setRow(Row r, Value l,const Expr &e, Value u) {
alpar@1253
   798
      std::vector<int> indices;
alpar@1253
   799
      std::vector<Value> values;
alpar@1253
   800
      indices.push_back(0);
alpar@1253
   801
      values.push_back(0);
alpar@1258
   802
      for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i)
alpar@1256
   803
	if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
alpar@1256
   804
	  indices.push_back(cols.floatingId((*i).first.id));
alpar@1256
   805
	  values.push_back((*i).second);
alpar@1256
   806
	}
alpar@1253
   807
      _setRowCoeffs(rows.floatingId(r.id),indices.size()-1,
alpar@1253
   808
		    &indices[0],&values[0]);
athos@1405
   809
//       _setRowLowerBound(rows.floatingId(r.id),l-e.constComp());
athos@1405
   810
//       _setRowUpperBound(rows.floatingId(r.id),u-e.constComp());
athos@1405
   811
       _setRowBounds(rows.floatingId(r.id),l-e.constComp(),u-e.constComp());
alpar@1258
   812
    }
alpar@1258
   813
alpar@1445
   814
    ///Set a row (i.e a constraint) of the LP
alpar@1264
   815
alpar@1264
   816
    ///\param r is the row to be modified
alpar@1264
   817
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   818
    void setRow(Row r, const Constr &c) {
alpar@1273
   819
      setRow(r,
alpar@1275
   820
	     c.lowerBounded()?c.lowerBound():-INF,
alpar@1273
   821
	     c.expr(),
alpar@1275
   822
	     c.upperBounded()?c.upperBound():INF);
alpar@1264
   823
    }
alpar@1264
   824
alpar@1445
   825
    ///Add a new row (i.e a new constraint) to the LP
alpar@1258
   826
alpar@1259
   827
    ///\param l is the lower bound (-\ref INF means no bound)
alpar@1258
   828
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   829
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1258
   830
    ///\return The created row.
alpar@1258
   831
    ///\bug This is a temportary function. The interface will change to
alpar@1258
   832
    ///a better one.
alpar@1258
   833
    Row addRow(Value l,const Expr &e, Value u) {
alpar@1258
   834
      Row r=addRow();
alpar@1258
   835
      setRow(r,l,e,u);
alpar@1253
   836
      return r;
alpar@1253
   837
    }
alpar@1253
   838
alpar@1445
   839
    ///Add a new row (i.e a new constraint) to the LP
alpar@1264
   840
alpar@1264
   841
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   842
    ///\return The created row.
alpar@1264
   843
    Row addRow(const Constr &c) {
alpar@1264
   844
      Row r=addRow();
alpar@1264
   845
      setRow(r,c);
alpar@1264
   846
      return r;
alpar@1264
   847
    }
athos@1542
   848
    ///Erase a coloumn (i.e a variable) from the LP
athos@1542
   849
athos@1542
   850
    ///\param c is the coloumn to be deleted
athos@1542
   851
    ///\todo Please check this
athos@1542
   852
    void eraseCol(Col c) {
athos@1542
   853
      _eraseCol(cols.floatingId(c.id));
athos@1542
   854
      cols.erase(c.id);
athos@1542
   855
    }
athos@1542
   856
    ///Erase a  row (i.e a constraint) from the LP
athos@1542
   857
athos@1542
   858
    ///\param r is the row to be deleted
athos@1542
   859
    ///\todo Please check this
athos@1542
   860
    void eraseRow(Row r) {
athos@1542
   861
      _eraseRow(rows.floatingId(r.id));
athos@1542
   862
      rows.erase(r.id);
athos@1542
   863
    }
alpar@1264
   864
athos@1436
   865
    ///Set an element of the coefficient matrix of the LP
athos@1436
   866
athos@1436
   867
    ///\param r is the row of the element to be modified
athos@1436
   868
    ///\param c is the coloumn of the element to be modified
athos@1436
   869
    ///\param val is the new value of the coefficient
athos@1436
   870
    void setCoeff(Row r, Col c, Value val){
athos@1436
   871
      _setCoeff(rows.floatingId(r.id),cols.floatingId(c.id), val);
athos@1436
   872
    }
athos@1436
   873
alpar@1253
   874
    /// Set the lower bound of a column (i.e a variable)
alpar@1253
   875
alpar@1293
   876
    /// The upper bound of a variable (column) has to be given by an 
alpar@1253
   877
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   878
    /// Value or -\ref INF.
alpar@1293
   879
    void colLowerBound(Col c, Value value) {
alpar@1253
   880
      _setColLowerBound(cols.floatingId(c.id),value);
alpar@1253
   881
    }
alpar@1253
   882
    /// Set the upper bound of a column (i.e a variable)
alpar@1253
   883
alpar@1293
   884
    /// The upper bound of a variable (column) has to be given by an 
alpar@1253
   885
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   886
    /// Value or \ref INF.
alpar@1293
   887
    void colUpperBound(Col c, Value value) {
alpar@1253
   888
      _setColUpperBound(cols.floatingId(c.id),value);
alpar@1253
   889
    };
alpar@1293
   890
    /// Set the lower and the upper bounds of a column (i.e a variable)
alpar@1293
   891
alpar@1293
   892
    /// The lower and the upper bounds of
alpar@1293
   893
    /// a variable (column) have to be given by an 
alpar@1293
   894
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
   895
    /// Value, -\ref INF or \ref INF.
alpar@1293
   896
    void colBounds(Col c, Value lower, Value upper) {
alpar@1293
   897
      _setColLowerBound(cols.floatingId(c.id),lower);
alpar@1293
   898
      _setColUpperBound(cols.floatingId(c.id),upper);
alpar@1293
   899
    }
alpar@1293
   900
    
athos@1405
   901
//     /// Set the lower bound of a row (i.e a constraint)
alpar@1253
   902
athos@1405
   903
//     /// The lower bound of a linear expression (row) has to be given by an 
athos@1405
   904
//     /// extended number of type Value, i.e. a finite number of type 
athos@1405
   905
//     /// Value or -\ref INF.
athos@1405
   906
//     void rowLowerBound(Row r, Value value) {
athos@1405
   907
//       _setRowLowerBound(rows.floatingId(r.id),value);
athos@1405
   908
//     };
athos@1405
   909
//     /// Set the upper bound of a row (i.e a constraint)
alpar@1253
   910
athos@1405
   911
//     /// The upper bound of a linear expression (row) has to be given by an 
athos@1405
   912
//     /// extended number of type Value, i.e. a finite number of type 
athos@1405
   913
//     /// Value or \ref INF.
athos@1405
   914
//     void rowUpperBound(Row r, Value value) {
athos@1405
   915
//       _setRowUpperBound(rows.floatingId(r.id),value);
athos@1405
   916
//     };
athos@1405
   917
athos@1405
   918
    /// Set the lower and the upper bounds of a row (i.e a constraint)
alpar@1293
   919
alpar@1293
   920
    /// The lower and the upper bounds of
alpar@1293
   921
    /// a constraint (row) have to be given by an 
alpar@1293
   922
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
   923
    /// Value, -\ref INF or \ref INF.
alpar@1293
   924
    void rowBounds(Row c, Value lower, Value upper) {
athos@1379
   925
      _setRowBounds(rows.floatingId(c.id),lower, upper);
athos@1379
   926
      // _setRowUpperBound(rows.floatingId(c.id),upper);
alpar@1293
   927
    }
alpar@1293
   928
    
alpar@1253
   929
    ///Set an element of the objective function
alpar@1293
   930
    void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); };
alpar@1253
   931
    ///Set the objective function
alpar@1253
   932
    
alpar@1253
   933
    ///\param e is a linear expression of type \ref Expr.
alpar@1323
   934
    ///\bug The previous objective function is not cleared!
alpar@1253
   935
    void setObj(Expr e) {
athos@1377
   936
      _clearObj();
alpar@1253
   937
      for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
alpar@1293
   938
	objCoeff((*i).first,(*i).second);
alpar@1323
   939
      obj_const_comp=e.constComp();
alpar@1253
   940
    }
alpar@1263
   941
alpar@1312
   942
    ///Maximize
alpar@1312
   943
    void max() { _setMax(); }
alpar@1312
   944
    ///Minimize
alpar@1312
   945
    void min() { _setMin(); }
alpar@1312
   946
alpar@1312
   947
    
alpar@1263
   948
    ///@}
alpar@1263
   949
alpar@1263
   950
alpar@1294
   951
    ///\name Solve the LP
alpar@1263
   952
alpar@1263
   953
    ///@{
alpar@1263
   954
athos@1458
   955
    ///\e Solve the LP problem at hand
athos@1458
   956
    ///
athos@1458
   957
    ///\return The result of the optimization procedure. Possible values and their meanings can be found in the documentation of \ref SolveExitStatus.
athos@1458
   958
    ///
athos@1458
   959
    ///\todo Which method is used to solve the problem
alpar@1303
   960
    SolveExitStatus solve() { return _solve(); }
alpar@1263
   961
    
alpar@1263
   962
    ///@}
alpar@1263
   963
    
alpar@1294
   964
    ///\name Obtain the solution
alpar@1263
   965
alpar@1263
   966
    ///@{
alpar@1263
   967
athos@1460
   968
    /// The status of the primal problem (the original LP problem)
alpar@1312
   969
    SolutionStatus primalStatus() {
alpar@1312
   970
      return _getPrimalStatus();
alpar@1294
   971
    }
alpar@1294
   972
athos@1460
   973
    /// The status of the dual (of the original LP) problem 
athos@1460
   974
    SolutionStatus dualStatus() {
athos@1460
   975
      return _getDualStatus();
athos@1460
   976
    }
athos@1460
   977
athos@1460
   978
    ///The type of the original LP problem
athos@1462
   979
    ProblemTypes problemType() {
athos@1460
   980
      return _getProblemType();
athos@1460
   981
    }
athos@1460
   982
alpar@1294
   983
    ///\e
alpar@1293
   984
    Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); }
alpar@1263
   985
alpar@1312
   986
    ///\e
alpar@1312
   987
alpar@1312
   988
    ///\return
alpar@1312
   989
    ///- \ref INF or -\ref INF means either infeasibility or unboundedness
alpar@1312
   990
    /// of the primal problem, depending on whether we minimize or maximize.
alpar@1364
   991
    ///- \ref NaN if no primal solution is found.
alpar@1312
   992
    ///- The (finite) objective value if an optimal solution is found.
alpar@1323
   993
    Value primalValue() { return _getPrimalValue()+obj_const_comp;}
alpar@1263
   994
    ///@}
alpar@1253
   995
    
athos@1248
   996
  };  
athos@1246
   997
alpar@1272
   998
  ///\e
alpar@1272
   999
  
alpar@1272
  1000
  ///\relates LpSolverBase::Expr
alpar@1272
  1001
  ///
alpar@1272
  1002
  inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
alpar@1272
  1003
				      const LpSolverBase::Expr &b) 
alpar@1272
  1004
  {
alpar@1272
  1005
    LpSolverBase::Expr tmp(a);
alpar@1364
  1006
    tmp+=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
  1007
    return tmp;
alpar@1272
  1008
  }
alpar@1272
  1009
  ///\e
alpar@1272
  1010
  
alpar@1272
  1011
  ///\relates LpSolverBase::Expr
alpar@1272
  1012
  ///
alpar@1272
  1013
  inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
alpar@1272
  1014
				      const LpSolverBase::Expr &b) 
alpar@1272
  1015
  {
alpar@1272
  1016
    LpSolverBase::Expr tmp(a);
alpar@1364
  1017
    tmp-=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
  1018
    return tmp;
alpar@1272
  1019
  }
alpar@1272
  1020
  ///\e
alpar@1272
  1021
  
alpar@1272
  1022
  ///\relates LpSolverBase::Expr
alpar@1272
  1023
  ///
alpar@1272
  1024
  inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
alpar@1273
  1025
				      const LpSolverBase::Value &b) 
alpar@1272
  1026
  {
alpar@1272
  1027
    LpSolverBase::Expr tmp(a);
alpar@1364
  1028
    tmp*=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
  1029
    return tmp;
alpar@1272
  1030
  }
alpar@1272
  1031
  
alpar@1272
  1032
  ///\e
alpar@1272
  1033
  
alpar@1272
  1034
  ///\relates LpSolverBase::Expr
alpar@1272
  1035
  ///
alpar@1273
  1036
  inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
alpar@1272
  1037
				      const LpSolverBase::Expr &b) 
alpar@1272
  1038
  {
alpar@1272
  1039
    LpSolverBase::Expr tmp(b);
alpar@1364
  1040
    tmp*=a; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
  1041
    return tmp;
alpar@1272
  1042
  }
alpar@1272
  1043
  ///\e
alpar@1272
  1044
  
alpar@1272
  1045
  ///\relates LpSolverBase::Expr
alpar@1272
  1046
  ///
alpar@1272
  1047
  inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
alpar@1273
  1048
				      const LpSolverBase::Value &b) 
alpar@1272
  1049
  {
alpar@1272
  1050
    LpSolverBase::Expr tmp(a);
alpar@1364
  1051
    tmp/=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
  1052
    return tmp;
alpar@1272
  1053
  }
alpar@1272
  1054
  
alpar@1272
  1055
  ///\e
alpar@1272
  1056
  
alpar@1272
  1057
  ///\relates LpSolverBase::Constr
alpar@1272
  1058
  ///
alpar@1272
  1059
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1272
  1060
					 const LpSolverBase::Expr &f) 
alpar@1272
  1061
  {
alpar@1272
  1062
    return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
alpar@1272
  1063
  }
alpar@1272
  1064
alpar@1272
  1065
  ///\e
alpar@1272
  1066
  
alpar@1272
  1067
  ///\relates LpSolverBase::Constr
alpar@1272
  1068
  ///
alpar@1273
  1069
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
alpar@1272
  1070
					 const LpSolverBase::Expr &f) 
alpar@1272
  1071
  {
alpar@1272
  1072
    return LpSolverBase::Constr(e,f);
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@1273
  1080
					 const LpSolverBase::Value &f) 
alpar@1272
  1081
  {
alpar@1272
  1082
    return LpSolverBase::Constr(e,f);
alpar@1272
  1083
  }
alpar@1272
  1084
alpar@1272
  1085
  ///\e
alpar@1272
  1086
  
alpar@1272
  1087
  ///\relates LpSolverBase::Constr
alpar@1272
  1088
  ///
alpar@1272
  1089
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1272
  1090
					 const LpSolverBase::Expr &f) 
alpar@1272
  1091
  {
alpar@1272
  1092
    return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
alpar@1272
  1093
  }
alpar@1272
  1094
alpar@1272
  1095
alpar@1272
  1096
  ///\e
alpar@1272
  1097
  
alpar@1272
  1098
  ///\relates LpSolverBase::Constr
alpar@1272
  1099
  ///
alpar@1273
  1100
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
alpar@1272
  1101
					 const LpSolverBase::Expr &f) 
alpar@1272
  1102
  {
alpar@1272
  1103
    return LpSolverBase::Constr(f,e);
alpar@1272
  1104
  }
alpar@1272
  1105
alpar@1272
  1106
alpar@1272
  1107
  ///\e
alpar@1272
  1108
  
alpar@1272
  1109
  ///\relates LpSolverBase::Constr
alpar@1272
  1110
  ///
alpar@1272
  1111
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1273
  1112
					 const LpSolverBase::Value &f) 
alpar@1272
  1113
  {
alpar@1272
  1114
    return LpSolverBase::Constr(f,e);
alpar@1272
  1115
  }
alpar@1272
  1116
alpar@1272
  1117
  ///\e
alpar@1272
  1118
  
alpar@1272
  1119
  ///\relates LpSolverBase::Constr
alpar@1272
  1120
  ///
alpar@1272
  1121
  inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
alpar@1272
  1122
					 const LpSolverBase::Expr &f) 
alpar@1272
  1123
  {
alpar@1272
  1124
    return LpSolverBase::Constr(0,e-f,0);
alpar@1272
  1125
  }
alpar@1272
  1126
alpar@1272
  1127
  ///\e
alpar@1272
  1128
  
alpar@1272
  1129
  ///\relates LpSolverBase::Constr
alpar@1272
  1130
  ///
alpar@1273
  1131
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
alpar@1272
  1132
					 const LpSolverBase::Constr&c) 
alpar@1272
  1133
  {
alpar@1272
  1134
    LpSolverBase::Constr tmp(c);
alpar@1273
  1135
    ///\todo Create an own exception type.
alpar@1273
  1136
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
  1137
    else tmp.lowerBound()=n;
alpar@1272
  1138
    return tmp;
alpar@1272
  1139
  }
alpar@1272
  1140
  ///\e
alpar@1272
  1141
  
alpar@1272
  1142
  ///\relates LpSolverBase::Constr
alpar@1272
  1143
  ///
alpar@1272
  1144
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
alpar@1273
  1145
					 const LpSolverBase::Value &n)
alpar@1272
  1146
  {
alpar@1272
  1147
    LpSolverBase::Constr tmp(c);
alpar@1273
  1148
    ///\todo Create an own exception type.
alpar@1273
  1149
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
  1150
    else tmp.upperBound()=n;
alpar@1272
  1151
    return tmp;
alpar@1272
  1152
  }
alpar@1272
  1153
alpar@1272
  1154
  ///\e
alpar@1272
  1155
  
alpar@1272
  1156
  ///\relates LpSolverBase::Constr
alpar@1272
  1157
  ///
alpar@1273
  1158
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
alpar@1272
  1159
					 const LpSolverBase::Constr&c) 
alpar@1272
  1160
  {
alpar@1272
  1161
    LpSolverBase::Constr tmp(c);
alpar@1273
  1162
    ///\todo Create an own exception type.
alpar@1273
  1163
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
  1164
    else tmp.upperBound()=n;
alpar@1272
  1165
    return tmp;
alpar@1272
  1166
  }
alpar@1272
  1167
  ///\e
alpar@1272
  1168
  
alpar@1272
  1169
  ///\relates LpSolverBase::Constr
alpar@1272
  1170
  ///
alpar@1272
  1171
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
alpar@1273
  1172
					 const LpSolverBase::Value &n)
alpar@1272
  1173
  {
alpar@1272
  1174
    LpSolverBase::Constr tmp(c);
alpar@1273
  1175
    ///\todo Create an own exception type.
alpar@1273
  1176
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
  1177
    else tmp.lowerBound()=n;
alpar@1272
  1178
    return tmp;
alpar@1272
  1179
  }
alpar@1272
  1180
alpar@1445
  1181
  ///\e
alpar@1445
  1182
  
alpar@1445
  1183
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1184
  ///
alpar@1445
  1185
  inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a,
alpar@1445
  1186
				      const LpSolverBase::DualExpr &b) 
alpar@1445
  1187
  {
alpar@1445
  1188
    LpSolverBase::DualExpr tmp(a);
alpar@1445
  1189
    tmp+=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1445
  1190
    return tmp;
alpar@1445
  1191
  }
alpar@1445
  1192
  ///\e
alpar@1445
  1193
  
alpar@1445
  1194
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1195
  ///
alpar@1445
  1196
  inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a,
alpar@1445
  1197
				      const LpSolverBase::DualExpr &b) 
alpar@1445
  1198
  {
alpar@1445
  1199
    LpSolverBase::DualExpr tmp(a);
alpar@1445
  1200
    tmp-=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1445
  1201
    return tmp;
alpar@1445
  1202
  }
alpar@1445
  1203
  ///\e
alpar@1445
  1204
  
alpar@1445
  1205
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1206
  ///
alpar@1445
  1207
  inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a,
alpar@1445
  1208
				      const LpSolverBase::Value &b) 
alpar@1445
  1209
  {
alpar@1445
  1210
    LpSolverBase::DualExpr tmp(a);
alpar@1445
  1211
    tmp*=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1445
  1212
    return tmp;
alpar@1445
  1213
  }
alpar@1445
  1214
  
alpar@1445
  1215
  ///\e
alpar@1445
  1216
  
alpar@1445
  1217
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1218
  ///
alpar@1445
  1219
  inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a,
alpar@1445
  1220
				      const LpSolverBase::DualExpr &b) 
alpar@1445
  1221
  {
alpar@1445
  1222
    LpSolverBase::DualExpr tmp(b);
alpar@1445
  1223
    tmp*=a; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1445
  1224
    return tmp;
alpar@1445
  1225
  }
alpar@1445
  1226
  ///\e
alpar@1445
  1227
  
alpar@1445
  1228
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1229
  ///
alpar@1445
  1230
  inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a,
alpar@1445
  1231
				      const LpSolverBase::Value &b) 
alpar@1445
  1232
  {
alpar@1445
  1233
    LpSolverBase::DualExpr tmp(a);
alpar@1445
  1234
    tmp/=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1445
  1235
    return tmp;
alpar@1445
  1236
  }
alpar@1445
  1237
  
alpar@1272
  1238
athos@1246
  1239
} //namespace lemon
athos@1246
  1240
athos@1246
  1241
#endif //LEMON_LP_BASE_H