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