src/work/marci/lp/lp_solver_wrapper_3.h
author alpar
Fri, 28 Jan 2005 15:19:34 +0000
changeset 1105 6777f0b0e7b5
parent 1100 299d1127a846
child 1110 ba28dfbea5f2
permissions -rw-r--r--
One more trial/approach for named params (Under constr.)
marci@1031
     1
// -*- c++ -*-
marci@1031
     2
#ifndef LEMON_LP_SOLVER_WRAPPER_H
marci@1031
     3
#define LEMON_LP_SOLVER_WRAPPER_H
marci@1031
     4
marci@1031
     5
///\ingroup misc
marci@1031
     6
///\file
marci@1031
     7
///\brief Dijkstra algorithm.
marci@1031
     8
marci@1031
     9
// #include <stdio.h>
marci@1031
    10
#include <stdlib.h>
marci@1097
    11
#include <iostream>
marci@1097
    12
#include <map>
marci@1104
    13
#include <limits>
marci@1031
    14
// #include <stdio>
marci@1031
    15
//#include <stdlib>
marci@1031
    16
extern "C" {
marci@1031
    17
#include "glpk.h"
marci@1031
    18
}
marci@1031
    19
marci@1031
    20
#include <iostream>
marci@1031
    21
#include <vector>
marci@1031
    22
#include <string>
marci@1031
    23
#include <list>
marci@1031
    24
#include <memory>
marci@1031
    25
#include <utility>
marci@1031
    26
marci@1031
    27
//#include <sage_graph.h>
marci@1031
    28
//#include <lemon/list_graph.h>
marci@1031
    29
//#include <lemon/graph_wrapper.h>
marci@1031
    30
#include <lemon/invalid.h>
marci@1099
    31
#include <expression.h>
marci@1031
    32
//#include <bfs_dfs.h>
marci@1031
    33
//#include <stp.h>
marci@1031
    34
//#include <lemon/max_flow.h>
marci@1031
    35
//#include <augmenting_flow.h>
marci@1031
    36
//#include <iter_map.h>
marci@1031
    37
marci@1031
    38
using std::cout;
marci@1031
    39
using std::cin;
marci@1031
    40
using std::endl;
marci@1031
    41
marci@1031
    42
namespace lemon {
marci@1031
    43
  
marci@1031
    44
  /// \addtogroup misc
marci@1031
    45
  /// @{
marci@1031
    46
marci@1031
    47
  /// \brief A partitioned vector with iterable classes.
marci@1031
    48
  ///
marci@1031
    49
  /// This class implements a container in which the data is stored in an 
marci@1031
    50
  /// stl vector, the range is partitioned into sets and each set is 
marci@1031
    51
  /// doubly linked in a list. 
marci@1031
    52
  /// That is, each class is iterable by lemon iterators, and any member of 
marci@1031
    53
  /// the vector can bo moved to an other class.
marci@1031
    54
  template <typename T>
marci@1031
    55
  class IterablePartition {
marci@1031
    56
  protected:
marci@1031
    57
    struct Node {
marci@1031
    58
      T data;
marci@1031
    59
      int prev; //invalid az -1
marci@1031
    60
      int next; 
marci@1031
    61
    };
marci@1031
    62
    std::vector<Node> nodes;
marci@1031
    63
    struct Tip {
marci@1031
    64
      int first;
marci@1031
    65
      int last;
marci@1031
    66
    };
marci@1031
    67
    std::vector<Tip> tips;
marci@1031
    68
  public:
marci@1031
    69
    /// The classes are indexed by integers from \c 0 to \c classNum()-1.
marci@1031
    70
    int classNum() const { return tips.size(); }
marci@1031
    71
    /// This lemon style iterator iterates through a class. 
marci@1031
    72
    class ClassIt;
marci@1031
    73
    /// Constructor. The number of classes is to be given which is fixed 
marci@1031
    74
    /// over the life of the container. 
marci@1031
    75
    /// The partition classes are indexed from 0 to class_num-1. 
marci@1031
    76
    IterablePartition(int class_num) { 
marci@1031
    77
      for (int i=0; i<class_num; ++i) {
marci@1031
    78
	Tip t;
marci@1031
    79
	t.first=t.last=-1;
marci@1031
    80
	tips.push_back(t);
marci@1031
    81
      }
marci@1031
    82
    }
marci@1031
    83
  protected:
marci@1031
    84
    void befuz(ClassIt it, int class_id) {
marci@1031
    85
      if (tips[class_id].first==-1) {
marci@1031
    86
	if (tips[class_id].last==-1) {
marci@1031
    87
	  nodes[it.i].prev=nodes[it.i].next=-1;
marci@1031
    88
	  tips[class_id].first=tips[class_id].last=it.i;
marci@1031
    89
	}
marci@1031
    90
      } else {
marci@1031
    91
	nodes[it.i].prev=tips[class_id].last;
marci@1031
    92
	nodes[it.i].next=-1;
marci@1031
    93
	nodes[tips[class_id].last].next=it.i;
marci@1031
    94
	tips[class_id].last=it.i;
marci@1031
    95
      }
marci@1031
    96
    }
marci@1031
    97
    void kifuz(ClassIt it, int class_id) {
marci@1031
    98
      if (tips[class_id].first==it.i) {
marci@1031
    99
	if (tips[class_id].last==it.i) {
marci@1031
   100
	  tips[class_id].first=tips[class_id].last=-1;
marci@1031
   101
	} else {
marci@1031
   102
	  tips[class_id].first=nodes[it.i].next;
marci@1031
   103
	  nodes[nodes[it.i].next].prev=-1;
marci@1031
   104
	}
marci@1031
   105
      } else {
marci@1031
   106
	if (tips[class_id].last==it.i) {
marci@1031
   107
	  tips[class_id].last=nodes[it.i].prev;
marci@1031
   108
	  nodes[nodes[it.i].prev].next=-1;
marci@1031
   109
	} else {
marci@1031
   110
	  nodes[nodes[it.i].next].prev=nodes[it.i].prev;
marci@1031
   111
	  nodes[nodes[it.i].prev].next=nodes[it.i].next;
marci@1031
   112
	}
marci@1031
   113
      }
marci@1031
   114
    }
marci@1031
   115
  public:
marci@1031
   116
    /// A new element with data \c t is pushed into the vector and into class 
marci@1031
   117
    /// \c class_id.
marci@1031
   118
    ClassIt push_back(const T& t, int class_id) { 
marci@1031
   119
      Node n;
marci@1031
   120
      n.data=t;
marci@1031
   121
      nodes.push_back(n);
marci@1031
   122
      int i=nodes.size()-1;
marci@1031
   123
      befuz(i, class_id);
marci@1031
   124
      return i;
marci@1031
   125
    }
marci@1031
   126
    /// A member is moved to an other class.
marci@1031
   127
    void set(ClassIt it, int old_class_id, int new_class_id) {
marci@1031
   128
      kifuz(it.i, old_class_id);
marci@1031
   129
      befuz(it.i, new_class_id);
marci@1031
   130
    }
marci@1031
   131
    /// Returns the data pointed by \c it.
marci@1031
   132
    T& operator[](ClassIt it) { return nodes[it.i].data; }
marci@1031
   133
    /// Returns the data pointed by \c it.
marci@1031
   134
    const T& operator[](ClassIt it) const { return nodes[it.i].data; }
marci@1031
   135
    ///.
marci@1031
   136
    class ClassIt {
marci@1031
   137
      friend class IterablePartition;
marci@1031
   138
    protected:
marci@1031
   139
      int i;
marci@1031
   140
    public:
marci@1031
   141
      /// Default constructor.
marci@1031
   142
      ClassIt() { }
marci@1031
   143
      /// This constructor constructs an iterator which points
marci@1031
   144
      /// to the member of th container indexed by the integer _i.
marci@1031
   145
      ClassIt(const int& _i) : i(_i) { }
marci@1031
   146
      /// Invalid constructor.
marci@1031
   147
      ClassIt(const Invalid&) : i(-1) { }
marci@1099
   148
      friend bool operator<(const ClassIt& x, const ClassIt& y);
marci@1099
   149
      friend std::ostream& operator<<(std::ostream& os, 
marci@1099
   150
				      const ClassIt& it);
marci@1031
   151
    };
marci@1099
   152
    friend bool operator<(const ClassIt& x, const ClassIt& y) {
marci@1099
   153
      return (x.i < y.i);
marci@1099
   154
    }
marci@1099
   155
    friend std::ostream& operator<<(std::ostream& os, 
marci@1099
   156
				    const ClassIt& it) {
marci@1099
   157
      os << it.i;
marci@1099
   158
      return os;
marci@1099
   159
    }
marci@1031
   160
    /// First member of class \c class_id.
marci@1031
   161
    ClassIt& first(ClassIt& it, int class_id) const {
marci@1031
   162
      it.i=tips[class_id].first;
marci@1031
   163
      return it;
marci@1031
   164
    }
marci@1031
   165
    /// Next member.
marci@1031
   166
    ClassIt& next(ClassIt& it) const {
marci@1031
   167
      it.i=nodes[it.i].next;
marci@1031
   168
      return it;
marci@1031
   169
    }
marci@1031
   170
    /// True iff the iterator is valid.
marci@1031
   171
    bool valid(const ClassIt& it) const { return it.i!=-1; }
marci@1031
   172
  };
marci@1031
   173
marci@1097
   174
marci@1031
   175
  /*! \e
alpar@1100
   176
alpar@1100
   177
  \todo A[x,y]-t cserel. Jobboldal, baloldal csere.
alpar@1100
   178
  \todo LEKERDEZESEK!!!
alpar@1100
   179
  \todo DOKSI!!!! Doxygen group!!!
alpar@1100
   180
marci@1031
   181
   */
marci@1048
   182
  template <typename _Value>
marci@1031
   183
  class LPSolverBase {
marci@1031
   184
  public:
marci@1031
   185
    /// \e
marci@1048
   186
    typedef _Value Value;
marci@1048
   187
    /// \e
marci@1031
   188
    typedef IterablePartition<int>::ClassIt RowIt;
marci@1031
   189
    /// \e
marci@1031
   190
    typedef IterablePartition<int>::ClassIt ColIt;
marci@1074
   191
  public:
marci@1031
   192
    /// \e
marci@1031
   193
    IterablePartition<int> row_iter_map;
marci@1031
   194
    /// \e
marci@1031
   195
    IterablePartition<int> col_iter_map;
marci@1031
   196
    /// \e
marci@1074
   197
    const int VALID_CLASS;
marci@1031
   198
    /// \e
marci@1074
   199
    const int INVALID_CLASS;
marci@1104
   200
    /// \e 
marci@1104
   201
    static const _Value INF;
marci@1031
   202
  public:
marci@1031
   203
    /// \e
marci@1031
   204
    LPSolverBase() : row_iter_map(2), 
marci@1031
   205
		     col_iter_map(2), 
marci@1074
   206
		     VALID_CLASS(0), INVALID_CLASS(1) { }
marci@1031
   207
    /// \e
marci@1031
   208
    virtual ~LPSolverBase() { }
marci@1081
   209
marci@1081
   210
    //MATRIX INDEPEDENT MANIPULATING FUNCTIONS
marci@1081
   211
marci@1081
   212
  public:
marci@1031
   213
    /// \e
marci@1031
   214
    virtual void setMinimize() = 0;
marci@1031
   215
    /// \e
marci@1031
   216
    virtual void setMaximize() = 0;
marci@1081
   217
marci@1081
   218
    //LOW LEVEL INTERFACE, MATRIX MANIPULATING FUNCTIONS
marci@1081
   219
marci@1074
   220
  protected:
marci@1031
   221
    /// \e
marci@1074
   222
    virtual int _addRow() = 0;
marci@1031
   223
    /// \e
marci@1074
   224
    virtual int _addCol() = 0;
marci@1081
   225
    /// \e
marci@1081
   226
    virtual void _setRowCoeffs(int i, 
marci@1104
   227
			       const std::vector<std::pair<int, _Value> >& coeffs) = 0;
marci@1081
   228
    /// \e
marci@1081
   229
    virtual void _setColCoeffs(int i, 
marci@1104
   230
			       const std::vector<std::pair<int, _Value> >& coeffs) = 0;
marci@1081
   231
    /// \e
marci@1081
   232
    virtual void _eraseCol(int i) = 0;
marci@1081
   233
    /// \e
marci@1081
   234
    virtual void _eraseRow(int i) = 0;
marci@1081
   235
  public:
marci@1081
   236
    /// \e
marci@1081
   237
    enum Bound { FREE, LOWER, UPPER, DOUBLE, FIXED };
marci@1081
   238
  protected:
marci@1081
   239
    /// \e
marci@1081
   240
    virtual void _setColBounds(int i, Bound bound, 
marci@1081
   241
			       _Value lo, _Value up) = 0; 
marci@1081
   242
    /// \e
marci@1081
   243
    virtual void _setRowBounds(int i, Bound bound, 
marci@1081
   244
			       _Value lo, _Value up) = 0; 
marci@1081
   245
    /// \e
marci@1081
   246
    virtual void _setObjCoef(int i, _Value obj_coef) = 0;
marci@1081
   247
    /// \e
marci@1081
   248
    virtual _Value _getObjCoef(int i) = 0;
marci@1081
   249
marci@1081
   250
    //LOW LEVEL, SOLUTION RETRIEVING FUNCTIONS
marci@1081
   251
marci@1081
   252
  protected:
marci@1081
   253
    virtual _Value _getPrimal(int i) = 0;
marci@1081
   254
marci@1081
   255
    //HIGH LEVEL INTERFACE, MATRIX MANIPULATING FUNTIONS
marci@1081
   256
marci@1074
   257
  public:
marci@1074
   258
    /// \e
marci@1074
   259
    RowIt addRow() {
marci@1074
   260
      int i=_addRow(); 
marci@1074
   261
      RowIt row_it;
marci@1074
   262
      row_iter_map.first(row_it, INVALID_CLASS);
marci@1074
   263
      if (row_iter_map.valid(row_it)) { //van hasznalhato hely
marci@1074
   264
	row_iter_map.set(row_it, INVALID_CLASS, VALID_CLASS);
marci@1074
   265
	row_iter_map[row_it]=i;
marci@1074
   266
      } else { //a cucc vegere kell inzertalni mert nincs szabad hely
marci@1074
   267
	row_it=row_iter_map.push_back(i, VALID_CLASS);
marci@1074
   268
      }
marci@1074
   269
      return row_it;
marci@1074
   270
    }
marci@1074
   271
    /// \e
marci@1074
   272
    ColIt addCol() {
marci@1074
   273
      int i=_addCol();  
marci@1074
   274
      ColIt col_it;
marci@1074
   275
      col_iter_map.first(col_it, INVALID_CLASS);
marci@1074
   276
      if (col_iter_map.valid(col_it)) { //van hasznalhato hely
marci@1074
   277
	col_iter_map.set(col_it, INVALID_CLASS, VALID_CLASS);
marci@1074
   278
	col_iter_map[col_it]=i;
marci@1074
   279
      } else { //a cucc vegere kell inzertalni mert nincs szabad hely
marci@1074
   280
	col_it=col_iter_map.push_back(i, VALID_CLASS);
marci@1074
   281
      }
marci@1074
   282
      return col_it;
marci@1074
   283
    }
marci@1074
   284
    /// \e
marci@1031
   285
    template <typename Begin, typename End>
marci@1031
   286
    void setRowCoeffs(RowIt row_it, Begin begin, End end) {
marci@1074
   287
      std::vector<std::pair<int, double> > coeffs;
marci@1031
   288
      for ( ; begin!=end; ++begin) {
marci@1074
   289
	coeffs.push_back(std::
marci@1074
   290
			 make_pair(col_iter_map[begin->first], begin->second));
marci@1031
   291
      }
marci@1081
   292
      _setRowCoeffs(row_iter_map[row_it], coeffs);
marci@1031
   293
    }
marci@1031
   294
    /// \e
marci@1031
   295
    template <typename Begin, typename End>
marci@1031
   296
    void setColCoeffs(ColIt col_it, Begin begin, End end) {
marci@1074
   297
      std::vector<std::pair<int, double> > coeffs;
marci@1031
   298
      for ( ; begin!=end; ++begin) {
marci@1074
   299
	coeffs.push_back(std::
marci@1074
   300
			 make_pair(row_iter_map[begin->first], begin->second));
marci@1031
   301
      }
marci@1081
   302
      _setColCoeffs(col_iter_map[col_it], coeffs);
marci@1074
   303
    }
marci@1074
   304
    /// \e
marci@1074
   305
    void eraseCol(const ColIt& col_it) {
marci@1074
   306
      col_iter_map.set(col_it, VALID_CLASS, INVALID_CLASS);
marci@1074
   307
      int cols[2];
marci@1074
   308
      cols[1]=col_iter_map[col_it];
marci@1074
   309
      _eraseCol(cols[1]);
marci@1074
   310
      col_iter_map[col_it]=0; //glpk specifikus, de kell ez??
marci@1074
   311
      ColIt it;
marci@1074
   312
      for (col_iter_map.first(it, VALID_CLASS); 
marci@1074
   313
	   col_iter_map.valid(it); col_iter_map.next(it)) {
marci@1074
   314
	if (col_iter_map[it]>cols[1]) --col_iter_map[it];
marci@1074
   315
      }
marci@1031
   316
    }
marci@1031
   317
    /// \e
marci@1074
   318
    void eraseRow(const RowIt& row_it) {
marci@1074
   319
      row_iter_map.set(row_it, VALID_CLASS, INVALID_CLASS);
marci@1074
   320
      int rows[2];
marci@1074
   321
      rows[1]=row_iter_map[row_it];
marci@1074
   322
      _eraseRow(rows[1]);
marci@1074
   323
      row_iter_map[row_it]=0; //glpk specifikus, de kell ez??
marci@1074
   324
      RowIt it;
marci@1074
   325
      for (row_iter_map.first(it, VALID_CLASS); 
marci@1074
   326
	   row_iter_map.valid(it); row_iter_map.next(it)) {
marci@1074
   327
	if (row_iter_map[it]>rows[1]) --row_iter_map[it];
marci@1074
   328
      }
marci@1074
   329
    }
marci@1031
   330
    /// \e
marci@1081
   331
    void setColBounds(const ColIt& col_it, Bound bound, 
marci@1081
   332
		      _Value lo, _Value up) {
marci@1081
   333
      _setColBounds(col_iter_map[col_it], bound, lo, up);
marci@1081
   334
    }
marci@1031
   335
    /// \e
marci@1081
   336
    void setRowBounds(const RowIt& row_it, Bound bound, 
marci@1081
   337
		      _Value lo, _Value up) {
marci@1081
   338
      _setRowBounds(row_iter_map[row_it], bound, lo, up);
marci@1081
   339
    }
marci@1031
   340
    /// \e
marci@1081
   341
    void setObjCoef(const ColIt& col_it, _Value obj_coef) {
marci@1081
   342
      _setObjCoef(col_iter_map[col_it], obj_coef);
marci@1081
   343
    }
marci@1031
   344
    /// \e
marci@1081
   345
    _Value getObjCoef(const ColIt& col_it) {
marci@1081
   346
      return _getObjCoef(col_iter_map[col_it]);
marci@1081
   347
    }
marci@1081
   348
marci@1099
   349
    //MOST HIGH LEVEL, USER FRIEND FUNCTIONS
marci@1099
   350
marci@1099
   351
    /// \e
marci@1099
   352
    typedef Expr<ColIt, _Value> Expression;
marci@1099
   353
    /// \e
marci@1099
   354
    typedef Expr<RowIt, _Value> DualExpression;
marci@1099
   355
    /// \e
marci@1099
   356
    void setRowCoeffs(RowIt row_it, const Expression& expr) {
marci@1099
   357
      std::vector<std::pair<int, _Value> > row_coeffs;
marci@1099
   358
      for(typename Expression::Data::const_iterator i=expr.data.begin(); 
marci@1099
   359
	  i!=expr.data.end(); ++i) {
marci@1099
   360
	row_coeffs.push_back(std::make_pair
marci@1099
   361
			     (col_iter_map[(*i).first], (*i).second));
marci@1099
   362
      }
marci@1099
   363
      _setRowCoeffs(row_iter_map[row_it], row_coeffs);
marci@1099
   364
    }
marci@1099
   365
    /// \e
marci@1099
   366
    void setColCoeffs(ColIt col_it, const DualExpression& expr) {
marci@1099
   367
      std::vector<std::pair<int, _Value> > col_coeffs;
marci@1099
   368
      for(typename DualExpression::Data::const_iterator i=expr.data.begin(); 
marci@1099
   369
	  i!=expr.data.end(); ++i) {
marci@1099
   370
	col_coeffs.push_back(std::make_pair
marci@1099
   371
			     (row_iter_map[(*i).first], (*i).second));
marci@1099
   372
      }
marci@1099
   373
      _setColCoeffs(col_iter_map[col_it], col_coeffs);
marci@1099
   374
    }
marci@1099
   375
    /// \e
marci@1099
   376
    void setObjCoeffs(const Expression& expr) {
marci@1099
   377
      for(typename Expression::Data::const_iterator i=expr.data.begin(); 
marci@1099
   378
	  i!=expr.data.end(); ++i) {
marci@1099
   379
	setObjCoef((*i).first, (*i).second);
marci@1099
   380
      }
marci@1099
   381
    }
marci@1081
   382
    //SOLVER FUNCTIONS
marci@1081
   383
marci@1031
   384
    /// \e
marci@1031
   385
    virtual void solveSimplex() = 0;
marci@1031
   386
    /// \e
marci@1031
   387
    virtual void solvePrimalSimplex() = 0;
marci@1031
   388
    /// \e
marci@1031
   389
    virtual void solveDualSimplex() = 0;
marci@1031
   390
    /// \e
marci@1081
   391
marci@1081
   392
    //HIGH LEVEL, SOLUTION RETRIEVING FUNCTIONS
marci@1081
   393
marci@1081
   394
  public:
marci@1081
   395
    _Value getPrimal(const ColIt& col_it) {
marci@1081
   396
      return _getPrimal(col_iter_map[col_it]);
marci@1081
   397
    }
marci@1031
   398
    /// \e
marci@1048
   399
    virtual _Value getObjVal() = 0;
marci@1081
   400
marci@1081
   401
    //OTHER FUNCTIONS
marci@1081
   402
marci@1031
   403
    /// \e
marci@1031
   404
    virtual int rowNum() const = 0;
marci@1031
   405
    /// \e
marci@1031
   406
    virtual int colNum() const = 0;
marci@1031
   407
    /// \e
marci@1031
   408
    virtual int warmUp() = 0;
marci@1031
   409
    /// \e
marci@1031
   410
    virtual void printWarmUpStatus(int i) = 0;
marci@1031
   411
    /// \e
marci@1031
   412
    virtual int getPrimalStatus() = 0;
marci@1031
   413
    /// \e
marci@1031
   414
    virtual void printPrimalStatus(int i) = 0;
marci@1031
   415
    /// \e
marci@1031
   416
    virtual int getDualStatus() = 0;
marci@1031
   417
    /// \e
marci@1031
   418
    virtual void printDualStatus(int i) = 0;
marci@1031
   419
    /// Returns the status of the slack variable assigned to row \c row_it.
marci@1031
   420
    virtual int getRowStat(const RowIt& row_it) = 0;
marci@1031
   421
    /// \e
marci@1031
   422
    virtual void printRowStatus(int i) = 0;
marci@1031
   423
    /// Returns the status of the variable assigned to column \c col_it.
marci@1031
   424
    virtual int getColStat(const ColIt& col_it) = 0;
marci@1031
   425
    /// \e
marci@1031
   426
    virtual void printColStatus(int i) = 0;
marci@1031
   427
  };
marci@1031
   428
  
marci@1104
   429
  template <typename _Value>
marci@1104
   430
  const _Value LPSolverBase<_Value>::INF=std::numeric_limits<_Value>::infinity();
marci@1104
   431
marci@1048
   432
marci@1031
   433
  /// \brief Wrappers for LP solvers
marci@1031
   434
  /// 
marci@1031
   435
  /// This class implements a lemon wrapper for glpk.
marci@1031
   436
  /// Later other LP-solvers will be wrapped into lemon.
marci@1031
   437
  /// The aim of this class is to give a general surface to different 
marci@1031
   438
  /// solvers, i.e. it makes possible to write algorithms using LP's, 
marci@1031
   439
  /// in which the solver can be changed to an other one easily.
marci@1081
   440
  class LPGLPK : public LPSolverBase<double> {
marci@1031
   441
  public:
marci@1048
   442
    typedef LPSolverBase<double> Parent;
marci@1031
   443
marci@1031
   444
  public:
marci@1031
   445
    /// \e
marci@1031
   446
    LPX* lp;
marci@1031
   447
marci@1031
   448
  public:
marci@1031
   449
    /// \e
marci@1081
   450
    LPGLPK() : Parent(), 
marci@1031
   451
			lp(lpx_create_prob()) {
marci@1031
   452
      lpx_set_int_parm(lp, LPX_K_DUAL, 1);
marci@1031
   453
    }
marci@1031
   454
    /// \e
marci@1081
   455
    ~LPGLPK() {
marci@1031
   456
      lpx_delete_prob(lp);
marci@1031
   457
    }
marci@1081
   458
marci@1081
   459
    //MATRIX INDEPEDENT MANIPULATING FUNCTIONS
marci@1081
   460
marci@1031
   461
    /// \e
marci@1031
   462
    void setMinimize() { 
marci@1031
   463
      lpx_set_obj_dir(lp, LPX_MIN);
marci@1031
   464
    }
marci@1031
   465
    /// \e
marci@1031
   466
    void setMaximize() { 
marci@1031
   467
      lpx_set_obj_dir(lp, LPX_MAX);
marci@1031
   468
    }
marci@1081
   469
marci@1081
   470
    //LOW LEVEL INTERFACE, MATRIX MANIPULATING FUNCTIONS
marci@1081
   471
marci@1074
   472
  protected:
marci@1031
   473
    /// \e
marci@1074
   474
    int _addCol() { 
marci@1074
   475
      return lpx_add_cols(lp, 1);
marci@1031
   476
    }
marci@1031
   477
    /// \e
marci@1074
   478
    int _addRow() { 
marci@1074
   479
      return lpx_add_rows(lp, 1);
marci@1074
   480
    }
marci@1074
   481
    /// \e
marci@1081
   482
    virtual void _setRowCoeffs(int i, 
marci@1104
   483
			       const std::vector<std::pair<int, double> >& coeffs) {
marci@1074
   484
      int mem_length=1+colNum();
marci@1074
   485
      int* indices = new int[mem_length];
marci@1074
   486
      double* doubles = new double[mem_length];
marci@1074
   487
      int length=0;
marci@1074
   488
      for (std::vector<std::pair<int, double> >::
marci@1074
   489
	     const_iterator it=coeffs.begin(); it!=coeffs.end(); ++it) {
marci@1074
   490
	++length;
marci@1074
   491
	indices[length]=it->first;
marci@1074
   492
	doubles[length]=it->second;
marci@1081
   493
// 	std::cout << "  " << indices[length] << " " 
marci@1081
   494
// 		  << doubles[length] << std::endl;
marci@1031
   495
      }
marci@1081
   496
//      std::cout << i << " " << length << std::endl;
marci@1074
   497
      lpx_set_mat_row(lp, i, length, indices, doubles);
marci@1074
   498
      delete [] indices;
marci@1074
   499
      delete [] doubles;
marci@1031
   500
    }
marci@1074
   501
    /// \e
marci@1081
   502
    virtual void _setColCoeffs(int i, 
marci@1104
   503
			       const std::vector<std::pair<int, double> >& coeffs) {
marci@1074
   504
      int mem_length=1+rowNum();
marci@1074
   505
      int* indices = new int[mem_length];
marci@1074
   506
      double* doubles = new double[mem_length];
marci@1074
   507
      int length=0;
marci@1074
   508
      for (std::vector<std::pair<int, double> >::
marci@1074
   509
	     const_iterator it=coeffs.begin(); it!=coeffs.end(); ++it) {
marci@1074
   510
	++length;
marci@1074
   511
	indices[length]=it->first;
marci@1074
   512
	doubles[length]=it->second;
marci@1074
   513
      }
marci@1074
   514
      lpx_set_mat_col(lp, i, length, indices, doubles);
marci@1074
   515
      delete [] indices;
marci@1074
   516
      delete [] doubles;
marci@1031
   517
    }
marci@1031
   518
    /// \e
marci@1074
   519
    virtual void _eraseCol(int i) {
marci@1031
   520
      int cols[2];
marci@1074
   521
      cols[1]=i;
marci@1031
   522
      lpx_del_cols(lp, 1, cols);
marci@1031
   523
    }
marci@1074
   524
    virtual void _eraseRow(int i) {
marci@1031
   525
      int rows[2];
marci@1074
   526
      rows[1]=i;
marci@1031
   527
      lpx_del_rows(lp, 1, rows);
marci@1031
   528
    }
marci@1081
   529
    virtual void _setColBounds(int i, Bound bound, 
marci@1081
   530
			       double lo, double up) {
marci@1081
   531
      switch (bound) {
marci@1081
   532
      case FREE:
marci@1081
   533
	lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
marci@1081
   534
	break;
marci@1081
   535
      case LOWER:
marci@1081
   536
	lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
marci@1081
   537
	break;
marci@1081
   538
      case UPPER:
marci@1081
   539
	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
marci@1081
   540
	break;
marci@1081
   541
      case DOUBLE:
marci@1081
   542
	lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
marci@1081
   543
	break;
marci@1081
   544
      case FIXED:
marci@1081
   545
	lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
marci@1081
   546
	break;
marci@1081
   547
      }
marci@1081
   548
    } 
marci@1081
   549
    virtual void _setRowBounds(int i, Bound bound, 
marci@1081
   550
			       double lo, double up) {
marci@1081
   551
      switch (bound) {
marci@1081
   552
      case FREE:
marci@1081
   553
	lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
marci@1081
   554
	break;
marci@1081
   555
      case LOWER:
marci@1081
   556
	lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
marci@1081
   557
	break;
marci@1081
   558
      case UPPER:
marci@1081
   559
	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
marci@1081
   560
	break;
marci@1081
   561
      case DOUBLE:
marci@1081
   562
	lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
marci@1081
   563
	break;
marci@1081
   564
      case FIXED:
marci@1081
   565
	lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
marci@1081
   566
	break;
marci@1081
   567
      }
marci@1081
   568
    } 
marci@1081
   569
  protected:
marci@1031
   570
    /// \e
marci@1081
   571
    virtual double _getObjCoef(int i) { 
marci@1081
   572
      return lpx_get_obj_coef(lp, i);
marci@1031
   573
    }
marci@1031
   574
    /// \e
marci@1081
   575
    virtual void _setObjCoef(int i, double obj_coef) { 
marci@1081
   576
      lpx_set_obj_coef(lp, i, obj_coef);
marci@1031
   577
    }
marci@1081
   578
  public:
marci@1031
   579
    /// \e
marci@1031
   580
    void solveSimplex() { lpx_simplex(lp); }
marci@1031
   581
    /// \e
marci@1031
   582
    void solvePrimalSimplex() { lpx_simplex(lp); }
marci@1031
   583
    /// \e
marci@1031
   584
    void solveDualSimplex() { lpx_simplex(lp); }
marci@1031
   585
    /// \e
marci@1081
   586
  protected:
marci@1081
   587
    virtual double _getPrimal(int i) {
marci@1081
   588
      return lpx_get_col_prim(lp, i);
marci@1031
   589
    }
marci@1081
   590
  public:
marci@1031
   591
    /// \e
marci@1031
   592
    double getObjVal() { return lpx_get_obj_val(lp); }
marci@1031
   593
    /// \e
marci@1031
   594
    int rowNum() const { return lpx_get_num_rows(lp); }
marci@1031
   595
    /// \e
marci@1031
   596
    int colNum() const { return lpx_get_num_cols(lp); }
marci@1031
   597
    /// \e
marci@1031
   598
    int warmUp() { return lpx_warm_up(lp); }
marci@1031
   599
    /// \e
marci@1031
   600
    void printWarmUpStatus(int i) {
marci@1031
   601
      switch (i) {
marci@1031
   602
      case LPX_E_OK: cout << "LPX_E_OK" << endl; break;
marci@1031
   603
      case LPX_E_EMPTY: cout << "LPX_E_EMPTY" << endl; break;	
marci@1031
   604
      case LPX_E_BADB: cout << "LPX_E_BADB" << endl; break;
marci@1031
   605
      case LPX_E_SING: cout << "LPX_E_SING" << endl; break;
marci@1031
   606
      }
marci@1031
   607
    }
marci@1031
   608
    /// \e
marci@1031
   609
    int getPrimalStatus() { return lpx_get_prim_stat(lp); }
marci@1031
   610
    /// \e
marci@1031
   611
    void printPrimalStatus(int i) {
marci@1031
   612
      switch (i) {
marci@1031
   613
      case LPX_P_UNDEF: cout << "LPX_P_UNDEF" << endl; break;
marci@1031
   614
      case LPX_P_FEAS: cout << "LPX_P_FEAS" << endl; break;	
marci@1031
   615
      case LPX_P_INFEAS: cout << "LPX_P_INFEAS" << endl; break;
marci@1031
   616
      case LPX_P_NOFEAS: cout << "LPX_P_NOFEAS" << endl; break;
marci@1031
   617
      }
marci@1031
   618
    }
marci@1031
   619
    /// \e
marci@1031
   620
    int getDualStatus() { return lpx_get_dual_stat(lp); }
marci@1031
   621
    /// \e
marci@1031
   622
    void printDualStatus(int i) {
marci@1031
   623
      switch (i) {
marci@1031
   624
      case LPX_D_UNDEF: cout << "LPX_D_UNDEF" << endl; break;
marci@1031
   625
      case LPX_D_FEAS: cout << "LPX_D_FEAS" << endl; break;	
marci@1031
   626
      case LPX_D_INFEAS: cout << "LPX_D_INFEAS" << endl; break;
marci@1031
   627
      case LPX_D_NOFEAS: cout << "LPX_D_NOFEAS" << endl; break;
marci@1031
   628
      }
marci@1031
   629
    }
marci@1031
   630
    /// Returns the status of the slack variable assigned to row \c row_it.
marci@1031
   631
    int getRowStat(const RowIt& row_it) { 
marci@1031
   632
      return lpx_get_row_stat(lp, row_iter_map[row_it]); 
marci@1031
   633
    }
marci@1031
   634
    /// \e
marci@1031
   635
    void printRowStatus(int i) {
marci@1031
   636
      switch (i) {
marci@1031
   637
      case LPX_BS: cout << "LPX_BS" << endl; break;
marci@1031
   638
      case LPX_NL: cout << "LPX_NL" << endl; break;	
marci@1031
   639
      case LPX_NU: cout << "LPX_NU" << endl; break;
marci@1031
   640
      case LPX_NF: cout << "LPX_NF" << endl; break;
marci@1031
   641
      case LPX_NS: cout << "LPX_NS" << endl; break;
marci@1031
   642
      }
marci@1031
   643
    }
marci@1031
   644
    /// Returns the status of the variable assigned to column \c col_it.
marci@1031
   645
    int getColStat(const ColIt& col_it) { 
marci@1031
   646
      return lpx_get_col_stat(lp, col_iter_map[col_it]); 
marci@1031
   647
    }
marci@1031
   648
    /// \e
marci@1031
   649
    void printColStatus(int i) {
marci@1031
   650
      switch (i) {
marci@1031
   651
      case LPX_BS: cout << "LPX_BS" << endl; break;
marci@1031
   652
      case LPX_NL: cout << "LPX_NL" << endl; break;	
marci@1031
   653
      case LPX_NU: cout << "LPX_NU" << endl; break;
marci@1031
   654
      case LPX_NF: cout << "LPX_NF" << endl; break;
marci@1031
   655
      case LPX_NS: cout << "LPX_NS" << endl; break;
marci@1031
   656
      }
marci@1031
   657
    }
marci@1031
   658
  };
marci@1031
   659
  
marci@1031
   660
  /// @}
marci@1031
   661
marci@1031
   662
} //namespace lemon
marci@1031
   663
marci@1031
   664
#endif //LEMON_LP_SOLVER_WRAPPER_H