lemon/lp_base.h
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 20 Feb 2009 21:37:19 +0000
changeset 564 2b6d5d22bb23
parent 558 06787db0ef5f
child 587 9db62975c32b
permissions -rw-r--r--
Merge
deba@481
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@481
     2
 *
deba@481
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@481
     4
 *
deba@481
     5
 * Copyright (C) 2003-2008
deba@481
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@481
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@481
     8
 *
deba@481
     9
 * Permission to use, modify and distribute this software is granted
deba@481
    10
 * provided that this copyright notice appears in all copies. For
deba@481
    11
 * precise terms see the accompanying LICENSE file.
deba@481
    12
 *
deba@481
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@481
    14
 * express or implied, and with no claim as to its suitability for any
deba@481
    15
 * purpose.
deba@481
    16
 *
deba@481
    17
 */
deba@481
    18
deba@481
    19
#ifndef LEMON_LP_BASE_H
deba@481
    20
#define LEMON_LP_BASE_H
deba@481
    21
deba@481
    22
#include<iostream>
deba@481
    23
#include<vector>
deba@481
    24
#include<map>
deba@481
    25
#include<limits>
deba@481
    26
#include<lemon/math.h>
deba@481
    27
deba@482
    28
#include<lemon/error.h>
deba@482
    29
#include<lemon/assert.h>
deba@482
    30
deba@481
    31
#include<lemon/core.h>
deba@482
    32
#include<lemon/bits/solver_bits.h>
deba@481
    33
deba@481
    34
///\file
deba@481
    35
///\brief The interface of the LP solver interface.
deba@481
    36
///\ingroup lp_group
deba@481
    37
namespace lemon {
deba@481
    38
deba@482
    39
  ///Common base class for LP and MIP solvers
deba@481
    40
deba@482
    41
  ///Usually this class is not used directly, please use one of the concrete
deba@482
    42
  ///implementations of the solver interface.
deba@481
    43
  ///\ingroup lp_group
deba@482
    44
  class LpBase {
deba@481
    45
deba@481
    46
  protected:
deba@481
    47
deba@482
    48
    _solver_bits::VarIndex rows;
deba@482
    49
    _solver_bits::VarIndex cols;
deba@481
    50
deba@481
    51
  public:
deba@481
    52
deba@481
    53
    ///Possible outcomes of an LP solving procedure
deba@481
    54
    enum SolveExitStatus {
deba@481
    55
      ///This means that the problem has been successfully solved: either
deba@481
    56
      ///an optimal solution has been found or infeasibility/unboundedness
deba@481
    57
      ///has been proved.
deba@481
    58
      SOLVED = 0,
deba@481
    59
      ///Any other case (including the case when some user specified
deba@481
    60
      ///limit has been exceeded)
deba@481
    61
      UNSOLVED = 1
deba@481
    62
    };
deba@481
    63
deba@482
    64
    ///Direction of the optimization
deba@482
    65
    enum Sense {
deba@482
    66
      /// Minimization
deba@482
    67
      MIN,
deba@482
    68
      /// Maximization
deba@482
    69
      MAX
deba@481
    70
    };
deba@481
    71
deba@481
    72
    ///The floating point type used by the solver
deba@481
    73
    typedef double Value;
deba@481
    74
    ///The infinity constant
deba@481
    75
    static const Value INF;
deba@481
    76
    ///The not a number constant
deba@481
    77
    static const Value NaN;
deba@481
    78
deba@481
    79
    friend class Col;
deba@481
    80
    friend class ColIt;
deba@481
    81
    friend class Row;
deba@482
    82
    friend class RowIt;
deba@481
    83
deba@481
    84
    ///Refer to a column of the LP.
deba@481
    85
deba@481
    86
    ///This type is used to refer to a column of the LP.
deba@481
    87
    ///
deba@481
    88
    ///Its value remains valid and correct even after the addition or erase of
deba@481
    89
    ///other columns.
deba@481
    90
    ///
deba@482
    91
    ///\note This class is similar to other Item types in LEMON, like
deba@482
    92
    ///Node and Arc types in digraph.
deba@481
    93
    class Col {
deba@482
    94
      friend class LpBase;
deba@481
    95
    protected:
deba@482
    96
      int _id;
deba@482
    97
      explicit Col(int id) : _id(id) {}
deba@481
    98
    public:
deba@481
    99
      typedef Value ExprValue;
deba@482
   100
      typedef True LpCol;
deba@482
   101
      /// Default constructor
deba@482
   102
      
deba@482
   103
      /// \warning The default constructor sets the Col to an
deba@482
   104
      /// undefined value.
deba@481
   105
      Col() {}
deba@482
   106
      /// Invalid constructor \& conversion.
deba@482
   107
      
deba@482
   108
      /// This constructor initializes the Col to be invalid.
deba@482
   109
      /// \sa Invalid for more details.      
deba@482
   110
      Col(const Invalid&) : _id(-1) {}
deba@482
   111
      /// Equality operator
deba@482
   112
deba@482
   113
      /// Two \ref Col "Col"s are equal if and only if they point to
deba@482
   114
      /// the same LP column or both are invalid.
deba@482
   115
      bool operator==(Col c) const  {return _id == c._id;}
deba@482
   116
      /// Inequality operator
deba@482
   117
deba@482
   118
      /// \sa operator==(Col c)
deba@482
   119
      ///
deba@482
   120
      bool operator!=(Col c) const  {return _id != c._id;}
deba@482
   121
      /// Artificial ordering operator.
deba@482
   122
deba@482
   123
      /// To allow the use of this object in std::map or similar
deba@482
   124
      /// associative container we require this.
deba@482
   125
      ///
deba@482
   126
      /// \note This operator only have to define some strict ordering of
deba@482
   127
      /// the items; this order has nothing to do with the iteration
deba@482
   128
      /// ordering of the items.
deba@482
   129
      bool operator<(Col c) const  {return _id < c._id;}
deba@481
   130
    };
deba@481
   131
deba@482
   132
    ///Iterator for iterate over the columns of an LP problem
deba@482
   133
deba@482
   134
    /// Its usage is quite simple, for example you can count the number
deba@482
   135
    /// of columns in an LP \c lp:
deba@482
   136
    ///\code
deba@482
   137
    /// int count=0;
deba@482
   138
    /// for (LpBase::ColIt c(lp); c!=INVALID; ++c) ++count;
deba@482
   139
    ///\endcode
deba@481
   140
    class ColIt : public Col {
deba@482
   141
      const LpBase *_solver;
deba@481
   142
    public:
deba@482
   143
      /// Default constructor
deba@482
   144
      
deba@482
   145
      /// \warning The default constructor sets the iterator
deba@482
   146
      /// to an undefined value.
deba@481
   147
      ColIt() {}
deba@482
   148
      /// Sets the iterator to the first Col
deba@482
   149
      
deba@482
   150
      /// Sets the iterator to the first Col.
deba@482
   151
      ///
deba@482
   152
      ColIt(const LpBase &solver) : _solver(&solver)
deba@481
   153
      {
deba@482
   154
        _solver->cols.firstItem(_id);
deba@481
   155
      }
deba@482
   156
      /// Invalid constructor \& conversion
deba@482
   157
      
deba@482
   158
      /// Initialize the iterator to be invalid.
deba@482
   159
      /// \sa Invalid for more details.
deba@481
   160
      ColIt(const Invalid&) : Col(INVALID) {}
deba@482
   161
      /// Next column
deba@482
   162
      
deba@482
   163
      /// Assign the iterator to the next column.
deba@482
   164
      ///
deba@481
   165
      ColIt &operator++()
deba@481
   166
      {
deba@482
   167
        _solver->cols.nextItem(_id);
deba@481
   168
        return *this;
deba@481
   169
      }
deba@481
   170
    };
deba@481
   171
deba@482
   172
    /// \brief Returns the ID of the column.
deba@482
   173
    static int id(const Col& col) { return col._id; }
deba@482
   174
    /// \brief Returns the column with the given ID.
deba@482
   175
    ///
deba@482
   176
    /// \pre The argument should be a valid column ID in the LP problem.
deba@482
   177
    static Col colFromId(int id) { return Col(id); }
deba@481
   178
deba@481
   179
    ///Refer to a row of the LP.
deba@481
   180
deba@481
   181
    ///This type is used to refer to a row of the LP.
deba@481
   182
    ///
deba@481
   183
    ///Its value remains valid and correct even after the addition or erase of
deba@481
   184
    ///other rows.
deba@481
   185
    ///
deba@482
   186
    ///\note This class is similar to other Item types in LEMON, like
deba@482
   187
    ///Node and Arc types in digraph.
deba@481
   188
    class Row {
deba@482
   189
      friend class LpBase;
deba@481
   190
    protected:
deba@482
   191
      int _id;
deba@482
   192
      explicit Row(int id) : _id(id) {}
deba@481
   193
    public:
deba@481
   194
      typedef Value ExprValue;
deba@482
   195
      typedef True LpRow;
deba@482
   196
      /// Default constructor
deba@482
   197
      
deba@482
   198
      /// \warning The default constructor sets the Row to an
deba@482
   199
      /// undefined value.
deba@481
   200
      Row() {}
deba@482
   201
      /// Invalid constructor \& conversion.
deba@482
   202
      
deba@482
   203
      /// This constructor initializes the Row to be invalid.
deba@482
   204
      /// \sa Invalid for more details.      
deba@482
   205
      Row(const Invalid&) : _id(-1) {}
deba@482
   206
      /// Equality operator
deba@481
   207
deba@482
   208
      /// Two \ref Row "Row"s are equal if and only if they point to
deba@482
   209
      /// the same LP row or both are invalid.
deba@482
   210
      bool operator==(Row r) const  {return _id == r._id;}
deba@482
   211
      /// Inequality operator
deba@482
   212
      
deba@482
   213
      /// \sa operator==(Row r)
deba@482
   214
      ///
deba@482
   215
      bool operator!=(Row r) const  {return _id != r._id;}
deba@482
   216
      /// Artificial ordering operator.
deba@482
   217
deba@482
   218
      /// To allow the use of this object in std::map or similar
deba@482
   219
      /// associative container we require this.
deba@482
   220
      ///
deba@482
   221
      /// \note This operator only have to define some strict ordering of
deba@482
   222
      /// the items; this order has nothing to do with the iteration
deba@482
   223
      /// ordering of the items.
deba@482
   224
      bool operator<(Row r) const  {return _id < r._id;}
deba@481
   225
    };
deba@481
   226
deba@482
   227
    ///Iterator for iterate over the rows of an LP problem
deba@482
   228
deba@482
   229
    /// Its usage is quite simple, for example you can count the number
deba@482
   230
    /// of rows in an LP \c lp:
deba@482
   231
    ///\code
deba@482
   232
    /// int count=0;
deba@482
   233
    /// for (LpBase::RowIt c(lp); c!=INVALID; ++c) ++count;
deba@482
   234
    ///\endcode
deba@481
   235
    class RowIt : public Row {
deba@482
   236
      const LpBase *_solver;
deba@481
   237
    public:
deba@482
   238
      /// Default constructor
deba@482
   239
      
deba@482
   240
      /// \warning The default constructor sets the iterator
deba@482
   241
      /// to an undefined value.
deba@481
   242
      RowIt() {}
deba@482
   243
      /// Sets the iterator to the first Row
deba@482
   244
      
deba@482
   245
      /// Sets the iterator to the first Row.
deba@482
   246
      ///
deba@482
   247
      RowIt(const LpBase &solver) : _solver(&solver)
deba@481
   248
      {
deba@482
   249
        _solver->rows.firstItem(_id);
deba@481
   250
      }
deba@482
   251
      /// Invalid constructor \& conversion
deba@482
   252
      
deba@482
   253
      /// Initialize the iterator to be invalid.
deba@482
   254
      /// \sa Invalid for more details.
deba@481
   255
      RowIt(const Invalid&) : Row(INVALID) {}
deba@482
   256
      /// Next row
deba@482
   257
      
deba@482
   258
      /// Assign the iterator to the next row.
deba@482
   259
      ///
deba@481
   260
      RowIt &operator++()
deba@481
   261
      {
deba@482
   262
        _solver->rows.nextItem(_id);
deba@481
   263
        return *this;
deba@481
   264
      }
deba@481
   265
    };
deba@481
   266
deba@482
   267
    /// \brief Returns the ID of the row.
deba@482
   268
    static int id(const Row& row) { return row._id; }
deba@482
   269
    /// \brief Returns the row with the given ID.
deba@482
   270
    ///
deba@482
   271
    /// \pre The argument should be a valid row ID in the LP problem.
deba@482
   272
    static Row rowFromId(int id) { return Row(id); }
deba@481
   273
deba@481
   274
  public:
deba@481
   275
deba@481
   276
    ///Linear expression of variables and a constant component
deba@481
   277
deba@481
   278
    ///This data structure stores a linear expression of the variables
deba@481
   279
    ///(\ref Col "Col"s) and also has a constant component.
deba@481
   280
    ///
deba@481
   281
    ///There are several ways to access and modify the contents of this
deba@481
   282
    ///container.
deba@481
   283
    ///\code
deba@481
   284
    ///e[v]=5;
deba@481
   285
    ///e[v]+=12;
deba@481
   286
    ///e.erase(v);
deba@481
   287
    ///\endcode
deba@481
   288
    ///or you can also iterate through its elements.
deba@481
   289
    ///\code
deba@481
   290
    ///double s=0;
deba@482
   291
    ///for(LpBase::Expr::ConstCoeffIt i(e);i!=INVALID;++i)
deba@482
   292
    ///  s+=*i * primal(i);
deba@481
   293
    ///\endcode
deba@482
   294
    ///(This code computes the primal value of the expression).
deba@481
   295
    ///- Numbers (<tt>double</tt>'s)
deba@481
   296
    ///and variables (\ref Col "Col"s) directly convert to an
deba@481
   297
    ///\ref Expr and the usual linear operations are defined, so
deba@481
   298
    ///\code
deba@481
   299
    ///v+w
deba@481
   300
    ///2*v-3.12*(v-w/2)+2
deba@481
   301
    ///v*2.1+(3*v+(v*12+w+6)*3)/2
deba@481
   302
    ///\endcode
deba@482
   303
    ///are valid expressions.
deba@481
   304
    ///The usual assignment operations are also defined.
deba@481
   305
    ///\code
deba@481
   306
    ///e=v+w;
deba@481
   307
    ///e+=2*v-3.12*(v-w/2)+2;
deba@481
   308
    ///e*=3.4;
deba@481
   309
    ///e/=5;
deba@481
   310
    ///\endcode
deba@482
   311
    ///- The constant member can be set and read by dereference
deba@482
   312
    ///  operator (unary *)
deba@482
   313
    ///
deba@481
   314
    ///\code
deba@482
   315
    ///*e=12;
deba@482
   316
    ///double c=*e;
deba@481
   317
    ///\endcode
deba@481
   318
    ///
deba@481
   319
    ///\sa Constr
deba@482
   320
    class Expr {
deba@482
   321
      friend class LpBase;
deba@481
   322
    public:
deba@482
   323
      /// The key type of the expression
deba@482
   324
      typedef LpBase::Col Key;
deba@482
   325
      /// The value type of the expression
deba@482
   326
      typedef LpBase::Value Value;
deba@481
   327
deba@481
   328
    protected:
deba@482
   329
      Value const_comp;
deba@482
   330
      std::map<int, Value> comps;
deba@481
   331
deba@481
   332
    public:
deba@482
   333
      typedef True SolverExpr;
deba@482
   334
      /// Default constructor
deba@482
   335
      
deba@482
   336
      /// Construct an empty expression, the coefficients and
deba@482
   337
      /// the constant component are initialized to zero.
deba@482
   338
      Expr() : const_comp(0) {}
deba@482
   339
      /// Construct an expression from a column
deba@482
   340
deba@482
   341
      /// Construct an expression, which has a term with \c c variable
deba@482
   342
      /// and 1.0 coefficient.
deba@482
   343
      Expr(const Col &c) : const_comp(0) {
deba@482
   344
        typedef std::map<int, Value>::value_type pair_type;
deba@482
   345
        comps.insert(pair_type(id(c), 1));
deba@481
   346
      }
deba@482
   347
      /// Construct an expression from a constant
deba@482
   348
deba@482
   349
      /// Construct an expression, which's constant component is \c v.
deba@482
   350
      ///
deba@481
   351
      Expr(const Value &v) : const_comp(v) {}
deba@482
   352
      /// Returns the coefficient of the column
deba@482
   353
      Value operator[](const Col& c) const {
deba@482
   354
        std::map<int, Value>::const_iterator it=comps.find(id(c));
deba@482
   355
        if (it != comps.end()) {
deba@482
   356
          return it->second;
deba@482
   357
        } else {
deba@482
   358
          return 0;
deba@481
   359
        }
deba@481
   360
      }
deba@482
   361
      /// Returns the coefficient of the column
deba@482
   362
      Value& operator[](const Col& c) {
deba@482
   363
        return comps[id(c)];
deba@482
   364
      }
deba@482
   365
      /// Sets the coefficient of the column
deba@482
   366
      void set(const Col &c, const Value &v) {
deba@482
   367
        if (v != 0.0) {
deba@482
   368
          typedef std::map<int, Value>::value_type pair_type;
deba@482
   369
          comps.insert(pair_type(id(c), v));
deba@482
   370
        } else {
deba@482
   371
          comps.erase(id(c));
deba@482
   372
        }
deba@482
   373
      }
deba@482
   374
      /// Returns the constant component of the expression
deba@482
   375
      Value& operator*() { return const_comp; }
deba@482
   376
      /// Returns the constant component of the expression
deba@482
   377
      const Value& operator*() const { return const_comp; }
deba@482
   378
      /// \brief Removes the coefficients which's absolute value does
deba@482
   379
      /// not exceed \c epsilon. It also sets to zero the constant
deba@482
   380
      /// component, if it does not exceed epsilon in absolute value.
deba@482
   381
      void simplify(Value epsilon = 0.0) {
deba@482
   382
        std::map<int, Value>::iterator it=comps.begin();
deba@482
   383
        while (it != comps.end()) {
deba@482
   384
          std::map<int, Value>::iterator jt=it;
deba@482
   385
          ++jt;
deba@482
   386
          if (std::fabs((*it).second) <= epsilon) comps.erase(it);
deba@482
   387
          it=jt;
deba@482
   388
        }
deba@482
   389
        if (std::fabs(const_comp) <= epsilon) const_comp = 0;
deba@481
   390
      }
deba@481
   391
deba@482
   392
      void simplify(Value epsilon = 0.0) const {
deba@482
   393
        const_cast<Expr*>(this)->simplify(epsilon);
deba@481
   394
      }
deba@481
   395
deba@481
   396
      ///Sets all coefficients and the constant component to 0.
deba@481
   397
      void clear() {
deba@482
   398
        comps.clear();
deba@481
   399
        const_comp=0;
deba@481
   400
      }
deba@481
   401
deba@482
   402
      ///Compound assignment
deba@481
   403
      Expr &operator+=(const Expr &e) {
deba@482
   404
        for (std::map<int, Value>::const_iterator it=e.comps.begin();
deba@482
   405
             it!=e.comps.end(); ++it)
deba@482
   406
          comps[it->first]+=it->second;
deba@481
   407
        const_comp+=e.const_comp;
deba@481
   408
        return *this;
deba@481
   409
      }
deba@482
   410
      ///Compound assignment
deba@481
   411
      Expr &operator-=(const Expr &e) {
deba@482
   412
        for (std::map<int, Value>::const_iterator it=e.comps.begin();
deba@482
   413
             it!=e.comps.end(); ++it)
deba@482
   414
          comps[it->first]-=it->second;
deba@481
   415
        const_comp-=e.const_comp;
deba@481
   416
        return *this;
deba@481
   417
      }
deba@482
   418
      ///Multiply with a constant
deba@482
   419
      Expr &operator*=(const Value &v) {
deba@482
   420
        for (std::map<int, Value>::iterator it=comps.begin();
deba@482
   421
             it!=comps.end(); ++it)
deba@482
   422
          it->second*=v;
deba@482
   423
        const_comp*=v;
deba@481
   424
        return *this;
deba@481
   425
      }
deba@482
   426
      ///Division with a constant
deba@481
   427
      Expr &operator/=(const Value &c) {
deba@482
   428
        for (std::map<int, Value>::iterator it=comps.begin();
deba@482
   429
             it!=comps.end(); ++it)
deba@482
   430
          it->second/=c;
deba@481
   431
        const_comp/=c;
deba@481
   432
        return *this;
deba@481
   433
      }
deba@481
   434
deba@482
   435
      ///Iterator over the expression
deba@482
   436
      
deba@482
   437
      ///The iterator iterates over the terms of the expression. 
deba@482
   438
      /// 
deba@482
   439
      ///\code
deba@482
   440
      ///double s=0;
deba@482
   441
      ///for(LpBase::Expr::CoeffIt i(e);i!=INVALID;++i)
deba@482
   442
      ///  s+= *i * primal(i);
deba@482
   443
      ///\endcode
deba@482
   444
      class CoeffIt {
deba@482
   445
      private:
deba@482
   446
deba@482
   447
        std::map<int, Value>::iterator _it, _end;
deba@482
   448
deba@482
   449
      public:
deba@482
   450
deba@482
   451
        /// Sets the iterator to the first term
deba@482
   452
        
deba@482
   453
        /// Sets the iterator to the first term of the expression.
deba@482
   454
        ///
deba@482
   455
        CoeffIt(Expr& e)
deba@482
   456
          : _it(e.comps.begin()), _end(e.comps.end()){}
deba@482
   457
deba@482
   458
        /// Convert the iterator to the column of the term
deba@482
   459
        operator Col() const {
deba@482
   460
          return colFromId(_it->first);
deba@482
   461
        }
deba@482
   462
deba@482
   463
        /// Returns the coefficient of the term
deba@482
   464
        Value& operator*() { return _it->second; }
deba@482
   465
deba@482
   466
        /// Returns the coefficient of the term
deba@482
   467
        const Value& operator*() const { return _it->second; }
deba@482
   468
        /// Next term
deba@482
   469
        
deba@482
   470
        /// Assign the iterator to the next term.
deba@482
   471
        ///
deba@482
   472
        CoeffIt& operator++() { ++_it; return *this; }
deba@482
   473
deba@482
   474
        /// Equality operator
deba@482
   475
        bool operator==(Invalid) const { return _it == _end; }
deba@482
   476
        /// Inequality operator
deba@482
   477
        bool operator!=(Invalid) const { return _it != _end; }
deba@482
   478
      };
deba@482
   479
deba@482
   480
      /// Const iterator over the expression
deba@482
   481
      
deba@482
   482
      ///The iterator iterates over the terms of the expression. 
deba@482
   483
      /// 
deba@482
   484
      ///\code
deba@482
   485
      ///double s=0;
deba@482
   486
      ///for(LpBase::Expr::ConstCoeffIt i(e);i!=INVALID;++i)
deba@482
   487
      ///  s+=*i * primal(i);
deba@482
   488
      ///\endcode
deba@482
   489
      class ConstCoeffIt {
deba@482
   490
      private:
deba@482
   491
deba@482
   492
        std::map<int, Value>::const_iterator _it, _end;
deba@482
   493
deba@482
   494
      public:
deba@482
   495
deba@482
   496
        /// Sets the iterator to the first term
deba@482
   497
        
deba@482
   498
        /// Sets the iterator to the first term of the expression.
deba@482
   499
        ///
deba@482
   500
        ConstCoeffIt(const Expr& e)
deba@482
   501
          : _it(e.comps.begin()), _end(e.comps.end()){}
deba@482
   502
deba@482
   503
        /// Convert the iterator to the column of the term
deba@482
   504
        operator Col() const {
deba@482
   505
          return colFromId(_it->first);
deba@482
   506
        }
deba@482
   507
deba@482
   508
        /// Returns the coefficient of the term
deba@482
   509
        const Value& operator*() const { return _it->second; }
deba@482
   510
deba@482
   511
        /// Next term
deba@482
   512
        
deba@482
   513
        /// Assign the iterator to the next term.
deba@482
   514
        ///
deba@482
   515
        ConstCoeffIt& operator++() { ++_it; return *this; }
deba@482
   516
deba@482
   517
        /// Equality operator
deba@482
   518
        bool operator==(Invalid) const { return _it == _end; }
deba@482
   519
        /// Inequality operator
deba@482
   520
        bool operator!=(Invalid) const { return _it != _end; }
deba@482
   521
      };
deba@482
   522
deba@481
   523
    };
deba@481
   524
deba@481
   525
    ///Linear constraint
deba@481
   526
deba@481
   527
    ///This data stucture represents a linear constraint in the LP.
deba@481
   528
    ///Basically it is a linear expression with a lower or an upper bound
deba@481
   529
    ///(or both). These parts of the constraint can be obtained by the member
deba@481
   530
    ///functions \ref expr(), \ref lowerBound() and \ref upperBound(),
deba@481
   531
    ///respectively.
deba@481
   532
    ///There are two ways to construct a constraint.
deba@481
   533
    ///- You can set the linear expression and the bounds directly
deba@481
   534
    ///  by the functions above.
deba@481
   535
    ///- The operators <tt>\<=</tt>, <tt>==</tt> and  <tt>\>=</tt>
deba@481
   536
    ///  are defined between expressions, or even between constraints whenever
deba@481
   537
    ///  it makes sense. Therefore if \c e and \c f are linear expressions and
deba@481
   538
    ///  \c s and \c t are numbers, then the followings are valid expressions
deba@481
   539
    ///  and thus they can be used directly e.g. in \ref addRow() whenever
deba@481
   540
    ///  it makes sense.
deba@481
   541
    ///\code
deba@481
   542
    ///  e<=s
deba@481
   543
    ///  e<=f
deba@481
   544
    ///  e==f
deba@481
   545
    ///  s<=e<=t
deba@481
   546
    ///  e>=t
deba@481
   547
    ///\endcode
deba@482
   548
    ///\warning The validity of a constraint is checked only at run
deba@482
   549
    ///time, so e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will
deba@482
   550
    ///compile, but will fail an assertion.
deba@481
   551
    class Constr
deba@481
   552
    {
deba@481
   553
    public:
deba@482
   554
      typedef LpBase::Expr Expr;
deba@481
   555
      typedef Expr::Key Key;
deba@481
   556
      typedef Expr::Value Value;
deba@481
   557
deba@481
   558
    protected:
deba@481
   559
      Expr _expr;
deba@481
   560
      Value _lb,_ub;
deba@481
   561
    public:
deba@481
   562
      ///\e
deba@481
   563
      Constr() : _expr(), _lb(NaN), _ub(NaN) {}
deba@481
   564
      ///\e
deba@482
   565
      Constr(Value lb, const Expr &e, Value ub) :
deba@481
   566
        _expr(e), _lb(lb), _ub(ub) {}
deba@481
   567
      Constr(const Expr &e) :
deba@481
   568
        _expr(e), _lb(NaN), _ub(NaN) {}
deba@481
   569
      ///\e
deba@481
   570
      void clear()
deba@481
   571
      {
deba@481
   572
        _expr.clear();
deba@481
   573
        _lb=_ub=NaN;
deba@481
   574
      }
deba@481
   575
deba@481
   576
      ///Reference to the linear expression
deba@481
   577
      Expr &expr() { return _expr; }
deba@481
   578
      ///Cont reference to the linear expression
deba@481
   579
      const Expr &expr() const { return _expr; }
deba@481
   580
      ///Reference to the lower bound.
deba@481
   581
deba@481
   582
      ///\return
deba@481
   583
      ///- \ref INF "INF": the constraint is lower unbounded.
deba@481
   584
      ///- \ref NaN "NaN": lower bound has not been set.
deba@481
   585
      ///- finite number: the lower bound
deba@481
   586
      Value &lowerBound() { return _lb; }
deba@481
   587
      ///The const version of \ref lowerBound()
deba@481
   588
      const Value &lowerBound() const { return _lb; }
deba@481
   589
      ///Reference to the upper bound.
deba@481
   590
deba@481
   591
      ///\return
deba@481
   592
      ///- \ref INF "INF": the constraint is upper unbounded.
deba@481
   593
      ///- \ref NaN "NaN": upper bound has not been set.
deba@481
   594
      ///- finite number: the upper bound
deba@481
   595
      Value &upperBound() { return _ub; }
deba@481
   596
      ///The const version of \ref upperBound()
deba@481
   597
      const Value &upperBound() const { return _ub; }
deba@481
   598
      ///Is the constraint lower bounded?
deba@481
   599
      bool lowerBounded() const {
alpar@558
   600
        return _lb != -INF && !isNaN(_lb);
deba@481
   601
      }
deba@481
   602
      ///Is the constraint upper bounded?
deba@481
   603
      bool upperBounded() const {
alpar@558
   604
        return _ub != INF && !isNaN(_ub);
deba@481
   605
      }
deba@481
   606
deba@481
   607
    };
deba@481
   608
deba@481
   609
    ///Linear expression of rows
deba@481
   610
deba@481
   611
    ///This data structure represents a column of the matrix,
deba@481
   612
    ///thas is it strores a linear expression of the dual variables
deba@481
   613
    ///(\ref Row "Row"s).
deba@481
   614
    ///
deba@481
   615
    ///There are several ways to access and modify the contents of this
deba@481
   616
    ///container.
deba@481
   617
    ///\code
deba@481
   618
    ///e[v]=5;
deba@481
   619
    ///e[v]+=12;
deba@481
   620
    ///e.erase(v);
deba@481
   621
    ///\endcode
deba@481
   622
    ///or you can also iterate through its elements.
deba@481
   623
    ///\code
deba@481
   624
    ///double s=0;
deba@482
   625
    ///for(LpBase::DualExpr::ConstCoeffIt i(e);i!=INVALID;++i)
deba@482
   626
    ///  s+=*i;
deba@481
   627
    ///\endcode
deba@481
   628
    ///(This code computes the sum of all coefficients).
deba@481
   629
    ///- Numbers (<tt>double</tt>'s)
deba@481
   630
    ///and variables (\ref Row "Row"s) directly convert to an
deba@481
   631
    ///\ref DualExpr and the usual linear operations are defined, so
deba@481
   632
    ///\code
deba@481
   633
    ///v+w
deba@481
   634
    ///2*v-3.12*(v-w/2)
deba@481
   635
    ///v*2.1+(3*v+(v*12+w)*3)/2
deba@481
   636
    ///\endcode
deba@482
   637
    ///are valid \ref DualExpr dual expressions.
deba@481
   638
    ///The usual assignment operations are also defined.
deba@481
   639
    ///\code
deba@481
   640
    ///e=v+w;
deba@481
   641
    ///e+=2*v-3.12*(v-w/2);
deba@481
   642
    ///e*=3.4;
deba@481
   643
    ///e/=5;
deba@481
   644
    ///\endcode
deba@481
   645
    ///
deba@481
   646
    ///\sa Expr
deba@482
   647
    class DualExpr {
deba@482
   648
      friend class LpBase;
deba@481
   649
    public:
deba@482
   650
      /// The key type of the expression
deba@482
   651
      typedef LpBase::Row Key;
deba@482
   652
      /// The value type of the expression
deba@482
   653
      typedef LpBase::Value Value;
deba@481
   654
deba@481
   655
    protected:
deba@482
   656
      std::map<int, Value> comps;
deba@481
   657
deba@481
   658
    public:
deba@482
   659
      typedef True SolverExpr;
deba@482
   660
      /// Default constructor
deba@482
   661
      
deba@482
   662
      /// Construct an empty expression, the coefficients are
deba@482
   663
      /// initialized to zero.
deba@482
   664
      DualExpr() {}
deba@482
   665
      /// Construct an expression from a row
deba@482
   666
deba@482
   667
      /// Construct an expression, which has a term with \c r dual
deba@482
   668
      /// variable and 1.0 coefficient.
deba@482
   669
      DualExpr(const Row &r) {
deba@482
   670
        typedef std::map<int, Value>::value_type pair_type;
deba@482
   671
        comps.insert(pair_type(id(r), 1));
deba@481
   672
      }
deba@482
   673
      /// Returns the coefficient of the row
deba@482
   674
      Value operator[](const Row& r) const {
deba@482
   675
        std::map<int, Value>::const_iterator it = comps.find(id(r));
deba@482
   676
        if (it != comps.end()) {
deba@482
   677
          return it->second;
deba@482
   678
        } else {
deba@482
   679
          return 0;
deba@482
   680
        }
deba@481
   681
      }
deba@482
   682
      /// Returns the coefficient of the row
deba@482
   683
      Value& operator[](const Row& r) {
deba@482
   684
        return comps[id(r)];
deba@482
   685
      }
deba@482
   686
      /// Sets the coefficient of the row
deba@482
   687
      void set(const Row &r, const Value &v) {
deba@482
   688
        if (v != 0.0) {
deba@482
   689
          typedef std::map<int, Value>::value_type pair_type;
deba@482
   690
          comps.insert(pair_type(id(r), v));
deba@482
   691
        } else {
deba@482
   692
          comps.erase(id(r));
deba@482
   693
        }
deba@482
   694
      }
deba@482
   695
      /// \brief Removes the coefficients which's absolute value does
deba@482
   696
      /// not exceed \c epsilon. 
deba@482
   697
      void simplify(Value epsilon = 0.0) {
deba@482
   698
        std::map<int, Value>::iterator it=comps.begin();
deba@482
   699
        while (it != comps.end()) {
deba@482
   700
          std::map<int, Value>::iterator jt=it;
deba@482
   701
          ++jt;
deba@482
   702
          if (std::fabs((*it).second) <= epsilon) comps.erase(it);
deba@482
   703
          it=jt;
deba@481
   704
        }
deba@481
   705
      }
deba@481
   706
deba@482
   707
      void simplify(Value epsilon = 0.0) const {
deba@482
   708
        const_cast<DualExpr*>(this)->simplify(epsilon);
deba@481
   709
      }
deba@481
   710
deba@481
   711
      ///Sets all coefficients to 0.
deba@481
   712
      void clear() {
deba@482
   713
        comps.clear();
deba@482
   714
      }
deba@482
   715
      ///Compound assignment
deba@482
   716
      DualExpr &operator+=(const DualExpr &e) {
deba@482
   717
        for (std::map<int, Value>::const_iterator it=e.comps.begin();
deba@482
   718
             it!=e.comps.end(); ++it)
deba@482
   719
          comps[it->first]+=it->second;
deba@482
   720
        return *this;
deba@482
   721
      }
deba@482
   722
      ///Compound assignment
deba@482
   723
      DualExpr &operator-=(const DualExpr &e) {
deba@482
   724
        for (std::map<int, Value>::const_iterator it=e.comps.begin();
deba@482
   725
             it!=e.comps.end(); ++it)
deba@482
   726
          comps[it->first]-=it->second;
deba@482
   727
        return *this;
deba@482
   728
      }
deba@482
   729
      ///Multiply with a constant
deba@482
   730
      DualExpr &operator*=(const Value &v) {
deba@482
   731
        for (std::map<int, Value>::iterator it=comps.begin();
deba@482
   732
             it!=comps.end(); ++it)
deba@482
   733
          it->second*=v;
deba@482
   734
        return *this;
deba@482
   735
      }
deba@482
   736
      ///Division with a constant
deba@482
   737
      DualExpr &operator/=(const Value &v) {
deba@482
   738
        for (std::map<int, Value>::iterator it=comps.begin();
deba@482
   739
             it!=comps.end(); ++it)
deba@482
   740
          it->second/=v;
deba@482
   741
        return *this;
deba@481
   742
      }
deba@481
   743
deba@482
   744
      ///Iterator over the expression
deba@482
   745
      
deba@482
   746
      ///The iterator iterates over the terms of the expression. 
deba@482
   747
      /// 
deba@482
   748
      ///\code
deba@482
   749
      ///double s=0;
deba@482
   750
      ///for(LpBase::DualExpr::CoeffIt i(e);i!=INVALID;++i)
deba@482
   751
      ///  s+= *i * dual(i);
deba@482
   752
      ///\endcode
deba@482
   753
      class CoeffIt {
deba@482
   754
      private:
deba@482
   755
deba@482
   756
        std::map<int, Value>::iterator _it, _end;
deba@482
   757
deba@482
   758
      public:
deba@482
   759
deba@482
   760
        /// Sets the iterator to the first term
deba@482
   761
        
deba@482
   762
        /// Sets the iterator to the first term of the expression.
deba@482
   763
        ///
deba@482
   764
        CoeffIt(DualExpr& e)
deba@482
   765
          : _it(e.comps.begin()), _end(e.comps.end()){}
deba@482
   766
deba@482
   767
        /// Convert the iterator to the row of the term
deba@482
   768
        operator Row() const {
deba@482
   769
          return rowFromId(_it->first);
deba@482
   770
        }
deba@482
   771
deba@482
   772
        /// Returns the coefficient of the term
deba@482
   773
        Value& operator*() { return _it->second; }
deba@482
   774
deba@482
   775
        /// Returns the coefficient of the term
deba@482
   776
        const Value& operator*() const { return _it->second; }
deba@482
   777
deba@482
   778
        /// Next term
deba@482
   779
        
deba@482
   780
        /// Assign the iterator to the next term.
deba@482
   781
        ///
deba@482
   782
        CoeffIt& operator++() { ++_it; return *this; }
deba@482
   783
deba@482
   784
        /// Equality operator
deba@482
   785
        bool operator==(Invalid) const { return _it == _end; }
deba@482
   786
        /// Inequality operator
deba@482
   787
        bool operator!=(Invalid) const { return _it != _end; }
deba@482
   788
      };
deba@482
   789
deba@482
   790
      ///Iterator over the expression
deba@482
   791
      
deba@482
   792
      ///The iterator iterates over the terms of the expression. 
deba@482
   793
      /// 
deba@482
   794
      ///\code
deba@482
   795
      ///double s=0;
deba@482
   796
      ///for(LpBase::DualExpr::ConstCoeffIt i(e);i!=INVALID;++i)
deba@482
   797
      ///  s+= *i * dual(i);
deba@482
   798
      ///\endcode
deba@482
   799
      class ConstCoeffIt {
deba@482
   800
      private:
deba@482
   801
deba@482
   802
        std::map<int, Value>::const_iterator _it, _end;
deba@482
   803
deba@482
   804
      public:
deba@482
   805
deba@482
   806
        /// Sets the iterator to the first term
deba@482
   807
        
deba@482
   808
        /// Sets the iterator to the first term of the expression.
deba@482
   809
        ///
deba@482
   810
        ConstCoeffIt(const DualExpr& e)
deba@482
   811
          : _it(e.comps.begin()), _end(e.comps.end()){}
deba@482
   812
deba@482
   813
        /// Convert the iterator to the row of the term
deba@482
   814
        operator Row() const {
deba@482
   815
          return rowFromId(_it->first);
deba@482
   816
        }
deba@482
   817
deba@482
   818
        /// Returns the coefficient of the term
deba@482
   819
        const Value& operator*() const { return _it->second; }
deba@482
   820
deba@482
   821
        /// Next term
deba@482
   822
        
deba@482
   823
        /// Assign the iterator to the next term.
deba@482
   824
        ///
deba@482
   825
        ConstCoeffIt& operator++() { ++_it; return *this; }
deba@482
   826
deba@482
   827
        /// Equality operator
deba@482
   828
        bool operator==(Invalid) const { return _it == _end; }
deba@482
   829
        /// Inequality operator
deba@482
   830
        bool operator!=(Invalid) const { return _it != _end; }
deba@482
   831
      };
deba@481
   832
    };
deba@481
   833
deba@481
   834
deba@482
   835
  protected:
deba@481
   836
deba@482
   837
    class InsertIterator {
deba@482
   838
    private:
deba@482
   839
deba@482
   840
      std::map<int, Value>& _host;
deba@482
   841
      const _solver_bits::VarIndex& _index;
deba@482
   842
deba@481
   843
    public:
deba@481
   844
deba@481
   845
      typedef std::output_iterator_tag iterator_category;
deba@481
   846
      typedef void difference_type;
deba@481
   847
      typedef void value_type;
deba@481
   848
      typedef void reference;
deba@481
   849
      typedef void pointer;
deba@481
   850
deba@482
   851
      InsertIterator(std::map<int, Value>& host,
deba@482
   852
                   const _solver_bits::VarIndex& index)
deba@482
   853
        : _host(host), _index(index) {}
deba@481
   854
deba@482
   855
      InsertIterator& operator=(const std::pair<int, Value>& value) {
deba@482
   856
        typedef std::map<int, Value>::value_type pair_type;
deba@482
   857
        _host.insert(pair_type(_index[value.first], value.second));
deba@481
   858
        return *this;
deba@481
   859
      }
deba@481
   860
deba@482
   861
      InsertIterator& operator*() { return *this; }
deba@482
   862
      InsertIterator& operator++() { return *this; }
deba@482
   863
      InsertIterator operator++(int) { return *this; }
deba@481
   864
deba@481
   865
    };
deba@481
   866
deba@482
   867
    class ExprIterator {
deba@482
   868
    private:
deba@482
   869
      std::map<int, Value>::const_iterator _host_it;
deba@482
   870
      const _solver_bits::VarIndex& _index;
deba@481
   871
    public:
deba@481
   872
deba@482
   873
      typedef std::bidirectional_iterator_tag iterator_category;
deba@482
   874
      typedef std::ptrdiff_t difference_type;
deba@481
   875
      typedef const std::pair<int, Value> value_type;
deba@481
   876
      typedef value_type reference;
deba@482
   877
deba@481
   878
      class pointer {
deba@481
   879
      public:
deba@481
   880
        pointer(value_type& _value) : value(_value) {}
deba@481
   881
        value_type* operator->() { return &value; }
deba@481
   882
      private:
deba@481
   883
        value_type value;
deba@481
   884
      };
deba@481
   885
deba@482
   886
      ExprIterator(const std::map<int, Value>::const_iterator& host_it,
deba@482
   887
                   const _solver_bits::VarIndex& index)
deba@482
   888
        : _host_it(host_it), _index(index) {}
deba@481
   889
deba@481
   890
      reference operator*() {
deba@482
   891
        return std::make_pair(_index(_host_it->first), _host_it->second);
deba@481
   892
      }
deba@481
   893
deba@481
   894
      pointer operator->() {
deba@481
   895
        return pointer(operator*());
deba@481
   896
      }
deba@481
   897
deba@482
   898
      ExprIterator& operator++() { ++_host_it; return *this; }
deba@482
   899
      ExprIterator operator++(int) {
deba@482
   900
        ExprIterator tmp(*this); ++_host_it; return tmp;
deba@481
   901
      }
deba@481
   902
deba@482
   903
      ExprIterator& operator--() { --_host_it; return *this; }
deba@482
   904
      ExprIterator operator--(int) {
deba@482
   905
        ExprIterator tmp(*this); --_host_it; return tmp;
deba@481
   906
      }
deba@481
   907
deba@482
   908
      bool operator==(const ExprIterator& it) const {
deba@482
   909
        return _host_it == it._host_it;
deba@481
   910
      }
deba@481
   911
deba@482
   912
      bool operator!=(const ExprIterator& it) const {
deba@482
   913
        return _host_it != it._host_it;
deba@481
   914
      }
deba@481
   915
deba@481
   916
    };
deba@481
   917
deba@481
   918
  protected:
deba@481
   919
deba@482
   920
    //Abstract virtual functions
deba@482
   921
    virtual LpBase* _newSolver() const = 0;
deba@482
   922
    virtual LpBase* _cloneSolver() const = 0;
deba@481
   923
deba@482
   924
    virtual int _addColId(int col) { return cols.addIndex(col); }
deba@482
   925
    virtual int _addRowId(int row) { return rows.addIndex(row); }
deba@481
   926
deba@482
   927
    virtual void _eraseColId(int col) { cols.eraseIndex(col); }
deba@482
   928
    virtual void _eraseRowId(int row) { rows.eraseIndex(row); }
deba@481
   929
deba@481
   930
    virtual int _addCol() = 0;
deba@481
   931
    virtual int _addRow() = 0;
deba@481
   932
deba@481
   933
    virtual void _eraseCol(int col) = 0;
deba@481
   934
    virtual void _eraseRow(int row) = 0;
deba@481
   935
deba@482
   936
    virtual void _getColName(int col, std::string& name) const = 0;
deba@482
   937
    virtual void _setColName(int col, const std::string& name) = 0;
deba@481
   938
    virtual int _colByName(const std::string& name) const = 0;
deba@481
   939
deba@482
   940
    virtual void _getRowName(int row, std::string& name) const = 0;
deba@482
   941
    virtual void _setRowName(int row, const std::string& name) = 0;
deba@482
   942
    virtual int _rowByName(const std::string& name) const = 0;
deba@482
   943
deba@482
   944
    virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e) = 0;
deba@482
   945
    virtual void _getRowCoeffs(int i, InsertIterator b) const = 0;
deba@482
   946
deba@482
   947
    virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e) = 0;
deba@482
   948
    virtual void _getColCoeffs(int i, InsertIterator b) const = 0;
deba@482
   949
deba@481
   950
    virtual void _setCoeff(int row, int col, Value value) = 0;
deba@481
   951
    virtual Value _getCoeff(int row, int col) const = 0;
deba@482
   952
deba@481
   953
    virtual void _setColLowerBound(int i, Value value) = 0;
deba@481
   954
    virtual Value _getColLowerBound(int i) const = 0;
deba@482
   955
deba@481
   956
    virtual void _setColUpperBound(int i, Value value) = 0;
deba@481
   957
    virtual Value _getColUpperBound(int i) const = 0;
deba@482
   958
deba@482
   959
    virtual void _setRowLowerBound(int i, Value value) = 0;
deba@482
   960
    virtual Value _getRowLowerBound(int i) const = 0;
deba@482
   961
deba@482
   962
    virtual void _setRowUpperBound(int i, Value value) = 0;
deba@482
   963
    virtual Value _getRowUpperBound(int i) const = 0;
deba@482
   964
deba@482
   965
    virtual void _setObjCoeffs(ExprIterator b, ExprIterator e) = 0;
deba@482
   966
    virtual void _getObjCoeffs(InsertIterator b) const = 0;
deba@481
   967
deba@481
   968
    virtual void _setObjCoeff(int i, Value obj_coef) = 0;
deba@481
   969
    virtual Value _getObjCoeff(int i) const = 0;
deba@481
   970
deba@482
   971
    virtual void _setSense(Sense) = 0;
deba@482
   972
    virtual Sense _getSense() const = 0;
deba@481
   973
deba@482
   974
    virtual void _clear() = 0;
deba@481
   975
deba@482
   976
    virtual const char* _solverName() const = 0;
deba@481
   977
deba@481
   978
    //Own protected stuff
deba@481
   979
deba@481
   980
    //Constant component of the objective function
deba@481
   981
    Value obj_const_comp;
deba@481
   982
deba@482
   983
    LpBase() : rows(), cols(), obj_const_comp(0) {}
deba@482
   984
deba@481
   985
  public:
deba@481
   986
deba@482
   987
    /// Virtual destructor
deba@482
   988
    virtual ~LpBase() {}
deba@481
   989
deba@481
   990
    ///Creates a new LP problem
deba@482
   991
    LpBase* newSolver() {return _newSolver();}
deba@481
   992
    ///Makes a copy of the LP problem
deba@482
   993
    LpBase* cloneSolver() {return _cloneSolver();}
deba@482
   994
deba@482
   995
    ///Gives back the name of the solver.
deba@482
   996
    const char* solverName() const {return _solverName();}
deba@481
   997
deba@481
   998
    ///\name Build up and modify the LP
deba@481
   999
deba@481
  1000
    ///@{
deba@481
  1001
deba@481
  1002
    ///Add a new empty column (i.e a new variable) to the LP
deba@482
  1003
    Col addCol() { Col c; c._id = _addColId(_addCol()); return c;}
deba@481
  1004
deba@482
  1005
    ///\brief Adds several new columns (i.e variables) at once
deba@481
  1006
    ///
deba@482
  1007
    ///This magic function takes a container as its argument and fills
deba@482
  1008
    ///its elements with new columns (i.e. variables)
deba@481
  1009
    ///\param t can be
deba@481
  1010
    ///- a standard STL compatible iterable container with
deba@482
  1011
    ///\ref Col as its \c values_type like
deba@481
  1012
    ///\code
deba@482
  1013
    ///std::vector<LpBase::Col>
deba@482
  1014
    ///std::list<LpBase::Col>
deba@481
  1015
    ///\endcode
deba@481
  1016
    ///- a standard STL compatible iterable container with
deba@482
  1017
    ///\ref Col as its \c mapped_type like
deba@481
  1018
    ///\code
deba@482
  1019
    ///std::map<AnyType,LpBase::Col>
deba@481
  1020
    ///\endcode
deba@481
  1021
    ///- an iterable lemon \ref concepts::WriteMap "write map" like
deba@481
  1022
    ///\code
deba@482
  1023
    ///ListGraph::NodeMap<LpBase::Col>
deba@482
  1024
    ///ListGraph::ArcMap<LpBase::Col>
deba@481
  1025
    ///\endcode
deba@481
  1026
    ///\return The number of the created column.
deba@481
  1027
#ifdef DOXYGEN
deba@481
  1028
    template<class T>
deba@481
  1029
    int addColSet(T &t) { return 0;}
deba@481
  1030
#else
deba@481
  1031
    template<class T>
deba@482
  1032
    typename enable_if<typename T::value_type::LpCol,int>::type
deba@481
  1033
    addColSet(T &t,dummy<0> = 0) {
deba@481
  1034
      int s=0;
deba@481
  1035
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
deba@481
  1036
      return s;
deba@481
  1037
    }
deba@481
  1038
    template<class T>
deba@482
  1039
    typename enable_if<typename T::value_type::second_type::LpCol,
deba@481
  1040
                       int>::type
deba@481
  1041
    addColSet(T &t,dummy<1> = 1) {
deba@481
  1042
      int s=0;
deba@481
  1043
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
deba@481
  1044
        i->second=addCol();
deba@481
  1045
        s++;
deba@481
  1046
      }
deba@481
  1047
      return s;
deba@481
  1048
    }
deba@481
  1049
    template<class T>
deba@482
  1050
    typename enable_if<typename T::MapIt::Value::LpCol,
deba@481
  1051
                       int>::type
deba@481
  1052
    addColSet(T &t,dummy<2> = 2) {
deba@481
  1053
      int s=0;
deba@481
  1054
      for(typename T::MapIt i(t); i!=INVALID; ++i)
deba@481
  1055
        {
deba@481
  1056
          i.set(addCol());
deba@481
  1057
          s++;
deba@481
  1058
        }
deba@481
  1059
      return s;
deba@481
  1060
    }
deba@481
  1061
#endif
deba@481
  1062
deba@481
  1063
    ///Set a column (i.e a dual constraint) of the LP
deba@481
  1064
deba@481
  1065
    ///\param c is the column to be modified
deba@481
  1066
    ///\param e is a dual linear expression (see \ref DualExpr)
deba@481
  1067
    ///a better one.
deba@482
  1068
    void col(Col c, const DualExpr &e) {
deba@481
  1069
      e.simplify();
deba@494
  1070
      _setColCoeffs(cols(id(c)), ExprIterator(e.comps.begin(), rows),
deba@494
  1071
                    ExprIterator(e.comps.end(), rows));
deba@481
  1072
    }
deba@481
  1073
deba@481
  1074
    ///Get a column (i.e a dual constraint) of the LP
deba@481
  1075
deba@482
  1076
    ///\param c is the column to get
deba@481
  1077
    ///\return the dual expression associated to the column
deba@481
  1078
    DualExpr col(Col c) const {
deba@481
  1079
      DualExpr e;
deba@482
  1080
      _getColCoeffs(cols(id(c)), InsertIterator(e.comps, rows));
deba@481
  1081
      return e;
deba@481
  1082
    }
deba@481
  1083
deba@481
  1084
    ///Add a new column to the LP
deba@481
  1085
deba@481
  1086
    ///\param e is a dual linear expression (see \ref DualExpr)
deba@482
  1087
    ///\param o is the corresponding component of the objective
deba@481
  1088
    ///function. It is 0 by default.
deba@481
  1089
    ///\return The created column.
deba@481
  1090
    Col addCol(const DualExpr &e, Value o = 0) {
deba@481
  1091
      Col c=addCol();
deba@481
  1092
      col(c,e);
deba@481
  1093
      objCoeff(c,o);
deba@481
  1094
      return c;
deba@481
  1095
    }
deba@481
  1096
deba@481
  1097
    ///Add a new empty row (i.e a new constraint) to the LP
deba@481
  1098
deba@481
  1099
    ///This function adds a new empty row (i.e a new constraint) to the LP.
deba@481
  1100
    ///\return The created row
deba@482
  1101
    Row addRow() { Row r; r._id = _addRowId(_addRow()); return r;}
deba@481
  1102
deba@482
  1103
    ///\brief Add several new rows (i.e constraints) at once
deba@481
  1104
    ///
deba@482
  1105
    ///This magic function takes a container as its argument and fills
deba@482
  1106
    ///its elements with new row (i.e. variables)
deba@481
  1107
    ///\param t can be
deba@481
  1108
    ///- a standard STL compatible iterable container with
deba@482
  1109
    ///\ref Row as its \c values_type like
deba@481
  1110
    ///\code
deba@482
  1111
    ///std::vector<LpBase::Row>
deba@482
  1112
    ///std::list<LpBase::Row>
deba@481
  1113
    ///\endcode
deba@481
  1114
    ///- a standard STL compatible iterable container with
deba@482
  1115
    ///\ref Row as its \c mapped_type like
deba@481
  1116
    ///\code
deba@482
  1117
    ///std::map<AnyType,LpBase::Row>
deba@481
  1118
    ///\endcode
deba@481
  1119
    ///- an iterable lemon \ref concepts::WriteMap "write map" like
deba@481
  1120
    ///\code
deba@482
  1121
    ///ListGraph::NodeMap<LpBase::Row>
deba@482
  1122
    ///ListGraph::ArcMap<LpBase::Row>
deba@481
  1123
    ///\endcode
deba@481
  1124
    ///\return The number of rows created.
deba@481
  1125
#ifdef DOXYGEN
deba@481
  1126
    template<class T>
deba@481
  1127
    int addRowSet(T &t) { return 0;}
deba@481
  1128
#else
deba@481
  1129
    template<class T>
deba@482
  1130
    typename enable_if<typename T::value_type::LpRow,int>::type
deba@482
  1131
    addRowSet(T &t, dummy<0> = 0) {
deba@481
  1132
      int s=0;
deba@481
  1133
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;}
deba@481
  1134
      return s;
deba@481
  1135
    }
deba@481
  1136
    template<class T>
deba@482
  1137
    typename enable_if<typename T::value_type::second_type::LpRow, int>::type
deba@482
  1138
    addRowSet(T &t, dummy<1> = 1) {
deba@481
  1139
      int s=0;
deba@481
  1140
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
deba@481
  1141
        i->second=addRow();
deba@481
  1142
        s++;
deba@481
  1143
      }
deba@481
  1144
      return s;
deba@481
  1145
    }
deba@481
  1146
    template<class T>
deba@482
  1147
    typename enable_if<typename T::MapIt::Value::LpRow, int>::type
deba@482
  1148
    addRowSet(T &t, dummy<2> = 2) {
deba@481
  1149
      int s=0;
deba@481
  1150
      for(typename T::MapIt i(t); i!=INVALID; ++i)
deba@481
  1151
        {
deba@481
  1152
          i.set(addRow());
deba@481
  1153
          s++;
deba@481
  1154
        }
deba@481
  1155
      return s;
deba@481
  1156
    }
deba@481
  1157
#endif
deba@481
  1158
deba@481
  1159
    ///Set a row (i.e a constraint) of the LP
deba@481
  1160
deba@481
  1161
    ///\param r is the row to be modified
deba@481
  1162
    ///\param l is lower bound (-\ref INF means no bound)
deba@481
  1163
    ///\param e is a linear expression (see \ref Expr)
deba@481
  1164
    ///\param u is the upper bound (\ref INF means no bound)
deba@481
  1165
    void row(Row r, Value l, const Expr &e, Value u) {
deba@481
  1166
      e.simplify();
deba@482
  1167
      _setRowCoeffs(rows(id(r)), ExprIterator(e.comps.begin(), cols),
deba@482
  1168
                    ExprIterator(e.comps.end(), cols));
deba@482
  1169
      _setRowLowerBound(rows(id(r)),l - *e);
deba@482
  1170
      _setRowUpperBound(rows(id(r)),u - *e);
deba@481
  1171
    }
deba@481
  1172
deba@481
  1173
    ///Set a row (i.e a constraint) of the LP
deba@481
  1174
deba@481
  1175
    ///\param r is the row to be modified
deba@481
  1176
    ///\param c is a linear expression (see \ref Constr)
deba@481
  1177
    void row(Row r, const Constr &c) {
deba@481
  1178
      row(r, c.lowerBounded()?c.lowerBound():-INF,
deba@481
  1179
          c.expr(), c.upperBounded()?c.upperBound():INF);
deba@481
  1180
    }
deba@481
  1181
deba@481
  1182
deba@481
  1183
    ///Get a row (i.e a constraint) of the LP
deba@481
  1184
deba@481
  1185
    ///\param r is the row to get
deba@481
  1186
    ///\return the expression associated to the row
deba@481
  1187
    Expr row(Row r) const {
deba@481
  1188
      Expr e;
deba@482
  1189
      _getRowCoeffs(rows(id(r)), InsertIterator(e.comps, cols));
deba@481
  1190
      return e;
deba@481
  1191
    }
deba@481
  1192
deba@481
  1193
    ///Add a new row (i.e a new constraint) to the LP
deba@481
  1194
deba@481
  1195
    ///\param l is the lower bound (-\ref INF means no bound)
deba@481
  1196
    ///\param e is a linear expression (see \ref Expr)
deba@481
  1197
    ///\param u is the upper bound (\ref INF means no bound)
deba@481
  1198
    ///\return The created row.
deba@481
  1199
    Row addRow(Value l,const Expr &e, Value u) {
deba@481
  1200
      Row r=addRow();
deba@481
  1201
      row(r,l,e,u);
deba@481
  1202
      return r;
deba@481
  1203
    }
deba@481
  1204
deba@481
  1205
    ///Add a new row (i.e a new constraint) to the LP
deba@481
  1206
deba@481
  1207
    ///\param c is a linear expression (see \ref Constr)
deba@481
  1208
    ///\return The created row.
deba@481
  1209
    Row addRow(const Constr &c) {
deba@481
  1210
      Row r=addRow();
deba@481
  1211
      row(r,c);
deba@481
  1212
      return r;
deba@481
  1213
    }
deba@482
  1214
    ///Erase a column (i.e a variable) from the LP
deba@481
  1215
deba@482
  1216
    ///\param c is the column to be deleted
deba@482
  1217
    void erase(Col c) {
deba@482
  1218
      _eraseCol(cols(id(c)));
deba@482
  1219
      _eraseColId(cols(id(c)));
deba@481
  1220
    }
deba@482
  1221
    ///Erase a row (i.e a constraint) from the LP
deba@481
  1222
deba@481
  1223
    ///\param r is the row to be deleted
deba@482
  1224
    void erase(Row r) {
deba@482
  1225
      _eraseRow(rows(id(r)));
deba@482
  1226
      _eraseRowId(rows(id(r)));
deba@481
  1227
    }
deba@481
  1228
deba@481
  1229
    /// Get the name of a column
deba@481
  1230
deba@482
  1231
    ///\param c is the coresponding column
deba@481
  1232
    ///\return The name of the colunm
deba@481
  1233
    std::string colName(Col c) const {
deba@481
  1234
      std::string name;
deba@482
  1235
      _getColName(cols(id(c)), name);
deba@481
  1236
      return name;
deba@481
  1237
    }
deba@481
  1238
deba@481
  1239
    /// Set the name of a column
deba@481
  1240
deba@482
  1241
    ///\param c is the coresponding column
deba@481
  1242
    ///\param name The name to be given
deba@481
  1243
    void colName(Col c, const std::string& name) {
deba@482
  1244
      _setColName(cols(id(c)), name);
deba@481
  1245
    }
deba@481
  1246
deba@481
  1247
    /// Get the column by its name
deba@481
  1248
deba@481
  1249
    ///\param name The name of the column
deba@481
  1250
    ///\return the proper column or \c INVALID
deba@481
  1251
    Col colByName(const std::string& name) const {
deba@481
  1252
      int k = _colByName(name);
deba@482
  1253
      return k != -1 ? Col(cols[k]) : Col(INVALID);
deba@482
  1254
    }
deba@482
  1255
deba@482
  1256
    /// Get the name of a row
deba@482
  1257
deba@482
  1258
    ///\param r is the coresponding row
deba@482
  1259
    ///\return The name of the row
deba@482
  1260
    std::string rowName(Row r) const {
deba@482
  1261
      std::string name;
deba@482
  1262
      _getRowName(rows(id(r)), name);
deba@482
  1263
      return name;
deba@482
  1264
    }
deba@482
  1265
deba@482
  1266
    /// Set the name of a row
deba@482
  1267
deba@482
  1268
    ///\param r is the coresponding row
deba@482
  1269
    ///\param name The name to be given
deba@482
  1270
    void rowName(Row r, const std::string& name) {
deba@482
  1271
      _setRowName(rows(id(r)), name);
deba@482
  1272
    }
deba@482
  1273
deba@482
  1274
    /// Get the row by its name
deba@482
  1275
deba@482
  1276
    ///\param name The name of the row
deba@482
  1277
    ///\return the proper row or \c INVALID
deba@482
  1278
    Row rowByName(const std::string& name) const {
deba@482
  1279
      int k = _rowByName(name);
deba@482
  1280
      return k != -1 ? Row(rows[k]) : Row(INVALID);
deba@481
  1281
    }
deba@481
  1282
deba@481
  1283
    /// Set an element of the coefficient matrix of the LP
deba@481
  1284
deba@481
  1285
    ///\param r is the row of the element to be modified
deba@482
  1286
    ///\param c is the column of the element to be modified
deba@481
  1287
    ///\param val is the new value of the coefficient
deba@481
  1288
    void coeff(Row r, Col c, Value val) {
deba@482
  1289
      _setCoeff(rows(id(r)),cols(id(c)), val);
deba@481
  1290
    }
deba@481
  1291
deba@481
  1292
    /// Get an element of the coefficient matrix of the LP
deba@481
  1293
deba@482
  1294
    ///\param r is the row of the element
deba@482
  1295
    ///\param c is the column of the element
deba@481
  1296
    ///\return the corresponding coefficient
deba@481
  1297
    Value coeff(Row r, Col c) const {
deba@482
  1298
      return _getCoeff(rows(id(r)),cols(id(c)));
deba@481
  1299
    }
deba@481
  1300
deba@481
  1301
    /// Set the lower bound of a column (i.e a variable)
deba@481
  1302
deba@481
  1303
    /// The lower bound of a variable (column) has to be given by an
deba@481
  1304
    /// extended number of type Value, i.e. a finite number of type
deba@481
  1305
    /// Value or -\ref INF.
deba@481
  1306
    void colLowerBound(Col c, Value value) {
deba@482
  1307
      _setColLowerBound(cols(id(c)),value);
deba@481
  1308
    }
deba@481
  1309
deba@481
  1310
    /// Get the lower bound of a column (i.e a variable)
deba@481
  1311
deba@482
  1312
    /// This function returns the lower bound for column (variable) \c c
deba@481
  1313
    /// (this might be -\ref INF as well).
deba@482
  1314
    ///\return The lower bound for column \c c
deba@481
  1315
    Value colLowerBound(Col c) const {
deba@482
  1316
      return _getColLowerBound(cols(id(c)));
deba@481
  1317
    }
deba@481
  1318
deba@481
  1319
    ///\brief Set the lower bound of  several columns
deba@482
  1320
    ///(i.e variables) at once
deba@481
  1321
    ///
deba@481
  1322
    ///This magic function takes a container as its argument
deba@481
  1323
    ///and applies the function on all of its elements.
deba@482
  1324
    ///The lower bound of a variable (column) has to be given by an
deba@482
  1325
    ///extended number of type Value, i.e. a finite number of type
deba@482
  1326
    ///Value or -\ref INF.
deba@481
  1327
#ifdef DOXYGEN
deba@481
  1328
    template<class T>
deba@481
  1329
    void colLowerBound(T &t, Value value) { return 0;}
deba@481
  1330
#else
deba@481
  1331
    template<class T>
deba@482
  1332
    typename enable_if<typename T::value_type::LpCol,void>::type
deba@481
  1333
    colLowerBound(T &t, Value value,dummy<0> = 0) {
deba@481
  1334
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
deba@481
  1335
        colLowerBound(*i, value);
deba@481
  1336
      }
deba@481
  1337
    }
deba@481
  1338
    template<class T>
deba@482
  1339
    typename enable_if<typename T::value_type::second_type::LpCol,
deba@481
  1340
                       void>::type
deba@481
  1341
    colLowerBound(T &t, Value value,dummy<1> = 1) {
deba@481
  1342
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
deba@481
  1343
        colLowerBound(i->second, value);
deba@481
  1344
      }
deba@481
  1345
    }
deba@481
  1346
    template<class T>
deba@482
  1347
    typename enable_if<typename T::MapIt::Value::LpCol,
deba@481
  1348
                       void>::type
deba@481
  1349
    colLowerBound(T &t, Value value,dummy<2> = 2) {
deba@481
  1350
      for(typename T::MapIt i(t); i!=INVALID; ++i){
deba@481
  1351
        colLowerBound(*i, value);
deba@481
  1352
      }
deba@481
  1353
    }
deba@481
  1354
#endif
deba@481
  1355
deba@481
  1356
    /// Set the upper bound of a column (i.e a variable)
deba@481
  1357
deba@481
  1358
    /// The upper bound of a variable (column) has to be given by an
deba@481
  1359
    /// extended number of type Value, i.e. a finite number of type
deba@481
  1360
    /// Value or \ref INF.
deba@481
  1361
    void colUpperBound(Col c, Value value) {
deba@482
  1362
      _setColUpperBound(cols(id(c)),value);
deba@481
  1363
    };
deba@481
  1364
deba@481
  1365
    /// Get the upper bound of a column (i.e a variable)
deba@481
  1366
deba@482
  1367
    /// This function returns the upper bound for column (variable) \c c
deba@481
  1368
    /// (this might be \ref INF as well).
deba@482
  1369
    /// \return The upper bound for column \c c
deba@481
  1370
    Value colUpperBound(Col c) const {
deba@482
  1371
      return _getColUpperBound(cols(id(c)));
deba@481
  1372
    }
deba@481
  1373
deba@481
  1374
    ///\brief Set the upper bound of  several columns
deba@482
  1375
    ///(i.e variables) at once
deba@481
  1376
    ///
deba@481
  1377
    ///This magic function takes a container as its argument
deba@481
  1378
    ///and applies the function on all of its elements.
deba@482
  1379
    ///The upper bound of a variable (column) has to be given by an
deba@482
  1380
    ///extended number of type Value, i.e. a finite number of type
deba@482
  1381
    ///Value or \ref INF.
deba@481
  1382
#ifdef DOXYGEN
deba@481
  1383
    template<class T>
deba@481
  1384
    void colUpperBound(T &t, Value value) { return 0;}
deba@481
  1385
#else
tapolcai@561
  1386
    template<class T1>
tapolcai@561
  1387
    typename enable_if<typename T1::value_type::LpCol,void>::type
tapolcai@561
  1388
    colUpperBound(T1 &t, Value value,dummy<0> = 0) {
tapolcai@561
  1389
      for(typename T1::iterator i=t.begin();i!=t.end();++i) {
deba@481
  1390
        colUpperBound(*i, value);
deba@481
  1391
      }
deba@481
  1392
    }
tapolcai@561
  1393
    template<class T1>
tapolcai@561
  1394
    typename enable_if<typename T1::value_type::second_type::LpCol,
deba@481
  1395
                       void>::type
tapolcai@561
  1396
    colUpperBound(T1 &t, Value value,dummy<1> = 1) {
tapolcai@561
  1397
      for(typename T1::iterator i=t.begin();i!=t.end();++i) {
deba@481
  1398
        colUpperBound(i->second, value);
deba@481
  1399
      }
deba@481
  1400
    }
tapolcai@561
  1401
    template<class T1>
tapolcai@561
  1402
    typename enable_if<typename T1::MapIt::Value::LpCol,
deba@481
  1403
                       void>::type
tapolcai@561
  1404
    colUpperBound(T1 &t, Value value,dummy<2> = 2) {
tapolcai@561
  1405
      for(typename T1::MapIt i(t); i!=INVALID; ++i){
deba@481
  1406
        colUpperBound(*i, value);
deba@481
  1407
      }
deba@481
  1408
    }
deba@481
  1409
#endif
deba@481
  1410
deba@481
  1411
    /// Set the lower and the upper bounds of a column (i.e a variable)
deba@481
  1412
deba@481
  1413
    /// The lower and the upper bounds of
deba@481
  1414
    /// a variable (column) have to be given by an
deba@481
  1415
    /// extended number of type Value, i.e. a finite number of type
deba@481
  1416
    /// Value, -\ref INF or \ref INF.
deba@481
  1417
    void colBounds(Col c, Value lower, Value upper) {
deba@482
  1418
      _setColLowerBound(cols(id(c)),lower);
deba@482
  1419
      _setColUpperBound(cols(id(c)),upper);
deba@481
  1420
    }
deba@481
  1421
deba@481
  1422
    ///\brief Set the lower and the upper bound of several columns
deba@482
  1423
    ///(i.e variables) at once
deba@481
  1424
    ///
deba@481
  1425
    ///This magic function takes a container as its argument
deba@481
  1426
    ///and applies the function on all of its elements.
deba@481
  1427
    /// The lower and the upper bounds of
deba@481
  1428
    /// a variable (column) have to be given by an
deba@481
  1429
    /// extended number of type Value, i.e. a finite number of type
deba@481
  1430
    /// Value, -\ref INF or \ref INF.
deba@481
  1431
#ifdef DOXYGEN
deba@481
  1432
    template<class T>
deba@481
  1433
    void colBounds(T &t, Value lower, Value upper) { return 0;}
deba@481
  1434
#else
tapolcai@561
  1435
    template<class T2>
tapolcai@561
  1436
    typename enable_if<typename T2::value_type::LpCol,void>::type
tapolcai@561
  1437
    colBounds(T2 &t, Value lower, Value upper,dummy<0> = 0) {
tapolcai@561
  1438
      for(typename T2::iterator i=t.begin();i!=t.end();++i) {
deba@481
  1439
        colBounds(*i, lower, upper);
deba@481
  1440
      }
deba@481
  1441
    }
tapolcai@561
  1442
    template<class T2>
tapolcai@561
  1443
    typename enable_if<typename T2::value_type::second_type::LpCol, void>::type
tapolcai@561
  1444
    colBounds(T2 &t, Value lower, Value upper,dummy<1> = 1) {
tapolcai@561
  1445
      for(typename T2::iterator i=t.begin();i!=t.end();++i) {
deba@481
  1446
        colBounds(i->second, lower, upper);
deba@481
  1447
      }
deba@481
  1448
    }
tapolcai@561
  1449
    template<class T2>
tapolcai@561
  1450
    typename enable_if<typename T2::MapIt::Value::LpCol, void>::type
tapolcai@561
  1451
    colBounds(T2 &t, Value lower, Value upper,dummy<2> = 2) {
tapolcai@561
  1452
      for(typename T2::MapIt i(t); i!=INVALID; ++i){
deba@481
  1453
        colBounds(*i, lower, upper);
deba@481
  1454
      }
deba@481
  1455
    }
deba@481
  1456
#endif
deba@481
  1457
deba@482
  1458
    /// Set the lower bound of a row (i.e a constraint)
deba@481
  1459
deba@482
  1460
    /// The lower bound of a constraint (row) has to be given by an
deba@482
  1461
    /// extended number of type Value, i.e. a finite number of type
deba@482
  1462
    /// Value or -\ref INF.
deba@482
  1463
    void rowLowerBound(Row r, Value value) {
deba@482
  1464
      _setRowLowerBound(rows(id(r)),value);
deba@481
  1465
    }
deba@481
  1466
deba@482
  1467
    /// Get the lower bound of a row (i.e a constraint)
deba@481
  1468
deba@482
  1469
    /// This function returns the lower bound for row (constraint) \c c
deba@482
  1470
    /// (this might be -\ref INF as well).
deba@482
  1471
    ///\return The lower bound for row \c r
deba@482
  1472
    Value rowLowerBound(Row r) const {
deba@482
  1473
      return _getRowLowerBound(rows(id(r)));
deba@482
  1474
    }
deba@482
  1475
deba@482
  1476
    /// Set the upper bound of a row (i.e a constraint)
deba@482
  1477
deba@482
  1478
    /// The upper bound of a constraint (row) has to be given by an
deba@482
  1479
    /// extended number of type Value, i.e. a finite number of type
deba@482
  1480
    /// Value or -\ref INF.
deba@482
  1481
    void rowUpperBound(Row r, Value value) {
deba@482
  1482
      _setRowUpperBound(rows(id(r)),value);
deba@482
  1483
    }
deba@482
  1484
deba@482
  1485
    /// Get the upper bound of a row (i.e a constraint)
deba@482
  1486
deba@482
  1487
    /// This function returns the upper bound for row (constraint) \c c
deba@482
  1488
    /// (this might be -\ref INF as well).
deba@482
  1489
    ///\return The upper bound for row \c r
deba@482
  1490
    Value rowUpperBound(Row r) const {
deba@482
  1491
      return _getRowUpperBound(rows(id(r)));
deba@481
  1492
    }
deba@481
  1493
deba@481
  1494
    ///Set an element of the objective function
deba@482
  1495
    void objCoeff(Col c, Value v) {_setObjCoeff(cols(id(c)),v); };
deba@481
  1496
deba@481
  1497
    ///Get an element of the objective function
deba@482
  1498
    Value objCoeff(Col c) const { return _getObjCoeff(cols(id(c))); };
deba@481
  1499
deba@481
  1500
    ///Set the objective function
deba@481
  1501
deba@481
  1502
    ///\param e is a linear expression of type \ref Expr.
deba@482
  1503
    ///
deba@482
  1504
    void obj(const Expr& e) {
deba@482
  1505
      _setObjCoeffs(ExprIterator(e.comps.begin(), cols),
deba@482
  1506
                    ExprIterator(e.comps.end(), cols));
deba@482
  1507
      obj_const_comp = *e;
deba@481
  1508
    }
deba@481
  1509
deba@481
  1510
    ///Get the objective function
deba@481
  1511
deba@482
  1512
    ///\return the objective function as a linear expression of type
deba@482
  1513
    ///Expr.
deba@481
  1514
    Expr obj() const {
deba@481
  1515
      Expr e;
deba@482
  1516
      _getObjCoeffs(InsertIterator(e.comps, cols));
deba@482
  1517
      *e = obj_const_comp;
deba@481
  1518
      return e;
deba@481
  1519
    }
deba@481
  1520
deba@481
  1521
deba@482
  1522
    ///Set the direction of optimization
deba@482
  1523
    void sense(Sense sense) { _setSense(sense); }
deba@481
  1524
deba@482
  1525
    ///Query the direction of the optimization
deba@482
  1526
    Sense sense() const {return _getSense(); }
deba@481
  1527
deba@482
  1528
    ///Set the sense to maximization
deba@482
  1529
    void max() { _setSense(MAX); }
deba@482
  1530
deba@482
  1531
    ///Set the sense to maximization
deba@482
  1532
    void min() { _setSense(MIN); }
deba@482
  1533
deba@482
  1534
    ///Clears the problem
deba@482
  1535
    void clear() { _clear(); }
deba@481
  1536
deba@481
  1537
    ///@}
deba@481
  1538
deba@482
  1539
  };
deba@482
  1540
deba@482
  1541
  /// Addition
deba@482
  1542
deba@482
  1543
  ///\relates LpBase::Expr
deba@482
  1544
  ///
deba@482
  1545
  inline LpBase::Expr operator+(const LpBase::Expr &a, const LpBase::Expr &b) {
deba@482
  1546
    LpBase::Expr tmp(a);
deba@482
  1547
    tmp+=b;
deba@482
  1548
    return tmp;
deba@482
  1549
  }
deba@482
  1550
  ///Substraction
deba@482
  1551
deba@482
  1552
  ///\relates LpBase::Expr
deba@482
  1553
  ///
deba@482
  1554
  inline LpBase::Expr operator-(const LpBase::Expr &a, const LpBase::Expr &b) {
deba@482
  1555
    LpBase::Expr tmp(a);
deba@482
  1556
    tmp-=b;
deba@482
  1557
    return tmp;
deba@482
  1558
  }
deba@482
  1559
  ///Multiply with constant
deba@482
  1560
deba@482
  1561
  ///\relates LpBase::Expr
deba@482
  1562
  ///
deba@482
  1563
  inline LpBase::Expr operator*(const LpBase::Expr &a, const LpBase::Value &b) {
deba@482
  1564
    LpBase::Expr tmp(a);
deba@482
  1565
    tmp*=b;
deba@482
  1566
    return tmp;
deba@482
  1567
  }
deba@482
  1568
deba@482
  1569
  ///Multiply with constant
deba@482
  1570
deba@482
  1571
  ///\relates LpBase::Expr
deba@482
  1572
  ///
deba@482
  1573
  inline LpBase::Expr operator*(const LpBase::Value &a, const LpBase::Expr &b) {
deba@482
  1574
    LpBase::Expr tmp(b);
deba@482
  1575
    tmp*=a;
deba@482
  1576
    return tmp;
deba@482
  1577
  }
deba@482
  1578
  ///Divide with constant
deba@482
  1579
deba@482
  1580
  ///\relates LpBase::Expr
deba@482
  1581
  ///
deba@482
  1582
  inline LpBase::Expr operator/(const LpBase::Expr &a, const LpBase::Value &b) {
deba@482
  1583
    LpBase::Expr tmp(a);
deba@482
  1584
    tmp/=b;
deba@482
  1585
    return tmp;
deba@482
  1586
  }
deba@482
  1587
deba@482
  1588
  ///Create constraint
deba@482
  1589
deba@482
  1590
  ///\relates LpBase::Constr
deba@482
  1591
  ///
deba@482
  1592
  inline LpBase::Constr operator<=(const LpBase::Expr &e,
deba@482
  1593
                                   const LpBase::Expr &f) {
deba@482
  1594
    return LpBase::Constr(0, f - e, LpBase::INF);
deba@482
  1595
  }
deba@482
  1596
deba@482
  1597
  ///Create constraint
deba@482
  1598
deba@482
  1599
  ///\relates LpBase::Constr
deba@482
  1600
  ///
deba@482
  1601
  inline LpBase::Constr operator<=(const LpBase::Value &e,
deba@482
  1602
                                   const LpBase::Expr &f) {
deba@482
  1603
    return LpBase::Constr(e, f, LpBase::NaN);
deba@482
  1604
  }
deba@482
  1605
deba@482
  1606
  ///Create constraint
deba@482
  1607
deba@482
  1608
  ///\relates LpBase::Constr
deba@482
  1609
  ///
deba@482
  1610
  inline LpBase::Constr operator<=(const LpBase::Expr &e,
deba@482
  1611
                                   const LpBase::Value &f) {
deba@482
  1612
    return LpBase::Constr(- LpBase::INF, e, f);
deba@482
  1613
  }
deba@482
  1614
deba@482
  1615
  ///Create constraint
deba@482
  1616
deba@482
  1617
  ///\relates LpBase::Constr
deba@482
  1618
  ///
deba@482
  1619
  inline LpBase::Constr operator>=(const LpBase::Expr &e,
deba@482
  1620
                                   const LpBase::Expr &f) {
deba@482
  1621
    return LpBase::Constr(0, e - f, LpBase::INF);
deba@482
  1622
  }
deba@482
  1623
deba@482
  1624
deba@482
  1625
  ///Create constraint
deba@482
  1626
deba@482
  1627
  ///\relates LpBase::Constr
deba@482
  1628
  ///
deba@482
  1629
  inline LpBase::Constr operator>=(const LpBase::Value &e,
deba@482
  1630
                                   const LpBase::Expr &f) {
deba@482
  1631
    return LpBase::Constr(LpBase::NaN, f, e);
deba@482
  1632
  }
deba@482
  1633
deba@482
  1634
deba@482
  1635
  ///Create constraint
deba@482
  1636
deba@482
  1637
  ///\relates LpBase::Constr
deba@482
  1638
  ///
deba@482
  1639
  inline LpBase::Constr operator>=(const LpBase::Expr &e,
deba@482
  1640
                                   const LpBase::Value &f) {
deba@482
  1641
    return LpBase::Constr(f, e, LpBase::INF);
deba@482
  1642
  }
deba@482
  1643
deba@482
  1644
  ///Create constraint
deba@482
  1645
deba@482
  1646
  ///\relates LpBase::Constr
deba@482
  1647
  ///
deba@482
  1648
  inline LpBase::Constr operator==(const LpBase::Expr &e,
deba@482
  1649
                                   const LpBase::Value &f) {
deba@482
  1650
    return LpBase::Constr(f, e, f);
deba@482
  1651
  }
deba@482
  1652
deba@482
  1653
  ///Create constraint
deba@482
  1654
deba@482
  1655
  ///\relates LpBase::Constr
deba@482
  1656
  ///
deba@482
  1657
  inline LpBase::Constr operator==(const LpBase::Expr &e,
deba@482
  1658
                                   const LpBase::Expr &f) {
deba@482
  1659
    return LpBase::Constr(0, f - e, 0);
deba@482
  1660
  }
deba@482
  1661
deba@482
  1662
  ///Create constraint
deba@482
  1663
deba@482
  1664
  ///\relates LpBase::Constr
deba@482
  1665
  ///
deba@482
  1666
  inline LpBase::Constr operator<=(const LpBase::Value &n,
deba@482
  1667
                                   const LpBase::Constr &c) {
deba@482
  1668
    LpBase::Constr tmp(c);
alpar@558
  1669
    LEMON_ASSERT(isNaN(tmp.lowerBound()), "Wrong LP constraint");
deba@482
  1670
    tmp.lowerBound()=n;
deba@482
  1671
    return tmp;
deba@482
  1672
  }
deba@482
  1673
  ///Create constraint
deba@482
  1674
deba@482
  1675
  ///\relates LpBase::Constr
deba@482
  1676
  ///
deba@482
  1677
  inline LpBase::Constr operator<=(const LpBase::Constr &c,
deba@482
  1678
                                   const LpBase::Value &n)
deba@482
  1679
  {
deba@482
  1680
    LpBase::Constr tmp(c);
alpar@558
  1681
    LEMON_ASSERT(isNaN(tmp.upperBound()), "Wrong LP constraint");
deba@482
  1682
    tmp.upperBound()=n;
deba@482
  1683
    return tmp;
deba@482
  1684
  }
deba@482
  1685
deba@482
  1686
  ///Create constraint
deba@482
  1687
deba@482
  1688
  ///\relates LpBase::Constr
deba@482
  1689
  ///
deba@482
  1690
  inline LpBase::Constr operator>=(const LpBase::Value &n,
deba@482
  1691
                                   const LpBase::Constr &c) {
deba@482
  1692
    LpBase::Constr tmp(c);
alpar@558
  1693
    LEMON_ASSERT(isNaN(tmp.upperBound()), "Wrong LP constraint");
deba@482
  1694
    tmp.upperBound()=n;
deba@482
  1695
    return tmp;
deba@482
  1696
  }
deba@482
  1697
  ///Create constraint
deba@482
  1698
deba@482
  1699
  ///\relates LpBase::Constr
deba@482
  1700
  ///
deba@482
  1701
  inline LpBase::Constr operator>=(const LpBase::Constr &c,
deba@482
  1702
                                   const LpBase::Value &n)
deba@482
  1703
  {
deba@482
  1704
    LpBase::Constr tmp(c);
alpar@558
  1705
    LEMON_ASSERT(isNaN(tmp.lowerBound()), "Wrong LP constraint");
deba@482
  1706
    tmp.lowerBound()=n;
deba@482
  1707
    return tmp;
deba@482
  1708
  }
deba@482
  1709
deba@482
  1710
  ///Addition
deba@482
  1711
deba@482
  1712
  ///\relates LpBase::DualExpr
deba@482
  1713
  ///
deba@482
  1714
  inline LpBase::DualExpr operator+(const LpBase::DualExpr &a,
deba@482
  1715
                                    const LpBase::DualExpr &b) {
deba@482
  1716
    LpBase::DualExpr tmp(a);
deba@482
  1717
    tmp+=b;
deba@482
  1718
    return tmp;
deba@482
  1719
  }
deba@482
  1720
  ///Substraction
deba@482
  1721
deba@482
  1722
  ///\relates LpBase::DualExpr
deba@482
  1723
  ///
deba@482
  1724
  inline LpBase::DualExpr operator-(const LpBase::DualExpr &a,
deba@482
  1725
                                    const LpBase::DualExpr &b) {
deba@482
  1726
    LpBase::DualExpr tmp(a);
deba@482
  1727
    tmp-=b;
deba@482
  1728
    return tmp;
deba@482
  1729
  }
deba@482
  1730
  ///Multiply with constant
deba@482
  1731
deba@482
  1732
  ///\relates LpBase::DualExpr
deba@482
  1733
  ///
deba@482
  1734
  inline LpBase::DualExpr operator*(const LpBase::DualExpr &a,
deba@482
  1735
                                    const LpBase::Value &b) {
deba@482
  1736
    LpBase::DualExpr tmp(a);
deba@482
  1737
    tmp*=b;
deba@482
  1738
    return tmp;
deba@482
  1739
  }
deba@482
  1740
deba@482
  1741
  ///Multiply with constant
deba@482
  1742
deba@482
  1743
  ///\relates LpBase::DualExpr
deba@482
  1744
  ///
deba@482
  1745
  inline LpBase::DualExpr operator*(const LpBase::Value &a,
deba@482
  1746
                                    const LpBase::DualExpr &b) {
deba@482
  1747
    LpBase::DualExpr tmp(b);
deba@482
  1748
    tmp*=a;
deba@482
  1749
    return tmp;
deba@482
  1750
  }
deba@482
  1751
  ///Divide with constant
deba@482
  1752
deba@482
  1753
  ///\relates LpBase::DualExpr
deba@482
  1754
  ///
deba@482
  1755
  inline LpBase::DualExpr operator/(const LpBase::DualExpr &a,
deba@482
  1756
                                    const LpBase::Value &b) {
deba@482
  1757
    LpBase::DualExpr tmp(a);
deba@482
  1758
    tmp/=b;
deba@482
  1759
    return tmp;
deba@482
  1760
  }
deba@482
  1761
deba@482
  1762
  /// \ingroup lp_group
deba@482
  1763
  ///
deba@482
  1764
  /// \brief Common base class for LP solvers
deba@482
  1765
  ///
deba@482
  1766
  /// This class is an abstract base class for LP solvers. This class
deba@482
  1767
  /// provides a full interface for set and modify an LP problem,
deba@482
  1768
  /// solve it and retrieve the solution. You can use one of the
deba@482
  1769
  /// descendants as a concrete implementation, or the \c Lp
deba@482
  1770
  /// default LP solver. However, if you would like to handle LP
deba@482
  1771
  /// solvers as reference or pointer in a generic way, you can use
deba@482
  1772
  /// this class directly.
deba@482
  1773
  class LpSolver : virtual public LpBase {
deba@482
  1774
  public:
deba@482
  1775
deba@482
  1776
    /// The problem types for primal and dual problems
deba@482
  1777
    enum ProblemType {
deba@482
  1778
      ///Feasible solution hasn't been found (but may exist).
deba@482
  1779
      UNDEFINED = 0,
deba@482
  1780
      ///The problem has no feasible solution
deba@482
  1781
      INFEASIBLE = 1,
deba@482
  1782
      ///Feasible solution found
deba@482
  1783
      FEASIBLE = 2,
deba@482
  1784
      ///Optimal solution exists and found
deba@482
  1785
      OPTIMAL = 3,
deba@482
  1786
      ///The cost function is unbounded
deba@482
  1787
      UNBOUNDED = 4
deba@482
  1788
    };
deba@482
  1789
deba@482
  1790
    ///The basis status of variables
deba@482
  1791
    enum VarStatus {
deba@482
  1792
      /// The variable is in the basis
deba@482
  1793
      BASIC, 
deba@482
  1794
      /// The variable is free, but not basic
deba@482
  1795
      FREE,
deba@482
  1796
      /// The variable has active lower bound 
deba@482
  1797
      LOWER,
deba@482
  1798
      /// The variable has active upper bound
deba@482
  1799
      UPPER,
deba@482
  1800
      /// The variable is non-basic and fixed
deba@482
  1801
      FIXED
deba@482
  1802
    };
deba@482
  1803
deba@482
  1804
  protected:
deba@482
  1805
deba@482
  1806
    virtual SolveExitStatus _solve() = 0;
deba@482
  1807
deba@482
  1808
    virtual Value _getPrimal(int i) const = 0;
deba@482
  1809
    virtual Value _getDual(int i) const = 0;
deba@482
  1810
deba@482
  1811
    virtual Value _getPrimalRay(int i) const = 0;
deba@482
  1812
    virtual Value _getDualRay(int i) const = 0;
deba@482
  1813
deba@482
  1814
    virtual Value _getPrimalValue() const = 0;
deba@482
  1815
deba@482
  1816
    virtual VarStatus _getColStatus(int i) const = 0;
deba@482
  1817
    virtual VarStatus _getRowStatus(int i) const = 0;
deba@482
  1818
deba@482
  1819
    virtual ProblemType _getPrimalType() const = 0;
deba@482
  1820
    virtual ProblemType _getDualType() const = 0;
deba@482
  1821
deba@482
  1822
  public:
deba@481
  1823
deba@481
  1824
    ///\name Solve the LP
deba@481
  1825
deba@481
  1826
    ///@{
deba@481
  1827
deba@481
  1828
    ///\e Solve the LP problem at hand
deba@481
  1829
    ///
deba@481
  1830
    ///\return The result of the optimization procedure. Possible
deba@481
  1831
    ///values and their meanings can be found in the documentation of
deba@481
  1832
    ///\ref SolveExitStatus.
deba@481
  1833
    SolveExitStatus solve() { return _solve(); }
deba@481
  1834
deba@481
  1835
    ///@}
deba@481
  1836
deba@481
  1837
    ///\name Obtain the solution
deba@481
  1838
deba@481
  1839
    ///@{
deba@481
  1840
deba@482
  1841
    /// The type of the primal problem
deba@482
  1842
    ProblemType primalType() const {
deba@482
  1843
      return _getPrimalType();
deba@481
  1844
    }
deba@481
  1845
deba@482
  1846
    /// The type of the dual problem
deba@482
  1847
    ProblemType dualType() const {
deba@482
  1848
      return _getDualType();
deba@481
  1849
    }
deba@481
  1850
deba@482
  1851
    /// Return the primal value of the column
deba@482
  1852
deba@482
  1853
    /// Return the primal value of the column.
deba@482
  1854
    /// \pre The problem is solved.
deba@482
  1855
    Value primal(Col c) const { return _getPrimal(cols(id(c))); }
deba@482
  1856
deba@482
  1857
    /// Return the primal value of the expression
deba@482
  1858
deba@482
  1859
    /// Return the primal value of the expression, i.e. the dot
deba@482
  1860
    /// product of the primal solution and the expression.
deba@482
  1861
    /// \pre The problem is solved.
deba@482
  1862
    Value primal(const Expr& e) const {
deba@482
  1863
      double res = *e;
deba@482
  1864
      for (Expr::ConstCoeffIt c(e); c != INVALID; ++c) {
deba@482
  1865
        res += *c * primal(c);
deba@482
  1866
      }
deba@482
  1867
      return res;
deba@481
  1868
    }
deba@482
  1869
    /// Returns a component of the primal ray
deba@482
  1870
    
deba@482
  1871
    /// The primal ray is solution of the modified primal problem,
deba@482
  1872
    /// where we change each finite bound to 0, and we looking for a
deba@482
  1873
    /// negative objective value in case of minimization, and positive
deba@482
  1874
    /// objective value for maximization. If there is such solution,
deba@482
  1875
    /// that proofs the unsolvability of the dual problem, and if a
deba@482
  1876
    /// feasible primal solution exists, then the unboundness of
deba@482
  1877
    /// primal problem.
deba@482
  1878
    ///
deba@482
  1879
    /// \pre The problem is solved and the dual problem is infeasible.
deba@482
  1880
    /// \note Some solvers does not provide primal ray calculation
deba@482
  1881
    /// functions.
deba@482
  1882
    Value primalRay(Col c) const { return _getPrimalRay(cols(id(c))); }
deba@481
  1883
deba@482
  1884
    /// Return the dual value of the row
deba@482
  1885
deba@482
  1886
    /// Return the dual value of the row.
deba@482
  1887
    /// \pre The problem is solved.
deba@482
  1888
    Value dual(Row r) const { return _getDual(rows(id(r))); }
deba@482
  1889
deba@482
  1890
    /// Return the dual value of the dual expression
deba@482
  1891
deba@482
  1892
    /// Return the dual value of the dual expression, i.e. the dot
deba@482
  1893
    /// product of the dual solution and the dual expression.
deba@482
  1894
    /// \pre The problem is solved.
deba@482
  1895
    Value dual(const DualExpr& e) const {
deba@482
  1896
      double res = 0.0;
deba@482
  1897
      for (DualExpr::ConstCoeffIt r(e); r != INVALID; ++r) {
deba@482
  1898
        res += *r * dual(r);
deba@481
  1899
      }
deba@481
  1900
      return res;
deba@481
  1901
    }
deba@481
  1902
deba@482
  1903
    /// Returns a component of the dual ray
deba@482
  1904
    
deba@482
  1905
    /// The dual ray is solution of the modified primal problem, where
deba@482
  1906
    /// we change each finite bound to 0 (i.e. the objective function
deba@482
  1907
    /// coefficients in the primal problem), and we looking for a
deba@482
  1908
    /// ositive objective value. If there is such solution, that
deba@482
  1909
    /// proofs the unsolvability of the primal problem, and if a
deba@482
  1910
    /// feasible dual solution exists, then the unboundness of
deba@482
  1911
    /// dual problem.
deba@482
  1912
    ///
deba@482
  1913
    /// \pre The problem is solved and the primal problem is infeasible.
deba@482
  1914
    /// \note Some solvers does not provide dual ray calculation
deba@482
  1915
    /// functions.
deba@482
  1916
    Value dualRay(Row r) const { return _getDualRay(rows(id(r))); }
deba@481
  1917
deba@482
  1918
    /// Return the basis status of the column
deba@481
  1919
deba@482
  1920
    /// \see VarStatus
deba@482
  1921
    VarStatus colStatus(Col c) const { return _getColStatus(cols(id(c))); }
deba@482
  1922
deba@482
  1923
    /// Return the basis status of the row
deba@482
  1924
deba@482
  1925
    /// \see VarStatus
deba@482
  1926
    VarStatus rowStatus(Row r) const { return _getRowStatus(rows(id(r))); }
deba@482
  1927
deba@482
  1928
    ///The value of the objective function
deba@481
  1929
deba@481
  1930
    ///\return
deba@481
  1931
    ///- \ref INF or -\ref INF means either infeasibility or unboundedness
deba@481
  1932
    /// of the primal problem, depending on whether we minimize or maximize.
deba@481
  1933
    ///- \ref NaN if no primal solution is found.
deba@481
  1934
    ///- The (finite) objective value if an optimal solution is found.
deba@482
  1935
    Value primal() const { return _getPrimalValue()+obj_const_comp;}
deba@481
  1936
    ///@}
deba@481
  1937
deba@482
  1938
    LpSolver* newSolver() {return _newSolver();}
deba@482
  1939
    LpSolver* cloneSolver() {return _cloneSolver();}
deba@482
  1940
deba@482
  1941
  protected:
deba@482
  1942
deba@482
  1943
    virtual LpSolver* _newSolver() const = 0;
deba@482
  1944
    virtual LpSolver* _cloneSolver() const = 0;
deba@481
  1945
  };
deba@481
  1946
deba@481
  1947
deba@481
  1948
  /// \ingroup lp_group
deba@481
  1949
  ///
deba@481
  1950
  /// \brief Common base class for MIP solvers
deba@482
  1951
  ///
deba@482
  1952
  /// This class is an abstract base class for MIP solvers. This class
deba@482
  1953
  /// provides a full interface for set and modify an MIP problem,
deba@482
  1954
  /// solve it and retrieve the solution. You can use one of the
deba@482
  1955
  /// descendants as a concrete implementation, or the \c Lp
deba@482
  1956
  /// default MIP solver. However, if you would like to handle MIP
deba@482
  1957
  /// solvers as reference or pointer in a generic way, you can use
deba@482
  1958
  /// this class directly.
deba@482
  1959
  class MipSolver : virtual public LpBase {
deba@481
  1960
  public:
deba@481
  1961
deba@482
  1962
    /// The problem types for MIP problems
deba@482
  1963
    enum ProblemType {
deba@482
  1964
      ///Feasible solution hasn't been found (but may exist).
deba@482
  1965
      UNDEFINED = 0,
deba@482
  1966
      ///The problem has no feasible solution
deba@482
  1967
      INFEASIBLE = 1,
deba@482
  1968
      ///Feasible solution found
deba@482
  1969
      FEASIBLE = 2,
deba@482
  1970
      ///Optimal solution exists and found
deba@482
  1971
      OPTIMAL = 3,
deba@482
  1972
      ///The cost function is unbounded
deba@482
  1973
      ///
deba@482
  1974
      ///The Mip or at least the relaxed problem is unbounded
deba@482
  1975
      UNBOUNDED = 4
deba@482
  1976
    };
deba@482
  1977
deba@482
  1978
    ///\name Solve the MIP
deba@482
  1979
deba@482
  1980
    ///@{
deba@482
  1981
deba@482
  1982
    /// Solve the MIP problem at hand
deba@482
  1983
    ///
deba@482
  1984
    ///\return The result of the optimization procedure. Possible
deba@482
  1985
    ///values and their meanings can be found in the documentation of
deba@482
  1986
    ///\ref SolveExitStatus.
deba@482
  1987
    SolveExitStatus solve() { return _solve(); }
deba@482
  1988
deba@482
  1989
    ///@}
deba@482
  1990
deba@482
  1991
    ///\name Setting column type
deba@482
  1992
    ///@{
deba@482
  1993
deba@482
  1994
    ///Possible variable (column) types (e.g. real, integer, binary etc.)
deba@481
  1995
    enum ColTypes {
deba@482
  1996
      ///Continuous variable (default)
deba@481
  1997
      REAL = 0,
deba@481
  1998
      ///Integer variable
deba@482
  1999
      INTEGER = 1
deba@481
  2000
    };
deba@481
  2001
deba@482
  2002
    ///Sets the type of the given column to the given type
deba@482
  2003
deba@482
  2004
    ///Sets the type of the given column to the given type.
deba@481
  2005
    ///
deba@481
  2006
    void colType(Col c, ColTypes col_type) {
deba@482
  2007
      _setColType(cols(id(c)),col_type);
deba@481
  2008
    }
deba@481
  2009
deba@481
  2010
    ///Gives back the type of the column.
deba@482
  2011
deba@482
  2012
    ///Gives back the type of the column.
deba@481
  2013
    ///
deba@481
  2014
    ColTypes colType(Col c) const {
deba@482
  2015
      return _getColType(cols(id(c)));
deba@482
  2016
    }
deba@482
  2017
    ///@}
deba@482
  2018
deba@482
  2019
    ///\name Obtain the solution
deba@482
  2020
deba@482
  2021
    ///@{
deba@482
  2022
deba@482
  2023
    /// The type of the MIP problem
deba@482
  2024
    ProblemType type() const {
deba@482
  2025
      return _getType();
deba@481
  2026
    }
deba@481
  2027
deba@482
  2028
    /// Return the value of the row in the solution
deba@482
  2029
deba@482
  2030
    ///  Return the value of the row in the solution.
deba@482
  2031
    /// \pre The problem is solved.
deba@482
  2032
    Value sol(Col c) const { return _getSol(cols(id(c))); }
deba@482
  2033
deba@482
  2034
    /// Return the value of the expression in the solution
deba@482
  2035
deba@482
  2036
    /// Return the value of the expression in the solution, i.e. the
deba@482
  2037
    /// dot product of the solution and the expression.
deba@482
  2038
    /// \pre The problem is solved.
deba@482
  2039
    Value sol(const Expr& e) const {
deba@482
  2040
      double res = *e;
deba@482
  2041
      for (Expr::ConstCoeffIt c(e); c != INVALID; ++c) {
deba@482
  2042
        res += *c * sol(c);
deba@482
  2043
      }
deba@482
  2044
      return res;
deba@481
  2045
    }
deba@482
  2046
    ///The value of the objective function
deba@482
  2047
    
deba@482
  2048
    ///\return
deba@482
  2049
    ///- \ref INF or -\ref INF means either infeasibility or unboundedness
deba@482
  2050
    /// of the problem, depending on whether we minimize or maximize.
deba@482
  2051
    ///- \ref NaN if no primal solution is found.
deba@482
  2052
    ///- The (finite) objective value if an optimal solution is found.
deba@482
  2053
    Value solValue() const { return _getSolValue()+obj_const_comp;}
deba@482
  2054
    ///@}
deba@481
  2055
deba@481
  2056
  protected:
deba@481
  2057
deba@482
  2058
    virtual SolveExitStatus _solve() = 0;
deba@482
  2059
    virtual ColTypes _getColType(int col) const = 0;
deba@482
  2060
    virtual void _setColType(int col, ColTypes col_type) = 0;
deba@482
  2061
    virtual ProblemType _getType() const = 0;
deba@482
  2062
    virtual Value _getSol(int i) const = 0;
deba@482
  2063
    virtual Value _getSolValue() const = 0;
deba@481
  2064
deba@482
  2065
  public:
deba@482
  2066
deba@482
  2067
    MipSolver* newSolver() {return _newSolver();}
deba@482
  2068
    MipSolver* cloneSolver() {return _cloneSolver();}
deba@482
  2069
deba@482
  2070
  protected:
deba@482
  2071
deba@482
  2072
    virtual MipSolver* _newSolver() const = 0;
deba@482
  2073
    virtual MipSolver* _cloneSolver() const = 0;
deba@481
  2074
  };
deba@481
  2075
deba@481
  2076
deba@481
  2077
deba@481
  2078
} //namespace lemon
deba@481
  2079
deba@481
  2080
#endif //LEMON_LP_BASE_H