lemon/lp_base.h
author alpar
Thu, 09 Jun 2005 09:49:56 +0000
changeset 1459 2ee881cf30a8
parent 1445 4635352e5524
child 1460 7c58aabb9eea
permissions -rw-r--r--
- InDegMap fixed
- OutDegMap added
- test cases added for them both
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@1253
    50
    int fixId(int n) {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@1253
    55
    int floatingId(int n) { 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@1291
   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
    };
alpar@1263
   140
      
alpar@1256
   141
    ///The floating point type used by the solver
athos@1247
   142
    typedef double Value;
alpar@1256
   143
    ///The infinity constant
athos@1247
   144
    static const Value INF;
alpar@1264
   145
    ///The not a number constant
alpar@1264
   146
    static const Value NaN;
alpar@1253
   147
    
alpar@1256
   148
    ///Refer to a column of the LP.
alpar@1256
   149
alpar@1256
   150
    ///This type is used to refer to a column of the LP.
alpar@1256
   151
    ///
alpar@1256
   152
    ///Its value remains valid and correct even after the addition or erase of
alpar@1273
   153
    ///other columns.
alpar@1256
   154
    ///
alpar@1256
   155
    ///\todo Document what can one do with a Col (INVALID, comparing,
alpar@1256
   156
    ///it is similar to Node/Edge)
alpar@1256
   157
    class Col {
alpar@1256
   158
    protected:
alpar@1256
   159
      int id;
alpar@1256
   160
      friend class LpSolverBase;
alpar@1256
   161
    public:
alpar@1259
   162
      typedef Value ExprValue;
alpar@1256
   163
      typedef True LpSolverCol;
alpar@1256
   164
      Col() {}
alpar@1256
   165
      Col(const Invalid&) : id(-1) {}
alpar@1256
   166
      bool operator<(Col c) const  {return id<c.id;}
alpar@1256
   167
      bool operator==(Col c) const  {return id==c.id;}
alpar@1256
   168
      bool operator!=(Col c) const  {return id==c.id;}
alpar@1256
   169
    };
alpar@1256
   170
alpar@1256
   171
    ///Refer to a row of the LP.
alpar@1256
   172
alpar@1256
   173
    ///This type is used to refer to a row of the LP.
alpar@1256
   174
    ///
alpar@1256
   175
    ///Its value remains valid and correct even after the addition or erase of
alpar@1273
   176
    ///other rows.
alpar@1256
   177
    ///
alpar@1256
   178
    ///\todo Document what can one do with a Row (INVALID, comparing,
alpar@1256
   179
    ///it is similar to Node/Edge)
alpar@1256
   180
    class Row {
alpar@1256
   181
    protected:
alpar@1256
   182
      int id;
alpar@1256
   183
      friend class LpSolverBase;
alpar@1256
   184
    public:
alpar@1259
   185
      typedef Value ExprValue;
alpar@1256
   186
      typedef True LpSolverRow;
alpar@1256
   187
      Row() {}
alpar@1256
   188
      Row(const Invalid&) : id(-1) {}
alpar@1439
   189
alpar@1256
   190
      bool operator<(Row c) const  {return id<c.id;}
alpar@1256
   191
      bool operator==(Row c) const  {return id==c.id;}
alpar@1256
   192
      bool operator!=(Row c) const  {return id==c.id;} 
alpar@1256
   193
   };
alpar@1259
   194
    
alpar@1279
   195
    ///Linear expression of variables and a constant component
alpar@1279
   196
    
alpar@1279
   197
    ///This data structure strores 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@1279
   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;
alpar@1272
   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);
alpar@1272
   280
	  j=i;
alpar@1272
   281
	}
alpar@1272
   282
      }
alpar@1273
   283
alpar@1273
   284
      ///Sets all coefficients and the constant component to 0.
alpar@1273
   285
      void clear() {
alpar@1273
   286
	Base::clear();
alpar@1273
   287
	const_comp=0;
alpar@1273
   288
      }
alpar@1273
   289
alpar@1272
   290
      ///\e
alpar@1272
   291
      Expr &operator+=(const Expr &e) {
alpar@1272
   292
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1272
   293
	  (*this)[j->first]+=j->second;
alpar@1272
   294
	///\todo it might be speeded up using "hints"
alpar@1272
   295
	const_comp+=e.const_comp;
alpar@1272
   296
	return *this;
alpar@1272
   297
      }
alpar@1272
   298
      ///\e
alpar@1272
   299
      Expr &operator-=(const Expr &e) {
alpar@1272
   300
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1272
   301
	  (*this)[j->first]-=j->second;
alpar@1272
   302
	const_comp-=e.const_comp;
alpar@1272
   303
	return *this;
alpar@1272
   304
      }
alpar@1272
   305
      ///\e
alpar@1273
   306
      Expr &operator*=(const Value &c) {
alpar@1272
   307
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1272
   308
	  j->second*=c;
alpar@1272
   309
	const_comp*=c;
alpar@1272
   310
	return *this;
alpar@1272
   311
      }
alpar@1272
   312
      ///\e
alpar@1273
   313
      Expr &operator/=(const Value &c) {
alpar@1272
   314
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1272
   315
	  j->second/=c;
alpar@1272
   316
	const_comp/=c;
alpar@1272
   317
	return *this;
alpar@1272
   318
      }
alpar@1272
   319
    };
alpar@1272
   320
    
alpar@1264
   321
    ///Linear constraint
alpar@1328
   322
alpar@1364
   323
    ///This data stucture represents a linear constraint in the LP.
alpar@1364
   324
    ///Basically it is a linear expression with a lower or an upper bound
alpar@1364
   325
    ///(or both). These parts of the constraint can be obtained by the member
alpar@1364
   326
    ///functions \ref expr(), \ref lowerBound() and \ref upperBound(),
alpar@1364
   327
    ///respectively.
alpar@1364
   328
    ///There are two ways to construct a constraint.
alpar@1364
   329
    ///- You can set the linear expression and the bounds directly
alpar@1364
   330
    ///  by the functions above.
alpar@1364
   331
    ///- The operators <tt>\<=</tt>, <tt>==</tt> and  <tt>\>=</tt>
alpar@1364
   332
    ///  are defined between expressions, or even between constraints whenever
alpar@1364
   333
    ///  it makes sense. Therefore if \c e and \c f are linear expressions and
alpar@1364
   334
    ///  \c s and \c t are numbers, then the followings are valid expressions
alpar@1364
   335
    ///  and thus they can be used directly e.g. in \ref addRow() whenever
alpar@1364
   336
    ///  it makes sense.
alpar@1364
   337
    ///  \code
alpar@1364
   338
    ///  e<=s
alpar@1364
   339
    ///  e<=f
alpar@1364
   340
    ///  s<=e<=t
alpar@1364
   341
    ///  e>=t
alpar@1364
   342
    ///  \endcode
alpar@1364
   343
    ///\warning The validity of a constraint is checked only at run time, so
alpar@1364
   344
    ///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw a
alpar@1364
   345
    ///\ref LogicError exception.
alpar@1272
   346
    class Constr
alpar@1272
   347
    {
alpar@1272
   348
    public:
alpar@1272
   349
      typedef LpSolverBase::Expr Expr;
alpar@1273
   350
      typedef Expr::Key Key;
alpar@1273
   351
      typedef Expr::Value Value;
alpar@1272
   352
      
alpar@1364
   353
//       static const Value INF;
alpar@1364
   354
//       static const Value NaN;
alpar@1364
   355
alpar@1273
   356
    protected:
alpar@1273
   357
      Expr _expr;
alpar@1273
   358
      Value _lb,_ub;
alpar@1273
   359
    public:
alpar@1273
   360
      ///\e
alpar@1273
   361
      Constr() : _expr(), _lb(NaN), _ub(NaN) {}
alpar@1273
   362
      ///\e
alpar@1273
   363
      Constr(Value lb,const Expr &e,Value ub) :
alpar@1273
   364
	_expr(e), _lb(lb), _ub(ub) {}
alpar@1273
   365
      ///\e
alpar@1273
   366
      Constr(const Expr &e,Value ub) : 
alpar@1273
   367
	_expr(e), _lb(NaN), _ub(ub) {}
alpar@1273
   368
      ///\e
alpar@1273
   369
      Constr(Value lb,const Expr &e) :
alpar@1273
   370
	_expr(e), _lb(lb), _ub(NaN) {}
alpar@1273
   371
      ///\e
alpar@1272
   372
      Constr(const Expr &e) : 
alpar@1273
   373
	_expr(e), _lb(NaN), _ub(NaN) {}
alpar@1273
   374
      ///\e
alpar@1273
   375
      void clear() 
alpar@1273
   376
      {
alpar@1273
   377
	_expr.clear();
alpar@1273
   378
	_lb=_ub=NaN;
alpar@1273
   379
      }
alpar@1364
   380
alpar@1364
   381
      ///Reference to the linear expression 
alpar@1273
   382
      Expr &expr() { return _expr; }
alpar@1364
   383
      ///Cont reference to the linear expression 
alpar@1273
   384
      const Expr &expr() const { return _expr; }
alpar@1364
   385
      ///Reference to the lower bound.
alpar@1364
   386
alpar@1364
   387
      ///\return
alpar@1364
   388
      ///- -\ref INF: the constraint is lower unbounded.
alpar@1364
   389
      ///- -\ref NaN: lower bound has not been set.
alpar@1364
   390
      ///- finite number: the lower bound
alpar@1273
   391
      Value &lowerBound() { return _lb; }
alpar@1364
   392
      ///The const version of \ref lowerBound()
alpar@1273
   393
      const Value &lowerBound() const { return _lb; }
alpar@1364
   394
      ///Reference to the upper bound.
alpar@1364
   395
alpar@1364
   396
      ///\return
alpar@1364
   397
      ///- -\ref INF: the constraint is upper unbounded.
alpar@1364
   398
      ///- -\ref NaN: upper bound has not been set.
alpar@1364
   399
      ///- finite number: the upper bound
alpar@1273
   400
      Value &upperBound() { return _ub; }
alpar@1364
   401
      ///The const version of \ref upperBound()
alpar@1273
   402
      const Value &upperBound() const { return _ub; }
alpar@1364
   403
      ///Is the constraint lower bounded?
alpar@1295
   404
      bool lowerBounded() const { 
alpar@1295
   405
	using namespace std;
alpar@1397
   406
	return finite(_lb);
alpar@1295
   407
      }
alpar@1364
   408
      ///Is the constraint upper bounded?
alpar@1295
   409
      bool upperBounded() const {
alpar@1295
   410
	using namespace std;
alpar@1397
   411
	return finite(_ub);
alpar@1295
   412
      }
alpar@1272
   413
    };
alpar@1272
   414
    
alpar@1445
   415
    ///Linear expression of rows
alpar@1445
   416
    
alpar@1445
   417
    ///This data structure represents a column of the matrix,
alpar@1445
   418
    ///thas is it strores a linear expression of the dual variables
alpar@1445
   419
    ///(\ref Row "Row"s).
alpar@1445
   420
    ///
alpar@1445
   421
    ///There are several ways to access and modify the contents of this
alpar@1445
   422
    ///container.
alpar@1445
   423
    ///- Its it fully compatible with \c std::map<Row,double>, so for expamle
alpar@1445
   424
    ///if \c e is an DualExpr and \c v
alpar@1445
   425
    ///and \c w are of type \ref Row, then you can
alpar@1445
   426
    ///read and modify the coefficients like
alpar@1445
   427
    ///these.
alpar@1445
   428
    ///\code
alpar@1445
   429
    ///e[v]=5;
alpar@1445
   430
    ///e[v]+=12;
alpar@1445
   431
    ///e.erase(v);
alpar@1445
   432
    ///\endcode
alpar@1445
   433
    ///or you can also iterate through its elements.
alpar@1445
   434
    ///\code
alpar@1445
   435
    ///double s=0;
alpar@1445
   436
    ///for(LpSolverBase::DualExpr::iterator i=e.begin();i!=e.end();++i)
alpar@1445
   437
    ///  s+=i->second;
alpar@1445
   438
    ///\endcode
alpar@1445
   439
    ///(This code computes the sum of all coefficients).
alpar@1445
   440
    ///- Numbers (<tt>double</tt>'s)
alpar@1445
   441
    ///and variables (\ref Row "Row"s) directly convert to an
alpar@1445
   442
    ///\ref DualExpr and the usual linear operations are defined so  
alpar@1445
   443
    ///\code
alpar@1445
   444
    ///v+w
alpar@1445
   445
    ///2*v-3.12*(v-w/2)
alpar@1445
   446
    ///v*2.1+(3*v+(v*12+w)*3)/2
alpar@1445
   447
    ///\endcode
alpar@1445
   448
    ///are valid \ref DualExpr "DualExpr"essions.
alpar@1445
   449
    ///The usual assignment operations are also defined.
alpar@1445
   450
    ///\code
alpar@1445
   451
    ///e=v+w;
alpar@1445
   452
    ///e+=2*v-3.12*(v-w/2);
alpar@1445
   453
    ///e*=3.4;
alpar@1445
   454
    ///e/=5;
alpar@1445
   455
    ///\endcode
alpar@1445
   456
    ///
alpar@1445
   457
    ///\sa Expr
alpar@1445
   458
    ///
alpar@1445
   459
    class DualExpr : public std::map<Row,Value>
alpar@1445
   460
    {
alpar@1445
   461
    public:
alpar@1445
   462
      typedef LpSolverBase::Row Key; 
alpar@1445
   463
      typedef LpSolverBase::Value Value;
alpar@1445
   464
      
alpar@1445
   465
    protected:
alpar@1445
   466
      typedef std::map<Row,Value> Base;
alpar@1445
   467
      
alpar@1445
   468
    public:
alpar@1445
   469
      typedef True IsLinExpression;
alpar@1445
   470
      ///\e
alpar@1445
   471
      DualExpr() : Base() { }
alpar@1445
   472
      ///\e
alpar@1445
   473
      DualExpr(const Key &v) {
alpar@1445
   474
	Base::insert(std::make_pair(v, 1));
alpar@1445
   475
      }
alpar@1445
   476
      ///\e
alpar@1445
   477
      DualExpr(const Value &v) {}
alpar@1445
   478
      ///\e
alpar@1445
   479
      void set(const Key &v,const Value &c) {
alpar@1445
   480
	Base::insert(std::make_pair(v, c));
alpar@1445
   481
      }
alpar@1445
   482
      
alpar@1445
   483
      ///Removes the components with zero coefficient.
alpar@1445
   484
      void simplify() {
alpar@1445
   485
	for (Base::iterator i=Base::begin(); i!=Base::end();) {
alpar@1445
   486
	  Base::iterator j=i;
alpar@1445
   487
	  ++j;
alpar@1445
   488
	  if ((*i).second==0) Base::erase(i);
alpar@1445
   489
	  j=i;
alpar@1445
   490
	}
alpar@1445
   491
      }
alpar@1445
   492
alpar@1445
   493
      ///Sets all coefficients to 0.
alpar@1445
   494
      void clear() {
alpar@1445
   495
	Base::clear();
alpar@1445
   496
      }
alpar@1445
   497
alpar@1445
   498
      ///\e
alpar@1445
   499
      DualExpr &operator+=(const DualExpr &e) {
alpar@1445
   500
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1445
   501
	  (*this)[j->first]+=j->second;
alpar@1445
   502
	///\todo it might be speeded up using "hints"
alpar@1445
   503
	return *this;
alpar@1445
   504
      }
alpar@1445
   505
      ///\e
alpar@1445
   506
      DualExpr &operator-=(const DualExpr &e) {
alpar@1445
   507
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1445
   508
	  (*this)[j->first]-=j->second;
alpar@1445
   509
	return *this;
alpar@1445
   510
      }
alpar@1445
   511
      ///\e
alpar@1445
   512
      DualExpr &operator*=(const Value &c) {
alpar@1445
   513
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1445
   514
	  j->second*=c;
alpar@1445
   515
	return *this;
alpar@1445
   516
      }
alpar@1445
   517
      ///\e
alpar@1445
   518
      DualExpr &operator/=(const Value &c) {
alpar@1445
   519
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1445
   520
	  j->second/=c;
alpar@1445
   521
	return *this;
alpar@1445
   522
      }
alpar@1445
   523
    };
alpar@1445
   524
    
alpar@1253
   525
alpar@1253
   526
  protected:
alpar@1253
   527
    _FixId rows;
alpar@1253
   528
    _FixId cols;
athos@1246
   529
alpar@1323
   530
    //Abstract virtual functions
alpar@1364
   531
    virtual LpSolverBase &_newLp() = 0;
athos@1436
   532
    virtual LpSolverBase &_copyLp(){
athos@1436
   533
      ///\todo This should be implemented here, too,  when we have problem retrieving routines. It can be overriden.
athos@1436
   534
athos@1436
   535
      //Starting:
athos@1436
   536
      LpSolverBase & newlp(_newLp());
athos@1436
   537
      return newlp;
athos@1436
   538
      //return *(LpSolverBase*)0;
athos@1436
   539
    };
alpar@1364
   540
athos@1246
   541
    virtual int _addCol() = 0;
athos@1246
   542
    virtual int _addRow() = 0;
athos@1246
   543
    virtual void _setRowCoeffs(int i, 
athos@1251
   544
			       int length,
athos@1247
   545
                               int  const * indices, 
athos@1247
   546
                               Value  const * values ) = 0;
athos@1246
   547
    virtual void _setColCoeffs(int i, 
athos@1251
   548
			       int length,
athos@1247
   549
                               int  const * indices, 
athos@1247
   550
                               Value  const * values ) = 0;
athos@1431
   551
    virtual void _setCoeff(int row, int col, Value value) = 0;
alpar@1294
   552
    virtual void _setColLowerBound(int i, Value value) = 0;
alpar@1294
   553
    virtual void _setColUpperBound(int i, Value value) = 0;
athos@1405
   554
//     virtual void _setRowLowerBound(int i, Value value) = 0;
athos@1405
   555
//     virtual void _setRowUpperBound(int i, Value value) = 0;
athos@1379
   556
    virtual void _setRowBounds(int i, Value lower, Value upper) = 0;
alpar@1294
   557
    virtual void _setObjCoeff(int i, Value obj_coef) = 0;
athos@1377
   558
    virtual void _clearObj()=0;
athos@1377
   559
//     virtual void _setObj(int length,
athos@1377
   560
//                          int  const * indices, 
athos@1377
   561
//                          Value  const * values ) = 0;
alpar@1303
   562
    virtual SolveExitStatus _solve() = 0;
alpar@1294
   563
    virtual Value _getPrimal(int i) = 0;
alpar@1312
   564
    virtual Value _getPrimalValue() = 0;
alpar@1312
   565
    virtual SolutionStatus _getPrimalStatus() = 0;
alpar@1312
   566
    virtual void _setMax() = 0;
alpar@1312
   567
    virtual void _setMin() = 0;
alpar@1312
   568
    
alpar@1323
   569
    //Own protected stuff
alpar@1323
   570
    
alpar@1323
   571
    //Constant component of the objective function
alpar@1323
   572
    Value obj_const_comp;
alpar@1323
   573
    
athos@1377
   574
athos@1377
   575
alpar@1323
   576
    
alpar@1253
   577
  public:
alpar@1253
   578
alpar@1323
   579
    ///\e
alpar@1323
   580
    LpSolverBase() : obj_const_comp(0) {}
alpar@1253
   581
alpar@1253
   582
    ///\e
alpar@1253
   583
    virtual ~LpSolverBase() {}
alpar@1253
   584
alpar@1364
   585
    ///Creates a new LP problem
alpar@1364
   586
    LpSolverBase &newLp() {return _newLp();}
alpar@1381
   587
    ///Makes a copy of the LP problem
alpar@1364
   588
    LpSolverBase &copyLp() {return _copyLp();}
alpar@1364
   589
    
alpar@1294
   590
    ///\name Build up and modify of the LP
alpar@1263
   591
alpar@1263
   592
    ///@{
alpar@1263
   593
alpar@1253
   594
    ///Add a new empty column (i.e a new variable) to the LP
alpar@1253
   595
    Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
alpar@1263
   596
alpar@1294
   597
    ///\brief Adds several new columns
alpar@1294
   598
    ///(i.e a variables) at once
alpar@1256
   599
    ///
alpar@1273
   600
    ///This magic function takes a container as its argument
alpar@1256
   601
    ///and fills its elements
alpar@1256
   602
    ///with new columns (i.e. variables)
alpar@1273
   603
    ///\param t can be
alpar@1273
   604
    ///- a standard STL compatible iterable container with
alpar@1273
   605
    ///\ref Col as its \c values_type
alpar@1273
   606
    ///like
alpar@1273
   607
    ///\code
alpar@1273
   608
    ///std::vector<LpSolverBase::Col>
alpar@1273
   609
    ///std::list<LpSolverBase::Col>
alpar@1273
   610
    ///\endcode
alpar@1273
   611
    ///- a standard STL compatible iterable container with
alpar@1273
   612
    ///\ref Col as its \c mapped_type
alpar@1273
   613
    ///like
alpar@1273
   614
    ///\code
alpar@1364
   615
    ///std::map<AnyType,LpSolverBase::Col>
alpar@1273
   616
    ///\endcode
alpar@1273
   617
    ///- an iterable lemon \ref concept::WriteMap "write map" like 
alpar@1273
   618
    ///\code
alpar@1273
   619
    ///ListGraph::NodeMap<LpSolverBase::Col>
alpar@1273
   620
    ///ListGraph::EdgeMap<LpSolverBase::Col>
alpar@1273
   621
    ///\endcode
alpar@1256
   622
    ///\return The number of the created column.
alpar@1256
   623
#ifdef DOXYGEN
alpar@1256
   624
    template<class T>
alpar@1256
   625
    int addColSet(T &t) { return 0;} 
alpar@1256
   626
#else
alpar@1256
   627
    template<class T>
alpar@1256
   628
    typename enable_if<typename T::value_type::LpSolverCol,int>::type
alpar@1256
   629
    addColSet(T &t,dummy<0> = 0) {
alpar@1256
   630
      int s=0;
alpar@1256
   631
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
alpar@1256
   632
      return s;
alpar@1256
   633
    }
alpar@1256
   634
    template<class T>
alpar@1256
   635
    typename enable_if<typename T::value_type::second_type::LpSolverCol,
alpar@1256
   636
		       int>::type
alpar@1256
   637
    addColSet(T &t,dummy<1> = 1) { 
alpar@1256
   638
      int s=0;
alpar@1256
   639
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1256
   640
	i->second=addCol();
alpar@1256
   641
	s++;
alpar@1256
   642
      }
alpar@1256
   643
      return s;
alpar@1256
   644
    }
alpar@1272
   645
    template<class T>
alpar@1272
   646
    typename enable_if<typename T::ValueSet::value_type::LpSolverCol,
alpar@1272
   647
		       int>::type
alpar@1272
   648
    addColSet(T &t,dummy<2> = 2) { 
alpar@1272
   649
      ///\bug <tt>return addColSet(t.valueSet());</tt> should also work.
alpar@1272
   650
      int s=0;
alpar@1272
   651
      for(typename T::ValueSet::iterator i=t.valueSet().begin();
alpar@1272
   652
	  i!=t.valueSet().end();
alpar@1272
   653
	  ++i)
alpar@1272
   654
	{
alpar@1272
   655
	  *i=addCol();
alpar@1272
   656
	  s++;
alpar@1272
   657
	}
alpar@1272
   658
      return s;
alpar@1272
   659
    }
alpar@1256
   660
#endif
alpar@1263
   661
alpar@1445
   662
    ///Set a column (i.e a dual constraint) of the LP
alpar@1258
   663
alpar@1445
   664
    ///\param c is the column to be modified
alpar@1445
   665
    ///\param e is a dual linear expression (see \ref DualExpr)
alpar@1445
   666
    ///\bug This is a temportary function. The interface will change to
alpar@1445
   667
    ///a better one.
alpar@1445
   668
    void setCol(Col c,const DualExpr &e) {
alpar@1445
   669
      std::vector<int> indices;
alpar@1445
   670
      std::vector<Value> values;
alpar@1445
   671
      indices.push_back(0);
alpar@1445
   672
      values.push_back(0);
alpar@1445
   673
      for(DualExpr::const_iterator i=e.begin(); i!=e.end(); ++i)
alpar@1445
   674
	if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
alpar@1445
   675
	  indices.push_back(cols.floatingId((*i).first.id));
alpar@1445
   676
	  values.push_back((*i).second);
alpar@1445
   677
	}
alpar@1445
   678
      _setColCoeffs(cols.floatingId(c.id),indices.size()-1,
alpar@1445
   679
		    &indices[0],&values[0]);
alpar@1445
   680
    }
alpar@1445
   681
alpar@1445
   682
    ///Add a new column to the LP
alpar@1445
   683
alpar@1445
   684
    ///\param e is a dual linear expression (see \ref DualExpr)
alpar@1445
   685
    ///\param obj is the corresponding component of the objective
alpar@1445
   686
    ///function. It is 0 by default.
alpar@1445
   687
    ///\return The created column.
alpar@1445
   688
    ///\bug This is a temportary function. The interface will change to
alpar@1445
   689
    ///a better one.
alpar@1445
   690
    Col addCol(Value l,const DualExpr &e, Value obj=0) {
alpar@1445
   691
      Col c=addCol();
alpar@1445
   692
      setCol(c,e);
alpar@1445
   693
      objCoeff(c,0);
alpar@1445
   694
      return c;
alpar@1445
   695
    }
alpar@1445
   696
alpar@1445
   697
    ///Add a new empty row (i.e a new constraint) to the LP
alpar@1445
   698
alpar@1445
   699
    ///This function adds a new empty row (i.e a new constraint) to the LP.
alpar@1258
   700
    ///\return The created row
alpar@1253
   701
    Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
alpar@1253
   702
alpar@1445
   703
    ///\brief Adds several new row
alpar@1445
   704
    ///(i.e a variables) at once
alpar@1445
   705
    ///
alpar@1445
   706
    ///This magic function takes a container as its argument
alpar@1445
   707
    ///and fills its elements
alpar@1445
   708
    ///with new row (i.e. variables)
alpar@1445
   709
    ///\param t can be
alpar@1445
   710
    ///- a standard STL compatible iterable container with
alpar@1445
   711
    ///\ref Row as its \c values_type
alpar@1445
   712
    ///like
alpar@1445
   713
    ///\code
alpar@1445
   714
    ///std::vector<LpSolverBase::Row>
alpar@1445
   715
    ///std::list<LpSolverBase::Row>
alpar@1445
   716
    ///\endcode
alpar@1445
   717
    ///- a standard STL compatible iterable container with
alpar@1445
   718
    ///\ref Row as its \c mapped_type
alpar@1445
   719
    ///like
alpar@1445
   720
    ///\code
alpar@1445
   721
    ///std::map<AnyType,LpSolverBase::Row>
alpar@1445
   722
    ///\endcode
alpar@1445
   723
    ///- an iterable lemon \ref concept::WriteMap "write map" like 
alpar@1445
   724
    ///\code
alpar@1445
   725
    ///ListGraph::NodeMap<LpSolverBase::Row>
alpar@1445
   726
    ///ListGraph::EdgeMap<LpSolverBase::Row>
alpar@1445
   727
    ///\endcode
alpar@1445
   728
    ///\return The number of rows created.
alpar@1445
   729
#ifdef DOXYGEN
alpar@1445
   730
    template<class T>
alpar@1445
   731
    int addRowSet(T &t) { return 0;} 
alpar@1445
   732
#else
alpar@1445
   733
    template<class T>
alpar@1445
   734
    typename enable_if<typename T::value_type::LpSolverRow,int>::type
alpar@1445
   735
    addRowSet(T &t,dummy<0> = 0) {
alpar@1445
   736
      int s=0;
alpar@1445
   737
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;}
alpar@1445
   738
      return s;
alpar@1445
   739
    }
alpar@1445
   740
    template<class T>
alpar@1445
   741
    typename enable_if<typename T::value_type::second_type::LpSolverRow,
alpar@1445
   742
		       int>::type
alpar@1445
   743
    addRowSet(T &t,dummy<1> = 1) { 
alpar@1445
   744
      int s=0;
alpar@1445
   745
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1445
   746
	i->second=addRow();
alpar@1445
   747
	s++;
alpar@1445
   748
      }
alpar@1445
   749
      return s;
alpar@1445
   750
    }
alpar@1445
   751
    template<class T>
alpar@1445
   752
    typename enable_if<typename T::ValueSet::value_type::LpSolverRow,
alpar@1445
   753
		       int>::type
alpar@1445
   754
    addRowSet(T &t,dummy<2> = 2) { 
alpar@1445
   755
      ///\bug <tt>return addRowSet(t.valueSet());</tt> should also work.
alpar@1445
   756
      int s=0;
alpar@1445
   757
      for(typename T::ValueSet::iterator i=t.valueSet().begin();
alpar@1445
   758
	  i!=t.valueSet().end();
alpar@1445
   759
	  ++i)
alpar@1445
   760
	{
alpar@1445
   761
	  *i=addRow();
alpar@1445
   762
	  s++;
alpar@1445
   763
	}
alpar@1445
   764
      return s;
alpar@1445
   765
    }
alpar@1445
   766
#endif
alpar@1445
   767
alpar@1445
   768
    ///Set a row (i.e a constraint) of the LP
alpar@1253
   769
alpar@1258
   770
    ///\param r is the row to be modified
alpar@1259
   771
    ///\param l is lower bound (-\ref INF means no bound)
alpar@1258
   772
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   773
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1253
   774
    ///\bug This is a temportary function. The interface will change to
alpar@1253
   775
    ///a better one.
alpar@1328
   776
    ///\todo Option to control whether a constraint with a single variable is
alpar@1328
   777
    ///added or not.
alpar@1258
   778
    void setRow(Row r, Value l,const Expr &e, Value u) {
alpar@1253
   779
      std::vector<int> indices;
alpar@1253
   780
      std::vector<Value> values;
alpar@1253
   781
      indices.push_back(0);
alpar@1253
   782
      values.push_back(0);
alpar@1258
   783
      for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i)
alpar@1256
   784
	if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
alpar@1256
   785
	  indices.push_back(cols.floatingId((*i).first.id));
alpar@1256
   786
	  values.push_back((*i).second);
alpar@1256
   787
	}
alpar@1253
   788
      _setRowCoeffs(rows.floatingId(r.id),indices.size()-1,
alpar@1253
   789
		    &indices[0],&values[0]);
athos@1405
   790
//       _setRowLowerBound(rows.floatingId(r.id),l-e.constComp());
athos@1405
   791
//       _setRowUpperBound(rows.floatingId(r.id),u-e.constComp());
athos@1405
   792
       _setRowBounds(rows.floatingId(r.id),l-e.constComp(),u-e.constComp());
alpar@1258
   793
    }
alpar@1258
   794
alpar@1445
   795
    ///Set a row (i.e a constraint) of the LP
alpar@1264
   796
alpar@1264
   797
    ///\param r is the row to be modified
alpar@1264
   798
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   799
    void setRow(Row r, const Constr &c) {
alpar@1273
   800
      setRow(r,
alpar@1275
   801
	     c.lowerBounded()?c.lowerBound():-INF,
alpar@1273
   802
	     c.expr(),
alpar@1275
   803
	     c.upperBounded()?c.upperBound():INF);
alpar@1264
   804
    }
alpar@1264
   805
alpar@1445
   806
    ///Add a new row (i.e a new constraint) to the LP
alpar@1258
   807
alpar@1259
   808
    ///\param l is the lower bound (-\ref INF means no bound)
alpar@1258
   809
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   810
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1258
   811
    ///\return The created row.
alpar@1258
   812
    ///\bug This is a temportary function. The interface will change to
alpar@1258
   813
    ///a better one.
alpar@1258
   814
    Row addRow(Value l,const Expr &e, Value u) {
alpar@1258
   815
      Row r=addRow();
alpar@1258
   816
      setRow(r,l,e,u);
alpar@1253
   817
      return r;
alpar@1253
   818
    }
alpar@1253
   819
alpar@1445
   820
    ///Add a new row (i.e a new constraint) to the LP
alpar@1264
   821
alpar@1264
   822
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   823
    ///\return The created row.
alpar@1264
   824
    Row addRow(const Constr &c) {
alpar@1264
   825
      Row r=addRow();
alpar@1264
   826
      setRow(r,c);
alpar@1264
   827
      return r;
alpar@1264
   828
    }
alpar@1264
   829
athos@1436
   830
    ///Set an element of the coefficient matrix of the LP
athos@1436
   831
athos@1436
   832
    ///\param r is the row of the element to be modified
athos@1436
   833
    ///\param c is the coloumn of the element to be modified
athos@1436
   834
    ///\param val is the new value of the coefficient
athos@1436
   835
    void setCoeff(Row r, Col c, Value val){
athos@1436
   836
      _setCoeff(rows.floatingId(r.id),cols.floatingId(c.id), val);
athos@1436
   837
    }
athos@1436
   838
alpar@1253
   839
    /// Set the lower bound of a column (i.e a variable)
alpar@1253
   840
alpar@1293
   841
    /// The upper bound of a variable (column) has to be given by an 
alpar@1253
   842
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   843
    /// Value or -\ref INF.
alpar@1293
   844
    void colLowerBound(Col c, Value value) {
alpar@1253
   845
      _setColLowerBound(cols.floatingId(c.id),value);
alpar@1253
   846
    }
alpar@1253
   847
    /// Set the upper bound of a column (i.e a variable)
alpar@1253
   848
alpar@1293
   849
    /// The upper bound of a variable (column) has to be given by an 
alpar@1253
   850
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   851
    /// Value or \ref INF.
alpar@1293
   852
    void colUpperBound(Col c, Value value) {
alpar@1253
   853
      _setColUpperBound(cols.floatingId(c.id),value);
alpar@1253
   854
    };
alpar@1293
   855
    /// Set the lower and the upper bounds of a column (i.e a variable)
alpar@1293
   856
alpar@1293
   857
    /// The lower and the upper bounds of
alpar@1293
   858
    /// a variable (column) have to be given by an 
alpar@1293
   859
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
   860
    /// Value, -\ref INF or \ref INF.
alpar@1293
   861
    void colBounds(Col c, Value lower, Value upper) {
alpar@1293
   862
      _setColLowerBound(cols.floatingId(c.id),lower);
alpar@1293
   863
      _setColUpperBound(cols.floatingId(c.id),upper);
alpar@1293
   864
    }
alpar@1293
   865
    
athos@1405
   866
//     /// Set the lower bound of a row (i.e a constraint)
alpar@1253
   867
athos@1405
   868
//     /// The lower bound of a linear expression (row) has to be given by an 
athos@1405
   869
//     /// extended number of type Value, i.e. a finite number of type 
athos@1405
   870
//     /// Value or -\ref INF.
athos@1405
   871
//     void rowLowerBound(Row r, Value value) {
athos@1405
   872
//       _setRowLowerBound(rows.floatingId(r.id),value);
athos@1405
   873
//     };
athos@1405
   874
//     /// Set the upper bound of a row (i.e a constraint)
alpar@1253
   875
athos@1405
   876
//     /// The upper bound of a linear expression (row) has to be given by an 
athos@1405
   877
//     /// extended number of type Value, i.e. a finite number of type 
athos@1405
   878
//     /// Value or \ref INF.
athos@1405
   879
//     void rowUpperBound(Row r, Value value) {
athos@1405
   880
//       _setRowUpperBound(rows.floatingId(r.id),value);
athos@1405
   881
//     };
athos@1405
   882
athos@1405
   883
    /// Set the lower and the upper bounds of a row (i.e a constraint)
alpar@1293
   884
alpar@1293
   885
    /// The lower and the upper bounds of
alpar@1293
   886
    /// a constraint (row) have to be given by an 
alpar@1293
   887
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
   888
    /// Value, -\ref INF or \ref INF.
alpar@1293
   889
    void rowBounds(Row c, Value lower, Value upper) {
athos@1379
   890
      _setRowBounds(rows.floatingId(c.id),lower, upper);
athos@1379
   891
      // _setRowUpperBound(rows.floatingId(c.id),upper);
alpar@1293
   892
    }
alpar@1293
   893
    
alpar@1253
   894
    ///Set an element of the objective function
alpar@1293
   895
    void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); };
alpar@1253
   896
    ///Set the objective function
alpar@1253
   897
    
alpar@1253
   898
    ///\param e is a linear expression of type \ref Expr.
alpar@1323
   899
    ///\bug The previous objective function is not cleared!
alpar@1253
   900
    void setObj(Expr e) {
athos@1377
   901
      _clearObj();
alpar@1253
   902
      for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
alpar@1293
   903
	objCoeff((*i).first,(*i).second);
alpar@1323
   904
      obj_const_comp=e.constComp();
alpar@1253
   905
    }
alpar@1263
   906
alpar@1312
   907
    ///Maximize
alpar@1312
   908
    void max() { _setMax(); }
alpar@1312
   909
    ///Minimize
alpar@1312
   910
    void min() { _setMin(); }
alpar@1312
   911
alpar@1312
   912
    
alpar@1263
   913
    ///@}
alpar@1263
   914
alpar@1263
   915
alpar@1294
   916
    ///\name Solve the LP
alpar@1263
   917
alpar@1263
   918
    ///@{
alpar@1263
   919
athos@1458
   920
    ///\e Solve the LP problem at hand
athos@1458
   921
    ///
athos@1458
   922
    ///\return The result of the optimization procedure. Possible values and their meanings can be found in the documentation of \ref SolveExitStatus.
athos@1458
   923
    ///
athos@1458
   924
    ///\todo Which method is used to solve the problem
alpar@1303
   925
    SolveExitStatus solve() { return _solve(); }
alpar@1263
   926
    
alpar@1263
   927
    ///@}
alpar@1263
   928
    
alpar@1294
   929
    ///\name Obtain the solution
alpar@1263
   930
alpar@1263
   931
    ///@{
alpar@1263
   932
athos@1458
   933
    ///\e 
alpar@1312
   934
    SolutionStatus primalStatus() {
alpar@1312
   935
      return _getPrimalStatus();
alpar@1294
   936
    }
alpar@1294
   937
alpar@1294
   938
    ///\e
alpar@1293
   939
    Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); }
alpar@1263
   940
alpar@1312
   941
    ///\e
alpar@1312
   942
alpar@1312
   943
    ///\return
alpar@1312
   944
    ///- \ref INF or -\ref INF means either infeasibility or unboundedness
alpar@1312
   945
    /// of the primal problem, depending on whether we minimize or maximize.
alpar@1364
   946
    ///- \ref NaN if no primal solution is found.
alpar@1312
   947
    ///- The (finite) objective value if an optimal solution is found.
alpar@1323
   948
    Value primalValue() { return _getPrimalValue()+obj_const_comp;}
alpar@1263
   949
    ///@}
alpar@1253
   950
    
athos@1248
   951
  };  
athos@1246
   952
alpar@1272
   953
  ///\e
alpar@1272
   954
  
alpar@1272
   955
  ///\relates LpSolverBase::Expr
alpar@1272
   956
  ///
alpar@1272
   957
  inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
alpar@1272
   958
				      const LpSolverBase::Expr &b) 
alpar@1272
   959
  {
alpar@1272
   960
    LpSolverBase::Expr tmp(a);
alpar@1364
   961
    tmp+=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
   962
    return tmp;
alpar@1272
   963
  }
alpar@1272
   964
  ///\e
alpar@1272
   965
  
alpar@1272
   966
  ///\relates LpSolverBase::Expr
alpar@1272
   967
  ///
alpar@1272
   968
  inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
alpar@1272
   969
				      const LpSolverBase::Expr &b) 
alpar@1272
   970
  {
alpar@1272
   971
    LpSolverBase::Expr tmp(a);
alpar@1364
   972
    tmp-=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
   973
    return tmp;
alpar@1272
   974
  }
alpar@1272
   975
  ///\e
alpar@1272
   976
  
alpar@1272
   977
  ///\relates LpSolverBase::Expr
alpar@1272
   978
  ///
alpar@1272
   979
  inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
alpar@1273
   980
				      const LpSolverBase::Value &b) 
alpar@1272
   981
  {
alpar@1272
   982
    LpSolverBase::Expr tmp(a);
alpar@1364
   983
    tmp*=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
   984
    return tmp;
alpar@1272
   985
  }
alpar@1272
   986
  
alpar@1272
   987
  ///\e
alpar@1272
   988
  
alpar@1272
   989
  ///\relates LpSolverBase::Expr
alpar@1272
   990
  ///
alpar@1273
   991
  inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
alpar@1272
   992
				      const LpSolverBase::Expr &b) 
alpar@1272
   993
  {
alpar@1272
   994
    LpSolverBase::Expr tmp(b);
alpar@1364
   995
    tmp*=a; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
   996
    return tmp;
alpar@1272
   997
  }
alpar@1272
   998
  ///\e
alpar@1272
   999
  
alpar@1272
  1000
  ///\relates LpSolverBase::Expr
alpar@1272
  1001
  ///
alpar@1272
  1002
  inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
alpar@1273
  1003
				      const LpSolverBase::Value &b) 
alpar@1272
  1004
  {
alpar@1272
  1005
    LpSolverBase::Expr tmp(a);
alpar@1364
  1006
    tmp/=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1272
  1007
    return tmp;
alpar@1272
  1008
  }
alpar@1272
  1009
  
alpar@1272
  1010
  ///\e
alpar@1272
  1011
  
alpar@1272
  1012
  ///\relates LpSolverBase::Constr
alpar@1272
  1013
  ///
alpar@1272
  1014
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1272
  1015
					 const LpSolverBase::Expr &f) 
alpar@1272
  1016
  {
alpar@1272
  1017
    return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
alpar@1272
  1018
  }
alpar@1272
  1019
alpar@1272
  1020
  ///\e
alpar@1272
  1021
  
alpar@1272
  1022
  ///\relates LpSolverBase::Constr
alpar@1272
  1023
  ///
alpar@1273
  1024
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
alpar@1272
  1025
					 const LpSolverBase::Expr &f) 
alpar@1272
  1026
  {
alpar@1272
  1027
    return LpSolverBase::Constr(e,f);
alpar@1272
  1028
  }
alpar@1272
  1029
alpar@1272
  1030
  ///\e
alpar@1272
  1031
  
alpar@1272
  1032
  ///\relates LpSolverBase::Constr
alpar@1272
  1033
  ///
alpar@1272
  1034
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1273
  1035
					 const LpSolverBase::Value &f) 
alpar@1272
  1036
  {
alpar@1272
  1037
    return LpSolverBase::Constr(e,f);
alpar@1272
  1038
  }
alpar@1272
  1039
alpar@1272
  1040
  ///\e
alpar@1272
  1041
  
alpar@1272
  1042
  ///\relates LpSolverBase::Constr
alpar@1272
  1043
  ///
alpar@1272
  1044
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1272
  1045
					 const LpSolverBase::Expr &f) 
alpar@1272
  1046
  {
alpar@1272
  1047
    return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
alpar@1272
  1048
  }
alpar@1272
  1049
alpar@1272
  1050
alpar@1272
  1051
  ///\e
alpar@1272
  1052
  
alpar@1272
  1053
  ///\relates LpSolverBase::Constr
alpar@1272
  1054
  ///
alpar@1273
  1055
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
alpar@1272
  1056
					 const LpSolverBase::Expr &f) 
alpar@1272
  1057
  {
alpar@1272
  1058
    return LpSolverBase::Constr(f,e);
alpar@1272
  1059
  }
alpar@1272
  1060
alpar@1272
  1061
alpar@1272
  1062
  ///\e
alpar@1272
  1063
  
alpar@1272
  1064
  ///\relates LpSolverBase::Constr
alpar@1272
  1065
  ///
alpar@1272
  1066
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1273
  1067
					 const LpSolverBase::Value &f) 
alpar@1272
  1068
  {
alpar@1272
  1069
    return LpSolverBase::Constr(f,e);
alpar@1272
  1070
  }
alpar@1272
  1071
alpar@1272
  1072
  ///\e
alpar@1272
  1073
  
alpar@1272
  1074
  ///\relates LpSolverBase::Constr
alpar@1272
  1075
  ///
alpar@1272
  1076
  inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
alpar@1272
  1077
					 const LpSolverBase::Expr &f) 
alpar@1272
  1078
  {
alpar@1272
  1079
    return LpSolverBase::Constr(0,e-f,0);
alpar@1272
  1080
  }
alpar@1272
  1081
alpar@1272
  1082
  ///\e
alpar@1272
  1083
  
alpar@1272
  1084
  ///\relates LpSolverBase::Constr
alpar@1272
  1085
  ///
alpar@1273
  1086
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
alpar@1272
  1087
					 const LpSolverBase::Constr&c) 
alpar@1272
  1088
  {
alpar@1272
  1089
    LpSolverBase::Constr tmp(c);
alpar@1273
  1090
    ///\todo Create an own exception type.
alpar@1273
  1091
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
  1092
    else tmp.lowerBound()=n;
alpar@1272
  1093
    return tmp;
alpar@1272
  1094
  }
alpar@1272
  1095
  ///\e
alpar@1272
  1096
  
alpar@1272
  1097
  ///\relates LpSolverBase::Constr
alpar@1272
  1098
  ///
alpar@1272
  1099
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
alpar@1273
  1100
					 const LpSolverBase::Value &n)
alpar@1272
  1101
  {
alpar@1272
  1102
    LpSolverBase::Constr tmp(c);
alpar@1273
  1103
    ///\todo Create an own exception type.
alpar@1273
  1104
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
  1105
    else tmp.upperBound()=n;
alpar@1272
  1106
    return tmp;
alpar@1272
  1107
  }
alpar@1272
  1108
alpar@1272
  1109
  ///\e
alpar@1272
  1110
  
alpar@1272
  1111
  ///\relates LpSolverBase::Constr
alpar@1272
  1112
  ///
alpar@1273
  1113
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
alpar@1272
  1114
					 const LpSolverBase::Constr&c) 
alpar@1272
  1115
  {
alpar@1272
  1116
    LpSolverBase::Constr tmp(c);
alpar@1273
  1117
    ///\todo Create an own exception type.
alpar@1273
  1118
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
  1119
    else tmp.upperBound()=n;
alpar@1272
  1120
    return tmp;
alpar@1272
  1121
  }
alpar@1272
  1122
  ///\e
alpar@1272
  1123
  
alpar@1272
  1124
  ///\relates LpSolverBase::Constr
alpar@1272
  1125
  ///
alpar@1272
  1126
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
alpar@1273
  1127
					 const LpSolverBase::Value &n)
alpar@1272
  1128
  {
alpar@1272
  1129
    LpSolverBase::Constr tmp(c);
alpar@1273
  1130
    ///\todo Create an own exception type.
alpar@1273
  1131
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
  1132
    else tmp.lowerBound()=n;
alpar@1272
  1133
    return tmp;
alpar@1272
  1134
  }
alpar@1272
  1135
alpar@1445
  1136
  ///\e
alpar@1445
  1137
  
alpar@1445
  1138
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1139
  ///
alpar@1445
  1140
  inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a,
alpar@1445
  1141
				      const LpSolverBase::DualExpr &b) 
alpar@1445
  1142
  {
alpar@1445
  1143
    LpSolverBase::DualExpr tmp(a);
alpar@1445
  1144
    tmp+=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1445
  1145
    return tmp;
alpar@1445
  1146
  }
alpar@1445
  1147
  ///\e
alpar@1445
  1148
  
alpar@1445
  1149
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1150
  ///
alpar@1445
  1151
  inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a,
alpar@1445
  1152
				      const LpSolverBase::DualExpr &b) 
alpar@1445
  1153
  {
alpar@1445
  1154
    LpSolverBase::DualExpr tmp(a);
alpar@1445
  1155
    tmp-=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1445
  1156
    return tmp;
alpar@1445
  1157
  }
alpar@1445
  1158
  ///\e
alpar@1445
  1159
  
alpar@1445
  1160
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1161
  ///
alpar@1445
  1162
  inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a,
alpar@1445
  1163
				      const LpSolverBase::Value &b) 
alpar@1445
  1164
  {
alpar@1445
  1165
    LpSolverBase::DualExpr tmp(a);
alpar@1445
  1166
    tmp*=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1445
  1167
    return tmp;
alpar@1445
  1168
  }
alpar@1445
  1169
  
alpar@1445
  1170
  ///\e
alpar@1445
  1171
  
alpar@1445
  1172
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1173
  ///
alpar@1445
  1174
  inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a,
alpar@1445
  1175
				      const LpSolverBase::DualExpr &b) 
alpar@1445
  1176
  {
alpar@1445
  1177
    LpSolverBase::DualExpr tmp(b);
alpar@1445
  1178
    tmp*=a; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1445
  1179
    return tmp;
alpar@1445
  1180
  }
alpar@1445
  1181
  ///\e
alpar@1445
  1182
  
alpar@1445
  1183
  ///\relates LpSolverBase::DualExpr
alpar@1445
  1184
  ///
alpar@1445
  1185
  inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a,
alpar@1445
  1186
				      const LpSolverBase::Value &b) 
alpar@1445
  1187
  {
alpar@1445
  1188
    LpSolverBase::DualExpr tmp(a);
alpar@1445
  1189
    tmp/=b; ///\todo Doesn't STL have some special 'merge' algorithm?
alpar@1445
  1190
    return tmp;
alpar@1445
  1191
  }
alpar@1445
  1192
  
alpar@1272
  1193
athos@1246
  1194
} //namespace lemon
athos@1246
  1195
athos@1246
  1196
#endif //LEMON_LP_BASE_H