lemon/lp_base.h
author hegyi
Thu, 20 Oct 2005 15:50:23 +0000
changeset 1731 616bc933c2bc
parent 1610 893dacc1866c
child 1766 6c59b1386fe8
permissions -rw-r--r--
Mapselector widget reached its first release, but there are still work to do on it, I know...
athos@1247
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/lp_base.h - Part of LEMON, a generic C++ optimization library
athos@1247
     3
 *
athos@1247
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
athos@1247
     6
 *
athos@1247
     7
 * Permission to use, modify and distribute this software is granted
athos@1247
     8
 * provided that this copyright notice appears in all copies. For
athos@1247
     9
 * precise terms see the accompanying LICENSE file.
athos@1247
    10
 *
athos@1247
    11
 * This software is provided "AS IS" with no warranty of any kind,
athos@1247
    12
 * express or implied, and with no claim as to its suitability for any
athos@1247
    13
 * purpose.
athos@1247
    14
 *
athos@1247
    15
 */
athos@1247
    16
athos@1246
    17
#ifndef LEMON_LP_BASE_H
athos@1246
    18
#define LEMON_LP_BASE_H
athos@1246
    19
alpar@1253
    20
#include<vector>
alpar@1272
    21
#include<map>
alpar@1256
    22
#include<limits>
alpar@1397
    23
#include<cmath>
alpar@1253
    24
alpar@1256
    25
#include<lemon/utility.h>
alpar@1253
    26
#include<lemon/error.h>
alpar@1256
    27
#include<lemon/invalid.h>
alpar@1253
    28
athos@1246
    29
///\file
athos@1246
    30
///\brief The interface of the LP solver interface.
alpar@1328
    31
///\ingroup gen_opt_group
athos@1246
    32
namespace lemon {
alpar@1253
    33
  
alpar@1253
    34
  ///Internal data structure to convert floating id's to fix one's
alpar@1253
    35
    
alpar@1279
    36
  ///\todo This might be implemented to be also usable in other places.
alpar@1253
    37
  class _FixId 
alpar@1253
    38
  {
alpar@1253
    39
    std::vector<int> index;
alpar@1253
    40
    std::vector<int> cross;
alpar@1253
    41
    int first_free;
alpar@1253
    42
  public:
alpar@1253
    43
    _FixId() : first_free(-1) {};
alpar@1253
    44
    ///Convert a floating id to a fix one
alpar@1253
    45
alpar@1253
    46
    ///\param n is a floating id
alpar@1253
    47
    ///\return the corresponding fix id
alpar@1484
    48
    int fixId(int n) const {return cross[n];}
alpar@1253
    49
    ///Convert a fix id to a floating one
alpar@1253
    50
alpar@1253
    51
    ///\param n is a fix id
alpar@1253
    52
    ///\return the corresponding floating id
alpar@1484
    53
    int floatingId(int n) const { return index[n];}
alpar@1253
    54
    ///Add a new floating id.
alpar@1253
    55
alpar@1253
    56
    ///\param n is a floating id
alpar@1253
    57
    ///\return the fix id of the new value
alpar@1253
    58
    ///\todo Multiple additions should also be handled.
alpar@1253
    59
    int insert(int n)
alpar@1253
    60
    {
alpar@1253
    61
      if(n>=int(cross.size())) {
alpar@1253
    62
	cross.resize(n+1);
alpar@1253
    63
	if(first_free==-1) {
alpar@1253
    64
	  cross[n]=index.size();
alpar@1253
    65
	  index.push_back(n);
alpar@1253
    66
	}
alpar@1253
    67
	else {
alpar@1253
    68
	  cross[n]=first_free;
alpar@1253
    69
	  int next=index[first_free];
alpar@1253
    70
	  index[first_free]=n;
alpar@1253
    71
	  first_free=next;
alpar@1253
    72
	}
alpar@1256
    73
	return cross[n];
alpar@1253
    74
      }
alpar@1273
    75
      ///\todo Create an own exception type.
alpar@1253
    76
      else throw LogicError(); //floatingId-s must form a continuous range;
alpar@1253
    77
    }
alpar@1253
    78
    ///Remove a fix id.
alpar@1253
    79
alpar@1253
    80
    ///\param n is a fix id
alpar@1253
    81
    ///
alpar@1253
    82
    void erase(int n) 
alpar@1253
    83
    {
alpar@1253
    84
      int fl=index[n];
alpar@1253
    85
      index[n]=first_free;
alpar@1253
    86
      first_free=n;
alpar@1253
    87
      for(int i=fl+1;i<int(cross.size());++i) {
alpar@1253
    88
	cross[i-1]=cross[i];
alpar@1253
    89
	index[cross[i]]--;
alpar@1253
    90
      }
alpar@1253
    91
      cross.pop_back();
alpar@1253
    92
    }
alpar@1253
    93
    ///An upper bound on the largest fix id.
alpar@1253
    94
alpar@1253
    95
    ///\todo Do we need this?
alpar@1253
    96
    ///
alpar@1253
    97
    std::size_t maxFixId() { return cross.size()-1; }
alpar@1253
    98
  
alpar@1253
    99
  };
alpar@1253
   100
    
alpar@1253
   101
  ///Common base class for LP solvers
alpar@1328
   102
  
alpar@1328
   103
  ///\todo Much more docs
alpar@1328
   104
  ///\ingroup gen_opt_group
athos@1246
   105
  class LpSolverBase {
alpar@1323
   106
athos@1247
   107
  public:
athos@1247
   108
athos@1458
   109
    ///Possible outcomes of an LP solving procedure
alpar@1303
   110
    enum SolveExitStatus {
athos@1458
   111
      ///This means that the problem has been successfully solved: either
athos@1458
   112
      ///an optimal solution has been found or infeasibility/unboundedness
athos@1458
   113
      ///has been proved.
alpar@1293
   114
      SOLVED = 0,
athos@1458
   115
      ///Any other case (including the case when some user specified limit has been exceeded)
alpar@1293
   116
      UNSOLVED = 1
athos@1291
   117
    };
athos@1291
   118
      
athos@1460
   119
      ///\e
alpar@1303
   120
    enum SolutionStatus {
alpar@1295
   121
      ///Feasible solution has'n been found (but may exist).
alpar@1295
   122
alpar@1295
   123
      ///\todo NOTFOUND might be a better name.
alpar@1295
   124
      ///
alpar@1293
   125
      UNDEFINED = 0,
alpar@1295
   126
      ///The problem has no feasible solution
alpar@1293
   127
      INFEASIBLE = 1,
alpar@1295
   128
      ///Feasible solution found
alpar@1293
   129
      FEASIBLE = 2,
alpar@1295
   130
      ///Optimal solution exists and found
alpar@1295
   131
      OPTIMAL = 3,
alpar@1295
   132
      ///The cost function is unbounded
alpar@1295
   133
alpar@1295
   134
      ///\todo Give a feasible solution and an infinite ray (and the
alpar@1295
   135
      ///corresponding bases)
alpar@1295
   136
      INFINITE = 4
alpar@1263
   137
    };
athos@1460
   138
athos@1542
   139
    ///\e The type of the investigated LP problem
athos@1542
   140
    enum ProblemTypes {
athos@1542
   141
      ///Primal-dual feasible
athos@1542
   142
      PRIMAL_DUAL_FEASIBLE = 0,
athos@1542
   143
      ///Primal feasible dual infeasible
athos@1542
   144
      PRIMAL_FEASIBLE_DUAL_INFEASIBLE = 1,
athos@1542
   145
      ///Primal infeasible dual feasible
athos@1542
   146
      PRIMAL_INFEASIBLE_DUAL_FEASIBLE = 2,
athos@1542
   147
      ///Primal-dual infeasible
athos@1542
   148
      PRIMAL_DUAL_INFEASIBLE = 3,
athos@1542
   149
      ///Could not determine so far
athos@1542
   150
      UNKNOWN = 4
athos@1542
   151
    };
athos@1508
   152
alpar@1256
   153
    ///The floating point type used by the solver
athos@1247
   154
    typedef double Value;
alpar@1256
   155
    ///The infinity constant
athos@1247
   156
    static const Value INF;
alpar@1264
   157
    ///The not a number constant
alpar@1264
   158
    static const Value NaN;
alpar@1253
   159
    
alpar@1256
   160
    ///Refer to a column of the LP.
alpar@1256
   161
alpar@1256
   162
    ///This type is used to refer to a column of the LP.
alpar@1256
   163
    ///
alpar@1256
   164
    ///Its value remains valid and correct even after the addition or erase of
alpar@1273
   165
    ///other columns.
alpar@1256
   166
    ///
alpar@1256
   167
    ///\todo Document what can one do with a Col (INVALID, comparing,
alpar@1256
   168
    ///it is similar to Node/Edge)
alpar@1256
   169
    class Col {
alpar@1256
   170
    protected:
alpar@1256
   171
      int id;
alpar@1256
   172
      friend class LpSolverBase;
alpar@1256
   173
    public:
alpar@1259
   174
      typedef Value ExprValue;
alpar@1256
   175
      typedef True LpSolverCol;
alpar@1256
   176
      Col() {}
alpar@1256
   177
      Col(const Invalid&) : id(-1) {}
alpar@1256
   178
      bool operator<(Col c) const  {return id<c.id;}
alpar@1256
   179
      bool operator==(Col c) const  {return id==c.id;}
alpar@1256
   180
      bool operator!=(Col c) const  {return id==c.id;}
alpar@1256
   181
    };
alpar@1256
   182
alpar@1256
   183
    ///Refer to a row of the LP.
alpar@1256
   184
alpar@1256
   185
    ///This type is used to refer to a row of the LP.
alpar@1256
   186
    ///
alpar@1256
   187
    ///Its value remains valid and correct even after the addition or erase of
alpar@1273
   188
    ///other rows.
alpar@1256
   189
    ///
alpar@1256
   190
    ///\todo Document what can one do with a Row (INVALID, comparing,
alpar@1256
   191
    ///it is similar to Node/Edge)
alpar@1256
   192
    class Row {
alpar@1256
   193
    protected:
alpar@1256
   194
      int id;
alpar@1256
   195
      friend class LpSolverBase;
alpar@1256
   196
    public:
alpar@1259
   197
      typedef Value ExprValue;
alpar@1256
   198
      typedef True LpSolverRow;
alpar@1256
   199
      Row() {}
alpar@1256
   200
      Row(const Invalid&) : id(-1) {}
alpar@1439
   201
alpar@1256
   202
      bool operator<(Row c) const  {return id<c.id;}
alpar@1256
   203
      bool operator==(Row c) const  {return id==c.id;}
alpar@1256
   204
      bool operator!=(Row c) const  {return id==c.id;} 
alpar@1256
   205
   };
alpar@1259
   206
    
alpar@1279
   207
    ///Linear expression of variables and a constant component
alpar@1279
   208
    
alpar@1279
   209
    ///This data structure strores a linear expression of the variables
alpar@1279
   210
    ///(\ref Col "Col"s) and also has a constant component.
alpar@1279
   211
    ///
alpar@1279
   212
    ///There are several ways to access and modify the contents of this
alpar@1279
   213
    ///container.
alpar@1279
   214
    ///- Its it fully compatible with \c std::map<Col,double>, so for expamle
alpar@1364
   215
    ///if \c e is an Expr and \c v and \c w are of type \ref Col, then you can
alpar@1279
   216
    ///read and modify the coefficients like
alpar@1279
   217
    ///these.
alpar@1279
   218
    ///\code
alpar@1279
   219
    ///e[v]=5;
alpar@1279
   220
    ///e[v]+=12;
alpar@1279
   221
    ///e.erase(v);
alpar@1279
   222
    ///\endcode
alpar@1279
   223
    ///or you can also iterate through its elements.
alpar@1279
   224
    ///\code
alpar@1279
   225
    ///double s=0;
alpar@1279
   226
    ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i)
alpar@1279
   227
    ///  s+=i->second;
alpar@1279
   228
    ///\endcode
alpar@1279
   229
    ///(This code computes the sum of all coefficients).
alpar@1279
   230
    ///- Numbers (<tt>double</tt>'s)
alpar@1279
   231
    ///and variables (\ref Col "Col"s) directly convert to an
alpar@1279
   232
    ///\ref Expr and the usual linear operations are defined so  
alpar@1279
   233
    ///\code
alpar@1279
   234
    ///v+w
alpar@1279
   235
    ///2*v-3.12*(v-w/2)+2
alpar@1279
   236
    ///v*2.1+(3*v+(v*12+w+6)*3)/2
alpar@1279
   237
    ///\endcode
alpar@1328
   238
    ///are valid \ref Expr "Expr"essions.
alpar@1328
   239
    ///The usual assignment operations are also defined.
alpar@1279
   240
    ///\code
alpar@1279
   241
    ///e=v+w;
alpar@1279
   242
    ///e+=2*v-3.12*(v-w/2)+2;
alpar@1279
   243
    ///e*=3.4;
alpar@1279
   244
    ///e/=5;
alpar@1279
   245
    ///\endcode
alpar@1279
   246
    ///- The constant member can be set and read by \ref constComp()
alpar@1279
   247
    ///\code
alpar@1279
   248
    ///e.constComp()=12;
alpar@1279
   249
    ///double c=e.constComp();
alpar@1279
   250
    ///\endcode
alpar@1279
   251
    ///
alpar@1328
   252
    ///\note \ref clear() not only sets all coefficients to 0 but also
alpar@1279
   253
    ///clears the constant components.
alpar@1328
   254
    ///
alpar@1328
   255
    ///\sa Constr
alpar@1328
   256
    ///
alpar@1273
   257
    class Expr : public std::map<Col,Value>
alpar@1272
   258
    {
alpar@1272
   259
    public:
alpar@1273
   260
      typedef LpSolverBase::Col Key; 
alpar@1273
   261
      typedef LpSolverBase::Value Value;
alpar@1272
   262
      
alpar@1272
   263
    protected:
alpar@1273
   264
      typedef std::map<Col,Value> Base;
alpar@1272
   265
      
alpar@1273
   266
      Value const_comp;
alpar@1272
   267
  public:
alpar@1272
   268
      typedef True IsLinExpression;
alpar@1272
   269
      ///\e
alpar@1272
   270
      Expr() : Base(), const_comp(0) { }
alpar@1272
   271
      ///\e
alpar@1273
   272
      Expr(const Key &v) : const_comp(0) {
alpar@1272
   273
	Base::insert(std::make_pair(v, 1));
alpar@1272
   274
      }
alpar@1272
   275
      ///\e
alpar@1273
   276
      Expr(const Value &v) : const_comp(v) {}
alpar@1272
   277
      ///\e
alpar@1273
   278
      void set(const Key &v,const Value &c) {
alpar@1272
   279
	Base::insert(std::make_pair(v, c));
alpar@1272
   280
      }
alpar@1272
   281
      ///\e
alpar@1273
   282
      Value &constComp() { return const_comp; }
alpar@1272
   283
      ///\e
alpar@1273
   284
      const Value &constComp() const { return const_comp; }
alpar@1272
   285
      
alpar@1272
   286
      ///Removes the components with zero coefficient.
alpar@1272
   287
      void simplify() {
alpar@1272
   288
	for (Base::iterator i=Base::begin(); i!=Base::end();) {
alpar@1272
   289
	  Base::iterator j=i;
alpar@1272
   290
	  ++j;
alpar@1272
   291
	  if ((*i).second==0) Base::erase(i);
alpar@1272
   292
	  j=i;
alpar@1272
   293
	}
alpar@1272
   294
      }
alpar@1273
   295
alpar@1273
   296
      ///Sets all coefficients and the constant component to 0.
alpar@1273
   297
      void clear() {
alpar@1273
   298
	Base::clear();
alpar@1273
   299
	const_comp=0;
alpar@1273
   300
      }
alpar@1273
   301
alpar@1272
   302
      ///\e
alpar@1272
   303
      Expr &operator+=(const Expr &e) {
alpar@1272
   304
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1272
   305
	  (*this)[j->first]+=j->second;
alpar@1272
   306
	///\todo it might be speeded up using "hints"
alpar@1272
   307
	const_comp+=e.const_comp;
alpar@1272
   308
	return *this;
alpar@1272
   309
      }
alpar@1272
   310
      ///\e
alpar@1272
   311
      Expr &operator-=(const Expr &e) {
alpar@1272
   312
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1272
   313
	  (*this)[j->first]-=j->second;
alpar@1272
   314
	const_comp-=e.const_comp;
alpar@1272
   315
	return *this;
alpar@1272
   316
      }
alpar@1272
   317
      ///\e
alpar@1273
   318
      Expr &operator*=(const Value &c) {
alpar@1272
   319
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1272
   320
	  j->second*=c;
alpar@1272
   321
	const_comp*=c;
alpar@1272
   322
	return *this;
alpar@1272
   323
      }
alpar@1272
   324
      ///\e
alpar@1273
   325
      Expr &operator/=(const Value &c) {
alpar@1272
   326
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1272
   327
	  j->second/=c;
alpar@1272
   328
	const_comp/=c;
alpar@1272
   329
	return *this;
alpar@1272
   330
      }
alpar@1272
   331
    };
alpar@1272
   332
    
alpar@1264
   333
    ///Linear constraint
alpar@1328
   334
alpar@1364
   335
    ///This data stucture represents a linear constraint in the LP.
alpar@1364
   336
    ///Basically it is a linear expression with a lower or an upper bound
alpar@1364
   337
    ///(or both). These parts of the constraint can be obtained by the member
alpar@1364
   338
    ///functions \ref expr(), \ref lowerBound() and \ref upperBound(),
alpar@1364
   339
    ///respectively.
alpar@1364
   340
    ///There are two ways to construct a constraint.
alpar@1364
   341
    ///- You can set the linear expression and the bounds directly
alpar@1364
   342
    ///  by the functions above.
alpar@1364
   343
    ///- The operators <tt>\<=</tt>, <tt>==</tt> and  <tt>\>=</tt>
alpar@1364
   344
    ///  are defined between expressions, or even between constraints whenever
alpar@1364
   345
    ///  it makes sense. Therefore if \c e and \c f are linear expressions and
alpar@1364
   346
    ///  \c s and \c t are numbers, then the followings are valid expressions
alpar@1364
   347
    ///  and thus they can be used directly e.g. in \ref addRow() whenever
alpar@1364
   348
    ///  it makes sense.
alpar@1364
   349
    ///  \code
alpar@1364
   350
    ///  e<=s
alpar@1364
   351
    ///  e<=f
alpar@1364
   352
    ///  s<=e<=t
alpar@1364
   353
    ///  e>=t
alpar@1364
   354
    ///  \endcode
alpar@1364
   355
    ///\warning The validity of a constraint is checked only at run time, so
alpar@1364
   356
    ///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw a
alpar@1364
   357
    ///\ref LogicError exception.
alpar@1272
   358
    class Constr
alpar@1272
   359
    {
alpar@1272
   360
    public:
alpar@1272
   361
      typedef LpSolverBase::Expr Expr;
alpar@1273
   362
      typedef Expr::Key Key;
alpar@1273
   363
      typedef Expr::Value Value;
alpar@1272
   364
      
alpar@1364
   365
//       static const Value INF;
alpar@1364
   366
//       static const Value NaN;
alpar@1364
   367
alpar@1273
   368
    protected:
alpar@1273
   369
      Expr _expr;
alpar@1273
   370
      Value _lb,_ub;
alpar@1273
   371
    public:
alpar@1273
   372
      ///\e
alpar@1273
   373
      Constr() : _expr(), _lb(NaN), _ub(NaN) {}
alpar@1273
   374
      ///\e
alpar@1273
   375
      Constr(Value lb,const Expr &e,Value ub) :
alpar@1273
   376
	_expr(e), _lb(lb), _ub(ub) {}
alpar@1273
   377
      ///\e
alpar@1273
   378
      Constr(const Expr &e,Value ub) : 
alpar@1273
   379
	_expr(e), _lb(NaN), _ub(ub) {}
alpar@1273
   380
      ///\e
alpar@1273
   381
      Constr(Value lb,const Expr &e) :
alpar@1273
   382
	_expr(e), _lb(lb), _ub(NaN) {}
alpar@1273
   383
      ///\e
alpar@1272
   384
      Constr(const Expr &e) : 
alpar@1273
   385
	_expr(e), _lb(NaN), _ub(NaN) {}
alpar@1273
   386
      ///\e
alpar@1273
   387
      void clear() 
alpar@1273
   388
      {
alpar@1273
   389
	_expr.clear();
alpar@1273
   390
	_lb=_ub=NaN;
alpar@1273
   391
      }
alpar@1364
   392
alpar@1364
   393
      ///Reference to the linear expression 
alpar@1273
   394
      Expr &expr() { return _expr; }
alpar@1364
   395
      ///Cont reference to the linear expression 
alpar@1273
   396
      const Expr &expr() const { return _expr; }
alpar@1364
   397
      ///Reference to the lower bound.
alpar@1364
   398
alpar@1364
   399
      ///\return
alpar@1536
   400
      ///- \ref INF "INF": the constraint is lower unbounded.
alpar@1536
   401
      ///- \ref NaN "NaN": lower bound has not been set.
alpar@1364
   402
      ///- finite number: the lower bound
alpar@1273
   403
      Value &lowerBound() { return _lb; }
alpar@1364
   404
      ///The const version of \ref lowerBound()
alpar@1273
   405
      const Value &lowerBound() const { return _lb; }
alpar@1364
   406
      ///Reference to the upper bound.
alpar@1364
   407
alpar@1364
   408
      ///\return
alpar@1536
   409
      ///- \ref INF "INF": the constraint is upper unbounded.
alpar@1536
   410
      ///- \ref NaN "NaN": upper bound has not been set.
alpar@1364
   411
      ///- finite number: the upper bound
alpar@1273
   412
      Value &upperBound() { return _ub; }
alpar@1364
   413
      ///The const version of \ref upperBound()
alpar@1273
   414
      const Value &upperBound() const { return _ub; }
alpar@1364
   415
      ///Is the constraint lower bounded?
alpar@1295
   416
      bool lowerBounded() const { 
alpar@1295
   417
	using namespace std;
alpar@1397
   418
	return finite(_lb);
alpar@1295
   419
      }
alpar@1364
   420
      ///Is the constraint upper bounded?
alpar@1295
   421
      bool upperBounded() const {
alpar@1295
   422
	using namespace std;
alpar@1397
   423
	return finite(_ub);
alpar@1295
   424
      }
alpar@1272
   425
    };
alpar@1272
   426
    
alpar@1445
   427
    ///Linear expression of rows
alpar@1445
   428
    
alpar@1445
   429
    ///This data structure represents a column of the matrix,
alpar@1445
   430
    ///thas is it strores a linear expression of the dual variables
alpar@1445
   431
    ///(\ref Row "Row"s).
alpar@1445
   432
    ///
alpar@1445
   433
    ///There are several ways to access and modify the contents of this
alpar@1445
   434
    ///container.
alpar@1445
   435
    ///- Its it fully compatible with \c std::map<Row,double>, so for expamle
alpar@1445
   436
    ///if \c e is an DualExpr and \c v
alpar@1445
   437
    ///and \c w are of type \ref Row, then you can
alpar@1445
   438
    ///read and modify the coefficients like
alpar@1445
   439
    ///these.
alpar@1445
   440
    ///\code
alpar@1445
   441
    ///e[v]=5;
alpar@1445
   442
    ///e[v]+=12;
alpar@1445
   443
    ///e.erase(v);
alpar@1445
   444
    ///\endcode
alpar@1445
   445
    ///or you can also iterate through its elements.
alpar@1445
   446
    ///\code
alpar@1445
   447
    ///double s=0;
alpar@1445
   448
    ///for(LpSolverBase::DualExpr::iterator i=e.begin();i!=e.end();++i)
alpar@1445
   449
    ///  s+=i->second;
alpar@1445
   450
    ///\endcode
alpar@1445
   451
    ///(This code computes the sum of all coefficients).
alpar@1445
   452
    ///- Numbers (<tt>double</tt>'s)
alpar@1445
   453
    ///and variables (\ref Row "Row"s) directly convert to an
alpar@1445
   454
    ///\ref DualExpr and the usual linear operations are defined so  
alpar@1445
   455
    ///\code
alpar@1445
   456
    ///v+w
alpar@1445
   457
    ///2*v-3.12*(v-w/2)
alpar@1445
   458
    ///v*2.1+(3*v+(v*12+w)*3)/2
alpar@1445
   459
    ///\endcode
alpar@1445
   460
    ///are valid \ref DualExpr "DualExpr"essions.
alpar@1445
   461
    ///The usual assignment operations are also defined.
alpar@1445
   462
    ///\code
alpar@1445
   463
    ///e=v+w;
alpar@1445
   464
    ///e+=2*v-3.12*(v-w/2);
alpar@1445
   465
    ///e*=3.4;
alpar@1445
   466
    ///e/=5;
alpar@1445
   467
    ///\endcode
alpar@1445
   468
    ///
alpar@1445
   469
    ///\sa Expr
alpar@1445
   470
    ///
alpar@1445
   471
    class DualExpr : public std::map<Row,Value>
alpar@1445
   472
    {
alpar@1445
   473
    public:
alpar@1445
   474
      typedef LpSolverBase::Row Key; 
alpar@1445
   475
      typedef LpSolverBase::Value Value;
alpar@1445
   476
      
alpar@1445
   477
    protected:
alpar@1445
   478
      typedef std::map<Row,Value> Base;
alpar@1445
   479
      
alpar@1445
   480
    public:
alpar@1445
   481
      typedef True IsLinExpression;
alpar@1445
   482
      ///\e
alpar@1445
   483
      DualExpr() : Base() { }
alpar@1445
   484
      ///\e
alpar@1445
   485
      DualExpr(const Key &v) {
alpar@1445
   486
	Base::insert(std::make_pair(v, 1));
alpar@1445
   487
      }
alpar@1445
   488
      ///\e
alpar@1445
   489
      void set(const Key &v,const Value &c) {
alpar@1445
   490
	Base::insert(std::make_pair(v, c));
alpar@1445
   491
      }
alpar@1445
   492
      
alpar@1445
   493
      ///Removes the components with zero coefficient.
alpar@1445
   494
      void simplify() {
alpar@1445
   495
	for (Base::iterator i=Base::begin(); i!=Base::end();) {
alpar@1445
   496
	  Base::iterator j=i;
alpar@1445
   497
	  ++j;
alpar@1445
   498
	  if ((*i).second==0) Base::erase(i);
alpar@1445
   499
	  j=i;
alpar@1445
   500
	}
alpar@1445
   501
      }
alpar@1445
   502
alpar@1445
   503
      ///Sets all coefficients to 0.
alpar@1445
   504
      void clear() {
alpar@1445
   505
	Base::clear();
alpar@1445
   506
      }
alpar@1445
   507
alpar@1445
   508
      ///\e
alpar@1445
   509
      DualExpr &operator+=(const DualExpr &e) {
alpar@1445
   510
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1445
   511
	  (*this)[j->first]+=j->second;
alpar@1445
   512
	///\todo it might be speeded up using "hints"
alpar@1445
   513
	return *this;
alpar@1445
   514
      }
alpar@1445
   515
      ///\e
alpar@1445
   516
      DualExpr &operator-=(const DualExpr &e) {
alpar@1445
   517
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1445
   518
	  (*this)[j->first]-=j->second;
alpar@1445
   519
	return *this;
alpar@1445
   520
      }
alpar@1445
   521
      ///\e
alpar@1445
   522
      DualExpr &operator*=(const Value &c) {
alpar@1445
   523
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1445
   524
	  j->second*=c;
alpar@1445
   525
	return *this;
alpar@1445
   526
      }
alpar@1445
   527
      ///\e
alpar@1445
   528
      DualExpr &operator/=(const Value &c) {
alpar@1445
   529
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1445
   530
	  j->second/=c;
alpar@1445
   531
	return *this;
alpar@1445
   532
      }
alpar@1445
   533
    };
alpar@1445
   534
    
alpar@1253
   535
alpar@1253
   536
  protected:
alpar@1253
   537
    _FixId rows;
alpar@1253
   538
    _FixId cols;
athos@1246
   539
alpar@1323
   540
    //Abstract virtual functions
alpar@1364
   541
    virtual LpSolverBase &_newLp() = 0;
athos@1436
   542
    virtual LpSolverBase &_copyLp(){
athos@1436
   543
      ///\todo This should be implemented here, too,  when we have problem retrieving routines. It can be overriden.
athos@1436
   544
athos@1436
   545
      //Starting:
athos@1436
   546
      LpSolverBase & newlp(_newLp());
athos@1436
   547
      return newlp;
athos@1436
   548
      //return *(LpSolverBase*)0;
athos@1436
   549
    };
alpar@1364
   550
athos@1246
   551
    virtual int _addCol() = 0;
athos@1246
   552
    virtual int _addRow() = 0;
athos@1542
   553
    virtual void _eraseCol(int col) = 0;
athos@1542
   554
    virtual void _eraseRow(int row) = 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@1612
   607
    ///\name Build up and modify 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)
athos@1542
   683
    ///\bug This is a temporary 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
athos@1542
   720
    ///\brief Add several new rows
athos@1542
   721
    ///(i.e a constraints) 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
    }
athos@1542
   846
    ///Erase a coloumn (i.e a variable) from the LP
athos@1542
   847
athos@1542
   848
    ///\param c is the coloumn to be deleted
athos@1542
   849
    ///\todo Please check this
athos@1542
   850
    void eraseCol(Col c) {
athos@1542
   851
      _eraseCol(cols.floatingId(c.id));
athos@1542
   852
      cols.erase(c.id);
athos@1542
   853
    }
athos@1542
   854
    ///Erase a  row (i.e a constraint) from the LP
athos@1542
   855
athos@1542
   856
    ///\param r is the row to be deleted
athos@1542
   857
    ///\todo Please check this
athos@1542
   858
    void eraseRow(Row r) {
athos@1542
   859
      _eraseRow(rows.floatingId(r.id));
athos@1542
   860
      rows.erase(r.id);
athos@1542
   861
    }
alpar@1264
   862
athos@1436
   863
    ///Set an element of the coefficient matrix of the LP
athos@1436
   864
athos@1436
   865
    ///\param r is the row of the element to be modified
athos@1436
   866
    ///\param c is the coloumn of the element to be modified
athos@1436
   867
    ///\param val is the new value of the coefficient
athos@1436
   868
    void setCoeff(Row r, Col c, Value val){
athos@1436
   869
      _setCoeff(rows.floatingId(r.id),cols.floatingId(c.id), val);
athos@1436
   870
    }
athos@1436
   871
alpar@1253
   872
    /// Set the lower bound of a column (i.e a variable)
alpar@1253
   873
alpar@1293
   874
    /// The upper bound of a variable (column) has to be given by an 
alpar@1253
   875
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   876
    /// Value or -\ref INF.
alpar@1293
   877
    void colLowerBound(Col c, Value value) {
alpar@1253
   878
      _setColLowerBound(cols.floatingId(c.id),value);
alpar@1253
   879
    }
alpar@1253
   880
    /// Set the upper bound of a column (i.e a variable)
alpar@1253
   881
alpar@1293
   882
    /// The upper bound of a variable (column) has to be given by an 
alpar@1253
   883
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   884
    /// Value or \ref INF.
alpar@1293
   885
    void colUpperBound(Col c, Value value) {
alpar@1253
   886
      _setColUpperBound(cols.floatingId(c.id),value);
alpar@1253
   887
    };
alpar@1293
   888
    /// Set the lower and the upper bounds of a column (i.e a variable)
alpar@1293
   889
alpar@1293
   890
    /// The lower and the upper bounds of
alpar@1293
   891
    /// a variable (column) have to be given by an 
alpar@1293
   892
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
   893
    /// Value, -\ref INF or \ref INF.
alpar@1293
   894
    void colBounds(Col c, Value lower, Value upper) {
alpar@1293
   895
      _setColLowerBound(cols.floatingId(c.id),lower);
alpar@1293
   896
      _setColUpperBound(cols.floatingId(c.id),upper);
alpar@1293
   897
    }
alpar@1293
   898
    
athos@1405
   899
//     /// Set the lower bound of a row (i.e a constraint)
alpar@1253
   900
athos@1405
   901
//     /// The lower bound of a linear expression (row) has to be given by an 
athos@1405
   902
//     /// extended number of type Value, i.e. a finite number of type 
athos@1405
   903
//     /// Value or -\ref INF.
athos@1405
   904
//     void rowLowerBound(Row r, Value value) {
athos@1405
   905
//       _setRowLowerBound(rows.floatingId(r.id),value);
athos@1405
   906
//     };
athos@1405
   907
//     /// Set the upper bound of a row (i.e a constraint)
alpar@1253
   908
athos@1405
   909
//     /// The upper bound of a linear expression (row) has to be given by an 
athos@1405
   910
//     /// extended number of type Value, i.e. a finite number of type 
athos@1405
   911
//     /// Value or \ref INF.
athos@1405
   912
//     void rowUpperBound(Row r, Value value) {
athos@1405
   913
//       _setRowUpperBound(rows.floatingId(r.id),value);
athos@1405
   914
//     };
athos@1405
   915
athos@1405
   916
    /// Set the lower and the upper bounds of a row (i.e a constraint)
alpar@1293
   917
alpar@1293
   918
    /// The lower and the upper bounds of
alpar@1293
   919
    /// a constraint (row) have to be given by an 
alpar@1293
   920
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
   921
    /// Value, -\ref INF or \ref INF.
alpar@1293
   922
    void rowBounds(Row c, Value lower, Value upper) {
athos@1379
   923
      _setRowBounds(rows.floatingId(c.id),lower, upper);
athos@1379
   924
      // _setRowUpperBound(rows.floatingId(c.id),upper);
alpar@1293
   925
    }
alpar@1293
   926
    
alpar@1253
   927
    ///Set an element of the objective function
alpar@1293
   928
    void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); };
alpar@1253
   929
    ///Set the objective function
alpar@1253
   930
    
alpar@1253
   931
    ///\param e is a linear expression of type \ref Expr.
alpar@1323
   932
    ///\bug The previous objective function is not cleared!
alpar@1253
   933
    void setObj(Expr e) {
athos@1377
   934
      _clearObj();
alpar@1253
   935
      for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
alpar@1293
   936
	objCoeff((*i).first,(*i).second);
alpar@1323
   937
      obj_const_comp=e.constComp();
alpar@1253
   938
    }
alpar@1263
   939
alpar@1312
   940
    ///Maximize
alpar@1312
   941
    void max() { _setMax(); }
alpar@1312
   942
    ///Minimize
alpar@1312
   943
    void min() { _setMin(); }
alpar@1312
   944
alpar@1312
   945
    
alpar@1263
   946
    ///@}
alpar@1263
   947
alpar@1263
   948
alpar@1294
   949
    ///\name Solve the LP
alpar@1263
   950
alpar@1263
   951
    ///@{
alpar@1263
   952
athos@1458
   953
    ///\e Solve the LP problem at hand
athos@1458
   954
    ///
athos@1458
   955
    ///\return The result of the optimization procedure. Possible values and their meanings can be found in the documentation of \ref SolveExitStatus.
athos@1458
   956
    ///
athos@1458
   957
    ///\todo Which method is used to solve the problem
alpar@1303
   958
    SolveExitStatus solve() { return _solve(); }
alpar@1263
   959
    
alpar@1263
   960
    ///@}
alpar@1263
   961
    
alpar@1294
   962
    ///\name Obtain the solution
alpar@1263
   963
alpar@1263
   964
    ///@{
alpar@1263
   965
athos@1460
   966
    /// The status of the primal problem (the original LP problem)
alpar@1312
   967
    SolutionStatus primalStatus() {
alpar@1312
   968
      return _getPrimalStatus();
alpar@1294
   969
    }
alpar@1294
   970
athos@1460
   971
    /// The status of the dual (of the original LP) problem 
athos@1460
   972
    SolutionStatus dualStatus() {
athos@1460
   973
      return _getDualStatus();
athos@1460
   974
    }
athos@1460
   975
athos@1460
   976
    ///The type of the original LP problem
athos@1462
   977
    ProblemTypes problemType() {
athos@1460
   978
      return _getProblemType();
athos@1460
   979
    }
athos@1460
   980
alpar@1294
   981
    ///\e
alpar@1293
   982
    Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); }
alpar@1263
   983
alpar@1312
   984
    ///\e
alpar@1312
   985
alpar@1312
   986
    ///\return
alpar@1312
   987
    ///- \ref INF or -\ref INF means either infeasibility or unboundedness
alpar@1312
   988
    /// of the primal problem, depending on whether we minimize or maximize.
alpar@1364
   989
    ///- \ref NaN if no primal solution is found.
alpar@1312
   990
    ///- The (finite) objective value if an optimal solution is found.
alpar@1323
   991
    Value primalValue() { return _getPrimalValue()+obj_const_comp;}
alpar@1263
   992
    ///@}
alpar@1253
   993
    
athos@1248
   994
  };  
athos@1246
   995
alpar@1272
   996
  ///\e
alpar@1272
   997
  
alpar@1272
   998
  ///\relates LpSolverBase::Expr
alpar@1272
   999
  ///
alpar@1272
  1000
  inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
alpar@1272
  1001
				      const LpSolverBase::Expr &b) 
alpar@1272
  1002
  {
alpar@1272
  1003
    LpSolverBase::Expr tmp(a);
alpar@1364
  1004
    tmp+=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
  1005
    return tmp;
alpar@1272
  1006
  }
alpar@1272
  1007
  ///\e
alpar@1272
  1008
  
alpar@1272
  1009
  ///\relates LpSolverBase::Expr
alpar@1272
  1010
  ///
alpar@1272
  1011
  inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
alpar@1272
  1012
				      const LpSolverBase::Expr &b) 
alpar@1272
  1013
  {
alpar@1272
  1014
    LpSolverBase::Expr tmp(a);
alpar@1364
  1015
    tmp-=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
  1016
    return tmp;
alpar@1272
  1017
  }
alpar@1272
  1018
  ///\e
alpar@1272
  1019
  
alpar@1272
  1020
  ///\relates LpSolverBase::Expr
alpar@1272
  1021
  ///
alpar@1272
  1022
  inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
alpar@1273
  1023
				      const LpSolverBase::Value &b) 
alpar@1272
  1024
  {
alpar@1272
  1025
    LpSolverBase::Expr tmp(a);
alpar@1364
  1026
    tmp*=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
  1027
    return tmp;
alpar@1272
  1028
  }
alpar@1272
  1029
  
alpar@1272
  1030
  ///\e
alpar@1272
  1031
  
alpar@1272
  1032
  ///\relates LpSolverBase::Expr
alpar@1272
  1033
  ///
alpar@1273
  1034
  inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
alpar@1272
  1035
				      const LpSolverBase::Expr &b) 
alpar@1272
  1036
  {
alpar@1272
  1037
    LpSolverBase::Expr tmp(b);
alpar@1364
  1038
    tmp*=a; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
  1039
    return tmp;
alpar@1272
  1040
  }
alpar@1272
  1041
  ///\e
alpar@1272
  1042
  
alpar@1272
  1043
  ///\relates LpSolverBase::Expr
alpar@1272
  1044
  ///
alpar@1272
  1045
  inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
alpar@1273
  1046
				      const LpSolverBase::Value &b) 
alpar@1272
  1047
  {
alpar@1272
  1048
    LpSolverBase::Expr tmp(a);
alpar@1364
  1049
    tmp/=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
  1050
    return tmp;
alpar@1272
  1051
  }
alpar@1272
  1052
  
alpar@1272
  1053
  ///\e
alpar@1272
  1054
  
alpar@1272
  1055
  ///\relates LpSolverBase::Constr
alpar@1272
  1056
  ///
alpar@1272
  1057
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1272
  1058
					 const LpSolverBase::Expr &f) 
alpar@1272
  1059
  {
alpar@1272
  1060
    return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
alpar@1272
  1061
  }
alpar@1272
  1062
alpar@1272
  1063
  ///\e
alpar@1272
  1064
  
alpar@1272
  1065
  ///\relates LpSolverBase::Constr
alpar@1272
  1066
  ///
alpar@1273
  1067
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
alpar@1272
  1068
					 const LpSolverBase::Expr &f) 
alpar@1272
  1069
  {
alpar@1272
  1070
    return LpSolverBase::Constr(e,f);
alpar@1272
  1071
  }
alpar@1272
  1072
alpar@1272
  1073
  ///\e
alpar@1272
  1074
  
alpar@1272
  1075
  ///\relates LpSolverBase::Constr
alpar@1272
  1076
  ///
alpar@1272
  1077
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1273
  1078
					 const LpSolverBase::Value &f) 
alpar@1272
  1079
  {
alpar@1272
  1080
    return LpSolverBase::Constr(e,f);
alpar@1272
  1081
  }
alpar@1272
  1082
alpar@1272
  1083
  ///\e
alpar@1272
  1084
  
alpar@1272
  1085
  ///\relates LpSolverBase::Constr
alpar@1272
  1086
  ///
alpar@1272
  1087
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1272
  1088
					 const LpSolverBase::Expr &f) 
alpar@1272
  1089
  {
alpar@1272
  1090
    return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
alpar@1272
  1091
  }
alpar@1272
  1092
alpar@1272
  1093
alpar@1272
  1094
  ///\e
alpar@1272
  1095
  
alpar@1272
  1096
  ///\relates LpSolverBase::Constr
alpar@1272
  1097
  ///
alpar@1273
  1098
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
alpar@1272
  1099
					 const LpSolverBase::Expr &f) 
alpar@1272
  1100
  {
alpar@1272
  1101
    return LpSolverBase::Constr(f,e);
alpar@1272
  1102
  }
alpar@1272
  1103
alpar@1272
  1104
alpar@1272
  1105
  ///\e
alpar@1272
  1106
  
alpar@1272
  1107
  ///\relates LpSolverBase::Constr
alpar@1272
  1108
  ///
alpar@1272
  1109
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1273
  1110
					 const LpSolverBase::Value &f) 
alpar@1272
  1111
  {
alpar@1272
  1112
    return LpSolverBase::Constr(f,e);
alpar@1272
  1113
  }
alpar@1272
  1114
alpar@1272
  1115
  ///\e
alpar@1272
  1116
  
alpar@1272
  1117
  ///\relates LpSolverBase::Constr
alpar@1272
  1118
  ///
alpar@1272
  1119
  inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
alpar@1272
  1120
					 const LpSolverBase::Expr &f) 
alpar@1272
  1121
  {
alpar@1272
  1122
    return LpSolverBase::Constr(0,e-f,0);
alpar@1272
  1123
  }
alpar@1272
  1124
alpar@1272
  1125
  ///\e
alpar@1272
  1126
  
alpar@1272
  1127
  ///\relates LpSolverBase::Constr
alpar@1272
  1128
  ///
alpar@1273
  1129
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
alpar@1272
  1130
					 const LpSolverBase::Constr&c) 
alpar@1272
  1131
  {
alpar@1272
  1132
    LpSolverBase::Constr tmp(c);
alpar@1273
  1133
    ///\todo Create an own exception type.
alpar@1273
  1134
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
  1135
    else tmp.lowerBound()=n;
alpar@1272
  1136
    return tmp;
alpar@1272
  1137
  }
alpar@1272
  1138
  ///\e
alpar@1272
  1139
  
alpar@1272
  1140
  ///\relates LpSolverBase::Constr
alpar@1272
  1141
  ///
alpar@1272
  1142
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
alpar@1273
  1143
					 const LpSolverBase::Value &n)
alpar@1272
  1144
  {
alpar@1272
  1145
    LpSolverBase::Constr tmp(c);
alpar@1273
  1146
    ///\todo Create an own exception type.
alpar@1273
  1147
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
  1148
    else tmp.upperBound()=n;
alpar@1272
  1149
    return tmp;
alpar@1272
  1150
  }
alpar@1272
  1151
alpar@1272
  1152
  ///\e
alpar@1272
  1153
  
alpar@1272
  1154
  ///\relates LpSolverBase::Constr
alpar@1272
  1155
  ///
alpar@1273
  1156
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
alpar@1272
  1157
					 const LpSolverBase::Constr&c) 
alpar@1272
  1158
  {
alpar@1272
  1159
    LpSolverBase::Constr tmp(c);
alpar@1273
  1160
    ///\todo Create an own exception type.
alpar@1273
  1161
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
  1162
    else tmp.upperBound()=n;
alpar@1272
  1163
    return tmp;
alpar@1272
  1164
  }
alpar@1272
  1165
  ///\e
alpar@1272
  1166
  
alpar@1272
  1167
  ///\relates LpSolverBase::Constr
alpar@1272
  1168
  ///
alpar@1272
  1169
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
alpar@1273
  1170
					 const LpSolverBase::Value &n)
alpar@1272
  1171
  {
alpar@1272
  1172
    LpSolverBase::Constr tmp(c);
alpar@1273
  1173
    ///\todo Create an own exception type.
alpar@1273
  1174
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
  1175
    else tmp.lowerBound()=n;
alpar@1272
  1176
    return tmp;
alpar@1272
  1177
  }
alpar@1272
  1178
alpar@1445
  1179
  ///\e
alpar@1445
  1180
  
alpar@1445
  1181
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1182
  ///
alpar@1445
  1183
  inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a,
alpar@1445
  1184
				      const LpSolverBase::DualExpr &b) 
alpar@1445
  1185
  {
alpar@1445
  1186
    LpSolverBase::DualExpr tmp(a);
alpar@1445
  1187
    tmp+=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1445
  1188
    return tmp;
alpar@1445
  1189
  }
alpar@1445
  1190
  ///\e
alpar@1445
  1191
  
alpar@1445
  1192
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1193
  ///
alpar@1445
  1194
  inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a,
alpar@1445
  1195
				      const LpSolverBase::DualExpr &b) 
alpar@1445
  1196
  {
alpar@1445
  1197
    LpSolverBase::DualExpr tmp(a);
alpar@1445
  1198
    tmp-=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1445
  1199
    return tmp;
alpar@1445
  1200
  }
alpar@1445
  1201
  ///\e
alpar@1445
  1202
  
alpar@1445
  1203
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1204
  ///
alpar@1445
  1205
  inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a,
alpar@1445
  1206
				      const LpSolverBase::Value &b) 
alpar@1445
  1207
  {
alpar@1445
  1208
    LpSolverBase::DualExpr tmp(a);
alpar@1445
  1209
    tmp*=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1445
  1210
    return tmp;
alpar@1445
  1211
  }
alpar@1445
  1212
  
alpar@1445
  1213
  ///\e
alpar@1445
  1214
  
alpar@1445
  1215
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1216
  ///
alpar@1445
  1217
  inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a,
alpar@1445
  1218
				      const LpSolverBase::DualExpr &b) 
alpar@1445
  1219
  {
alpar@1445
  1220
    LpSolverBase::DualExpr tmp(b);
alpar@1445
  1221
    tmp*=a; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1445
  1222
    return tmp;
alpar@1445
  1223
  }
alpar@1445
  1224
  ///\e
alpar@1445
  1225
  
alpar@1445
  1226
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1227
  ///
alpar@1445
  1228
  inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a,
alpar@1445
  1229
				      const LpSolverBase::Value &b) 
alpar@1445
  1230
  {
alpar@1445
  1231
    LpSolverBase::DualExpr tmp(a);
alpar@1445
  1232
    tmp/=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1445
  1233
    return tmp;
alpar@1445
  1234
  }
alpar@1445
  1235
  
alpar@1272
  1236
athos@1246
  1237
} //namespace lemon
athos@1246
  1238
athos@1246
  1239
#endif //LEMON_LP_BASE_H