lemon/lp_base.h
author athos
Mon, 04 Dec 2006 16:48:13 +0000
changeset 2324 18fc834761d9
parent 2312 07e46cbb7d85
child 2328 b4931ae52069
permissions -rw-r--r--
Some query functions got implemented, but only for GLPK.
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;
athos@2324
   706
    virtual Value _getCoeff(int row, int col) = 0;
athos@2324
   707
alpar@1294
   708
    virtual void _setColLowerBound(int i, Value value) = 0;
alpar@1294
   709
    virtual void _setColUpperBound(int i, Value value) = 0;
athos@1405
   710
//     virtual void _setRowLowerBound(int i, Value value) = 0;
athos@1405
   711
//     virtual void _setRowUpperBound(int i, Value value) = 0;
athos@1379
   712
    virtual void _setRowBounds(int i, Value lower, Value upper) = 0;
alpar@1294
   713
    virtual void _setObjCoeff(int i, Value obj_coef) = 0;
athos@2324
   714
    virtual Value _getObjCoeff(int i) = 0;
athos@1377
   715
    virtual void _clearObj()=0;
deba@2312
   716
alpar@1303
   717
    virtual SolveExitStatus _solve() = 0;
alpar@1294
   718
    virtual Value _getPrimal(int i) = 0;
marci@1787
   719
    virtual Value _getDual(int i) = 0;
alpar@1312
   720
    virtual Value _getPrimalValue() = 0;
marci@1840
   721
    virtual bool _isBasicCol(int i) = 0;
alpar@1312
   722
    virtual SolutionStatus _getPrimalStatus() = 0;
athos@1460
   723
    virtual SolutionStatus _getDualStatus() = 0;
athos@1460
   724
    ///\todo This could be implemented here, too, using _getPrimalStatus() and
athos@1460
   725
    ///_getDualStatus()
athos@1460
   726
    virtual ProblemTypes _getProblemType() = 0;
athos@1460
   727
alpar@1312
   728
    virtual void _setMax() = 0;
alpar@1312
   729
    virtual void _setMin() = 0;
alpar@1312
   730
    
athos@2324
   731
athos@2324
   732
    virtual bool _isMax() = 0;
athos@2324
   733
alpar@1323
   734
    //Own protected stuff
alpar@1323
   735
    
alpar@1323
   736
    //Constant component of the objective function
alpar@1323
   737
    Value obj_const_comp;
deba@2312
   738
        
alpar@1253
   739
  public:
alpar@1253
   740
alpar@1323
   741
    ///\e
alpar@1323
   742
    LpSolverBase() : obj_const_comp(0) {}
alpar@1253
   743
alpar@1253
   744
    ///\e
alpar@1253
   745
    virtual ~LpSolverBase() {}
alpar@1253
   746
alpar@1364
   747
    ///Creates a new LP problem
alpar@1364
   748
    LpSolverBase &newLp() {return _newLp();}
alpar@1381
   749
    ///Makes a copy of the LP problem
alpar@1364
   750
    LpSolverBase &copyLp() {return _copyLp();}
alpar@1364
   751
    
alpar@1612
   752
    ///\name Build up and modify the LP
alpar@1263
   753
alpar@1263
   754
    ///@{
alpar@1263
   755
alpar@1253
   756
    ///Add a new empty column (i.e a new variable) to the LP
alpar@1253
   757
    Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
alpar@1263
   758
alpar@1294
   759
    ///\brief Adds several new columns
alpar@1294
   760
    ///(i.e a variables) at once
alpar@1256
   761
    ///
alpar@1273
   762
    ///This magic function takes a container as its argument
alpar@1256
   763
    ///and fills its elements
alpar@1256
   764
    ///with new columns (i.e. variables)
alpar@1273
   765
    ///\param t can be
alpar@1273
   766
    ///- a standard STL compatible iterable container with
alpar@1273
   767
    ///\ref Col as its \c values_type
alpar@1273
   768
    ///like
alpar@1273
   769
    ///\code
alpar@1273
   770
    ///std::vector<LpSolverBase::Col>
alpar@1273
   771
    ///std::list<LpSolverBase::Col>
alpar@1273
   772
    ///\endcode
alpar@1273
   773
    ///- a standard STL compatible iterable container with
alpar@1273
   774
    ///\ref Col as its \c mapped_type
alpar@1273
   775
    ///like
alpar@1273
   776
    ///\code
alpar@1364
   777
    ///std::map<AnyType,LpSolverBase::Col>
alpar@1273
   778
    ///\endcode
alpar@2260
   779
    ///- an iterable lemon \ref concepts::WriteMap "write map" like 
alpar@1273
   780
    ///\code
alpar@1273
   781
    ///ListGraph::NodeMap<LpSolverBase::Col>
alpar@1273
   782
    ///ListGraph::EdgeMap<LpSolverBase::Col>
alpar@1273
   783
    ///\endcode
alpar@1256
   784
    ///\return The number of the created column.
alpar@1256
   785
#ifdef DOXYGEN
alpar@1256
   786
    template<class T>
alpar@1256
   787
    int addColSet(T &t) { return 0;} 
alpar@1256
   788
#else
alpar@1256
   789
    template<class T>
alpar@1256
   790
    typename enable_if<typename T::value_type::LpSolverCol,int>::type
alpar@1256
   791
    addColSet(T &t,dummy<0> = 0) {
alpar@1256
   792
      int s=0;
alpar@1256
   793
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
alpar@1256
   794
      return s;
alpar@1256
   795
    }
alpar@1256
   796
    template<class T>
alpar@1256
   797
    typename enable_if<typename T::value_type::second_type::LpSolverCol,
alpar@1256
   798
		       int>::type
alpar@1256
   799
    addColSet(T &t,dummy<1> = 1) { 
alpar@1256
   800
      int s=0;
alpar@1256
   801
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1256
   802
	i->second=addCol();
alpar@1256
   803
	s++;
alpar@1256
   804
      }
alpar@1256
   805
      return s;
alpar@1256
   806
    }
alpar@1272
   807
    template<class T>
deba@1810
   808
    typename enable_if<typename T::MapIt::Value::LpSolverCol,
alpar@1272
   809
		       int>::type
alpar@1272
   810
    addColSet(T &t,dummy<2> = 2) { 
alpar@1272
   811
      int s=0;
deba@1810
   812
      for(typename T::MapIt i(t); i!=INVALID; ++i)
alpar@1272
   813
	{
deba@1810
   814
	  i.set(addCol());
alpar@1272
   815
	  s++;
alpar@1272
   816
	}
alpar@1272
   817
      return s;
alpar@1272
   818
    }
alpar@1256
   819
#endif
alpar@1263
   820
alpar@1445
   821
    ///Set a column (i.e a dual constraint) of the LP
alpar@1258
   822
alpar@1445
   823
    ///\param c is the column to be modified
alpar@1445
   824
    ///\param e is a dual linear expression (see \ref DualExpr)
alpar@1445
   825
    ///a better one.
alpar@1899
   826
    void col(Col c,const DualExpr &e) {
deba@2312
   827
      e.simplify();
deba@2312
   828
      _setColCoeffs(_lpId(c), LpColIterator(e.begin(), *this), 
deba@2312
   829
                    LpColIterator(e.end(), *this));
alpar@1445
   830
    }
alpar@1445
   831
alpar@1445
   832
    ///Add a new column to the LP
alpar@1445
   833
alpar@1445
   834
    ///\param e is a dual linear expression (see \ref DualExpr)
alpar@1445
   835
    ///\param obj is the corresponding component of the objective
alpar@1445
   836
    ///function. It is 0 by default.
alpar@1445
   837
    ///\return The created column.
alpar@1493
   838
    Col addCol(const DualExpr &e, Value obj=0) {
alpar@1445
   839
      Col c=addCol();
alpar@1899
   840
      col(c,e);
alpar@1493
   841
      objCoeff(c,obj);
alpar@1445
   842
      return c;
alpar@1445
   843
    }
alpar@1445
   844
alpar@1445
   845
    ///Add a new empty row (i.e a new constraint) to the LP
alpar@1445
   846
alpar@1445
   847
    ///This function adds a new empty row (i.e a new constraint) to the LP.
alpar@1258
   848
    ///\return The created row
alpar@1253
   849
    Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
alpar@1253
   850
athos@1542
   851
    ///\brief Add several new rows
athos@1542
   852
    ///(i.e a constraints) at once
alpar@1445
   853
    ///
alpar@1445
   854
    ///This magic function takes a container as its argument
alpar@1445
   855
    ///and fills its elements
alpar@1445
   856
    ///with new row (i.e. variables)
alpar@1445
   857
    ///\param t can be
alpar@1445
   858
    ///- a standard STL compatible iterable container with
alpar@1445
   859
    ///\ref Row as its \c values_type
alpar@1445
   860
    ///like
alpar@1445
   861
    ///\code
alpar@1445
   862
    ///std::vector<LpSolverBase::Row>
alpar@1445
   863
    ///std::list<LpSolverBase::Row>
alpar@1445
   864
    ///\endcode
alpar@1445
   865
    ///- a standard STL compatible iterable container with
alpar@1445
   866
    ///\ref Row as its \c mapped_type
alpar@1445
   867
    ///like
alpar@1445
   868
    ///\code
alpar@1445
   869
    ///std::map<AnyType,LpSolverBase::Row>
alpar@1445
   870
    ///\endcode
alpar@2260
   871
    ///- an iterable lemon \ref concepts::WriteMap "write map" like 
alpar@1445
   872
    ///\code
alpar@1445
   873
    ///ListGraph::NodeMap<LpSolverBase::Row>
alpar@1445
   874
    ///ListGraph::EdgeMap<LpSolverBase::Row>
alpar@1445
   875
    ///\endcode
alpar@1445
   876
    ///\return The number of rows created.
alpar@1445
   877
#ifdef DOXYGEN
alpar@1445
   878
    template<class T>
alpar@1445
   879
    int addRowSet(T &t) { return 0;} 
alpar@1445
   880
#else
alpar@1445
   881
    template<class T>
alpar@1445
   882
    typename enable_if<typename T::value_type::LpSolverRow,int>::type
alpar@1445
   883
    addRowSet(T &t,dummy<0> = 0) {
alpar@1445
   884
      int s=0;
alpar@1445
   885
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;}
alpar@1445
   886
      return s;
alpar@1445
   887
    }
alpar@1445
   888
    template<class T>
alpar@1445
   889
    typename enable_if<typename T::value_type::second_type::LpSolverRow,
alpar@1445
   890
		       int>::type
alpar@1445
   891
    addRowSet(T &t,dummy<1> = 1) { 
alpar@1445
   892
      int s=0;
alpar@1445
   893
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1445
   894
	i->second=addRow();
alpar@1445
   895
	s++;
alpar@1445
   896
      }
alpar@1445
   897
      return s;
alpar@1445
   898
    }
alpar@1445
   899
    template<class T>
deba@1810
   900
    typename enable_if<typename T::MapIt::Value::LpSolverRow,
alpar@1445
   901
		       int>::type
alpar@1445
   902
    addRowSet(T &t,dummy<2> = 2) { 
alpar@1445
   903
      int s=0;
deba@1810
   904
      for(typename T::MapIt i(t); i!=INVALID; ++i)
alpar@1445
   905
	{
deba@1810
   906
	  i.set(addRow());
alpar@1445
   907
	  s++;
alpar@1445
   908
	}
alpar@1445
   909
      return s;
alpar@1445
   910
    }
alpar@1445
   911
#endif
alpar@1445
   912
alpar@1445
   913
    ///Set a row (i.e a constraint) of the LP
alpar@1253
   914
alpar@1258
   915
    ///\param r is the row to be modified
alpar@1259
   916
    ///\param l is lower bound (-\ref INF means no bound)
alpar@1258
   917
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   918
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1253
   919
    ///\bug This is a temportary function. The interface will change to
alpar@1253
   920
    ///a better one.
alpar@1328
   921
    ///\todo Option to control whether a constraint with a single variable is
alpar@1328
   922
    ///added or not.
alpar@1895
   923
    void row(Row r, Value l,const Expr &e, Value u) {
deba@2312
   924
      e.simplify();
deba@2312
   925
      _setRowCoeffs(_lpId(r), LpRowIterator(e.begin(), *this),
deba@2312
   926
                    LpRowIterator(e.end(), *this));
deba@2312
   927
//       _setRowLowerBound(_lpId(r),l-e.constComp());
deba@2312
   928
//       _setRowUpperBound(_lpId(r),u-e.constComp());
deba@2312
   929
       _setRowBounds(_lpId(r),l-e.constComp(),u-e.constComp());
alpar@1258
   930
    }
alpar@1258
   931
alpar@1445
   932
    ///Set a row (i.e a constraint) of the LP
alpar@1264
   933
alpar@1264
   934
    ///\param r is the row to be modified
alpar@1264
   935
    ///\param c is a linear expression (see \ref Constr)
alpar@1895
   936
    void row(Row r, const Constr &c) {
deba@2312
   937
      row(r, c.lowerBounded()?c.lowerBound():-INF,
deba@2312
   938
          c.expr(), c.upperBounded()?c.upperBound():INF);
alpar@1264
   939
    }
alpar@1264
   940
alpar@1445
   941
    ///Add a new row (i.e a new constraint) to the LP
alpar@1258
   942
alpar@1259
   943
    ///\param l is the lower bound (-\ref INF means no bound)
alpar@1258
   944
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   945
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1258
   946
    ///\return The created row.
alpar@1258
   947
    ///\bug This is a temportary function. The interface will change to
alpar@1258
   948
    ///a better one.
alpar@1258
   949
    Row addRow(Value l,const Expr &e, Value u) {
alpar@1258
   950
      Row r=addRow();
alpar@1895
   951
      row(r,l,e,u);
alpar@1253
   952
      return r;
alpar@1253
   953
    }
alpar@1253
   954
alpar@1445
   955
    ///Add a new row (i.e a new constraint) to the LP
alpar@1264
   956
alpar@1264
   957
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   958
    ///\return The created row.
alpar@1264
   959
    Row addRow(const Constr &c) {
alpar@1264
   960
      Row r=addRow();
alpar@1895
   961
      row(r,c);
alpar@1264
   962
      return r;
alpar@1264
   963
    }
athos@1542
   964
    ///Erase a coloumn (i.e a variable) from the LP
athos@1542
   965
athos@1542
   966
    ///\param c is the coloumn to be deleted
athos@1542
   967
    ///\todo Please check this
athos@1542
   968
    void eraseCol(Col c) {
deba@2312
   969
      _eraseCol(_lpId(c));
athos@1542
   970
      cols.erase(c.id);
athos@1542
   971
    }
athos@1542
   972
    ///Erase a  row (i.e a constraint) from the LP
athos@1542
   973
athos@1542
   974
    ///\param r is the row to be deleted
athos@1542
   975
    ///\todo Please check this
athos@1542
   976
    void eraseRow(Row r) {
deba@2312
   977
      _eraseRow(_lpId(r));
athos@1542
   978
      rows.erase(r.id);
athos@1542
   979
    }
alpar@1264
   980
alpar@1895
   981
    /// Get the name of a column
alpar@1895
   982
    
alpar@1895
   983
    ///\param c is the coresponding coloumn 
alpar@1895
   984
    ///\return The name of the colunm
athos@2268
   985
    std::string colName(Col c){
alpar@1895
   986
      std::string name;
deba@2312
   987
      _getColName(_lpId(c), name);
alpar@1895
   988
      return name;
alpar@1895
   989
    }
alpar@1895
   990
    
alpar@1895
   991
    /// Set the name of a column
alpar@1895
   992
    
alpar@1895
   993
    ///\param c is the coresponding coloumn 
alpar@1895
   994
    ///\param name The name to be given
deba@2312
   995
    void colName(Col c, const std::string& name){
deba@2312
   996
      _setColName(_lpId(c), name);
alpar@1895
   997
    }
alpar@1895
   998
    
alpar@1895
   999
    /// Set an element of the coefficient matrix of the LP
athos@1436
  1000
athos@1436
  1001
    ///\param r is the row of the element to be modified
athos@1436
  1002
    ///\param c is the coloumn of the element to be modified
athos@1436
  1003
    ///\param val is the new value of the coefficient
alpar@1895
  1004
athos@2268
  1005
    void coeff(Row r, Col c, Value val){
deba@2312
  1006
      _setCoeff(_lpId(r),_lpId(c), val);
athos@1436
  1007
    }
athos@1436
  1008
athos@2324
  1009
    /// Get an element of the coefficient matrix of the LP
athos@2324
  1010
athos@2324
  1011
    ///\param r is the row of the element in question
athos@2324
  1012
    ///\param c is the coloumn of the element in question
athos@2324
  1013
    ///\return the corresponding coefficient
athos@2324
  1014
athos@2324
  1015
    Value coeff(Row r, Col c){
athos@2324
  1016
      return _getCoeff(_lpId(r),_lpId(c));
athos@2324
  1017
    }
athos@2324
  1018
alpar@1253
  1019
    /// Set the lower bound of a column (i.e a variable)
alpar@1253
  1020
alpar@1895
  1021
    /// The lower bound of a variable (column) has to be given by an 
alpar@1253
  1022
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
  1023
    /// Value or -\ref INF.
alpar@1293
  1024
    void colLowerBound(Col c, Value value) {
deba@2312
  1025
      _setColLowerBound(_lpId(c),value);
alpar@1253
  1026
    }
alpar@1895
  1027
    
alpar@1895
  1028
    ///\brief Set the lower bound of  several columns
alpar@1895
  1029
    ///(i.e a variables) at once
alpar@1895
  1030
    ///
alpar@1895
  1031
    ///This magic function takes a container as its argument
alpar@1895
  1032
    ///and applies the function on all of its elements.
alpar@1895
  1033
    /// The lower bound of a variable (column) has to be given by an 
alpar@1895
  1034
    /// extended number of type Value, i.e. a finite number of type 
alpar@1895
  1035
    /// Value or -\ref INF.
alpar@1895
  1036
#ifdef DOXYGEN
alpar@1895
  1037
    template<class T>
alpar@1895
  1038
    void colLowerBound(T &t, Value value) { return 0;} 
alpar@1895
  1039
#else
alpar@1895
  1040
    template<class T>
alpar@1895
  1041
    typename enable_if<typename T::value_type::LpSolverCol,void>::type
alpar@1895
  1042
    colLowerBound(T &t, Value value,dummy<0> = 0) {
alpar@1895
  1043
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1895
  1044
	colLowerBound(*i, value);
alpar@1895
  1045
      }
alpar@1895
  1046
    }
alpar@1895
  1047
    template<class T>
alpar@1895
  1048
    typename enable_if<typename T::value_type::second_type::LpSolverCol,
alpar@1895
  1049
		       void>::type
alpar@1895
  1050
    colLowerBound(T &t, Value value,dummy<1> = 1) { 
alpar@1895
  1051
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1895
  1052
	colLowerBound(i->second, value);
alpar@1895
  1053
      }
alpar@1895
  1054
    }
alpar@1895
  1055
    template<class T>
alpar@1895
  1056
    typename enable_if<typename T::MapIt::Value::LpSolverCol,
alpar@1895
  1057
		       void>::type
alpar@1895
  1058
    colLowerBound(T &t, Value value,dummy<2> = 2) { 
alpar@1895
  1059
      for(typename T::MapIt i(t); i!=INVALID; ++i){
alpar@1895
  1060
	colLowerBound(*i, value);
alpar@1895
  1061
      }
alpar@1895
  1062
    }
alpar@1895
  1063
#endif
alpar@1895
  1064
    
alpar@1253
  1065
    /// Set the upper bound of a column (i.e a variable)
alpar@1253
  1066
alpar@1293
  1067
    /// The upper bound of a variable (column) has to be given by an 
alpar@1253
  1068
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
  1069
    /// Value or \ref INF.
alpar@1293
  1070
    void colUpperBound(Col c, Value value) {
deba@2312
  1071
      _setColUpperBound(_lpId(c),value);
alpar@1253
  1072
    };
alpar@1895
  1073
alpar@1895
  1074
    ///\brief Set the lower bound of  several columns
alpar@1895
  1075
    ///(i.e a variables) at once
alpar@1895
  1076
    ///
alpar@1895
  1077
    ///This magic function takes a container as its argument
alpar@1895
  1078
    ///and applies the function on all of its elements.
alpar@1895
  1079
    /// The upper bound of a variable (column) has to be given by an 
alpar@1895
  1080
    /// extended number of type Value, i.e. a finite number of type 
alpar@1895
  1081
    /// Value or \ref INF.
alpar@1895
  1082
#ifdef DOXYGEN
alpar@1895
  1083
    template<class T>
alpar@1895
  1084
    void colUpperBound(T &t, Value value) { return 0;} 
alpar@1895
  1085
#else
alpar@1895
  1086
    template<class T>
alpar@1895
  1087
    typename enable_if<typename T::value_type::LpSolverCol,void>::type
alpar@1895
  1088
    colUpperBound(T &t, Value value,dummy<0> = 0) {
alpar@1895
  1089
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1895
  1090
	colUpperBound(*i, value);
alpar@1895
  1091
      }
alpar@1895
  1092
    }
alpar@1895
  1093
    template<class T>
alpar@1895
  1094
    typename enable_if<typename T::value_type::second_type::LpSolverCol,
alpar@1895
  1095
		       void>::type
alpar@1895
  1096
    colUpperBound(T &t, Value value,dummy<1> = 1) { 
alpar@1895
  1097
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1895
  1098
	colUpperBound(i->second, value);
alpar@1895
  1099
      }
alpar@1895
  1100
    }
alpar@1895
  1101
    template<class T>
alpar@1895
  1102
    typename enable_if<typename T::MapIt::Value::LpSolverCol,
alpar@1895
  1103
		       void>::type
alpar@1895
  1104
    colUpperBound(T &t, Value value,dummy<2> = 2) { 
alpar@1895
  1105
      for(typename T::MapIt i(t); i!=INVALID; ++i){
alpar@1895
  1106
	colUpperBound(*i, value);
alpar@1895
  1107
      }
alpar@1895
  1108
    }
alpar@1895
  1109
#endif
alpar@1895
  1110
alpar@1293
  1111
    /// Set the lower and the upper bounds of a column (i.e a variable)
alpar@1293
  1112
alpar@1293
  1113
    /// The lower and the upper bounds of
alpar@1293
  1114
    /// a variable (column) have to be given by an 
alpar@1293
  1115
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
  1116
    /// Value, -\ref INF or \ref INF.
alpar@1293
  1117
    void colBounds(Col c, Value lower, Value upper) {
deba@2312
  1118
      _setColLowerBound(_lpId(c),lower);
deba@2312
  1119
      _setColUpperBound(_lpId(c),upper);
alpar@1293
  1120
    }
alpar@1293
  1121
    
alpar@1895
  1122
    ///\brief Set the lower and the upper bound of several columns
alpar@1895
  1123
    ///(i.e a variables) at once
alpar@1895
  1124
    ///
alpar@1895
  1125
    ///This magic function takes a container as its argument
alpar@1895
  1126
    ///and applies the function on all of its elements.
alpar@1895
  1127
    /// The lower and the upper bounds of
alpar@1895
  1128
    /// a variable (column) have to be given by an 
alpar@1895
  1129
    /// extended number of type Value, i.e. a finite number of type 
alpar@1895
  1130
    /// Value, -\ref INF or \ref INF.
alpar@1895
  1131
#ifdef DOXYGEN
alpar@1895
  1132
    template<class T>
alpar@1895
  1133
    void colBounds(T &t, Value lower, Value upper) { return 0;} 
alpar@1895
  1134
#else
alpar@1895
  1135
    template<class T>
alpar@1895
  1136
    typename enable_if<typename T::value_type::LpSolverCol,void>::type
alpar@1895
  1137
    colBounds(T &t, Value lower, Value upper,dummy<0> = 0) {
alpar@1895
  1138
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1895
  1139
	colBounds(*i, lower, upper);
alpar@1895
  1140
      }
alpar@1895
  1141
    }
alpar@1895
  1142
    template<class T>
alpar@1895
  1143
    typename enable_if<typename T::value_type::second_type::LpSolverCol,
alpar@1895
  1144
		       void>::type
alpar@1895
  1145
    colBounds(T &t, Value lower, Value upper,dummy<1> = 1) { 
alpar@1895
  1146
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1895
  1147
	colBounds(i->second, lower, upper);
alpar@1895
  1148
      }
alpar@1895
  1149
    }
alpar@1895
  1150
    template<class T>
alpar@1895
  1151
    typename enable_if<typename T::MapIt::Value::LpSolverCol,
alpar@1895
  1152
		       void>::type
alpar@1895
  1153
    colBounds(T &t, Value lower, Value upper,dummy<2> = 2) { 
alpar@1895
  1154
      for(typename T::MapIt i(t); i!=INVALID; ++i){
alpar@1895
  1155
	colBounds(*i, lower, upper);
alpar@1895
  1156
      }
alpar@1895
  1157
    }
alpar@1895
  1158
#endif
alpar@1895
  1159
    
athos@1405
  1160
//     /// Set the lower bound of a row (i.e a constraint)
alpar@1253
  1161
athos@1405
  1162
//     /// The lower bound of a linear expression (row) has to be given by an 
athos@1405
  1163
//     /// extended number of type Value, i.e. a finite number of type 
athos@1405
  1164
//     /// Value or -\ref INF.
athos@1405
  1165
//     void rowLowerBound(Row r, Value value) {
deba@2312
  1166
//       _setRowLowerBound(_lpId(r),value);
athos@1405
  1167
//     };
athos@1405
  1168
//     /// Set the upper bound of a row (i.e a constraint)
alpar@1253
  1169
athos@1405
  1170
//     /// The upper bound of a linear expression (row) has to be given by an 
athos@1405
  1171
//     /// extended number of type Value, i.e. a finite number of type 
athos@1405
  1172
//     /// Value or \ref INF.
athos@1405
  1173
//     void rowUpperBound(Row r, Value value) {
deba@2312
  1174
//       _setRowUpperBound(_lpId(r),value);
athos@1405
  1175
//     };
athos@1405
  1176
athos@1405
  1177
    /// Set the lower and the upper bounds of a row (i.e a constraint)
alpar@1293
  1178
alpar@1293
  1179
    /// The lower and the upper bounds of
alpar@1293
  1180
    /// a constraint (row) have to be given by an 
alpar@1293
  1181
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
  1182
    /// Value, -\ref INF or \ref INF.
alpar@1293
  1183
    void rowBounds(Row c, Value lower, Value upper) {
deba@2312
  1184
      _setRowBounds(_lpId(c),lower, upper);
deba@2312
  1185
      // _setRowUpperBound(_lpId(c),upper);
alpar@1293
  1186
    }
alpar@1293
  1187
    
alpar@1253
  1188
    ///Set an element of the objective function
deba@2312
  1189
    void objCoeff(Col c, Value v) {_setObjCoeff(_lpId(c),v); };
athos@2324
  1190
athos@2324
  1191
    ///Get an element of the objective function
athos@2324
  1192
    Value objCoeff(Col c) {return _getObjCoeff(_lpId(c)); };
athos@2324
  1193
alpar@1253
  1194
    ///Set the objective function
athos@2324
  1195
alpar@1253
  1196
    ///\param e is a linear expression of type \ref Expr.
alpar@1895
  1197
    ///\bug Is should be called obj()
alpar@1253
  1198
    void setObj(Expr e) {
athos@1377
  1199
      _clearObj();
alpar@1253
  1200
      for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
alpar@1293
  1201
	objCoeff((*i).first,(*i).second);
alpar@1323
  1202
      obj_const_comp=e.constComp();
alpar@1253
  1203
    }
alpar@1263
  1204
alpar@1312
  1205
    ///Maximize
alpar@1312
  1206
    void max() { _setMax(); }
alpar@1312
  1207
    ///Minimize
alpar@1312
  1208
    void min() { _setMin(); }
alpar@1312
  1209
athos@2324
  1210
    ///Query function: is this a maximization problem?
athos@2324
  1211
    bool is_max() {return _isMax(); }
athos@2324
  1212
athos@2324
  1213
    ///Query function: is this a minimization problem?
athos@2324
  1214
    bool is_min() {return !is_max(); }
alpar@1312
  1215
    
alpar@1263
  1216
    ///@}
alpar@1263
  1217
alpar@1263
  1218
alpar@1294
  1219
    ///\name Solve the LP
alpar@1263
  1220
alpar@1263
  1221
    ///@{
alpar@1263
  1222
athos@1458
  1223
    ///\e Solve the LP problem at hand
athos@1458
  1224
    ///
deba@2026
  1225
    ///\return The result of the optimization procedure. Possible 
deba@2026
  1226
    ///values and their meanings can be found in the documentation of 
deba@2026
  1227
    ///\ref SolveExitStatus.
athos@1458
  1228
    ///
athos@1458
  1229
    ///\todo Which method is used to solve the problem
alpar@1303
  1230
    SolveExitStatus solve() { return _solve(); }
alpar@1263
  1231
    
alpar@1263
  1232
    ///@}
alpar@1263
  1233
    
alpar@1294
  1234
    ///\name Obtain the solution
alpar@1263
  1235
alpar@1263
  1236
    ///@{
alpar@1263
  1237
athos@1460
  1238
    /// The status of the primal problem (the original LP problem)
alpar@1312
  1239
    SolutionStatus primalStatus() {
alpar@1312
  1240
      return _getPrimalStatus();
alpar@1294
  1241
    }
alpar@1294
  1242
athos@1460
  1243
    /// The status of the dual (of the original LP) problem 
athos@1460
  1244
    SolutionStatus dualStatus() {
athos@1460
  1245
      return _getDualStatus();
athos@1460
  1246
    }
athos@1460
  1247
athos@1460
  1248
    ///The type of the original LP problem
athos@1462
  1249
    ProblemTypes problemType() {
athos@1460
  1250
      return _getProblemType();
athos@1460
  1251
    }
athos@1460
  1252
alpar@1294
  1253
    ///\e
deba@2312
  1254
    Value primal(Col c) { return _getPrimal(_lpId(c)); }
alpar@1263
  1255
alpar@1312
  1256
    ///\e
deba@2312
  1257
    Value dual(Row r) { return _getDual(_lpId(r)); }
marci@1787
  1258
marci@1787
  1259
    ///\e
deba@2312
  1260
    bool isBasicCol(Col c) { return _isBasicCol(_lpId(c)); }
marci@1840
  1261
marci@1840
  1262
    ///\e
alpar@1312
  1263
alpar@1312
  1264
    ///\return
alpar@1312
  1265
    ///- \ref INF or -\ref INF means either infeasibility or unboundedness
alpar@1312
  1266
    /// of the primal problem, depending on whether we minimize or maximize.
alpar@1364
  1267
    ///- \ref NaN if no primal solution is found.
alpar@1312
  1268
    ///- The (finite) objective value if an optimal solution is found.
alpar@1323
  1269
    Value primalValue() { return _getPrimalValue()+obj_const_comp;}
alpar@1263
  1270
    ///@}
alpar@1253
  1271
    
athos@1248
  1272
  };  
athos@1246
  1273
athos@2144
  1274
athos@2148
  1275
  ///Common base class for MIP solvers
athos@2144
  1276
  ///\todo Much more docs
athos@2144
  1277
  ///\ingroup gen_opt_group
athos@2144
  1278
  class MipSolverBase : virtual public LpSolverBase{
athos@2144
  1279
  public:
athos@2144
  1280
athos@2148
  1281
    ///Possible variable (coloumn) types (e.g. real, integer, binary etc.)
athos@2148
  1282
    enum ColTypes {
athos@2148
  1283
      ///Continuous variable
athos@2148
  1284
      REAL = 0,
athos@2148
  1285
      ///Integer variable
athos@2218
  1286
athos@2218
  1287
      ///Unfortunately, cplex 7.5 somewhere writes something like
athos@2218
  1288
      ///#define INTEGER 'I'
athos@2267
  1289
      INT = 1
athos@2148
  1290
      ///\todo No support for other types yet.
athos@2148
  1291
    };
athos@2148
  1292
athos@2148
  1293
    ///Sets the type of the given coloumn to the given type
athos@2144
  1294
    ///
athos@2148
  1295
    ///Sets the type of the given coloumn to the given type.
athos@2148
  1296
    void colType(Col c, ColTypes col_type) {
deba@2312
  1297
      _colType(_lpId(c),col_type);
athos@2144
  1298
    }
athos@2144
  1299
athos@2144
  1300
    ///Gives back the type of the column.
athos@2144
  1301
    ///
athos@2144
  1302
    ///Gives back the type of the column.
athos@2148
  1303
    ColTypes colType(Col c){
deba@2312
  1304
      return _colType(_lpId(c));
athos@2148
  1305
    }
athos@2148
  1306
athos@2148
  1307
    ///Sets the type of the given Col to integer or remove that property.
athos@2148
  1308
    ///
athos@2148
  1309
    ///Sets the type of the given Col to integer or remove that property.
athos@2148
  1310
    void integer(Col c, bool enable) {
athos@2148
  1311
      if (enable)
athos@2267
  1312
	colType(c,INT);
athos@2148
  1313
      else
athos@2148
  1314
	colType(c,REAL);
athos@2148
  1315
    }
athos@2148
  1316
athos@2148
  1317
    ///Gives back whether the type of the column is integer or not.
athos@2148
  1318
    ///
athos@2148
  1319
    ///Gives back the type of the column.
athos@2144
  1320
    ///\return true if the column has integer type and false if not.
athos@2144
  1321
    bool integer(Col c){
athos@2267
  1322
      return (colType(c)==INT);
athos@2144
  1323
    }
athos@2144
  1324
athos@2185
  1325
    /// The status of the MIP problem
athos@2185
  1326
    SolutionStatus mipStatus() {
athos@2185
  1327
      return _getMipStatus();
athos@2185
  1328
    }
athos@2185
  1329
athos@2144
  1330
  protected:
athos@2144
  1331
athos@2148
  1332
    virtual ColTypes _colType(int col) = 0;
athos@2148
  1333
    virtual void _colType(int col, ColTypes col_type) = 0;
athos@2185
  1334
    virtual SolutionStatus _getMipStatus()=0;
athos@2148
  1335
athos@2144
  1336
  };
alpar@1272
  1337
  
alpar@1272
  1338
  ///\relates LpSolverBase::Expr
alpar@1272
  1339
  ///
alpar@1272
  1340
  inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
alpar@1272
  1341
				      const LpSolverBase::Expr &b) 
alpar@1272
  1342
  {
alpar@1272
  1343
    LpSolverBase::Expr tmp(a);
alpar@1766
  1344
    tmp+=b;
alpar@1272
  1345
    return tmp;
alpar@1272
  1346
  }
alpar@1272
  1347
  ///\e
alpar@1272
  1348
  
alpar@1272
  1349
  ///\relates LpSolverBase::Expr
alpar@1272
  1350
  ///
alpar@1272
  1351
  inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
alpar@1272
  1352
				      const LpSolverBase::Expr &b) 
alpar@1272
  1353
  {
alpar@1272
  1354
    LpSolverBase::Expr tmp(a);
alpar@1766
  1355
    tmp-=b;
alpar@1272
  1356
    return tmp;
alpar@1272
  1357
  }
alpar@1272
  1358
  ///\e
alpar@1272
  1359
  
alpar@1272
  1360
  ///\relates LpSolverBase::Expr
alpar@1272
  1361
  ///
alpar@1272
  1362
  inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
alpar@1273
  1363
				      const LpSolverBase::Value &b) 
alpar@1272
  1364
  {
alpar@1272
  1365
    LpSolverBase::Expr tmp(a);
alpar@1766
  1366
    tmp*=b;
alpar@1272
  1367
    return tmp;
alpar@1272
  1368
  }
alpar@1272
  1369
  
alpar@1272
  1370
  ///\e
alpar@1272
  1371
  
alpar@1272
  1372
  ///\relates LpSolverBase::Expr
alpar@1272
  1373
  ///
alpar@1273
  1374
  inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
alpar@1272
  1375
				      const LpSolverBase::Expr &b) 
alpar@1272
  1376
  {
alpar@1272
  1377
    LpSolverBase::Expr tmp(b);
alpar@1766
  1378
    tmp*=a;
alpar@1272
  1379
    return tmp;
alpar@1272
  1380
  }
alpar@1272
  1381
  ///\e
alpar@1272
  1382
  
alpar@1272
  1383
  ///\relates LpSolverBase::Expr
alpar@1272
  1384
  ///
alpar@1272
  1385
  inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
alpar@1273
  1386
				      const LpSolverBase::Value &b) 
alpar@1272
  1387
  {
alpar@1272
  1388
    LpSolverBase::Expr tmp(a);
alpar@1766
  1389
    tmp/=b;
alpar@1272
  1390
    return tmp;
alpar@1272
  1391
  }
alpar@1272
  1392
  
alpar@1272
  1393
  ///\e
alpar@1272
  1394
  
alpar@1272
  1395
  ///\relates LpSolverBase::Constr
alpar@1272
  1396
  ///
alpar@1272
  1397
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1272
  1398
					 const LpSolverBase::Expr &f) 
alpar@1272
  1399
  {
alpar@1272
  1400
    return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
alpar@1272
  1401
  }
alpar@1272
  1402
alpar@1272
  1403
  ///\e
alpar@1272
  1404
  
alpar@1272
  1405
  ///\relates LpSolverBase::Constr
alpar@1272
  1406
  ///
alpar@1273
  1407
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
alpar@1272
  1408
					 const LpSolverBase::Expr &f) 
alpar@1272
  1409
  {
alpar@1272
  1410
    return LpSolverBase::Constr(e,f);
alpar@1272
  1411
  }
alpar@1272
  1412
alpar@1272
  1413
  ///\e
alpar@1272
  1414
  
alpar@1272
  1415
  ///\relates LpSolverBase::Constr
alpar@1272
  1416
  ///
alpar@1272
  1417
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1273
  1418
					 const LpSolverBase::Value &f) 
alpar@1272
  1419
  {
alpar@1272
  1420
    return LpSolverBase::Constr(e,f);
alpar@1272
  1421
  }
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::Expr &e,
alpar@1272
  1428
					 const LpSolverBase::Expr &f) 
alpar@1272
  1429
  {
alpar@1272
  1430
    return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
alpar@1272
  1431
  }
alpar@1272
  1432
alpar@1272
  1433
alpar@1272
  1434
  ///\e
alpar@1272
  1435
  
alpar@1272
  1436
  ///\relates LpSolverBase::Constr
alpar@1272
  1437
  ///
alpar@1273
  1438
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
alpar@1272
  1439
					 const LpSolverBase::Expr &f) 
alpar@1272
  1440
  {
alpar@1272
  1441
    return LpSolverBase::Constr(f,e);
alpar@1272
  1442
  }
alpar@1272
  1443
alpar@1272
  1444
alpar@1272
  1445
  ///\e
alpar@1272
  1446
  
alpar@1272
  1447
  ///\relates LpSolverBase::Constr
alpar@1272
  1448
  ///
alpar@1272
  1449
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1273
  1450
					 const LpSolverBase::Value &f) 
alpar@1272
  1451
  {
alpar@1272
  1452
    return LpSolverBase::Constr(f,e);
alpar@1272
  1453
  }
alpar@1272
  1454
alpar@1272
  1455
  ///\e
alpar@1272
  1456
  
alpar@1272
  1457
  ///\relates LpSolverBase::Constr
alpar@1272
  1458
  ///
alpar@1272
  1459
  inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
alpar@1272
  1460
					 const LpSolverBase::Expr &f) 
alpar@1272
  1461
  {
alpar@1272
  1462
    return LpSolverBase::Constr(0,e-f,0);
alpar@1272
  1463
  }
alpar@1272
  1464
alpar@1272
  1465
  ///\e
alpar@1272
  1466
  
alpar@1272
  1467
  ///\relates LpSolverBase::Constr
alpar@1272
  1468
  ///
alpar@1273
  1469
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
alpar@1272
  1470
					 const LpSolverBase::Constr&c) 
alpar@1272
  1471
  {
alpar@1272
  1472
    LpSolverBase::Constr tmp(c);
alpar@1273
  1473
    ///\todo Create an own exception type.
deba@2026
  1474
    if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
alpar@1273
  1475
    else tmp.lowerBound()=n;
alpar@1272
  1476
    return tmp;
alpar@1272
  1477
  }
alpar@1272
  1478
  ///\e
alpar@1272
  1479
  
alpar@1272
  1480
  ///\relates LpSolverBase::Constr
alpar@1272
  1481
  ///
alpar@1272
  1482
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
alpar@1273
  1483
					 const LpSolverBase::Value &n)
alpar@1272
  1484
  {
alpar@1272
  1485
    LpSolverBase::Constr tmp(c);
alpar@1273
  1486
    ///\todo Create an own exception type.
deba@2026
  1487
    if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
alpar@1273
  1488
    else tmp.upperBound()=n;
alpar@1272
  1489
    return tmp;
alpar@1272
  1490
  }
alpar@1272
  1491
alpar@1272
  1492
  ///\e
alpar@1272
  1493
  
alpar@1272
  1494
  ///\relates LpSolverBase::Constr
alpar@1272
  1495
  ///
alpar@1273
  1496
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
alpar@1272
  1497
					 const LpSolverBase::Constr&c) 
alpar@1272
  1498
  {
alpar@1272
  1499
    LpSolverBase::Constr tmp(c);
alpar@1273
  1500
    ///\todo Create an own exception type.
deba@2026
  1501
    if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
alpar@1273
  1502
    else tmp.upperBound()=n;
alpar@1272
  1503
    return tmp;
alpar@1272
  1504
  }
alpar@1272
  1505
  ///\e
alpar@1272
  1506
  
alpar@1272
  1507
  ///\relates LpSolverBase::Constr
alpar@1272
  1508
  ///
alpar@1272
  1509
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
alpar@1273
  1510
					 const LpSolverBase::Value &n)
alpar@1272
  1511
  {
alpar@1272
  1512
    LpSolverBase::Constr tmp(c);
alpar@1273
  1513
    ///\todo Create an own exception type.
deba@2026
  1514
    if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
alpar@1273
  1515
    else tmp.lowerBound()=n;
alpar@1272
  1516
    return tmp;
alpar@1272
  1517
  }
alpar@1272
  1518
alpar@1445
  1519
  ///\e
alpar@1445
  1520
  
alpar@1445
  1521
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1522
  ///
alpar@1445
  1523
  inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a,
deba@2312
  1524
                                          const LpSolverBase::DualExpr &b) 
alpar@1445
  1525
  {
alpar@1445
  1526
    LpSolverBase::DualExpr tmp(a);
alpar@1766
  1527
    tmp+=b;
alpar@1445
  1528
    return tmp;
alpar@1445
  1529
  }
alpar@1445
  1530
  ///\e
alpar@1445
  1531
  
alpar@1445
  1532
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1533
  ///
alpar@1445
  1534
  inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a,
deba@2312
  1535
                                          const LpSolverBase::DualExpr &b) 
alpar@1445
  1536
  {
alpar@1445
  1537
    LpSolverBase::DualExpr tmp(a);
alpar@1766
  1538
    tmp-=b;
alpar@1445
  1539
    return tmp;
alpar@1445
  1540
  }
alpar@1445
  1541
  ///\e
alpar@1445
  1542
  
alpar@1445
  1543
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1544
  ///
alpar@1445
  1545
  inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a,
deba@2312
  1546
                                          const LpSolverBase::Value &b) 
alpar@1445
  1547
  {
alpar@1445
  1548
    LpSolverBase::DualExpr tmp(a);
alpar@1766
  1549
    tmp*=b;
alpar@1445
  1550
    return tmp;
alpar@1445
  1551
  }
alpar@1445
  1552
  
alpar@1445
  1553
  ///\e
alpar@1445
  1554
  
alpar@1445
  1555
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1556
  ///
alpar@1445
  1557
  inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a,
deba@2312
  1558
                                          const LpSolverBase::DualExpr &b) 
alpar@1445
  1559
  {
alpar@1445
  1560
    LpSolverBase::DualExpr tmp(b);
alpar@1766
  1561
    tmp*=a;
alpar@1445
  1562
    return tmp;
alpar@1445
  1563
  }
alpar@1445
  1564
  ///\e
alpar@1445
  1565
  
alpar@1445
  1566
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1567
  ///
alpar@1445
  1568
  inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a,
deba@2312
  1569
                                          const LpSolverBase::Value &b) 
alpar@1445
  1570
  {
alpar@1445
  1571
    LpSolverBase::DualExpr tmp(a);
alpar@1766
  1572
    tmp/=b;
alpar@1445
  1573
    return tmp;
alpar@1445
  1574
  }
alpar@1445
  1575
  
alpar@1272
  1576
athos@1246
  1577
} //namespace lemon
athos@1246
  1578
athos@1246
  1579
#endif //LEMON_LP_BASE_H