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