gravatar
deba@inf.elte.hu
deba@inf.elte.hu
Merge bugfix #337 to branch 1.1
0 1 0
merge 1.1
0 files changed with 23 insertions and 12 deletions:
↑ Collapse diff ↑
Ignore white space 96 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_GLPK_H
20 20
#define LEMON_GLPK_H
21 21

	
22 22
///\file
23 23
///\brief Header of the LEMON-GLPK lp solver interface.
24 24
///\ingroup lp_group
25 25

	
26 26
#include <lemon/lp_base.h>
27 27

	
28
// forward declaration
29
#if !defined _GLP_PROB && !defined GLP_PROB
30
#define _GLP_PROB
31
#define GLP_PROB
32
typedef struct { double _opaque_prob; } glp_prob;
33
/* LP/MIP problem object */
34
#endif
35

	
36 28
namespace lemon {
37 29

	
30
  namespace _solver_bits {
31
    class VoidPtr {
32
    private:
33
      void *_ptr;      
34
    public:
35
      VoidPtr() : _ptr(0) {}
36

	
37
      template <typename T>
38
      VoidPtr(T* ptr) : _ptr(reinterpret_cast<void*>(ptr)) {}
39

	
40
      template <typename T>
41
      VoidPtr& operator=(T* ptr) { 
42
        _ptr = reinterpret_cast<void*>(ptr); 
43
        return *this;
44
      }
45

	
46
      template <typename T>
47
      operator T*() const { return reinterpret_cast<T*>(_ptr); }
48
    };
49
  }
38 50

	
39 51
  /// \brief Base interface for the GLPK LP and MIP solver
40 52
  ///
41 53
  /// This class implements the common interface of the GLPK LP and MIP solver.
42 54
  /// \ingroup lp_group
43 55
  class GlpkBase : virtual public LpBase {
44 56
  protected:
45 57

	
46
    typedef glp_prob LPX;
47
    glp_prob* lp;
58
    _solver_bits::VoidPtr lp;
48 59

	
49 60
    GlpkBase();
50 61
    GlpkBase(const GlpkBase&);
51 62
    virtual ~GlpkBase();
52 63

	
53 64
  protected:
54 65

	
55 66
    virtual int _addCol();
56 67
    virtual int _addRow();
57 68

	
58 69
    virtual void _eraseCol(int i);
59 70
    virtual void _eraseRow(int i);
60 71

	
61 72
    virtual void _eraseColId(int i);
62 73
    virtual void _eraseRowId(int i);
63 74

	
64 75
    virtual void _getColName(int col, std::string& name) const;
65 76
    virtual void _setColName(int col, const std::string& name);
66 77
    virtual int _colByName(const std::string& name) const;
67 78

	
68 79
    virtual void _getRowName(int row, std::string& name) const;
69 80
    virtual void _setRowName(int row, const std::string& name);
70 81
    virtual int _rowByName(const std::string& name) const;
71 82

	
72 83
    virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
73 84
    virtual void _getRowCoeffs(int i, InsertIterator b) const;
74 85

	
75 86
    virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
76 87
    virtual void _getColCoeffs(int i, InsertIterator b) const;
77 88

	
78 89
    virtual void _setCoeff(int row, int col, Value value);
79 90
    virtual Value _getCoeff(int row, int col) const;
80 91

	
81 92
    virtual void _setColLowerBound(int i, Value value);
82 93
    virtual Value _getColLowerBound(int i) const;
83 94

	
84 95
    virtual void _setColUpperBound(int i, Value value);
85 96
    virtual Value _getColUpperBound(int i) const;
86 97

	
87 98
    virtual void _setRowLowerBound(int i, Value value);
88 99
    virtual Value _getRowLowerBound(int i) const;
89 100

	
90 101
    virtual void _setRowUpperBound(int i, Value value);
91 102
    virtual Value _getRowUpperBound(int i) const;
92 103

	
93 104
    virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
94 105
    virtual void _getObjCoeffs(InsertIterator b) const;
95 106

	
96 107
    virtual void _setObjCoeff(int i, Value obj_coef);
97 108
    virtual Value _getObjCoeff(int i) const;
98 109

	
99 110
    virtual void _setSense(Sense);
100 111
    virtual Sense _getSense() const;
101 112

	
102 113
    virtual void _clear();
103 114

	
104 115
    virtual void _messageLevel(MessageLevel level);
105 116

	
106 117
  private:
107 118

	
108 119
    static void freeEnv();
109 120

	
110 121
    struct FreeEnvHelper {
111 122
      ~FreeEnvHelper() {
112 123
        freeEnv();
113 124
      }
114 125
    };
115 126
    
116 127
    static FreeEnvHelper freeEnvHelper;
117 128

	
118 129
  protected:
119 130
    
120 131
    int _message_level;
121 132
    
122 133
  public:
123 134

	
124 135
    ///Pointer to the underlying GLPK data structure.
125
    LPX *lpx() {return lp;}
136
    _solver_bits::VoidPtr lpx() {return lp;}
126 137
    ///Const pointer to the underlying GLPK data structure.
127
    const LPX *lpx() const {return lp;}
138
    _solver_bits::VoidPtr lpx() const {return lp;}
128 139

	
129 140
    ///Returns the constraint identifier understood by GLPK.
130 141
    int lpxRow(Row r) const { return rows(id(r)); }
131 142

	
132 143
    ///Returns the variable identifier understood by GLPK.
133 144
    int lpxCol(Col c) const { return cols(id(c)); }
134 145

	
135 146
  };
136 147

	
137 148
  /// \brief Interface for the GLPK LP solver
138 149
  ///
139 150
  /// This class implements an interface for the GLPK LP solver.
140 151
  ///\ingroup lp_group
141 152
  class GlpkLp : public LpSolver, public GlpkBase {
142 153
  public:
143 154

	
144 155
    ///\e
145 156
    GlpkLp();
146 157
    ///\e
147 158
    GlpkLp(const GlpkLp&);
148 159

	
149 160
    ///\e
150 161
    virtual GlpkLp* cloneSolver() const;
151 162
    ///\e
152 163
    virtual GlpkLp* newSolver() const;
153 164

	
154 165
  private:
155 166

	
156 167
    mutable std::vector<double> _primal_ray;
157 168
    mutable std::vector<double> _dual_ray;
158 169

	
159 170
    void _clear_temporals();
160 171

	
161 172
  protected:
162 173

	
163 174
    virtual const char* _solverName() const;
164 175

	
165 176
    virtual SolveExitStatus _solve();
166 177
    virtual Value _getPrimal(int i) const;
167 178
    virtual Value _getDual(int i) const;
168 179

	
169 180
    virtual Value _getPrimalValue() const;
170 181

	
171 182
    virtual VarStatus _getColStatus(int i) const;
172 183
    virtual VarStatus _getRowStatus(int i) const;
173 184

	
174 185
    virtual Value _getPrimalRay(int i) const;
175 186
    virtual Value _getDualRay(int i) const;
0 comments (0 inline)