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