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