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