lemon/lp_base.h
author Peter Madarasi <madarasip@caesar.elte.hu>
Mon, 30 Mar 2015 17:42:30 +0200
changeset 1141 a037254714b3
parent 1092 dceba191c00d
child 1144 760a5f690163
permissions -rw-r--r--
VF2 algorithm added (#597)

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