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