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