lemon/lp_base.h
author deba
Thu, 15 Feb 2007 14:22:08 +0000
changeset 2363 2aabce558574
parent 2345 bfcaad2b84e8
child 2364 3a5e67bd42d2
permissions -rw-r--r--
Changes on the LP interface

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