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