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