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