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