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