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