src/lemon/lp_base.h
author athos
Thu, 07 Apr 2005 10:44:32 +0000
changeset 1316 daaf6b5c28d6
parent 1305 c3dc75d4af24
child 1323 3aaadfb7de3d
permissions -rw-r--r--
Makefile has changed.
athos@1247
     1
/* -*- C++ -*-
alpar@1253
     2
 * src/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
athos@1247
     5
 * (Egervary Combinatorial Optimization Research Group, 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@1273
    23
#include<math.h>
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.
athos@1246
    33
namespace lemon {
alpar@1253
    34
  
alpar@1253
    35
  ///Internal data structure to convert floating id's to fix one's
alpar@1253
    36
    
alpar@1279
    37
  ///\todo This might be implemented to be also usable in other places.
alpar@1253
    38
  class _FixId 
alpar@1253
    39
  {
alpar@1253
    40
    std::vector<int> index;
alpar@1253
    41
    std::vector<int> cross;
alpar@1253
    42
    int first_free;
alpar@1253
    43
  public:
alpar@1253
    44
    _FixId() : first_free(-1) {};
alpar@1253
    45
    ///Convert a floating id to a fix one
alpar@1253
    46
alpar@1253
    47
    ///\param n is a floating id
alpar@1253
    48
    ///\return the corresponding fix id
alpar@1253
    49
    int fixId(int n) {return cross[n];}
alpar@1253
    50
    ///Convert a fix id to a floating one
alpar@1253
    51
alpar@1253
    52
    ///\param n is a fix id
alpar@1253
    53
    ///\return the corresponding floating id
alpar@1253
    54
    int floatingId(int n) { return index[n];}
alpar@1253
    55
    ///Add a new floating id.
alpar@1253
    56
alpar@1253
    57
    ///\param n is a floating id
alpar@1253
    58
    ///\return the fix id of the new value
alpar@1253
    59
    ///\todo Multiple additions should also be handled.
alpar@1253
    60
    int insert(int n)
alpar@1253
    61
    {
alpar@1253
    62
      if(n>=int(cross.size())) {
alpar@1253
    63
	cross.resize(n+1);
alpar@1253
    64
	if(first_free==-1) {
alpar@1253
    65
	  cross[n]=index.size();
alpar@1253
    66
	  index.push_back(n);
alpar@1253
    67
	}
alpar@1253
    68
	else {
alpar@1253
    69
	  cross[n]=first_free;
alpar@1253
    70
	  int next=index[first_free];
alpar@1253
    71
	  index[first_free]=n;
alpar@1253
    72
	  first_free=next;
alpar@1253
    73
	}
alpar@1256
    74
	return cross[n];
alpar@1253
    75
      }
alpar@1273
    76
      ///\todo Create an own exception type.
alpar@1253
    77
      else throw LogicError(); //floatingId-s must form a continuous range;
alpar@1253
    78
    }
alpar@1253
    79
    ///Remove a fix id.
alpar@1253
    80
alpar@1253
    81
    ///\param n is a fix id
alpar@1253
    82
    ///
alpar@1253
    83
    void erase(int n) 
alpar@1253
    84
    {
alpar@1253
    85
      int fl=index[n];
alpar@1253
    86
      index[n]=first_free;
alpar@1253
    87
      first_free=n;
alpar@1253
    88
      for(int i=fl+1;i<int(cross.size());++i) {
alpar@1253
    89
	cross[i-1]=cross[i];
alpar@1253
    90
	index[cross[i]]--;
alpar@1253
    91
      }
alpar@1253
    92
      cross.pop_back();
alpar@1253
    93
    }
alpar@1253
    94
    ///An upper bound on the largest fix id.
alpar@1253
    95
alpar@1253
    96
    ///\todo Do we need this?
alpar@1253
    97
    ///
alpar@1253
    98
    std::size_t maxFixId() { return cross.size()-1; }
alpar@1253
    99
  
alpar@1253
   100
  };
alpar@1253
   101
    
alpar@1253
   102
  ///Common base class for LP solvers
athos@1246
   103
  class LpSolverBase {
alpar@1253
   104
    
athos@1247
   105
  public:
athos@1247
   106
alpar@1263
   107
    ///\e
alpar@1303
   108
    enum SolveExitStatus {
alpar@1263
   109
      ///\e
alpar@1293
   110
      SOLVED = 0,
alpar@1263
   111
      ///\e
alpar@1293
   112
      UNSOLVED = 1
athos@1291
   113
    };
athos@1291
   114
      
athos@1291
   115
    ///\e
alpar@1303
   116
    enum SolutionStatus {
alpar@1295
   117
      ///Feasible solution has'n been found (but may exist).
alpar@1295
   118
alpar@1295
   119
      ///\todo NOTFOUND might be a better name.
alpar@1295
   120
      ///
alpar@1293
   121
      UNDEFINED = 0,
alpar@1295
   122
      ///The problem has no feasible solution
alpar@1293
   123
      INFEASIBLE = 1,
alpar@1295
   124
      ///Feasible solution found
alpar@1293
   125
      FEASIBLE = 2,
alpar@1295
   126
      ///Optimal solution exists and found
alpar@1295
   127
      OPTIMAL = 3,
alpar@1295
   128
      ///The cost function is unbounded
alpar@1295
   129
alpar@1295
   130
      ///\todo Give a feasible solution and an infinite ray (and the
alpar@1295
   131
      ///corresponding bases)
alpar@1295
   132
      INFINITE = 4
alpar@1263
   133
    };
alpar@1263
   134
      
alpar@1256
   135
    ///The floating point type used by the solver
athos@1247
   136
    typedef double Value;
alpar@1256
   137
    ///The infinity constant
athos@1247
   138
    static const Value INF;
alpar@1264
   139
    ///The not a number constant
alpar@1264
   140
    static const Value NaN;
alpar@1253
   141
    
alpar@1256
   142
    ///Refer to a column of the LP.
alpar@1256
   143
alpar@1256
   144
    ///This type is used to refer to a column of the LP.
alpar@1256
   145
    ///
alpar@1256
   146
    ///Its value remains valid and correct even after the addition or erase of
alpar@1273
   147
    ///other columns.
alpar@1256
   148
    ///
alpar@1256
   149
    ///\todo Document what can one do with a Col (INVALID, comparing,
alpar@1256
   150
    ///it is similar to Node/Edge)
alpar@1256
   151
    class Col {
alpar@1256
   152
    protected:
alpar@1256
   153
      int id;
alpar@1256
   154
      friend class LpSolverBase;
alpar@1256
   155
    public:
alpar@1259
   156
      typedef Value ExprValue;
alpar@1256
   157
      typedef True LpSolverCol;
alpar@1256
   158
      Col() {}
alpar@1256
   159
      Col(const Invalid&) : id(-1) {}
alpar@1256
   160
      bool operator<(Col c) const  {return id<c.id;}
alpar@1256
   161
      bool operator==(Col c) const  {return id==c.id;}
alpar@1256
   162
      bool operator!=(Col c) const  {return id==c.id;}
alpar@1256
   163
    };
alpar@1256
   164
alpar@1256
   165
    ///Refer to a row of the LP.
alpar@1256
   166
alpar@1256
   167
    ///This type is used to refer to a row of the LP.
alpar@1256
   168
    ///
alpar@1256
   169
    ///Its value remains valid and correct even after the addition or erase of
alpar@1273
   170
    ///other rows.
alpar@1256
   171
    ///
alpar@1256
   172
    ///\todo Document what can one do with a Row (INVALID, comparing,
alpar@1256
   173
    ///it is similar to Node/Edge)
alpar@1256
   174
    class Row {
alpar@1256
   175
    protected:
alpar@1256
   176
      int id;
alpar@1256
   177
      friend class LpSolverBase;
alpar@1256
   178
    public:
alpar@1259
   179
      typedef Value ExprValue;
alpar@1256
   180
      typedef True LpSolverRow;
alpar@1256
   181
      Row() {}
alpar@1256
   182
      Row(const Invalid&) : id(-1) {}
alpar@1256
   183
      typedef True LpSolverRow;
alpar@1256
   184
      bool operator<(Row c) const  {return id<c.id;}
alpar@1256
   185
      bool operator==(Row c) const  {return id==c.id;}
alpar@1256
   186
      bool operator!=(Row c) const  {return id==c.id;} 
alpar@1256
   187
   };
alpar@1259
   188
    
alpar@1279
   189
    ///Linear expression of variables and a constant component
alpar@1279
   190
    
alpar@1279
   191
    ///This data structure strores a linear expression of the variables
alpar@1279
   192
    ///(\ref Col "Col"s) and also has a constant component.
alpar@1279
   193
    ///
alpar@1279
   194
    ///There are several ways to access and modify the contents of this
alpar@1279
   195
    ///container.
alpar@1279
   196
    ///- Its it fully compatible with \c std::map<Col,double>, so for expamle
alpar@1279
   197
    ///if \c e is an Expr and \c v and \c w are of type \ref Col then you can
alpar@1279
   198
    ///read and modify the coefficients like
alpar@1279
   199
    ///these.
alpar@1279
   200
    ///\code
alpar@1279
   201
    ///e[v]=5;
alpar@1279
   202
    ///e[v]+=12;
alpar@1279
   203
    ///e.erase(v);
alpar@1279
   204
    ///\endcode
alpar@1279
   205
    ///or you can also iterate through its elements.
alpar@1279
   206
    ///\code
alpar@1279
   207
    ///double s=0;
alpar@1279
   208
    ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i)
alpar@1279
   209
    ///  s+=i->second;
alpar@1279
   210
    ///\endcode
alpar@1279
   211
    ///(This code computes the sum of all coefficients).
alpar@1279
   212
    ///- Numbers (<tt>double</tt>'s)
alpar@1279
   213
    ///and variables (\ref Col "Col"s) directly convert to an
alpar@1279
   214
    ///\ref Expr and the usual linear operations are defined so  
alpar@1279
   215
    ///\code
alpar@1279
   216
    ///v+w
alpar@1279
   217
    ///2*v-3.12*(v-w/2)+2
alpar@1279
   218
    ///v*2.1+(3*v+(v*12+w+6)*3)/2
alpar@1279
   219
    ///\endcode
alpar@1279
   220
    ///are valid expressions. The usual assignment operations are also defined.
alpar@1279
   221
    ///\code
alpar@1279
   222
    ///e=v+w;
alpar@1279
   223
    ///e+=2*v-3.12*(v-w/2)+2;
alpar@1279
   224
    ///e*=3.4;
alpar@1279
   225
    ///e/=5;
alpar@1279
   226
    ///\endcode
alpar@1279
   227
    ///- The constant member can be set and read by \ref constComp()
alpar@1279
   228
    ///\code
alpar@1279
   229
    ///e.constComp()=12;
alpar@1279
   230
    ///double c=e.constComp();
alpar@1279
   231
    ///\endcode
alpar@1279
   232
    ///
alpar@1279
   233
    ///\note that \ref clear() not only sets all coefficients to 0 but also
alpar@1279
   234
    ///clears the constant components.
alpar@1273
   235
    class Expr : public std::map<Col,Value>
alpar@1272
   236
    {
alpar@1272
   237
    public:
alpar@1273
   238
      typedef LpSolverBase::Col Key; 
alpar@1273
   239
      typedef LpSolverBase::Value Value;
alpar@1272
   240
      
alpar@1272
   241
    protected:
alpar@1273
   242
      typedef std::map<Col,Value> Base;
alpar@1272
   243
      
alpar@1273
   244
      Value const_comp;
alpar@1272
   245
  public:
alpar@1272
   246
      typedef True IsLinExpression;
alpar@1272
   247
      ///\e
alpar@1272
   248
      Expr() : Base(), const_comp(0) { }
alpar@1272
   249
      ///\e
alpar@1273
   250
      Expr(const Key &v) : const_comp(0) {
alpar@1272
   251
	Base::insert(std::make_pair(v, 1));
alpar@1272
   252
      }
alpar@1272
   253
      ///\e
alpar@1273
   254
      Expr(const Value &v) : const_comp(v) {}
alpar@1272
   255
      ///\e
alpar@1273
   256
      void set(const Key &v,const Value &c) {
alpar@1272
   257
	Base::insert(std::make_pair(v, c));
alpar@1272
   258
      }
alpar@1272
   259
      ///\e
alpar@1273
   260
      Value &constComp() { return const_comp; }
alpar@1272
   261
      ///\e
alpar@1273
   262
      const Value &constComp() const { return const_comp; }
alpar@1272
   263
      
alpar@1272
   264
      ///Removes the components with zero coefficient.
alpar@1272
   265
      void simplify() {
alpar@1272
   266
	for (Base::iterator i=Base::begin(); i!=Base::end();) {
alpar@1272
   267
	  Base::iterator j=i;
alpar@1272
   268
	  ++j;
alpar@1272
   269
	  if ((*i).second==0) Base::erase(i);
alpar@1272
   270
	  j=i;
alpar@1272
   271
	}
alpar@1272
   272
      }
alpar@1273
   273
alpar@1273
   274
      ///Sets all coefficients and the constant component to 0.
alpar@1273
   275
      void clear() {
alpar@1273
   276
	Base::clear();
alpar@1273
   277
	const_comp=0;
alpar@1273
   278
      }
alpar@1273
   279
alpar@1272
   280
      ///\e
alpar@1272
   281
      Expr &operator+=(const Expr &e) {
alpar@1272
   282
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1272
   283
	  (*this)[j->first]+=j->second;
alpar@1272
   284
	///\todo it might be speeded up using "hints"
alpar@1272
   285
	const_comp+=e.const_comp;
alpar@1272
   286
	return *this;
alpar@1272
   287
      }
alpar@1272
   288
      ///\e
alpar@1272
   289
      Expr &operator-=(const Expr &e) {
alpar@1272
   290
	for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
alpar@1272
   291
	  (*this)[j->first]-=j->second;
alpar@1272
   292
	const_comp-=e.const_comp;
alpar@1272
   293
	return *this;
alpar@1272
   294
      }
alpar@1272
   295
      ///\e
alpar@1273
   296
      Expr &operator*=(const Value &c) {
alpar@1272
   297
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1272
   298
	  j->second*=c;
alpar@1272
   299
	const_comp*=c;
alpar@1272
   300
	return *this;
alpar@1272
   301
      }
alpar@1272
   302
      ///\e
alpar@1273
   303
      Expr &operator/=(const Value &c) {
alpar@1272
   304
	for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
alpar@1272
   305
	  j->second/=c;
alpar@1272
   306
	const_comp/=c;
alpar@1272
   307
	return *this;
alpar@1272
   308
      }
alpar@1272
   309
    };
alpar@1272
   310
    
alpar@1264
   311
    ///Linear constraint
alpar@1272
   312
    //typedef LinConstr<Expr> Constr;
alpar@1272
   313
    class Constr
alpar@1272
   314
    {
alpar@1272
   315
    public:
alpar@1272
   316
      typedef LpSolverBase::Expr Expr;
alpar@1273
   317
      typedef Expr::Key Key;
alpar@1273
   318
      typedef Expr::Value Value;
alpar@1272
   319
      
alpar@1273
   320
      static const Value INF;
alpar@1273
   321
      static const Value NaN;
alpar@1273
   322
      //     static const Value INF=0;
alpar@1273
   323
      //     static const Value NaN=1;
alpar@1272
   324
      
alpar@1273
   325
    protected:
alpar@1273
   326
      Expr _expr;
alpar@1273
   327
      Value _lb,_ub;
alpar@1273
   328
    public:
alpar@1273
   329
      ///\e
alpar@1273
   330
      Constr() : _expr(), _lb(NaN), _ub(NaN) {}
alpar@1273
   331
      ///\e
alpar@1273
   332
      Constr(Value lb,const Expr &e,Value ub) :
alpar@1273
   333
	_expr(e), _lb(lb), _ub(ub) {}
alpar@1273
   334
      ///\e
alpar@1273
   335
      Constr(const Expr &e,Value ub) : 
alpar@1273
   336
	_expr(e), _lb(NaN), _ub(ub) {}
alpar@1273
   337
      ///\e
alpar@1273
   338
      Constr(Value lb,const Expr &e) :
alpar@1273
   339
	_expr(e), _lb(lb), _ub(NaN) {}
alpar@1273
   340
      ///\e
alpar@1272
   341
      Constr(const Expr &e) : 
alpar@1273
   342
	_expr(e), _lb(NaN), _ub(NaN) {}
alpar@1273
   343
      ///\e
alpar@1273
   344
      void clear() 
alpar@1273
   345
      {
alpar@1273
   346
	_expr.clear();
alpar@1273
   347
	_lb=_ub=NaN;
alpar@1273
   348
      }
alpar@1273
   349
      ///\e
alpar@1273
   350
      Expr &expr() { return _expr; }
alpar@1273
   351
      ///\e
alpar@1273
   352
      const Expr &expr() const { return _expr; }
alpar@1273
   353
      ///\e
alpar@1273
   354
      Value &lowerBound() { return _lb; }
alpar@1273
   355
      ///\e
alpar@1273
   356
      const Value &lowerBound() const { return _lb; }
alpar@1273
   357
      ///\e
alpar@1273
   358
      Value &upperBound() { return _ub; }
alpar@1273
   359
      ///\e
alpar@1273
   360
      const Value &upperBound() const { return _ub; }
alpar@1275
   361
      ///\e
alpar@1295
   362
      bool lowerBounded() const { 
alpar@1295
   363
	using namespace std;
alpar@1295
   364
	return isfinite(_lb);
alpar@1295
   365
      }
alpar@1275
   366
      ///\e
alpar@1295
   367
      bool upperBounded() const {
alpar@1295
   368
	using namespace std;
alpar@1295
   369
	return isfinite(_ub);
alpar@1295
   370
      }
alpar@1272
   371
    };
alpar@1272
   372
    
alpar@1253
   373
alpar@1253
   374
  protected:
alpar@1253
   375
    _FixId rows;
alpar@1253
   376
    _FixId cols;
athos@1246
   377
athos@1246
   378
    virtual int _addCol() = 0;
athos@1246
   379
    virtual int _addRow() = 0;
athos@1246
   380
    virtual void _setRowCoeffs(int i, 
athos@1251
   381
			       int length,
athos@1247
   382
                               int  const * indices, 
athos@1247
   383
                               Value  const * values ) = 0;
athos@1246
   384
    virtual void _setColCoeffs(int i, 
athos@1251
   385
			       int length,
athos@1247
   386
                               int  const * indices, 
athos@1247
   387
                               Value  const * values ) = 0;
alpar@1294
   388
    virtual void _setColLowerBound(int i, Value value) = 0;
alpar@1294
   389
    virtual void _setColUpperBound(int i, Value value) = 0;
alpar@1294
   390
    virtual void _setRowLowerBound(int i, Value value) = 0;
alpar@1294
   391
    virtual void _setRowUpperBound(int i, Value value) = 0;
alpar@1294
   392
    virtual void _setObjCoeff(int i, Value obj_coef) = 0;
alpar@1303
   393
    virtual SolveExitStatus _solve() = 0;
alpar@1294
   394
    virtual Value _getPrimal(int i) = 0;
alpar@1312
   395
    virtual Value _getPrimalValue() = 0;
alpar@1312
   396
    virtual SolutionStatus _getPrimalStatus() = 0;
alpar@1312
   397
    virtual void _setMax() = 0;
alpar@1312
   398
    virtual void _setMin() = 0;
alpar@1312
   399
    
alpar@1253
   400
alpar@1253
   401
    void clearObj() {}
alpar@1253
   402
  public:
alpar@1253
   403
alpar@1253
   404
alpar@1253
   405
    ///\e
alpar@1253
   406
    virtual ~LpSolverBase() {}
alpar@1253
   407
alpar@1294
   408
    ///\name Build up and modify of the LP
alpar@1263
   409
alpar@1263
   410
    ///@{
alpar@1263
   411
alpar@1253
   412
    ///Add a new empty column (i.e a new variable) to the LP
alpar@1253
   413
    Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
alpar@1263
   414
alpar@1294
   415
    ///\brief Adds several new columns
alpar@1294
   416
    ///(i.e a variables) at once
alpar@1256
   417
    ///
alpar@1273
   418
    ///This magic function takes a container as its argument
alpar@1256
   419
    ///and fills its elements
alpar@1256
   420
    ///with new columns (i.e. variables)
alpar@1273
   421
    ///\param t can be
alpar@1273
   422
    ///- a standard STL compatible iterable container with
alpar@1273
   423
    ///\ref Col as its \c values_type
alpar@1273
   424
    ///like
alpar@1273
   425
    ///\code
alpar@1273
   426
    ///std::vector<LpSolverBase::Col>
alpar@1273
   427
    ///std::list<LpSolverBase::Col>
alpar@1273
   428
    ///\endcode
alpar@1273
   429
    ///- a standard STL compatible iterable container with
alpar@1273
   430
    ///\ref Col as its \c mapped_type
alpar@1273
   431
    ///like
alpar@1273
   432
    ///\code
alpar@1312
   433
    ///std::map<AnyStatus,LpSolverBase::Col>
alpar@1273
   434
    ///\endcode
alpar@1273
   435
    ///- an iterable lemon \ref concept::WriteMap "write map" like 
alpar@1273
   436
    ///\code
alpar@1273
   437
    ///ListGraph::NodeMap<LpSolverBase::Col>
alpar@1273
   438
    ///ListGraph::EdgeMap<LpSolverBase::Col>
alpar@1273
   439
    ///\endcode
alpar@1256
   440
    ///\return The number of the created column.
alpar@1256
   441
    ///\bug Iterable nodemap hasn't been implemented yet.
alpar@1256
   442
#ifdef DOXYGEN
alpar@1256
   443
    template<class T>
alpar@1256
   444
    int addColSet(T &t) { return 0;} 
alpar@1256
   445
#else
alpar@1256
   446
    template<class T>
alpar@1256
   447
    typename enable_if<typename T::value_type::LpSolverCol,int>::type
alpar@1256
   448
    addColSet(T &t,dummy<0> = 0) {
alpar@1256
   449
      int s=0;
alpar@1256
   450
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
alpar@1256
   451
      return s;
alpar@1256
   452
    }
alpar@1256
   453
    template<class T>
alpar@1256
   454
    typename enable_if<typename T::value_type::second_type::LpSolverCol,
alpar@1256
   455
		       int>::type
alpar@1256
   456
    addColSet(T &t,dummy<1> = 1) { 
alpar@1256
   457
      int s=0;
alpar@1256
   458
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
alpar@1256
   459
	i->second=addCol();
alpar@1256
   460
	s++;
alpar@1256
   461
      }
alpar@1256
   462
      return s;
alpar@1256
   463
    }
alpar@1272
   464
    template<class T>
alpar@1272
   465
    typename enable_if<typename T::ValueSet::value_type::LpSolverCol,
alpar@1272
   466
		       int>::type
alpar@1272
   467
    addColSet(T &t,dummy<2> = 2) { 
alpar@1272
   468
      ///\bug <tt>return addColSet(t.valueSet());</tt> should also work.
alpar@1272
   469
      int s=0;
alpar@1272
   470
      for(typename T::ValueSet::iterator i=t.valueSet().begin();
alpar@1272
   471
	  i!=t.valueSet().end();
alpar@1272
   472
	  ++i)
alpar@1272
   473
	{
alpar@1272
   474
	  *i=addCol();
alpar@1272
   475
	  s++;
alpar@1272
   476
	}
alpar@1272
   477
      return s;
alpar@1272
   478
    }
alpar@1256
   479
#endif
alpar@1263
   480
alpar@1253
   481
    ///Add a new empty row (i.e a new constaint) to the LP
alpar@1258
   482
alpar@1258
   483
    ///This function adds a new empty row (i.e a new constaint) to the LP.
alpar@1258
   484
    ///\return The created row
alpar@1253
   485
    Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
alpar@1253
   486
alpar@1258
   487
    ///Set a row (i.e a constaint) of the LP
alpar@1253
   488
alpar@1258
   489
    ///\param r is the row to be modified
alpar@1259
   490
    ///\param l is lower bound (-\ref INF means no bound)
alpar@1258
   491
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   492
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1253
   493
    ///\bug This is a temportary function. The interface will change to
alpar@1253
   494
    ///a better one.
alpar@1258
   495
    void setRow(Row r, Value l,const Expr &e, Value u) {
alpar@1253
   496
      std::vector<int> indices;
alpar@1253
   497
      std::vector<Value> values;
alpar@1253
   498
      indices.push_back(0);
alpar@1253
   499
      values.push_back(0);
alpar@1258
   500
      for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i)
alpar@1256
   501
	if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
alpar@1256
   502
	  indices.push_back(cols.floatingId((*i).first.id));
alpar@1256
   503
	  values.push_back((*i).second);
alpar@1256
   504
	}
alpar@1253
   505
      _setRowCoeffs(rows.floatingId(r.id),indices.size()-1,
alpar@1253
   506
		    &indices[0],&values[0]);
alpar@1256
   507
      _setRowLowerBound(rows.floatingId(r.id),l-e.constComp());
alpar@1256
   508
      _setRowUpperBound(rows.floatingId(r.id),u-e.constComp());
alpar@1258
   509
    }
alpar@1258
   510
alpar@1264
   511
    ///Set a row (i.e a constaint) of the LP
alpar@1264
   512
alpar@1264
   513
    ///\param r is the row to be modified
alpar@1264
   514
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   515
    void setRow(Row r, const Constr &c) {
alpar@1273
   516
      setRow(r,
alpar@1275
   517
	     c.lowerBounded()?c.lowerBound():-INF,
alpar@1273
   518
	     c.expr(),
alpar@1275
   519
	     c.upperBounded()?c.upperBound():INF);
alpar@1264
   520
    }
alpar@1264
   521
alpar@1258
   522
    ///Add a new row (i.e a new constaint) to the LP
alpar@1258
   523
alpar@1259
   524
    ///\param l is the lower bound (-\ref INF means no bound)
alpar@1258
   525
    ///\param e is a linear expression (see \ref Expr)
alpar@1259
   526
    ///\param u is the upper bound (\ref INF means no bound)
alpar@1258
   527
    ///\return The created row.
alpar@1258
   528
    ///\bug This is a temportary function. The interface will change to
alpar@1258
   529
    ///a better one.
alpar@1258
   530
    Row addRow(Value l,const Expr &e, Value u) {
alpar@1258
   531
      Row r=addRow();
alpar@1258
   532
      setRow(r,l,e,u);
alpar@1253
   533
      return r;
alpar@1253
   534
    }
alpar@1253
   535
alpar@1264
   536
    ///Add a new row (i.e a new constaint) to the LP
alpar@1264
   537
alpar@1264
   538
    ///\param c is a linear expression (see \ref Constr)
alpar@1264
   539
    ///\return The created row.
alpar@1264
   540
    Row addRow(const Constr &c) {
alpar@1264
   541
      Row r=addRow();
alpar@1264
   542
      setRow(r,c);
alpar@1264
   543
      return r;
alpar@1264
   544
    }
alpar@1264
   545
alpar@1253
   546
    /// Set the lower bound of a column (i.e a variable)
alpar@1253
   547
alpar@1293
   548
    /// The upper bound of a variable (column) has to be given by an 
alpar@1253
   549
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   550
    /// Value or -\ref INF.
alpar@1293
   551
    void colLowerBound(Col c, Value value) {
alpar@1253
   552
      _setColLowerBound(cols.floatingId(c.id),value);
alpar@1253
   553
    }
alpar@1253
   554
    /// Set the upper bound of a column (i.e a variable)
alpar@1253
   555
alpar@1293
   556
    /// The upper bound of a variable (column) has to be given by an 
alpar@1253
   557
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   558
    /// Value or \ref INF.
alpar@1293
   559
    void colUpperBound(Col c, Value value) {
alpar@1253
   560
      _setColUpperBound(cols.floatingId(c.id),value);
alpar@1253
   561
    };
alpar@1293
   562
    /// Set the lower and the upper bounds of a column (i.e a variable)
alpar@1293
   563
alpar@1293
   564
    /// The lower and the upper bounds of
alpar@1293
   565
    /// a variable (column) have to be given by an 
alpar@1293
   566
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
   567
    /// Value, -\ref INF or \ref INF.
alpar@1293
   568
    void colBounds(Col c, Value lower, Value upper) {
alpar@1293
   569
      _setColLowerBound(cols.floatingId(c.id),lower);
alpar@1293
   570
      _setColUpperBound(cols.floatingId(c.id),upper);
alpar@1293
   571
    }
alpar@1293
   572
    
alpar@1253
   573
    /// Set the lower bound of a row (i.e a constraint)
alpar@1253
   574
alpar@1293
   575
    /// The lower bound of a linear expression (row) has to be given by an 
alpar@1253
   576
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   577
    /// Value or -\ref INF.
alpar@1293
   578
    void rowLowerBound(Row r, Value value) {
alpar@1253
   579
      _setRowLowerBound(rows.floatingId(r.id),value);
alpar@1253
   580
    };
alpar@1253
   581
    /// Set the upper bound of a row (i.e a constraint)
alpar@1253
   582
alpar@1293
   583
    /// The upper bound of a linear expression (row) has to be given by an 
alpar@1253
   584
    /// extended number of type Value, i.e. a finite number of type 
alpar@1259
   585
    /// Value or \ref INF.
alpar@1293
   586
    void rowUpperBound(Row r, Value value) {
alpar@1253
   587
      _setRowUpperBound(rows.floatingId(r.id),value);
alpar@1253
   588
    };
alpar@1293
   589
    /// Set the lower and the upper bounds of a row (i.e a variable)
alpar@1293
   590
alpar@1293
   591
    /// The lower and the upper bounds of
alpar@1293
   592
    /// a constraint (row) have to be given by an 
alpar@1293
   593
    /// extended number of type Value, i.e. a finite number of type 
alpar@1293
   594
    /// Value, -\ref INF or \ref INF.
alpar@1293
   595
    void rowBounds(Row c, Value lower, Value upper) {
alpar@1293
   596
      _setRowLowerBound(rows.floatingId(c.id),lower);
alpar@1293
   597
      _setRowUpperBound(rows.floatingId(c.id),upper);
alpar@1293
   598
    }
alpar@1293
   599
    
alpar@1253
   600
    ///Set an element of the objective function
alpar@1293
   601
    void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); };
alpar@1253
   602
    ///Set the objective function
alpar@1253
   603
    
alpar@1253
   604
    ///\param e is a linear expression of type \ref Expr.
alpar@1253
   605
    ///\todo What to do with the constant component?
alpar@1253
   606
    void setObj(Expr e) {
alpar@1253
   607
      clearObj();
alpar@1253
   608
      for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
alpar@1293
   609
	objCoeff((*i).first,(*i).second);
alpar@1253
   610
    }
alpar@1263
   611
alpar@1312
   612
    ///Maximize
alpar@1312
   613
    void max() { _setMax(); }
alpar@1312
   614
    ///Minimize
alpar@1312
   615
    void min() { _setMin(); }
alpar@1312
   616
alpar@1312
   617
    
alpar@1263
   618
    ///@}
alpar@1263
   619
alpar@1263
   620
alpar@1294
   621
    ///\name Solve the LP
alpar@1263
   622
alpar@1263
   623
    ///@{
alpar@1263
   624
alpar@1263
   625
    ///\e
alpar@1303
   626
    SolveExitStatus solve() { return _solve(); }
alpar@1263
   627
    
alpar@1263
   628
    ///@}
alpar@1263
   629
    
alpar@1294
   630
    ///\name Obtain the solution
alpar@1263
   631
alpar@1263
   632
    ///@{
alpar@1263
   633
alpar@1263
   634
    ///\e
alpar@1312
   635
    SolutionStatus primalStatus() {
alpar@1312
   636
      return _getPrimalStatus();
alpar@1294
   637
    }
alpar@1294
   638
alpar@1294
   639
    ///\e
alpar@1293
   640
    Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); }
alpar@1263
   641
alpar@1312
   642
    ///\e
alpar@1312
   643
alpar@1312
   644
    ///\return
alpar@1312
   645
    ///- \ref INF or -\ref INF means either infeasibility or unboundedness
alpar@1312
   646
    /// of the primal problem, depending on whether we minimize or maximize.
alpar@1312
   647
    ///- \ref NAN if no primal solution is found.
alpar@1312
   648
    ///- The (finite) objective value if an optimal solution is found.
alpar@1312
   649
    Value primalValue() { return _getPrimalValue();}
alpar@1263
   650
    ///@}
alpar@1253
   651
    
athos@1248
   652
  };  
athos@1246
   653
alpar@1272
   654
  ///\e
alpar@1272
   655
  
alpar@1272
   656
  ///\relates LpSolverBase::Expr
alpar@1272
   657
  ///
alpar@1272
   658
  inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
alpar@1272
   659
				      const LpSolverBase::Expr &b) 
alpar@1272
   660
  {
alpar@1272
   661
    LpSolverBase::Expr tmp(a);
alpar@1272
   662
    tmp+=b; ///\todo Don't STL have some special 'merge' algorithm?
alpar@1272
   663
    return tmp;
alpar@1272
   664
  }
alpar@1272
   665
  ///\e
alpar@1272
   666
  
alpar@1272
   667
  ///\relates LpSolverBase::Expr
alpar@1272
   668
  ///
alpar@1272
   669
  inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
alpar@1272
   670
				      const LpSolverBase::Expr &b) 
alpar@1272
   671
  {
alpar@1272
   672
    LpSolverBase::Expr tmp(a);
alpar@1272
   673
    tmp-=b; ///\todo Don't STL have some special 'merge' algorithm?
alpar@1272
   674
    return tmp;
alpar@1272
   675
  }
alpar@1272
   676
  ///\e
alpar@1272
   677
  
alpar@1272
   678
  ///\relates LpSolverBase::Expr
alpar@1272
   679
  ///
alpar@1272
   680
  inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
alpar@1273
   681
				      const LpSolverBase::Value &b) 
alpar@1272
   682
  {
alpar@1272
   683
    LpSolverBase::Expr tmp(a);
alpar@1272
   684
    tmp*=b; ///\todo Don't STL have some special 'merge' algorithm?
alpar@1272
   685
    return tmp;
alpar@1272
   686
  }
alpar@1272
   687
  
alpar@1272
   688
  ///\e
alpar@1272
   689
  
alpar@1272
   690
  ///\relates LpSolverBase::Expr
alpar@1272
   691
  ///
alpar@1273
   692
  inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
alpar@1272
   693
				      const LpSolverBase::Expr &b) 
alpar@1272
   694
  {
alpar@1272
   695
    LpSolverBase::Expr tmp(b);
alpar@1272
   696
    tmp*=a; ///\todo Don't STL have some special 'merge' algorithm?
alpar@1272
   697
    return tmp;
alpar@1272
   698
  }
alpar@1272
   699
  ///\e
alpar@1272
   700
  
alpar@1272
   701
  ///\relates LpSolverBase::Expr
alpar@1272
   702
  ///
alpar@1272
   703
  inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
alpar@1273
   704
				      const LpSolverBase::Value &b) 
alpar@1272
   705
  {
alpar@1272
   706
    LpSolverBase::Expr tmp(a);
alpar@1272
   707
    tmp/=b; ///\todo Don't STL have some special 'merge' algorithm?
alpar@1272
   708
    return tmp;
alpar@1272
   709
  }
alpar@1272
   710
  
alpar@1272
   711
  ///\e
alpar@1272
   712
  
alpar@1272
   713
  ///\relates LpSolverBase::Constr
alpar@1272
   714
  ///
alpar@1272
   715
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1272
   716
					 const LpSolverBase::Expr &f) 
alpar@1272
   717
  {
alpar@1272
   718
    return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
alpar@1272
   719
  }
alpar@1272
   720
alpar@1272
   721
  ///\e
alpar@1272
   722
  
alpar@1272
   723
  ///\relates LpSolverBase::Constr
alpar@1272
   724
  ///
alpar@1273
   725
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
alpar@1272
   726
					 const LpSolverBase::Expr &f) 
alpar@1272
   727
  {
alpar@1272
   728
    return LpSolverBase::Constr(e,f);
alpar@1272
   729
  }
alpar@1272
   730
alpar@1272
   731
  ///\e
alpar@1272
   732
  
alpar@1272
   733
  ///\relates LpSolverBase::Constr
alpar@1272
   734
  ///
alpar@1272
   735
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
alpar@1273
   736
					 const LpSolverBase::Value &f) 
alpar@1272
   737
  {
alpar@1272
   738
    return LpSolverBase::Constr(e,f);
alpar@1272
   739
  }
alpar@1272
   740
alpar@1272
   741
  ///\e
alpar@1272
   742
  
alpar@1272
   743
  ///\relates LpSolverBase::Constr
alpar@1272
   744
  ///
alpar@1272
   745
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1272
   746
					 const LpSolverBase::Expr &f) 
alpar@1272
   747
  {
alpar@1272
   748
    return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
alpar@1272
   749
  }
alpar@1272
   750
alpar@1272
   751
alpar@1272
   752
  ///\e
alpar@1272
   753
  
alpar@1272
   754
  ///\relates LpSolverBase::Constr
alpar@1272
   755
  ///
alpar@1273
   756
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
alpar@1272
   757
					 const LpSolverBase::Expr &f) 
alpar@1272
   758
  {
alpar@1272
   759
    return LpSolverBase::Constr(f,e);
alpar@1272
   760
  }
alpar@1272
   761
alpar@1272
   762
alpar@1272
   763
  ///\e
alpar@1272
   764
  
alpar@1272
   765
  ///\relates LpSolverBase::Constr
alpar@1272
   766
  ///
alpar@1272
   767
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
alpar@1273
   768
					 const LpSolverBase::Value &f) 
alpar@1272
   769
  {
alpar@1272
   770
    return LpSolverBase::Constr(f,e);
alpar@1272
   771
  }
alpar@1272
   772
alpar@1272
   773
  ///\e
alpar@1272
   774
  
alpar@1272
   775
  ///\relates LpSolverBase::Constr
alpar@1272
   776
  ///
alpar@1272
   777
  inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
alpar@1272
   778
					 const LpSolverBase::Expr &f) 
alpar@1272
   779
  {
alpar@1272
   780
    return LpSolverBase::Constr(0,e-f,0);
alpar@1272
   781
  }
alpar@1272
   782
alpar@1272
   783
  ///\e
alpar@1272
   784
  
alpar@1272
   785
  ///\relates LpSolverBase::Constr
alpar@1272
   786
  ///
alpar@1273
   787
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
alpar@1272
   788
					 const LpSolverBase::Constr&c) 
alpar@1272
   789
  {
alpar@1272
   790
    LpSolverBase::Constr tmp(c);
alpar@1273
   791
    ///\todo Create an own exception type.
alpar@1273
   792
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
   793
    else tmp.lowerBound()=n;
alpar@1272
   794
    return tmp;
alpar@1272
   795
  }
alpar@1272
   796
  ///\e
alpar@1272
   797
  
alpar@1272
   798
  ///\relates LpSolverBase::Constr
alpar@1272
   799
  ///
alpar@1272
   800
  inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
alpar@1273
   801
					 const LpSolverBase::Value &n)
alpar@1272
   802
  {
alpar@1272
   803
    LpSolverBase::Constr tmp(c);
alpar@1273
   804
    ///\todo Create an own exception type.
alpar@1273
   805
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
   806
    else tmp.upperBound()=n;
alpar@1272
   807
    return tmp;
alpar@1272
   808
  }
alpar@1272
   809
alpar@1272
   810
  ///\e
alpar@1272
   811
  
alpar@1272
   812
  ///\relates LpSolverBase::Constr
alpar@1272
   813
  ///
alpar@1273
   814
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
alpar@1272
   815
					 const LpSolverBase::Constr&c) 
alpar@1272
   816
  {
alpar@1272
   817
    LpSolverBase::Constr tmp(c);
alpar@1273
   818
    ///\todo Create an own exception type.
alpar@1273
   819
    if(!isnan(tmp.upperBound())) throw LogicError();
alpar@1273
   820
    else tmp.upperBound()=n;
alpar@1272
   821
    return tmp;
alpar@1272
   822
  }
alpar@1272
   823
  ///\e
alpar@1272
   824
  
alpar@1272
   825
  ///\relates LpSolverBase::Constr
alpar@1272
   826
  ///
alpar@1272
   827
  inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
alpar@1273
   828
					 const LpSolverBase::Value &n)
alpar@1272
   829
  {
alpar@1272
   830
    LpSolverBase::Constr tmp(c);
alpar@1273
   831
    ///\todo Create an own exception type.
alpar@1273
   832
    if(!isnan(tmp.lowerBound())) throw LogicError();
alpar@1273
   833
    else tmp.lowerBound()=n;
alpar@1272
   834
    return tmp;
alpar@1272
   835
  }
alpar@1272
   836
alpar@1272
   837
athos@1246
   838
} //namespace lemon
athos@1246
   839
athos@1246
   840
#endif //LEMON_LP_BASE_H