lemon/lp_base.h
author deba
Mon, 17 Dec 2007 09:54:26 +0000
changeset 2542 faaa54ec4520
parent 2495 e4f8367beb41
child 2553 bfced05fa852
permissions -rw-r--r--
Bug fix
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@2391
     5
 * Copyright (C) 2003-2007
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@1397
    26
#include<cmath>
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;
ladanyi@2495
    46
    if (Lim::has_infinity && (value == Lim::infinity() || value ==
ladanyi@2495
    47
          -Lim::infinity()) ||
ladanyi@2495
    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
athos@2345
   374
      //std::ostream &
athos@2345
   375
      void prettyPrint(std::ostream &os) {
athos@2345
   376
	//std::fmtflags os.flags();
athos@2345
   377
	//os.setf(std::ios::showpos);
athos@2345
   378
	Base::iterator j=Base::begin();
athos@2345
   379
	if (j!=Base::end())
athos@2345
   380
	  os<<j->second<<"*x["<<id(j->first)<<"]";
athos@2345
   381
	++j;
athos@2345
   382
	for (; j!=Base::end(); ++j){
athos@2345
   383
	  if (j->second>=0)
athos@2345
   384
	    os<<"+";
athos@2345
   385
	  os<<j->second<<"*x["<<id(j->first)<<"]";
athos@2345
   386
	}
athos@2345
   387
	//Nem valami korrekt, de nem talaltam meg, hogy kell
athos@2345
   388
	//os.unsetf(std::ios::showpos);
athos@2345
   389
athos@2345
   390
	//return os;
athos@2345
   391
      }
athos@2345
   392
alpar@1272
   393
    };
alpar@1272
   394
    
alpar@1264
   395
    ///Linear constraint
alpar@1328
   396
alpar@1364
   397
    ///This data stucture represents a linear constraint in the LP.
alpar@1364
   398
    ///Basically it is a linear expression with a lower or an upper bound
alpar@1364
   399
    ///(or both). These parts of the constraint can be obtained by the member
alpar@1364
   400
    ///functions \ref expr(), \ref lowerBound() and \ref upperBound(),
alpar@1364
   401
    ///respectively.
alpar@1364
   402
    ///There are two ways to construct a constraint.
alpar@1364
   403
    ///- You can set the linear expression and the bounds directly
alpar@1364
   404
    ///  by the functions above.
alpar@1364
   405
    ///- The operators <tt>\<=</tt>, <tt>==</tt> and  <tt>\>=</tt>
alpar@1364
   406
    ///  are defined between expressions, or even between constraints whenever
alpar@1364
   407
    ///  it makes sense. Therefore if \c e and \c f are linear expressions and
alpar@1364
   408
    ///  \c s and \c t are numbers, then the followings are valid expressions
alpar@1364
   409
    ///  and thus they can be used directly e.g. in \ref addRow() whenever
alpar@1364
   410
    ///  it makes sense.
alpar@1908
   411
    ///\code
alpar@1364
   412
    ///  e<=s
alpar@1364
   413
    ///  e<=f
alpar@1908
   414
    ///  e==f
alpar@1364
   415
    ///  s<=e<=t
alpar@1364
   416
    ///  e>=t
alpar@1908
   417
    ///\endcode
alpar@1364
   418
    ///\warning The validity of a constraint is checked only at run time, so
alpar@1364
   419
    ///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw a
alpar@1364
   420
    ///\ref LogicError exception.
alpar@1272
   421
    class Constr
alpar@1272
   422
    {
alpar@1272
   423
    public:
alpar@1272
   424
      typedef LpSolverBase::Expr Expr;
alpar@1273
   425
      typedef Expr::Key Key;
alpar@1273
   426
      typedef Expr::Value Value;
alpar@1272
   427
      
alpar@1273
   428
    protected:
alpar@1273
   429
      Expr _expr;
alpar@1273
   430
      Value _lb,_ub;
alpar@1273
   431
    public:
alpar@1273
   432
      ///\e
alpar@1273
   433
      Constr() : _expr(), _lb(NaN), _ub(NaN) {}
alpar@1273
   434
      ///\e
alpar@1273
   435
      Constr(Value lb,const Expr &e,Value ub) :
alpar@1273
   436
	_expr(e), _lb(lb), _ub(ub) {}
alpar@1273
   437
      ///\e
alpar@1273
   438
      Constr(const Expr &e,Value ub) : 
alpar@1273
   439
	_expr(e), _lb(NaN), _ub(ub) {}
alpar@1273
   440
      ///\e
alpar@1273
   441
      Constr(Value lb,const Expr &e) :
alpar@1273
   442
	_expr(e), _lb(lb), _ub(NaN) {}
alpar@1273
   443
      ///\e
alpar@1272
   444
      Constr(const Expr &e) : 
alpar@1273
   445
	_expr(e), _lb(NaN), _ub(NaN) {}
alpar@1273
   446
      ///\e
alpar@1273
   447
      void clear() 
alpar@1273
   448
      {
alpar@1273
   449
	_expr.clear();
alpar@1273
   450
	_lb=_ub=NaN;
alpar@1273
   451
      }
alpar@1364
   452
alpar@1364
   453
      ///Reference to the linear expression 
alpar@1273
   454
      Expr &expr() { return _expr; }
alpar@1364
   455
      ///Cont reference to the linear expression 
alpar@1273
   456
      const Expr &expr() const { return _expr; }
alpar@1364
   457
      ///Reference to the lower bound.
alpar@1364
   458
alpar@1364
   459
      ///\return
alpar@1536
   460
      ///- \ref INF "INF": the constraint is lower unbounded.
alpar@1536
   461
      ///- \ref NaN "NaN": lower bound has not been set.
alpar@1364
   462
      ///- finite number: the lower bound
alpar@1273
   463
      Value &lowerBound() { return _lb; }
alpar@1364
   464
      ///The const version of \ref lowerBound()
alpar@1273
   465
      const Value &lowerBound() const { return _lb; }
alpar@1364
   466
      ///Reference to the upper bound.
alpar@1364
   467
alpar@1364
   468
      ///\return
alpar@1536
   469
      ///- \ref INF "INF": the constraint is upper unbounded.
alpar@1536
   470
      ///- \ref NaN "NaN": upper bound has not been set.
alpar@1364
   471
      ///- finite number: the upper bound
alpar@1273
   472
      Value &upperBound() { return _ub; }
alpar@1364
   473
      ///The const version of \ref upperBound()
alpar@1273
   474
      const Value &upperBound() const { return _ub; }
alpar@1364
   475
      ///Is the constraint lower bounded?
alpar@1295
   476
      bool lowerBounded() const { 
ladanyi@2495
   477
	return isFinite(_lb);
alpar@1295
   478
      }
alpar@1364
   479
      ///Is the constraint upper bounded?
alpar@1295
   480
      bool upperBounded() const {
ladanyi@2495
   481
	return isFinite(_ub);
alpar@1295
   482
      }
athos@2345
   483
athos@2345
   484
      void prettyPrint(std::ostream &os) {
athos@2345
   485
	if (_lb==-LpSolverBase::INF||isNaN(_lb))
athos@2345
   486
	  os<<"-infty<=";
athos@2345
   487
	else
athos@2345
   488
	  os<<_lb<<"<=";
athos@2345
   489
	_expr.prettyPrint(os);
athos@2345
   490
	if (_ub==LpSolverBase::INF)
athos@2345
   491
	  os<<"<=infty";
athos@2345
   492
	else
athos@2345
   493
	  os<<"<="<<_ub;
athos@2345
   494
	//return os;
athos@2345
   495
      }
athos@2345
   496
alpar@1272
   497
    };
alpar@1272
   498
    
alpar@1445
   499
    ///Linear expression of rows
alpar@1445
   500
    
alpar@1445
   501
    ///This data structure represents a column of the matrix,
alpar@1445
   502
    ///thas is it strores a linear expression of the dual variables
alpar@1445
   503
    ///(\ref Row "Row"s).
alpar@1445
   504
    ///
alpar@1445
   505
    ///There are several ways to access and modify the contents of this
alpar@1445
   506
    ///container.
alpar@1445
   507
    ///- Its it fully compatible with \c std::map<Row,double>, so for expamle
alpar@1445
   508
    ///if \c e is an DualExpr and \c v
alpar@1445
   509
    ///and \c w are of type \ref Row, then you can
alpar@1445
   510
    ///read and modify the coefficients like
alpar@1445
   511
    ///these.
alpar@1445
   512
    ///\code
alpar@1445
   513
    ///e[v]=5;
alpar@1445
   514
    ///e[v]+=12;
alpar@1445
   515
    ///e.erase(v);
alpar@1445
   516
    ///\endcode
alpar@1445
   517
    ///or you can also iterate through its elements.
alpar@1445
   518
    ///\code
alpar@1445
   519
    ///double s=0;
alpar@1445
   520
    ///for(LpSolverBase::DualExpr::iterator i=e.begin();i!=e.end();++i)
alpar@1445
   521
    ///  s+=i->second;
alpar@1445
   522
    ///\endcode
alpar@1445
   523
    ///(This code computes the sum of all coefficients).
alpar@1445
   524
    ///- Numbers (<tt>double</tt>'s)
alpar@1445
   525
    ///and variables (\ref Row "Row"s) directly convert to an
alpar@1908
   526
    ///\ref DualExpr and the usual linear operations are defined, so
alpar@1445
   527
    ///\code
alpar@1445
   528
    ///v+w
alpar@1445
   529
    ///2*v-3.12*(v-w/2)
alpar@1445
   530
    ///v*2.1+(3*v+(v*12+w)*3)/2
alpar@1445
   531
    ///\endcode
alpar@1445
   532
    ///are valid \ref DualExpr "DualExpr"essions.
alpar@1445
   533
    ///The usual assignment operations are also defined.
alpar@1445
   534
    ///\code
alpar@1445
   535
    ///e=v+w;
alpar@1445
   536
    ///e+=2*v-3.12*(v-w/2);
alpar@1445
   537
    ///e*=3.4;
alpar@1445
   538
    ///e/=5;
alpar@1445
   539
    ///\endcode
alpar@1445
   540
    ///
alpar@1445
   541
    ///\sa Expr
alpar@1445
   542
    ///
alpar@1445
   543
    class DualExpr : public std::map<Row,Value>
alpar@1445
   544
    {
alpar@1445
   545
    public:
alpar@1445
   546
      typedef LpSolverBase::Row Key; 
alpar@1445
   547
      typedef LpSolverBase::Value Value;
alpar@1445
   548
      
alpar@1445
   549
    protected:
alpar@1445
   550
      typedef std::map<Row,Value> Base;
alpar@1445
   551
      
alpar@1445
   552
    public:
alpar@1445
   553
      typedef True IsLinExpression;
alpar@1445
   554
      ///\e
alpar@1445
   555
      DualExpr() : Base() { }
alpar@1445
   556
      ///\e
alpar@1445
   557
      DualExpr(const Key &v) {
alpar@1445
   558
	Base::insert(std::make_pair(v, 1));
alpar@1445
   559
      }
alpar@1445
   560
      ///\e
alpar@1445
   561
      void set(const Key &v,const Value &c) {
alpar@1445
   562
	Base::insert(std::make_pair(v, c));
alpar@1445
   563
      }
alpar@1445
   564
      
alpar@1445
   565
      ///Removes the components with zero coefficient.
alpar@1445
   566
      void simplify() {
alpar@1445
   567
	for (Base::iterator i=Base::begin(); i!=Base::end();) {
alpar@1445
   568
	  Base::iterator j=i;
alpar@1445
   569
	  ++j;
alpar@1445
   570
	  if ((*i).second==0) Base::erase(i);
deba@2085
   571
	  i=j;
alpar@1445
   572
	}
alpar@1445
   573
      }
alpar@1445
   574
deba@2312
   575
      void simplify() const {
deba@2312
   576
        const_cast<DualExpr*>(this)->simplify();
deba@2312
   577
      }
deba@2312
   578
alpar@1771
   579
      ///Removes the coefficients closer to zero than \c tolerance.
alpar@1771
   580
      void simplify(double &tolerance) {
alpar@1771
   581
	for (Base::iterator i=Base::begin(); i!=Base::end();) {
alpar@1771
   582
	  Base::iterator j=i;
alpar@1771
   583
	  ++j;
alpar@1771
   584
	  if (std::fabs((*i).second)<tolerance) Base::erase(i);
deba@2085
   585
	  i=j;
alpar@1771
   586
	}
alpar@1771
   587
      }
alpar@1771
   588
alpar@1445
   589
      ///Sets all coefficients to 0.
alpar@1445
   590
      void clear() {
alpar@1445
   591
	Base::clear();
alpar@1445
   592
      }
alpar@1445
   593
alpar@1445
   594
      ///\e
alpar@1445
   595
      DualExpr &operator+=(const DualExpr &e) {
alpar@1445
   596
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1445
   597
	  (*this)[j->first]+=j->second;
alpar@1445
   598
	return *this;
alpar@1445
   599
      }
alpar@1445
   600
      ///\e
alpar@1445
   601
      DualExpr &operator-=(const DualExpr &e) {
alpar@1445
   602
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1445
   603
	  (*this)[j->first]-=j->second;
alpar@1445
   604
	return *this;
alpar@1445
   605
      }
alpar@1445
   606
      ///\e
alpar@1445
   607
      DualExpr &operator*=(const Value &c) {
alpar@1445
   608
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1445
   609
	  j->second*=c;
alpar@1445
   610
	return *this;
alpar@1445
   611
      }
alpar@1445
   612
      ///\e
alpar@1445
   613
      DualExpr &operator/=(const Value &c) {
alpar@1445
   614
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1445
   615
	  j->second/=c;
alpar@1445
   616
	return *this;
alpar@1445
   617
      }
alpar@1445
   618
    };
alpar@1445
   619
    
alpar@1253
   620
deba@2312
   621
  private:
deba@2312
   622
deba@2364
   623
    template <typename _Expr>
deba@2364
   624
    class MappedOutputIterator {
deba@2312
   625
    public:
deba@2312
   626
deba@2364
   627
      typedef std::insert_iterator<_Expr> Base;
deba@2364
   628
deba@2364
   629
      typedef std::output_iterator_tag iterator_category;
deba@2364
   630
      typedef void difference_type;
deba@2364
   631
      typedef void value_type;
deba@2364
   632
      typedef void reference;
deba@2364
   633
      typedef void pointer;
deba@2364
   634
      
deba@2364
   635
      MappedOutputIterator(const Base& _base, const LpSolverBase& _lp) 
deba@2364
   636
        : base(_base), lp(_lp) {}
deba@2364
   637
deba@2364
   638
      MappedOutputIterator& operator*() {
deba@2364
   639
        return *this;
deba@2364
   640
      }
deba@2364
   641
deba@2364
   642
      MappedOutputIterator& operator=(const std::pair<int, Value>& value) {
deba@2364
   643
        *base = std::make_pair(lp._item(value.first, typename _Expr::Key()), 
deba@2364
   644
                               value.second);
deba@2364
   645
        return *this;
deba@2364
   646
      }
deba@2364
   647
deba@2364
   648
      MappedOutputIterator& operator++() {
deba@2364
   649
        ++base;
deba@2364
   650
        return *this;
deba@2364
   651
      }
deba@2364
   652
deba@2364
   653
      MappedOutputIterator operator++(int) {
deba@2364
   654
        MappedOutputIterator tmp(*this);
deba@2364
   655
        ++base;
deba@2364
   656
        return tmp;
deba@2364
   657
      }
deba@2364
   658
deba@2364
   659
      bool operator==(const MappedOutputIterator& it) const {
deba@2364
   660
        return base == it.base;
deba@2364
   661
      }
deba@2364
   662
deba@2364
   663
      bool operator!=(const MappedOutputIterator& it) const {
deba@2364
   664
        return base != it.base;
deba@2364
   665
      }
deba@2364
   666
deba@2364
   667
    private:
deba@2364
   668
      Base base;
deba@2364
   669
      const LpSolverBase& lp;
deba@2364
   670
    };
deba@2364
   671
deba@2364
   672
    template <typename Expr>
deba@2364
   673
    class MappedInputIterator {
deba@2364
   674
    public:
deba@2364
   675
deba@2364
   676
      typedef typename Expr::const_iterator Base;
deba@2312
   677
deba@2312
   678
      typedef typename Base::iterator_category iterator_category;
deba@2312
   679
      typedef typename Base::difference_type difference_type;
deba@2312
   680
      typedef const std::pair<int, Value> value_type;
deba@2312
   681
      typedef value_type reference;
deba@2312
   682
      class pointer {
deba@2312
   683
      public:
deba@2312
   684
        pointer(value_type& _value) : value(_value) {}
deba@2312
   685
        value_type* operator->() { return &value; }
deba@2312
   686
      private:
deba@2312
   687
        value_type value;
deba@2312
   688
      };
deba@2312
   689
deba@2364
   690
      MappedInputIterator(const Base& _base, const LpSolverBase& _lp) 
deba@2312
   691
        : base(_base), lp(_lp) {}
deba@2312
   692
deba@2312
   693
      reference operator*() {
deba@2312
   694
        return std::make_pair(lp._lpId(base->first), base->second);
deba@2312
   695
      }
deba@2312
   696
deba@2312
   697
      pointer operator->() {
deba@2312
   698
        return pointer(operator*());
deba@2312
   699
      }
deba@2312
   700
deba@2364
   701
      MappedInputIterator& operator++() {
deba@2312
   702
        ++base;
deba@2312
   703
        return *this;
deba@2312
   704
      }
deba@2312
   705
deba@2364
   706
      MappedInputIterator operator++(int) {
deba@2364
   707
        MappedInputIterator tmp(*this);
deba@2312
   708
        ++base;
deba@2312
   709
        return tmp;
deba@2312
   710
      }
deba@2312
   711
deba@2364
   712
      bool operator==(const MappedInputIterator& it) const {
deba@2312
   713
        return base == it.base;
deba@2312
   714
      }
deba@2312
   715
deba@2364
   716
      bool operator!=(const MappedInputIterator& it) const {
deba@2312
   717
        return base != it.base;
deba@2312
   718
      }
deba@2312
   719
deba@2312
   720
    private:
deba@2312
   721
      Base base;
deba@2312
   722
      const LpSolverBase& lp;
deba@2312
   723
    };
deba@2312
   724
alpar@1253
   725
  protected:
athos@1246
   726
deba@2312
   727
    /// STL compatible iterator for lp col
deba@2364
   728
    typedef MappedInputIterator<Expr> ConstRowIterator;
deba@2312
   729
    /// STL compatible iterator for lp row
deba@2364
   730
    typedef MappedInputIterator<DualExpr> ConstColIterator;
deba@2364
   731
deba@2364
   732
    /// STL compatible iterator for lp col
deba@2364
   733
    typedef MappedOutputIterator<Expr> RowIterator;
deba@2364
   734
    /// STL compatible iterator for lp row
deba@2364
   735
    typedef MappedOutputIterator<DualExpr> ColIterator;
deba@2312
   736
alpar@1323
   737
    //Abstract virtual functions
alpar@1364
   738
    virtual LpSolverBase &_newLp() = 0;
athos@1436
   739
    virtual LpSolverBase &_copyLp(){
deba@2312
   740
      ///\todo This should be implemented here, too, when we have
deba@2312
   741
      ///problem retrieving routines. It can be overriden.
athos@1436
   742
athos@1436
   743
      //Starting:
athos@1436
   744
      LpSolverBase & newlp(_newLp());
athos@1436
   745
      return newlp;
athos@1436
   746
      //return *(LpSolverBase*)0;
athos@1436
   747
    };
alpar@1364
   748
athos@1246
   749
    virtual int _addCol() = 0;
alpar@2303
   750
    virtual int _addRow() = 0; 
deba@2366
   751
athos@1542
   752
    virtual void _eraseCol(int col) = 0;
athos@1542
   753
    virtual void _eraseRow(int row) = 0;
deba@2366
   754
deba@2366
   755
    virtual void _getColName(int col, std::string & name) const = 0;
alpar@1895
   756
    virtual void _setColName(int col, const std::string & name) = 0;
deba@2366
   757
    virtual int _colByName(const std::string& name) const = 0;
deba@2366
   758
deba@2364
   759
    virtual void _setRowCoeffs(int i, ConstRowIterator b, 
deba@2364
   760
                               ConstRowIterator e) = 0;
deba@2366
   761
    virtual void _getRowCoeffs(int i, RowIterator b) const = 0;
deba@2364
   762
    virtual void _setColCoeffs(int i, ConstColIterator b, 
deba@2364
   763
                               ConstColIterator e) = 0;
deba@2366
   764
    virtual void _getColCoeffs(int i, ColIterator b) const = 0;
athos@1431
   765
    virtual void _setCoeff(int row, int col, Value value) = 0;
deba@2366
   766
    virtual Value _getCoeff(int row, int col) const = 0;
alpar@1294
   767
    virtual void _setColLowerBound(int i, Value value) = 0;
deba@2366
   768
    virtual Value _getColLowerBound(int i) const = 0;
alpar@1294
   769
    virtual void _setColUpperBound(int i, Value value) = 0;
deba@2366
   770
    virtual Value _getColUpperBound(int i) const = 0;
athos@1379
   771
    virtual void _setRowBounds(int i, Value lower, Value upper) = 0;
deba@2366
   772
    virtual void _getRowBounds(int i, Value &lower, Value &upper) const = 0;
athos@2328
   773
alpar@1294
   774
    virtual void _setObjCoeff(int i, Value obj_coef) = 0;
deba@2366
   775
    virtual Value _getObjCoeff(int i) const = 0;
athos@1377
   776
    virtual void _clearObj()=0;
deba@2312
   777
alpar@1303
   778
    virtual SolveExitStatus _solve() = 0;
deba@2366
   779
    virtual Value _getPrimal(int i) const = 0;
deba@2366
   780
    virtual Value _getDual(int i) const = 0;
deba@2366
   781
    virtual Value _getPrimalValue() const = 0;
deba@2366
   782
    virtual bool _isBasicCol(int i) const = 0;
deba@2366
   783
    virtual SolutionStatus _getPrimalStatus() const = 0;
deba@2366
   784
    virtual SolutionStatus _getDualStatus() const = 0;
deba@2366
   785
    virtual ProblemTypes _getProblemType() const = 0;
athos@1460
   786
alpar@1312
   787
    virtual void _setMax() = 0;
alpar@1312
   788
    virtual void _setMin() = 0;
alpar@1312
   789
    
athos@2324
   790
deba@2366
   791
    virtual bool _isMax() const = 0;
athos@2324
   792
alpar@1323
   793
    //Own protected stuff
alpar@1323
   794
    
alpar@1323
   795
    //Constant component of the objective function
alpar@1323
   796
    Value obj_const_comp;
deba@2312
   797
        
alpar@1253
   798
  public:
alpar@1253
   799
alpar@1323
   800
    ///\e
alpar@1323
   801
    LpSolverBase() : obj_const_comp(0) {}
alpar@1253
   802
alpar@1253
   803
    ///\e
alpar@1253
   804
    virtual ~LpSolverBase() {}
alpar@1253
   805
alpar@1364
   806
    ///Creates a new LP problem
alpar@1364
   807
    LpSolverBase &newLp() {return _newLp();}
alpar@1381
   808
    ///Makes a copy of the LP problem
alpar@1364
   809
    LpSolverBase &copyLp() {return _copyLp();}
alpar@1364
   810
    
alpar@1612
   811
    ///\name Build up and modify the LP
alpar@1263
   812
alpar@1263
   813
    ///@{
alpar@1263
   814
alpar@1253
   815
    ///Add a new empty column (i.e a new variable) to the LP
deba@2363
   816
    Col addCol() { Col c; _addCol(); c.id = cols.addId(); return c;}
alpar@1263
   817
alpar@1294
   818
    ///\brief Adds several new columns
alpar@1294
   819
    ///(i.e a variables) at once
alpar@1256
   820
    ///
alpar@1273
   821
    ///This magic function takes a container as its argument
alpar@1256
   822
    ///and fills its elements
alpar@1256
   823
    ///with new columns (i.e. variables)
alpar@1273
   824
    ///\param t can be
alpar@1273
   825
    ///- a standard STL compatible iterable container with
alpar@1273
   826
    ///\ref Col as its \c values_type
alpar@1273
   827
    ///like
alpar@1273
   828
    ///\code
alpar@1273
   829
    ///std::vector<LpSolverBase::Col>
alpar@1273
   830
    ///std::list<LpSolverBase::Col>
alpar@1273
   831
    ///\endcode
alpar@1273
   832
    ///- a standard STL compatible iterable container with
alpar@1273
   833
    ///\ref Col as its \c mapped_type
alpar@1273
   834
    ///like
alpar@1273
   835
    ///\code
alpar@1364
   836
    ///std::map<AnyType,LpSolverBase::Col>
alpar@1273
   837
    ///\endcode
alpar@2260
   838
    ///- an iterable lemon \ref concepts::WriteMap "write map" like 
alpar@1273
   839
    ///\code
alpar@1273
   840
    ///ListGraph::NodeMap<LpSolverBase::Col>
alpar@1273
   841
    ///ListGraph::EdgeMap<LpSolverBase::Col>
alpar@1273
   842
    ///\endcode
alpar@1256
   843
    ///\return The number of the created column.
alpar@1256
   844
#ifdef DOXYGEN
alpar@1256
   845
    template<class T>
alpar@1256
   846
    int addColSet(T &t) { return 0;} 
alpar@1256
   847
#else
alpar@1256
   848
    template<class T>
alpar@1256
   849
    typename enable_if<typename T::value_type::LpSolverCol,int>::type
alpar@1256
   850
    addColSet(T &t,dummy<0> = 0) {
alpar@1256
   851
      int s=0;
alpar@1256
   852
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
alpar@1256
   853
      return s;
alpar@1256
   854
    }
alpar@1256
   855
    template<class T>
alpar@1256
   856
    typename enable_if<typename T::value_type::second_type::LpSolverCol,
alpar@1256
   857
		       int>::type
alpar@1256
   858
    addColSet(T &t,dummy<1> = 1) { 
alpar@1256
   859
      int s=0;
alpar@1256
   860
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1256
   861
	i->second=addCol();
alpar@1256
   862
	s++;
alpar@1256
   863
      }
alpar@1256
   864
      return s;
alpar@1256
   865
    }
alpar@1272
   866
    template<class T>
deba@1810
   867
    typename enable_if<typename T::MapIt::Value::LpSolverCol,
alpar@1272
   868
		       int>::type
alpar@1272
   869
    addColSet(T &t,dummy<2> = 2) { 
alpar@1272
   870
      int s=0;
deba@1810
   871
      for(typename T::MapIt i(t); i!=INVALID; ++i)
alpar@1272
   872
	{
deba@1810
   873
	  i.set(addCol());
alpar@1272
   874
	  s++;
alpar@1272
   875
	}
alpar@1272
   876
      return s;
alpar@1272
   877
    }
alpar@1256
   878
#endif
alpar@1263
   879
alpar@1445
   880
    ///Set a column (i.e a dual constraint) of the LP
alpar@1258
   881
alpar@1445
   882
    ///\param c is the column to be modified
alpar@1445
   883
    ///\param e is a dual linear expression (see \ref DualExpr)
alpar@1445
   884
    ///a better one.
alpar@1899
   885
    void col(Col c,const DualExpr &e) {
deba@2312
   886
      e.simplify();
deba@2364
   887
      _setColCoeffs(_lpId(c), ConstColIterator(e.begin(), *this), 
deba@2364
   888
                    ConstColIterator(e.end(), *this));
deba@2364
   889
    }
deba@2364
   890
deba@2364
   891
    ///Get a column (i.e a dual constraint) of the LP
deba@2364
   892
deba@2364
   893
    ///\param r is the column to get
deba@2364
   894
    ///\return the dual expression associated to the column
deba@2366
   895
    DualExpr col(Col c) const {
deba@2364
   896
      DualExpr e;
deba@2364
   897
      _getColCoeffs(_lpId(c), ColIterator(std::inserter(e, e.end()), *this));
deba@2364
   898
      return e;
alpar@1445
   899
    }
alpar@1445
   900
alpar@1445
   901
    ///Add a new column to the LP
alpar@1445
   902
alpar@1445
   903
    ///\param e is a dual linear expression (see \ref DualExpr)
alpar@1445
   904
    ///\param obj is the corresponding component of the objective
alpar@1445
   905
    ///function. It is 0 by default.
alpar@1445
   906
    ///\return The created column.
deba@2386
   907
    Col addCol(const DualExpr &e, Value o = 0) {
alpar@1445
   908
      Col c=addCol();
alpar@1899
   909
      col(c,e);
deba@2386
   910
      objCoeff(c,o);
alpar@1445
   911
      return c;
alpar@1445
   912
    }
alpar@1445
   913
alpar@1445
   914
    ///Add a new empty row (i.e a new constraint) to the LP
alpar@1445
   915
alpar@1445
   916
    ///This function adds a new empty row (i.e a new constraint) to the LP.
alpar@1258
   917
    ///\return The created row
deba@2363
   918
    Row addRow() { Row r; _addRow(); r.id = rows.addId(); return r;}
alpar@1253
   919
athos@1542
   920
    ///\brief Add several new rows
athos@1542
   921
    ///(i.e a constraints) at once
alpar@1445
   922
    ///
alpar@1445
   923
    ///This magic function takes a container as its argument
alpar@1445
   924
    ///and fills its elements
alpar@1445
   925
    ///with new row (i.e. variables)
alpar@1445
   926
    ///\param t can be
alpar@1445
   927
    ///- a standard STL compatible iterable container with
alpar@1445
   928
    ///\ref Row as its \c values_type
alpar@1445
   929
    ///like
alpar@1445
   930
    ///\code
alpar@1445
   931
    ///std::vector<LpSolverBase::Row>
alpar@1445
   932
    ///std::list<LpSolverBase::Row>
alpar@1445
   933
    ///\endcode
alpar@1445
   934
    ///- a standard STL compatible iterable container with
alpar@1445
   935
    ///\ref Row as its \c mapped_type
alpar@1445
   936
    ///like
alpar@1445
   937
    ///\code
alpar@1445
   938
    ///std::map<AnyType,LpSolverBase::Row>
alpar@1445
   939
    ///\endcode
alpar@2260
   940
    ///- an iterable lemon \ref concepts::WriteMap "write map" like 
alpar@1445
   941
    ///\code
alpar@1445
   942
    ///ListGraph::NodeMap<LpSolverBase::Row>
alpar@1445
   943
    ///ListGraph::EdgeMap<LpSolverBase::Row>
alpar@1445
   944
    ///\endcode
alpar@1445
   945
    ///\return The number of rows created.
alpar@1445
   946
#ifdef DOXYGEN
alpar@1445
   947
    template<class T>
alpar@1445
   948
    int addRowSet(T &t) { return 0;} 
alpar@1445
   949
#else
alpar@1445
   950
    template<class T>
alpar@1445
   951
    typename enable_if<typename T::value_type::LpSolverRow,int>::type
alpar@1445
   952
    addRowSet(T &t,dummy<0> = 0) {
alpar@1445
   953
      int s=0;
alpar@1445
   954
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;}
alpar@1445
   955
      return s;
alpar@1445
   956
    }
alpar@1445
   957
    template<class T>
alpar@1445
   958
    typename enable_if<typename T::value_type::second_type::LpSolverRow,
alpar@1445
   959
		       int>::type
alpar@1445
   960
    addRowSet(T &t,dummy<1> = 1) { 
alpar@1445
   961
      int s=0;
alpar@1445
   962
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1445
   963
	i->second=addRow();
alpar@1445
   964
	s++;
alpar@1445
   965
      }
alpar@1445
   966
      return s;
alpar@1445
   967
    }
alpar@1445
   968
    template<class T>
deba@1810
   969
    typename enable_if<typename T::MapIt::Value::LpSolverRow,
alpar@1445
   970
		       int>::type
alpar@1445
   971
    addRowSet(T &t,dummy<2> = 2) { 
alpar@1445
   972
      int s=0;
deba@1810
   973
      for(typename T::MapIt i(t); i!=INVALID; ++i)
alpar@1445
   974
	{
deba@1810
   975
	  i.set(addRow());
alpar@1445
   976
	  s++;
alpar@1445
   977
	}
alpar@1445
   978
      return s;
alpar@1445
   979
    }
alpar@1445
   980
#endif
alpar@1445
   981
alpar@1445
   982
    ///Set a row (i.e a constraint) of the LP
alpar@1253
   983
alpar@1258
   984
    ///\param r is the row to be modified
alpar@1259
   985
    ///\param l is lower bound (-\ref INF means no bound)
alpar@1258
   986
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   987
    ///\param u is the upper bound (\ref INF means no bound)
deba@2369
   988
    ///\bug This is a temporary function. The interface will change to
alpar@1253
   989
    ///a better one.
alpar@1328
   990
    ///\todo Option to control whether a constraint with a single variable is
alpar@1328
   991
    ///added or not.
deba@2366
   992
    void row(Row r, Value l, const Expr &e, Value u) {
deba@2312
   993
      e.simplify();
deba@2364
   994
      _setRowCoeffs(_lpId(r), ConstRowIterator(e.begin(), *this),
deba@2364
   995
                    ConstRowIterator(e.end(), *this));
deba@2364
   996
      _setRowBounds(_lpId(r),l-e.constComp(),u-e.constComp());
alpar@1258
   997
    }
alpar@1258
   998
alpar@1445
   999
    ///Set a row (i.e a constraint) of the LP
alpar@1264
  1000
alpar@1264
  1001
    ///\param r is the row to be modified
alpar@1264
  1002
    ///\param c is a linear expression (see \ref Constr)
alpar@1895
  1003
    void row(Row r, const Constr &c) {
deba@2312
  1004
      row(r, c.lowerBounded()?c.lowerBound():-INF,
deba@2312
  1005
          c.expr(), c.upperBounded()?c.upperBound():INF);
alpar@1264
  1006
    }
alpar@1264
  1007
deba@2364
  1008
    
deba@2364
  1009
    ///Get a row (i.e a constraint) of the LP
deba@2364
  1010
deba@2364
  1011
    ///\param r is the row to get
deba@2364
  1012
    ///\return the expression associated to the row
deba@2366
  1013
    Expr row(Row r) const {
deba@2364
  1014
      Expr e;
deba@2364
  1015
      _getRowCoeffs(_lpId(r), RowIterator(std::inserter(e, e.end()), *this));
deba@2364
  1016
      return e;
deba@2364
  1017
    }
deba@2364
  1018
alpar@1445
  1019
    ///Add a new row (i.e a new constraint) to the LP
alpar@1258
  1020
alpar@1259
  1021
    ///\param l is the lower bound (-\ref INF means no bound)
alpar@1258
  1022
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
  1023
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1258
  1024
    ///\return The created row.
deba@2369
  1025
    ///\bug This is a temporary function. The interface will change to
alpar@1258
  1026
    ///a better one.
alpar@1258
  1027
    Row addRow(Value l,const Expr &e, Value u) {
alpar@1258
  1028
      Row r=addRow();
alpar@1895
  1029
      row(r,l,e,u);
alpar@1253
  1030
      return r;
alpar@1253
  1031
    }
alpar@1253
  1032
alpar@1445
  1033
    ///Add a new row (i.e a new constraint) to the LP
alpar@1264
  1034
alpar@1264
  1035
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
  1036
    ///\return The created row.
alpar@1264
  1037
    Row addRow(const Constr &c) {
alpar@1264
  1038
      Row r=addRow();
alpar@1895
  1039
      row(r,c);
alpar@1264
  1040
      return r;
alpar@1264
  1041
    }
athos@1542
  1042
    ///Erase a coloumn (i.e a variable) from the LP
athos@1542
  1043
athos@1542
  1044
    ///\param c is the coloumn to be deleted
athos@1542
  1045
    ///\todo Please check this
athos@1542
  1046
    void eraseCol(Col c) {
deba@2312
  1047
      _eraseCol(_lpId(c));
deba@2363
  1048
      cols.eraseId(c.id);
athos@1542
  1049
    }
athos@1542
  1050
    ///Erase a  row (i.e a constraint) from the LP
athos@1542
  1051
athos@1542
  1052
    ///\param r is the row to be deleted
athos@1542
  1053
    ///\todo Please check this
athos@1542
  1054
    void eraseRow(Row r) {
deba@2312
  1055
      _eraseRow(_lpId(r));
deba@2363
  1056
      rows.eraseId(r.id);
athos@1542
  1057
    }
alpar@1264
  1058
alpar@1895
  1059
    /// Get the name of a column
alpar@1895
  1060
    
alpar@1895
  1061
    ///\param c is the coresponding coloumn 
alpar@1895
  1062
    ///\return The name of the colunm
deba@2366
  1063
    std::string colName(Col c) const {
alpar@1895
  1064
      std::string name;
deba@2312
  1065
      _getColName(_lpId(c), name);
alpar@1895
  1066
      return name;
alpar@1895
  1067
    }
alpar@1895
  1068
    
alpar@1895
  1069
    /// Set the name of a column
alpar@1895
  1070
    
alpar@1895
  1071
    ///\param c is the coresponding coloumn 
alpar@1895
  1072
    ///\param name The name to be given
deba@2366
  1073
    void colName(Col c, const std::string& name) {
deba@2312
  1074
      _setColName(_lpId(c), name);
alpar@1895
  1075
    }
deba@2368
  1076
deba@2368
  1077
    /// Get the column by its name
deba@2368
  1078
    
deba@2368
  1079
    ///\param name The name of the column
deba@2368
  1080
    ///\return the proper column or \c INVALID
deba@2368
  1081
    Col colByName(const std::string& name) const {
deba@2368
  1082
      int k = _colByName(name);
deba@2368
  1083
      return k != -1 ? Col(cols.fixId(k)) : Col(INVALID);
deba@2368
  1084
    }
alpar@1895
  1085
    
alpar@1895
  1086
    /// Set an element of the coefficient matrix of the LP
athos@1436
  1087
athos@1436
  1088
    ///\param r is the row of the element to be modified
athos@1436
  1089
    ///\param c is the coloumn of the element to be modified
athos@1436
  1090
    ///\param val is the new value of the coefficient
alpar@1895
  1091
deba@2366
  1092
    void coeff(Row r, Col c, Value val) {
deba@2312
  1093
      _setCoeff(_lpId(r),_lpId(c), val);
athos@1436
  1094
    }
athos@1436
  1095
athos@2324
  1096
    /// Get an element of the coefficient matrix of the LP
athos@2324
  1097
athos@2324
  1098
    ///\param r is the row of the element in question
athos@2324
  1099
    ///\param c is the coloumn of the element in question
athos@2324
  1100
    ///\return the corresponding coefficient
athos@2324
  1101
deba@2366
  1102
    Value coeff(Row r, Col c) const {
athos@2324
  1103
      return _getCoeff(_lpId(r),_lpId(c));
athos@2324
  1104
    }
athos@2324
  1105
alpar@1253
  1106
    /// Set the lower bound of a column (i.e a variable)
alpar@1253
  1107
alpar@1895
  1108
    /// The lower bound of a variable (column) has to be given by an 
alpar@1253
  1109
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
  1110
    /// Value or -\ref INF.
alpar@1293
  1111
    void colLowerBound(Col c, Value value) {
deba@2312
  1112
      _setColLowerBound(_lpId(c),value);
alpar@1253
  1113
    }
athos@2328
  1114
athos@2328
  1115
    /// Get the lower bound of a column (i.e a variable)
athos@2328
  1116
athos@2328
  1117
    /// This function returns the lower bound for column (variable) \t c
athos@2328
  1118
    /// (this might be -\ref INF as well).  
athos@2328
  1119
    ///\return The lower bound for coloumn \t c
deba@2366
  1120
    Value colLowerBound(Col c) const {
athos@2328
  1121
      return _getColLowerBound(_lpId(c));
athos@2328
  1122
    }
alpar@1895
  1123
    
alpar@1895
  1124
    ///\brief Set the lower bound of  several columns
alpar@1895
  1125
    ///(i.e a variables) at once
alpar@1895
  1126
    ///
alpar@1895
  1127
    ///This magic function takes a container as its argument
alpar@1895
  1128
    ///and applies the function on all of its elements.
alpar@1895
  1129
    /// The lower bound of a variable (column) has to be given by an 
alpar@1895
  1130
    /// extended number of type Value, i.e. a finite number of type 
alpar@1895
  1131
    /// Value or -\ref INF.
alpar@1895
  1132
#ifdef DOXYGEN
alpar@1895
  1133
    template<class T>
alpar@1895
  1134
    void colLowerBound(T &t, Value value) { return 0;} 
alpar@1895
  1135
#else
alpar@1895
  1136
    template<class T>
alpar@1895
  1137
    typename enable_if<typename T::value_type::LpSolverCol,void>::type
alpar@1895
  1138
    colLowerBound(T &t, Value value,dummy<0> = 0) {
alpar@1895
  1139
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1895
  1140
	colLowerBound(*i, value);
alpar@1895
  1141
      }
alpar@1895
  1142
    }
alpar@1895
  1143
    template<class T>
alpar@1895
  1144
    typename enable_if<typename T::value_type::second_type::LpSolverCol,
alpar@1895
  1145
		       void>::type
alpar@1895
  1146
    colLowerBound(T &t, Value value,dummy<1> = 1) { 
alpar@1895
  1147
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1895
  1148
	colLowerBound(i->second, value);
alpar@1895
  1149
      }
alpar@1895
  1150
    }
alpar@1895
  1151
    template<class T>
alpar@1895
  1152
    typename enable_if<typename T::MapIt::Value::LpSolverCol,
alpar@1895
  1153
		       void>::type
alpar@1895
  1154
    colLowerBound(T &t, Value value,dummy<2> = 2) { 
alpar@1895
  1155
      for(typename T::MapIt i(t); i!=INVALID; ++i){
alpar@1895
  1156
	colLowerBound(*i, value);
alpar@1895
  1157
      }
alpar@1895
  1158
    }
alpar@1895
  1159
#endif
alpar@1895
  1160
    
alpar@1253
  1161
    /// Set the upper bound of a column (i.e a variable)
alpar@1253
  1162
alpar@1293
  1163
    /// The upper bound of a variable (column) has to be given by an 
alpar@1253
  1164
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
  1165
    /// Value or \ref INF.
alpar@1293
  1166
    void colUpperBound(Col c, Value value) {
deba@2312
  1167
      _setColUpperBound(_lpId(c),value);
alpar@1253
  1168
    };
alpar@1895
  1169
athos@2328
  1170
    /// Get the upper bound of a column (i.e a variable)
athos@2328
  1171
athos@2328
  1172
    /// This function returns the upper bound for column (variable) \t c
athos@2328
  1173
    /// (this might be \ref INF as well).  
athos@2328
  1174
    ///\return The upper bound for coloumn \t c
deba@2366
  1175
    Value colUpperBound(Col c) const {
athos@2328
  1176
      return _getColUpperBound(_lpId(c));
athos@2328
  1177
    }
athos@2328
  1178
athos@2328
  1179
    ///\brief Set the upper bound of  several columns
alpar@1895
  1180
    ///(i.e a variables) at once
alpar@1895
  1181
    ///
alpar@1895
  1182
    ///This magic function takes a container as its argument
alpar@1895
  1183
    ///and applies the function on all of its elements.
alpar@1895
  1184
    /// The upper bound of a variable (column) has to be given by an 
alpar@1895
  1185
    /// extended number of type Value, i.e. a finite number of type 
alpar@1895
  1186
    /// Value or \ref INF.
alpar@1895
  1187
#ifdef DOXYGEN
alpar@1895
  1188
    template<class T>
alpar@1895
  1189
    void colUpperBound(T &t, Value value) { return 0;} 
alpar@1895
  1190
#else
alpar@1895
  1191
    template<class T>
alpar@1895
  1192
    typename enable_if<typename T::value_type::LpSolverCol,void>::type
alpar@1895
  1193
    colUpperBound(T &t, Value value,dummy<0> = 0) {
alpar@1895
  1194
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1895
  1195
	colUpperBound(*i, value);
alpar@1895
  1196
      }
alpar@1895
  1197
    }
alpar@1895
  1198
    template<class T>
alpar@1895
  1199
    typename enable_if<typename T::value_type::second_type::LpSolverCol,
alpar@1895
  1200
		       void>::type
alpar@1895
  1201
    colUpperBound(T &t, Value value,dummy<1> = 1) { 
alpar@1895
  1202
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1895
  1203
	colUpperBound(i->second, value);
alpar@1895
  1204
      }
alpar@1895
  1205
    }
alpar@1895
  1206
    template<class T>
alpar@1895
  1207
    typename enable_if<typename T::MapIt::Value::LpSolverCol,
alpar@1895
  1208
		       void>::type
alpar@1895
  1209
    colUpperBound(T &t, Value value,dummy<2> = 2) { 
alpar@1895
  1210
      for(typename T::MapIt i(t); i!=INVALID; ++i){
alpar@1895
  1211
	colUpperBound(*i, value);
alpar@1895
  1212
      }
alpar@1895
  1213
    }
alpar@1895
  1214
#endif
alpar@1895
  1215
alpar@1293
  1216
    /// Set the lower and the upper bounds of a column (i.e a variable)
alpar@1293
  1217
alpar@1293
  1218
    /// The lower and the upper bounds of
alpar@1293
  1219
    /// a variable (column) have to be given by an 
alpar@1293
  1220
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
  1221
    /// Value, -\ref INF or \ref INF.
alpar@1293
  1222
    void colBounds(Col c, Value lower, Value upper) {
deba@2312
  1223
      _setColLowerBound(_lpId(c),lower);
deba@2312
  1224
      _setColUpperBound(_lpId(c),upper);
alpar@1293
  1225
    }
alpar@1293
  1226
    
alpar@1895
  1227
    ///\brief Set the lower and the upper bound of several columns
alpar@1895
  1228
    ///(i.e a variables) at once
alpar@1895
  1229
    ///
alpar@1895
  1230
    ///This magic function takes a container as its argument
alpar@1895
  1231
    ///and applies the function on all of its elements.
alpar@1895
  1232
    /// The lower and the upper bounds of
alpar@1895
  1233
    /// a variable (column) have to be given by an 
alpar@1895
  1234
    /// extended number of type Value, i.e. a finite number of type 
alpar@1895
  1235
    /// Value, -\ref INF or \ref INF.
alpar@1895
  1236
#ifdef DOXYGEN
alpar@1895
  1237
    template<class T>
alpar@1895
  1238
    void colBounds(T &t, Value lower, Value upper) { return 0;} 
alpar@1895
  1239
#else
alpar@1895
  1240
    template<class T>
alpar@1895
  1241
    typename enable_if<typename T::value_type::LpSolverCol,void>::type
alpar@1895
  1242
    colBounds(T &t, Value lower, Value upper,dummy<0> = 0) {
alpar@1895
  1243
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1895
  1244
	colBounds(*i, lower, upper);
alpar@1895
  1245
      }
alpar@1895
  1246
    }
alpar@1895
  1247
    template<class T>
alpar@1895
  1248
    typename enable_if<typename T::value_type::second_type::LpSolverCol,
alpar@1895
  1249
		       void>::type
alpar@1895
  1250
    colBounds(T &t, Value lower, Value upper,dummy<1> = 1) { 
alpar@1895
  1251
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1895
  1252
	colBounds(i->second, lower, upper);
alpar@1895
  1253
      }
alpar@1895
  1254
    }
alpar@1895
  1255
    template<class T>
alpar@1895
  1256
    typename enable_if<typename T::MapIt::Value::LpSolverCol,
alpar@1895
  1257
		       void>::type
alpar@1895
  1258
    colBounds(T &t, Value lower, Value upper,dummy<2> = 2) { 
alpar@1895
  1259
      for(typename T::MapIt i(t); i!=INVALID; ++i){
alpar@1895
  1260
	colBounds(*i, lower, upper);
alpar@1895
  1261
      }
alpar@1895
  1262
    }
alpar@1895
  1263
#endif
alpar@1895
  1264
    
athos@1405
  1265
athos@1405
  1266
    /// Set the lower and the upper bounds of a row (i.e a constraint)
alpar@1293
  1267
deba@2363
  1268
    /// The lower and the upper bound of a constraint (row) have to be
deba@2363
  1269
    /// given by an extended number of type Value, i.e. a finite
deba@2363
  1270
    /// number of type Value, -\ref INF or \ref INF. There is no
deba@2363
  1271
    /// separate function for the lower and the upper bound because
deba@2363
  1272
    /// that would have been hard to implement for CPLEX.
alpar@1293
  1273
    void rowBounds(Row c, Value lower, Value upper) {
deba@2312
  1274
      _setRowBounds(_lpId(c),lower, upper);
alpar@1293
  1275
    }
alpar@1293
  1276
    
athos@2328
  1277
    /// Get the lower and the upper bounds of a row (i.e a constraint)
athos@2328
  1278
athos@2328
  1279
    /// The lower and the upper bound of
athos@2328
  1280
    /// a constraint (row) are  
athos@2328
  1281
    /// extended numbers of type Value, i.e.  finite numbers of type 
athos@2328
  1282
    /// Value, -\ref INF or \ref INF. 
athos@2328
  1283
    /// \todo There is no separate function for the 
athos@2328
  1284
    /// lower and the upper bound because we had problems with the 
athos@2328
  1285
    /// implementation of the setting functions for CPLEX:  
athos@2328
  1286
    /// check out whether this can be done for these functions.
deba@2366
  1287
    void getRowBounds(Row c, Value &lower, Value &upper) const {
athos@2328
  1288
      _getRowBounds(_lpId(c),lower, upper);
athos@2328
  1289
    }
athos@2328
  1290
alpar@1253
  1291
    ///Set an element of the objective function
deba@2312
  1292
    void objCoeff(Col c, Value v) {_setObjCoeff(_lpId(c),v); };
athos@2324
  1293
athos@2324
  1294
    ///Get an element of the objective function
deba@2366
  1295
    Value objCoeff(Col c) const { return _getObjCoeff(_lpId(c)); };
athos@2324
  1296
alpar@1253
  1297
    ///Set the objective function
athos@2324
  1298
alpar@1253
  1299
    ///\param e is a linear expression of type \ref Expr.
deba@2369
  1300
    void obj(Expr e) {
athos@1377
  1301
      _clearObj();
alpar@1253
  1302
      for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
alpar@1293
  1303
	objCoeff((*i).first,(*i).second);
alpar@1323
  1304
      obj_const_comp=e.constComp();
alpar@1253
  1305
    }
alpar@1263
  1306
deba@2364
  1307
    ///Get the objective function
deba@2364
  1308
deba@2364
  1309
    ///\return the objective function as a linear expression of type \ref Expr.
deba@2366
  1310
    Expr obj() const {
deba@2364
  1311
      Expr e;
deba@2364
  1312
      for (ColIt it(*this); it != INVALID; ++it) {
deba@2364
  1313
        double c = objCoeff(it);
deba@2364
  1314
        if (c != 0.0) {
deba@2364
  1315
          e.insert(std::make_pair(it, c));
deba@2364
  1316
        }
deba@2364
  1317
      }
deba@2364
  1318
      return e;
deba@2364
  1319
    }
deba@2364
  1320
    
deba@2364
  1321
alpar@1312
  1322
    ///Maximize
alpar@1312
  1323
    void max() { _setMax(); }
alpar@1312
  1324
    ///Minimize
alpar@1312
  1325
    void min() { _setMin(); }
alpar@1312
  1326
athos@2324
  1327
    ///Query function: is this a maximization problem?
deba@2369
  1328
    bool isMax() const {return _isMax(); }
athos@2324
  1329
athos@2324
  1330
    ///Query function: is this a minimization problem?
deba@2369
  1331
    bool isMin() const {return !isMax(); }
alpar@1312
  1332
    
alpar@1263
  1333
    ///@}
alpar@1263
  1334
alpar@1263
  1335
alpar@1294
  1336
    ///\name Solve the LP
alpar@1263
  1337
alpar@1263
  1338
    ///@{
alpar@1263
  1339
athos@1458
  1340
    ///\e Solve the LP problem at hand
athos@1458
  1341
    ///
deba@2026
  1342
    ///\return The result of the optimization procedure. Possible 
deba@2026
  1343
    ///values and their meanings can be found in the documentation of 
deba@2026
  1344
    ///\ref SolveExitStatus.
athos@1458
  1345
    ///
athos@1458
  1346
    ///\todo Which method is used to solve the problem
alpar@1303
  1347
    SolveExitStatus solve() { return _solve(); }
alpar@1263
  1348
    
alpar@1263
  1349
    ///@}
alpar@1263
  1350
    
alpar@1294
  1351
    ///\name Obtain the solution
alpar@1263
  1352
alpar@1263
  1353
    ///@{
alpar@1263
  1354
athos@1460
  1355
    /// The status of the primal problem (the original LP problem)
deba@2366
  1356
    SolutionStatus primalStatus() const {
alpar@1312
  1357
      return _getPrimalStatus();
alpar@1294
  1358
    }
alpar@1294
  1359
athos@1460
  1360
    /// The status of the dual (of the original LP) problem 
deba@2366
  1361
    SolutionStatus dualStatus() const {
athos@1460
  1362
      return _getDualStatus();
athos@1460
  1363
    }
athos@1460
  1364
athos@1460
  1365
    ///The type of the original LP problem
deba@2366
  1366
    ProblemTypes problemType() const {
athos@1460
  1367
      return _getProblemType();
athos@1460
  1368
    }
athos@1460
  1369
alpar@1294
  1370
    ///\e
deba@2366
  1371
    Value primal(Col c) const { return _getPrimal(_lpId(c)); }
deba@2513
  1372
    ///\e
deba@2513
  1373
    Value primal(const Expr& e) const {
deba@2513
  1374
      double res = e.constComp();
deba@2513
  1375
      for (std::map<Col, double>::const_iterator it = e.begin();
deba@2513
  1376
	   it != e.end(); ++it) {
deba@2513
  1377
	res += _getPrimal(_lpId(it->first)) * it->second;
deba@2513
  1378
      }
deba@2513
  1379
      return res; 
deba@2513
  1380
    }
alpar@1263
  1381
alpar@1312
  1382
    ///\e
deba@2366
  1383
    Value dual(Row r) const { return _getDual(_lpId(r)); }
deba@2513
  1384
    ///\e
deba@2513
  1385
    Value dual(const DualExpr& e) const {
deba@2513
  1386
      double res = 0.0;
deba@2513
  1387
      for (std::map<Row, double>::const_iterator it = e.begin();
deba@2513
  1388
	   it != e.end(); ++it) {
deba@2513
  1389
	res += _getPrimal(_lpId(it->first)) * it->second;
deba@2513
  1390
      }
deba@2513
  1391
      return res; 
deba@2513
  1392
    }
marci@1787
  1393
marci@1787
  1394
    ///\e
deba@2366
  1395
    bool isBasicCol(Col c) const { return _isBasicCol(_lpId(c)); }
marci@1840
  1396
marci@1840
  1397
    ///\e
alpar@1312
  1398
alpar@1312
  1399
    ///\return
alpar@1312
  1400
    ///- \ref INF or -\ref INF means either infeasibility or unboundedness
alpar@1312
  1401
    /// of the primal problem, depending on whether we minimize or maximize.
alpar@1364
  1402
    ///- \ref NaN if no primal solution is found.
alpar@1312
  1403
    ///- The (finite) objective value if an optimal solution is found.
deba@2366
  1404
    Value primalValue() const { return _getPrimalValue()+obj_const_comp;}
alpar@1263
  1405
    ///@}
alpar@1253
  1406
    
athos@1248
  1407
  };  
athos@1246
  1408
athos@2144
  1409
deba@2370
  1410
  /// \ingroup lp_group
deba@2370
  1411
  ///
deba@2370
  1412
  /// \brief Common base class for MIP solvers
deba@2370
  1413
  /// \todo Much more docs
athos@2144
  1414
  class MipSolverBase : virtual public LpSolverBase{
athos@2144
  1415
  public:
athos@2144
  1416
athos@2148
  1417
    ///Possible variable (coloumn) types (e.g. real, integer, binary etc.)
athos@2148
  1418
    enum ColTypes {
athos@2148
  1419
      ///Continuous variable
athos@2148
  1420
      REAL = 0,
athos@2148
  1421
      ///Integer variable
athos@2218
  1422
athos@2218
  1423
      ///Unfortunately, cplex 7.5 somewhere writes something like
athos@2218
  1424
      ///#define INTEGER 'I'
athos@2267
  1425
      INT = 1
athos@2148
  1426
      ///\todo No support for other types yet.
athos@2148
  1427
    };
athos@2148
  1428
athos@2148
  1429
    ///Sets the type of the given coloumn to the given type
athos@2144
  1430
    ///
athos@2148
  1431
    ///Sets the type of the given coloumn to the given type.
athos@2148
  1432
    void colType(Col c, ColTypes col_type) {
deba@2312
  1433
      _colType(_lpId(c),col_type);
athos@2144
  1434
    }
athos@2144
  1435
athos@2144
  1436
    ///Gives back the type of the column.
athos@2144
  1437
    ///
athos@2144
  1438
    ///Gives back the type of the column.
deba@2366
  1439
    ColTypes colType(Col c) const {
deba@2312
  1440
      return _colType(_lpId(c));
athos@2148
  1441
    }
athos@2148
  1442
athos@2148
  1443
    ///Sets the type of the given Col to integer or remove that property.
athos@2148
  1444
    ///
athos@2148
  1445
    ///Sets the type of the given Col to integer or remove that property.
athos@2148
  1446
    void integer(Col c, bool enable) {
athos@2148
  1447
      if (enable)
athos@2267
  1448
	colType(c,INT);
athos@2148
  1449
      else
athos@2148
  1450
	colType(c,REAL);
athos@2148
  1451
    }
athos@2148
  1452
athos@2148
  1453
    ///Gives back whether the type of the column is integer or not.
athos@2148
  1454
    ///
athos@2148
  1455
    ///Gives back the type of the column.
athos@2144
  1456
    ///\return true if the column has integer type and false if not.
deba@2366
  1457
    bool integer(Col c) const {
athos@2267
  1458
      return (colType(c)==INT);
athos@2144
  1459
    }
athos@2144
  1460
athos@2185
  1461
    /// The status of the MIP problem
deba@2366
  1462
    SolutionStatus mipStatus() const {
athos@2185
  1463
      return _getMipStatus();
athos@2185
  1464
    }
athos@2185
  1465
athos@2144
  1466
  protected:
athos@2144
  1467
deba@2366
  1468
    virtual ColTypes _colType(int col) const = 0;
athos@2148
  1469
    virtual void _colType(int col, ColTypes col_type) = 0;
deba@2366
  1470
    virtual SolutionStatus _getMipStatus() const = 0;
athos@2148
  1471
athos@2144
  1472
  };
alpar@1272
  1473
  
alpar@1272
  1474
  ///\relates LpSolverBase::Expr
alpar@1272
  1475
  ///
alpar@1272
  1476
  inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
alpar@1272
  1477
				      const LpSolverBase::Expr &b) 
alpar@1272
  1478
  {
alpar@1272
  1479
    LpSolverBase::Expr tmp(a);
alpar@1766
  1480
    tmp+=b;
alpar@1272
  1481
    return tmp;
alpar@1272
  1482
  }
alpar@1272
  1483
  ///\e
alpar@1272
  1484
  
alpar@1272
  1485
  ///\relates LpSolverBase::Expr
alpar@1272
  1486
  ///
alpar@1272
  1487
  inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
alpar@1272
  1488
				      const LpSolverBase::Expr &b) 
alpar@1272
  1489
  {
alpar@1272
  1490
    LpSolverBase::Expr tmp(a);
alpar@1766
  1491
    tmp-=b;
alpar@1272
  1492
    return tmp;
alpar@1272
  1493
  }
alpar@1272
  1494
  ///\e
alpar@1272
  1495
  
alpar@1272
  1496
  ///\relates LpSolverBase::Expr
alpar@1272
  1497
  ///
alpar@1272
  1498
  inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
alpar@1273
  1499
				      const LpSolverBase::Value &b) 
alpar@1272
  1500
  {
alpar@1272
  1501
    LpSolverBase::Expr tmp(a);
alpar@1766
  1502
    tmp*=b;
alpar@1272
  1503
    return tmp;
alpar@1272
  1504
  }
alpar@1272
  1505
  
alpar@1272
  1506
  ///\e
alpar@1272
  1507
  
alpar@1272
  1508
  ///\relates LpSolverBase::Expr
alpar@1272
  1509
  ///
alpar@1273
  1510
  inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
alpar@1272
  1511
				      const LpSolverBase::Expr &b) 
alpar@1272
  1512
  {
alpar@1272
  1513
    LpSolverBase::Expr tmp(b);
alpar@1766
  1514
    tmp*=a;
alpar@1272
  1515
    return tmp;
alpar@1272
  1516
  }
alpar@1272
  1517
  ///\e
alpar@1272
  1518
  
alpar@1272
  1519
  ///\relates LpSolverBase::Expr
alpar@1272
  1520
  ///
alpar@1272
  1521
  inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
alpar@1273
  1522
				      const LpSolverBase::Value &b) 
alpar@1272
  1523
  {
alpar@1272
  1524
    LpSolverBase::Expr tmp(a);
alpar@1766
  1525
    tmp/=b;
alpar@1272
  1526
    return tmp;
alpar@1272
  1527
  }
alpar@1272
  1528
  
alpar@1272
  1529
  ///\e
alpar@1272
  1530
  
alpar@1272
  1531
  ///\relates LpSolverBase::Constr
alpar@1272
  1532
  ///
alpar@1272
  1533
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1272
  1534
					 const LpSolverBase::Expr &f) 
alpar@1272
  1535
  {
alpar@1272
  1536
    return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
alpar@1272
  1537
  }
alpar@1272
  1538
alpar@1272
  1539
  ///\e
alpar@1272
  1540
  
alpar@1272
  1541
  ///\relates LpSolverBase::Constr
alpar@1272
  1542
  ///
alpar@1273
  1543
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
alpar@1272
  1544
					 const LpSolverBase::Expr &f) 
alpar@1272
  1545
  {
alpar@1272
  1546
    return LpSolverBase::Constr(e,f);
alpar@1272
  1547
  }
alpar@1272
  1548
alpar@1272
  1549
  ///\e
alpar@1272
  1550
  
alpar@1272
  1551
  ///\relates LpSolverBase::Constr
alpar@1272
  1552
  ///
alpar@1272
  1553
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1273
  1554
					 const LpSolverBase::Value &f) 
alpar@1272
  1555
  {
alpar@1272
  1556
    return LpSolverBase::Constr(e,f);
alpar@1272
  1557
  }
alpar@1272
  1558
alpar@1272
  1559
  ///\e
alpar@1272
  1560
  
alpar@1272
  1561
  ///\relates LpSolverBase::Constr
alpar@1272
  1562
  ///
alpar@1272
  1563
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1272
  1564
					 const LpSolverBase::Expr &f) 
alpar@1272
  1565
  {
alpar@1272
  1566
    return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
alpar@1272
  1567
  }
alpar@1272
  1568
alpar@1272
  1569
alpar@1272
  1570
  ///\e
alpar@1272
  1571
  
alpar@1272
  1572
  ///\relates LpSolverBase::Constr
alpar@1272
  1573
  ///
alpar@1273
  1574
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
alpar@1272
  1575
					 const LpSolverBase::Expr &f) 
alpar@1272
  1576
  {
alpar@1272
  1577
    return LpSolverBase::Constr(f,e);
alpar@1272
  1578
  }
alpar@1272
  1579
alpar@1272
  1580
alpar@1272
  1581
  ///\e
alpar@1272
  1582
  
alpar@1272
  1583
  ///\relates LpSolverBase::Constr
alpar@1272
  1584
  ///
alpar@1272
  1585
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1273
  1586
					 const LpSolverBase::Value &f) 
alpar@1272
  1587
  {
alpar@1272
  1588
    return LpSolverBase::Constr(f,e);
alpar@1272
  1589
  }
alpar@1272
  1590
alpar@1272
  1591
  ///\e
athos@2345
  1592
athos@2345
  1593
  ///\relates LpSolverBase::Constr
athos@2345
  1594
  ///
athos@2345
  1595
  inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
athos@2345
  1596
					 const LpSolverBase::Value &f) 
athos@2345
  1597
  {
athos@2345
  1598
    return LpSolverBase::Constr(f,e,f);
athos@2345
  1599
  }
athos@2345
  1600
athos@2345
  1601
  ///\e
alpar@1272
  1602
  
alpar@1272
  1603
  ///\relates LpSolverBase::Constr
alpar@1272
  1604
  ///
alpar@1272
  1605
  inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
alpar@1272
  1606
					 const LpSolverBase::Expr &f) 
alpar@1272
  1607
  {
alpar@1272
  1608
    return LpSolverBase::Constr(0,e-f,0);
alpar@1272
  1609
  }
alpar@1272
  1610
alpar@1272
  1611
  ///\e
alpar@1272
  1612
  
alpar@1272
  1613
  ///\relates LpSolverBase::Constr
alpar@1272
  1614
  ///
alpar@1273
  1615
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
alpar@1272
  1616
					 const LpSolverBase::Constr&c) 
alpar@1272
  1617
  {
alpar@1272
  1618
    LpSolverBase::Constr tmp(c);
alpar@1273
  1619
    ///\todo Create an own exception type.
deba@2026
  1620
    if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
alpar@1273
  1621
    else tmp.lowerBound()=n;
alpar@1272
  1622
    return tmp;
alpar@1272
  1623
  }
alpar@1272
  1624
  ///\e
alpar@1272
  1625
  
alpar@1272
  1626
  ///\relates LpSolverBase::Constr
alpar@1272
  1627
  ///
alpar@1272
  1628
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
alpar@1273
  1629
					 const LpSolverBase::Value &n)
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
alpar@1272
  1638
  ///\e
alpar@1272
  1639
  
alpar@1272
  1640
  ///\relates LpSolverBase::Constr
alpar@1272
  1641
  ///
alpar@1273
  1642
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
alpar@1272
  1643
					 const LpSolverBase::Constr&c) 
alpar@1272
  1644
  {
alpar@1272
  1645
    LpSolverBase::Constr tmp(c);
alpar@1273
  1646
    ///\todo Create an own exception type.
deba@2026
  1647
    if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
alpar@1273
  1648
    else tmp.upperBound()=n;
alpar@1272
  1649
    return tmp;
alpar@1272
  1650
  }
alpar@1272
  1651
  ///\e
alpar@1272
  1652
  
alpar@1272
  1653
  ///\relates LpSolverBase::Constr
alpar@1272
  1654
  ///
alpar@1272
  1655
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
alpar@1273
  1656
					 const LpSolverBase::Value &n)
alpar@1272
  1657
  {
alpar@1272
  1658
    LpSolverBase::Constr tmp(c);
alpar@1273
  1659
    ///\todo Create an own exception type.
deba@2026
  1660
    if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
alpar@1273
  1661
    else tmp.lowerBound()=n;
alpar@1272
  1662
    return tmp;
alpar@1272
  1663
  }
alpar@1272
  1664
alpar@1445
  1665
  ///\e
alpar@1445
  1666
  
alpar@1445
  1667
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1668
  ///
alpar@1445
  1669
  inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a,
deba@2312
  1670
                                          const LpSolverBase::DualExpr &b) 
alpar@1445
  1671
  {
alpar@1445
  1672
    LpSolverBase::DualExpr tmp(a);
alpar@1766
  1673
    tmp+=b;
alpar@1445
  1674
    return tmp;
alpar@1445
  1675
  }
alpar@1445
  1676
  ///\e
alpar@1445
  1677
  
alpar@1445
  1678
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1679
  ///
alpar@1445
  1680
  inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a,
deba@2312
  1681
                                          const LpSolverBase::DualExpr &b) 
alpar@1445
  1682
  {
alpar@1445
  1683
    LpSolverBase::DualExpr tmp(a);
alpar@1766
  1684
    tmp-=b;
alpar@1445
  1685
    return tmp;
alpar@1445
  1686
  }
alpar@1445
  1687
  ///\e
alpar@1445
  1688
  
alpar@1445
  1689
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1690
  ///
alpar@1445
  1691
  inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a,
deba@2312
  1692
                                          const LpSolverBase::Value &b) 
alpar@1445
  1693
  {
alpar@1445
  1694
    LpSolverBase::DualExpr tmp(a);
alpar@1766
  1695
    tmp*=b;
alpar@1445
  1696
    return tmp;
alpar@1445
  1697
  }
alpar@1445
  1698
  
alpar@1445
  1699
  ///\e
alpar@1445
  1700
  
alpar@1445
  1701
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1702
  ///
alpar@1445
  1703
  inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a,
deba@2312
  1704
                                          const LpSolverBase::DualExpr &b) 
alpar@1445
  1705
  {
alpar@1445
  1706
    LpSolverBase::DualExpr tmp(b);
alpar@1766
  1707
    tmp*=a;
alpar@1445
  1708
    return tmp;
alpar@1445
  1709
  }
alpar@1445
  1710
  ///\e
alpar@1445
  1711
  
alpar@1445
  1712
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1713
  ///
alpar@1445
  1714
  inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a,
deba@2312
  1715
                                          const LpSolverBase::Value &b) 
alpar@1445
  1716
  {
alpar@1445
  1717
    LpSolverBase::DualExpr tmp(a);
alpar@1766
  1718
    tmp/=b;
alpar@1445
  1719
    return tmp;
alpar@1445
  1720
  }
alpar@1445
  1721
  
alpar@1272
  1722
athos@1246
  1723
} //namespace lemon
athos@1246
  1724
athos@1246
  1725
#endif //LEMON_LP_BASE_H