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