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