marci@1031
|
1 |
// -*- c++ -*-
|
marci@1152
|
2 |
#ifndef LEMON_LP_SOLVER_BASE_H
|
marci@1152
|
3 |
#define LEMON_LP_SOLVER_BASE_H
|
marci@1031
|
4 |
|
marci@1031
|
5 |
///\ingroup misc
|
marci@1031
|
6 |
///\file
|
marci@1031
|
7 |
|
marci@1031
|
8 |
// #include <stdio.h>
|
marci@1031
|
9 |
#include <stdlib.h>
|
marci@1097
|
10 |
#include <iostream>
|
marci@1097
|
11 |
#include <map>
|
marci@1104
|
12 |
#include <limits>
|
marci@1031
|
13 |
// #include <stdio>
|
marci@1031
|
14 |
//#include <stdlib>
|
marci@1031
|
15 |
extern "C" {
|
marci@1031
|
16 |
#include "glpk.h"
|
marci@1031
|
17 |
}
|
marci@1031
|
18 |
|
marci@1031
|
19 |
#include <iostream>
|
marci@1031
|
20 |
#include <vector>
|
marci@1031
|
21 |
#include <string>
|
marci@1031
|
22 |
#include <list>
|
marci@1031
|
23 |
#include <memory>
|
marci@1031
|
24 |
#include <utility>
|
marci@1031
|
25 |
|
marci@1031
|
26 |
#include <lemon/invalid.h>
|
marci@1099
|
27 |
#include <expression.h>
|
marci@1031
|
28 |
//#include <stp.h>
|
marci@1031
|
29 |
//#include <lemon/max_flow.h>
|
marci@1031
|
30 |
//#include <augmenting_flow.h>
|
marci@1031
|
31 |
//#include <iter_map.h>
|
marci@1031
|
32 |
|
marci@1031
|
33 |
using std::cout;
|
marci@1031
|
34 |
using std::cin;
|
marci@1031
|
35 |
using std::endl;
|
marci@1031
|
36 |
|
marci@1031
|
37 |
namespace lemon {
|
marci@1031
|
38 |
|
marci@1031
|
39 |
/// \addtogroup misc
|
marci@1031
|
40 |
/// @{
|
marci@1031
|
41 |
|
marci@1031
|
42 |
/// \brief A partitioned vector with iterable classes.
|
marci@1031
|
43 |
///
|
marci@1031
|
44 |
/// This class implements a container in which the data is stored in an
|
marci@1031
|
45 |
/// stl vector, the range is partitioned into sets and each set is
|
marci@1031
|
46 |
/// doubly linked in a list.
|
marci@1031
|
47 |
/// That is, each class is iterable by lemon iterators, and any member of
|
marci@1031
|
48 |
/// the vector can bo moved to an other class.
|
marci@1031
|
49 |
template <typename T>
|
marci@1031
|
50 |
class IterablePartition {
|
marci@1031
|
51 |
protected:
|
marci@1031
|
52 |
struct Node {
|
marci@1031
|
53 |
T data;
|
marci@1031
|
54 |
int prev; //invalid az -1
|
marci@1031
|
55 |
int next;
|
marci@1031
|
56 |
};
|
marci@1031
|
57 |
std::vector<Node> nodes;
|
marci@1031
|
58 |
struct Tip {
|
marci@1031
|
59 |
int first;
|
marci@1031
|
60 |
int last;
|
marci@1031
|
61 |
};
|
marci@1031
|
62 |
std::vector<Tip> tips;
|
marci@1031
|
63 |
public:
|
marci@1031
|
64 |
/// The classes are indexed by integers from \c 0 to \c classNum()-1.
|
marci@1031
|
65 |
int classNum() const { return tips.size(); }
|
marci@1031
|
66 |
/// This lemon style iterator iterates through a class.
|
marci@1152
|
67 |
class Class;
|
marci@1031
|
68 |
/// Constructor. The number of classes is to be given which is fixed
|
marci@1031
|
69 |
/// over the life of the container.
|
marci@1031
|
70 |
/// The partition classes are indexed from 0 to class_num-1.
|
marci@1031
|
71 |
IterablePartition(int class_num) {
|
marci@1031
|
72 |
for (int i=0; i<class_num; ++i) {
|
marci@1031
|
73 |
Tip t;
|
marci@1031
|
74 |
t.first=t.last=-1;
|
marci@1031
|
75 |
tips.push_back(t);
|
marci@1031
|
76 |
}
|
marci@1031
|
77 |
}
|
marci@1031
|
78 |
protected:
|
marci@1152
|
79 |
void befuz(Class it, int class_id) {
|
marci@1031
|
80 |
if (tips[class_id].first==-1) {
|
marci@1031
|
81 |
if (tips[class_id].last==-1) {
|
marci@1031
|
82 |
nodes[it.i].prev=nodes[it.i].next=-1;
|
marci@1031
|
83 |
tips[class_id].first=tips[class_id].last=it.i;
|
marci@1031
|
84 |
}
|
marci@1031
|
85 |
} else {
|
marci@1031
|
86 |
nodes[it.i].prev=tips[class_id].last;
|
marci@1031
|
87 |
nodes[it.i].next=-1;
|
marci@1031
|
88 |
nodes[tips[class_id].last].next=it.i;
|
marci@1031
|
89 |
tips[class_id].last=it.i;
|
marci@1031
|
90 |
}
|
marci@1031
|
91 |
}
|
marci@1152
|
92 |
void kifuz(Class it, int class_id) {
|
marci@1031
|
93 |
if (tips[class_id].first==it.i) {
|
marci@1031
|
94 |
if (tips[class_id].last==it.i) {
|
marci@1031
|
95 |
tips[class_id].first=tips[class_id].last=-1;
|
marci@1031
|
96 |
} else {
|
marci@1031
|
97 |
tips[class_id].first=nodes[it.i].next;
|
marci@1031
|
98 |
nodes[nodes[it.i].next].prev=-1;
|
marci@1031
|
99 |
}
|
marci@1031
|
100 |
} else {
|
marci@1031
|
101 |
if (tips[class_id].last==it.i) {
|
marci@1031
|
102 |
tips[class_id].last=nodes[it.i].prev;
|
marci@1031
|
103 |
nodes[nodes[it.i].prev].next=-1;
|
marci@1031
|
104 |
} else {
|
marci@1031
|
105 |
nodes[nodes[it.i].next].prev=nodes[it.i].prev;
|
marci@1031
|
106 |
nodes[nodes[it.i].prev].next=nodes[it.i].next;
|
marci@1031
|
107 |
}
|
marci@1031
|
108 |
}
|
marci@1031
|
109 |
}
|
marci@1031
|
110 |
public:
|
marci@1031
|
111 |
/// A new element with data \c t is pushed into the vector and into class
|
marci@1031
|
112 |
/// \c class_id.
|
marci@1152
|
113 |
Class push_back(const T& t, int class_id) {
|
marci@1031
|
114 |
Node n;
|
marci@1031
|
115 |
n.data=t;
|
marci@1031
|
116 |
nodes.push_back(n);
|
marci@1031
|
117 |
int i=nodes.size()-1;
|
marci@1031
|
118 |
befuz(i, class_id);
|
marci@1031
|
119 |
return i;
|
marci@1031
|
120 |
}
|
marci@1031
|
121 |
/// A member is moved to an other class.
|
marci@1152
|
122 |
void set(Class it, int old_class_id, int new_class_id) {
|
marci@1031
|
123 |
kifuz(it.i, old_class_id);
|
marci@1031
|
124 |
befuz(it.i, new_class_id);
|
marci@1031
|
125 |
}
|
marci@1031
|
126 |
/// Returns the data pointed by \c it.
|
marci@1152
|
127 |
T& operator[](Class it) { return nodes[it.i].data; }
|
marci@1031
|
128 |
/// Returns the data pointed by \c it.
|
marci@1152
|
129 |
const T& operator[](Class it) const { return nodes[it.i].data; }
|
marci@1031
|
130 |
///.
|
marci@1152
|
131 |
class Class {
|
marci@1031
|
132 |
friend class IterablePartition;
|
marci@1031
|
133 |
protected:
|
marci@1031
|
134 |
int i;
|
marci@1031
|
135 |
public:
|
marci@1031
|
136 |
/// Default constructor.
|
marci@1152
|
137 |
Class() { }
|
marci@1031
|
138 |
/// This constructor constructs an iterator which points
|
marci@1031
|
139 |
/// to the member of th container indexed by the integer _i.
|
marci@1152
|
140 |
Class(const int& _i) : i(_i) { }
|
marci@1031
|
141 |
/// Invalid constructor.
|
marci@1152
|
142 |
Class(const Invalid&) : i(-1) { }
|
marci@1152
|
143 |
friend bool operator<(const Class& x, const Class& y);
|
marci@1099
|
144 |
friend std::ostream& operator<<(std::ostream& os,
|
marci@1152
|
145 |
const Class& it);
|
marci@1152
|
146 |
bool operator==(const Class& node) const {return i == node.i;}
|
marci@1152
|
147 |
bool operator!=(const Class& node) const {return i != node.i;}
|
marci@1031
|
148 |
};
|
marci@1152
|
149 |
friend bool operator<(const Class& x, const Class& y) {
|
marci@1099
|
150 |
return (x.i < y.i);
|
marci@1099
|
151 |
}
|
marci@1099
|
152 |
friend std::ostream& operator<<(std::ostream& os,
|
marci@1152
|
153 |
const Class& it) {
|
marci@1099
|
154 |
os << it.i;
|
marci@1099
|
155 |
return os;
|
marci@1099
|
156 |
}
|
marci@1031
|
157 |
/// First member of class \c class_id.
|
marci@1152
|
158 |
Class& first(Class& it, int class_id) const {
|
marci@1031
|
159 |
it.i=tips[class_id].first;
|
marci@1031
|
160 |
return it;
|
marci@1031
|
161 |
}
|
marci@1031
|
162 |
/// Next member.
|
marci@1152
|
163 |
Class& next(Class& it) const {
|
marci@1031
|
164 |
it.i=nodes[it.i].next;
|
marci@1031
|
165 |
return it;
|
marci@1031
|
166 |
}
|
marci@1031
|
167 |
/// True iff the iterator is valid.
|
marci@1152
|
168 |
bool valid(const Class& it) const { return it.i!=-1; }
|
marci@1152
|
169 |
|
marci@1152
|
170 |
class ClassIt : public Class {
|
marci@1152
|
171 |
const IterablePartition* iterable_partition;
|
marci@1152
|
172 |
public:
|
marci@1152
|
173 |
ClassIt() { }
|
marci@1152
|
174 |
ClassIt(Invalid i) : Class(i) { }
|
marci@1152
|
175 |
ClassIt(const IterablePartition& _iterable_partition,
|
marci@1152
|
176 |
const int& i) : iterable_partition(&_iterable_partition) {
|
marci@1152
|
177 |
_iterable_partition.first(*this, i);
|
marci@1152
|
178 |
}
|
marci@1152
|
179 |
ClassIt(const IterablePartition& _iterable_partition,
|
marci@1152
|
180 |
const Class& _class) :
|
marci@1152
|
181 |
Class(_class), iterable_partition(&_iterable_partition) { }
|
marci@1152
|
182 |
ClassIt& operator++() {
|
marci@1152
|
183 |
iterable_partition->next(*this);
|
marci@1152
|
184 |
return *this;
|
marci@1152
|
185 |
}
|
marci@1152
|
186 |
};
|
marci@1152
|
187 |
|
marci@1031
|
188 |
};
|
marci@1031
|
189 |
|
marci@1097
|
190 |
|
marci@1031
|
191 |
/*! \e
|
marci@1143
|
192 |
\todo kellenene uj iterable structure bele, mert ez nem az igazi
|
marci@1111
|
193 |
\todo A[x,y]-t cserel. Jobboldal, baloldal csere.
|
marci@1111
|
194 |
\todo LEKERDEZESEK!!!
|
marci@1111
|
195 |
\todo DOKSI!!!! Doxygen group!!!
|
marci@1111
|
196 |
The aim of this class is to give a general surface to different
|
marci@1111
|
197 |
solvers, i.e. it makes possible to write algorithms using LP's,
|
marci@1111
|
198 |
in which the solver can be changed to an other one easily.
|
marci@1112
|
199 |
\nosubgrouping
|
marci@1111
|
200 |
*/
|
marci@1048
|
201 |
template <typename _Value>
|
marci@1031
|
202 |
class LPSolverBase {
|
marci@1112
|
203 |
|
marci@1113
|
204 |
/*! @name Uncategorized functions and types (public members)
|
marci@1112
|
205 |
*/
|
marci@1112
|
206 |
//@{
|
marci@1031
|
207 |
public:
|
marci@1112
|
208 |
|
marci@1112
|
209 |
//UNCATEGORIZED
|
marci@1112
|
210 |
|
marci@1031
|
211 |
/// \e
|
marci@1152
|
212 |
typedef IterablePartition<int> Rows;
|
marci@1152
|
213 |
/// \e
|
marci@1152
|
214 |
typedef IterablePartition<int> Cols;
|
marci@1152
|
215 |
/// \e
|
marci@1048
|
216 |
typedef _Value Value;
|
marci@1048
|
217 |
/// \e
|
marci@1152
|
218 |
typedef Rows::Class Row;
|
marci@1031
|
219 |
/// \e
|
marci@1152
|
220 |
typedef Cols::Class Col;
|
marci@1074
|
221 |
public:
|
marci@1031
|
222 |
/// \e
|
marci@1031
|
223 |
IterablePartition<int> row_iter_map;
|
marci@1031
|
224 |
/// \e
|
marci@1031
|
225 |
IterablePartition<int> col_iter_map;
|
marci@1031
|
226 |
/// \e
|
marci@1144
|
227 |
std::vector<Row> int_row_map;
|
marci@1143
|
228 |
/// \e
|
marci@1144
|
229 |
std::vector<Col> int_col_map;
|
marci@1143
|
230 |
/// \e
|
marci@1074
|
231 |
const int VALID_CLASS;
|
marci@1031
|
232 |
/// \e
|
marci@1074
|
233 |
const int INVALID_CLASS;
|
marci@1104
|
234 |
/// \e
|
marci@1104
|
235 |
static const _Value INF;
|
marci@1031
|
236 |
public:
|
marci@1031
|
237 |
/// \e
|
marci@1031
|
238 |
LPSolverBase() : row_iter_map(2),
|
marci@1031
|
239 |
col_iter_map(2),
|
marci@1074
|
240 |
VALID_CLASS(0), INVALID_CLASS(1) { }
|
marci@1031
|
241 |
/// \e
|
marci@1031
|
242 |
virtual ~LPSolverBase() { }
|
marci@1112
|
243 |
//@}
|
marci@1081
|
244 |
|
marci@1112
|
245 |
/*! @name Medium level interface (public members)
|
marci@1112
|
246 |
These functions appear in the low level and also in the high level
|
marci@1112
|
247 |
interfaces thus these each of these functions have to be implemented
|
marci@1112
|
248 |
only once in the different interfaces.
|
marci@1112
|
249 |
This means that these functions have to be reimplemented for all of the
|
marci@1112
|
250 |
different lp solvers. These are basic functions, and have the same
|
marci@1112
|
251 |
parameter lists in the low and high level interfaces.
|
marci@1112
|
252 |
*/
|
marci@1112
|
253 |
//@{
|
marci@1112
|
254 |
public:
|
marci@1081
|
255 |
|
marci@1112
|
256 |
//UNCATEGORIZED FUNCTIONS
|
marci@1112
|
257 |
|
marci@1031
|
258 |
/// \e
|
marci@1031
|
259 |
virtual void setMinimize() = 0;
|
marci@1031
|
260 |
/// \e
|
marci@1031
|
261 |
virtual void setMaximize() = 0;
|
marci@1081
|
262 |
|
marci@1112
|
263 |
//SOLVER FUNCTIONS
|
marci@1081
|
264 |
|
marci@1112
|
265 |
/// \e
|
marci@1112
|
266 |
virtual void solveSimplex() = 0;
|
marci@1112
|
267 |
/// \e
|
marci@1112
|
268 |
virtual void solvePrimalSimplex() = 0;
|
marci@1112
|
269 |
/// \e
|
marci@1112
|
270 |
virtual void solveDualSimplex() = 0;
|
marci@1112
|
271 |
|
marci@1112
|
272 |
//SOLUTION RETRIEVING
|
marci@1112
|
273 |
|
marci@1112
|
274 |
/// \e
|
marci@1112
|
275 |
virtual _Value getObjVal() = 0;
|
marci@1112
|
276 |
|
marci@1112
|
277 |
//OTHER FUNCTIONS
|
marci@1112
|
278 |
|
marci@1112
|
279 |
/// \e
|
marci@1112
|
280 |
virtual int rowNum() const = 0;
|
marci@1112
|
281 |
/// \e
|
marci@1112
|
282 |
virtual int colNum() const = 0;
|
marci@1112
|
283 |
/// \e
|
marci@1112
|
284 |
virtual int warmUp() = 0;
|
marci@1112
|
285 |
/// \e
|
marci@1112
|
286 |
virtual void printWarmUpStatus(int i) = 0;
|
marci@1112
|
287 |
/// \e
|
marci@1112
|
288 |
virtual int getPrimalStatus() = 0;
|
marci@1112
|
289 |
/// \e
|
marci@1112
|
290 |
virtual void printPrimalStatus(int i) = 0;
|
marci@1112
|
291 |
/// \e
|
marci@1112
|
292 |
virtual int getDualStatus() = 0;
|
marci@1112
|
293 |
/// \e
|
marci@1112
|
294 |
virtual void printDualStatus(int i) = 0;
|
marci@1144
|
295 |
/// Returns the status of the slack variable assigned to row \c row.
|
marci@1144
|
296 |
virtual int getRowStat(const Row& row) = 0;
|
marci@1112
|
297 |
/// \e
|
marci@1112
|
298 |
virtual void printRowStatus(int i) = 0;
|
marci@1144
|
299 |
/// Returns the status of the variable assigned to column \c col.
|
marci@1144
|
300 |
virtual int getColStat(const Col& col) = 0;
|
marci@1112
|
301 |
/// \e
|
marci@1112
|
302 |
virtual void printColStatus(int i) = 0;
|
marci@1112
|
303 |
|
marci@1112
|
304 |
//@}
|
marci@1112
|
305 |
|
marci@1112
|
306 |
/*! @name Low level interface (protected members)
|
marci@1112
|
307 |
Problem manipulating functions in the low level interface
|
marci@1112
|
308 |
*/
|
marci@1112
|
309 |
//@{
|
marci@1074
|
310 |
protected:
|
marci@1112
|
311 |
|
marci@1112
|
312 |
//MATRIX MANIPULATING FUNCTIONS
|
marci@1112
|
313 |
|
marci@1031
|
314 |
/// \e
|
marci@1111
|
315 |
virtual int _addCol() = 0;
|
marci@1111
|
316 |
/// \e
|
marci@1074
|
317 |
virtual int _addRow() = 0;
|
marci@1031
|
318 |
/// \e
|
marci@1111
|
319 |
virtual void _eraseCol(int i) = 0;
|
marci@1111
|
320 |
/// \e
|
marci@1111
|
321 |
virtual void _eraseRow(int i) = 0;
|
marci@1081
|
322 |
/// \e
|
marci@1081
|
323 |
virtual void _setRowCoeffs(int i,
|
marci@1104
|
324 |
const std::vector<std::pair<int, _Value> >& coeffs) = 0;
|
marci@1081
|
325 |
/// \e
|
marci@1143
|
326 |
/// This routine modifies \c coeffs only by the \c push_back method.
|
marci@1143
|
327 |
virtual void _getRowCoeffs(int i,
|
marci@1143
|
328 |
std::vector<std::pair<int, _Value> >& coeffs) = 0;
|
marci@1152
|
329 |
/// \e
|
marci@1081
|
330 |
virtual void _setColCoeffs(int i,
|
marci@1104
|
331 |
const std::vector<std::pair<int, _Value> >& coeffs) = 0;
|
marci@1143
|
332 |
/// \e
|
marci@1143
|
333 |
/// This routine modifies \c coeffs only by the \c push_back method.
|
marci@1143
|
334 |
virtual void _getColCoeffs(int i,
|
marci@1143
|
335 |
std::vector<std::pair<int, _Value> >& coeffs) = 0;
|
marci@1081
|
336 |
/// \e
|
marci@1152
|
337 |
virtual void _setCoeff(int col, int row, _Value value) = 0;
|
marci@1152
|
338 |
/// \e
|
marci@1152
|
339 |
virtual _Value _getCoeff(int col, int row) = 0;
|
marci@1152
|
340 |
// public:
|
marci@1152
|
341 |
// /// \e
|
marci@1152
|
342 |
// enum Bound { FREE, LOWER, UPPER, DOUBLE, FIXED };
|
marci@1081
|
343 |
protected:
|
marci@1081
|
344 |
/// \e
|
marci@1110
|
345 |
/// The lower bound of a variable (column) have to be given by an
|
marci@1110
|
346 |
/// extended number of type _Value, i.e. a finite number of type
|
marci@1110
|
347 |
/// _Value or -INF.
|
marci@1110
|
348 |
virtual void _setColLowerBound(int i, _Value value) = 0;
|
marci@1110
|
349 |
/// \e
|
marci@1111
|
350 |
/// The lower bound of a variable (column) is an
|
marci@1111
|
351 |
/// extended number of type _Value, i.e. a finite number of type
|
marci@1111
|
352 |
/// _Value or -INF.
|
marci@1111
|
353 |
virtual _Value _getColLowerBound(int i) = 0;
|
marci@1111
|
354 |
/// \e
|
marci@1110
|
355 |
/// The upper bound of a variable (column) have to be given by an
|
marci@1110
|
356 |
/// extended number of type _Value, i.e. a finite number of type
|
marci@1110
|
357 |
/// _Value or INF.
|
marci@1110
|
358 |
virtual void _setColUpperBound(int i, _Value value) = 0;
|
marci@1110
|
359 |
/// \e
|
marci@1110
|
360 |
/// The upper bound of a variable (column) is an
|
marci@1110
|
361 |
/// extended number of type _Value, i.e. a finite number of type
|
marci@1110
|
362 |
/// _Value or INF.
|
marci@1110
|
363 |
virtual _Value _getColUpperBound(int i) = 0;
|
marci@1110
|
364 |
/// \e
|
marci@1111
|
365 |
/// The lower bound of a linear expression (row) have to be given by an
|
marci@1111
|
366 |
/// extended number of type _Value, i.e. a finite number of type
|
marci@1111
|
367 |
/// _Value or -INF.
|
marci@1111
|
368 |
virtual void _setRowLowerBound(int i, _Value value) = 0;
|
marci@1081
|
369 |
/// \e
|
marci@1111
|
370 |
/// The lower bound of a linear expression (row) is an
|
marci@1111
|
371 |
/// extended number of type _Value, i.e. a finite number of type
|
marci@1111
|
372 |
/// _Value or -INF.
|
marci@1111
|
373 |
virtual _Value _getRowLowerBound(int i) = 0;
|
marci@1111
|
374 |
/// \e
|
marci@1111
|
375 |
/// The upper bound of a linear expression (row) have to be given by an
|
marci@1111
|
376 |
/// extended number of type _Value, i.e. a finite number of type
|
marci@1111
|
377 |
/// _Value or INF.
|
marci@1111
|
378 |
virtual void _setRowUpperBound(int i, _Value value) = 0;
|
marci@1111
|
379 |
/// \e
|
marci@1111
|
380 |
/// The upper bound of a linear expression (row) is an
|
marci@1111
|
381 |
/// extended number of type _Value, i.e. a finite number of type
|
marci@1111
|
382 |
/// _Value or INF.
|
marci@1111
|
383 |
virtual _Value _getRowUpperBound(int i) = 0;
|
marci@1081
|
384 |
/// \e
|
marci@1152
|
385 |
virtual void _setObjCoeff(int i, _Value obj_coef) = 0;
|
marci@1081
|
386 |
/// \e
|
marci@1152
|
387 |
virtual _Value _getObjCoeff(int i) = 0;
|
marci@1112
|
388 |
|
marci@1112
|
389 |
//SOLUTION RETRIEVING
|
marci@1081
|
390 |
|
marci@1111
|
391 |
/// \e
|
marci@1081
|
392 |
virtual _Value _getPrimal(int i) = 0;
|
marci@1112
|
393 |
//@}
|
marci@1112
|
394 |
|
marci@1112
|
395 |
/*! @name High level interface (public members)
|
marci@1112
|
396 |
Problem manipulating functions in the high level interface
|
marci@1112
|
397 |
*/
|
marci@1112
|
398 |
//@{
|
marci@1112
|
399 |
public:
|
marci@1081
|
400 |
|
marci@1112
|
401 |
//MATRIX MANIPULATING FUNCTIONS
|
marci@1081
|
402 |
|
marci@1074
|
403 |
/// \e
|
marci@1144
|
404 |
Col addCol() {
|
marci@1074
|
405 |
int i=_addCol();
|
marci@1144
|
406 |
Col col;
|
marci@1144
|
407 |
col_iter_map.first(col, INVALID_CLASS);
|
marci@1144
|
408 |
if (col_iter_map.valid(col)) { //van hasznalhato hely
|
marci@1144
|
409 |
col_iter_map.set(col, INVALID_CLASS, VALID_CLASS);
|
marci@1144
|
410 |
col_iter_map[col]=i;
|
marci@1074
|
411 |
} else { //a cucc vegere kell inzertalni mert nincs szabad hely
|
marci@1144
|
412 |
col=col_iter_map.push_back(i, VALID_CLASS);
|
marci@1074
|
413 |
}
|
marci@1144
|
414 |
int_col_map.push_back(col);
|
marci@1144
|
415 |
return col;
|
marci@1074
|
416 |
}
|
marci@1074
|
417 |
/// \e
|
marci@1144
|
418 |
Row addRow() {
|
marci@1111
|
419 |
int i=_addRow();
|
marci@1144
|
420 |
Row row;
|
marci@1144
|
421 |
row_iter_map.first(row, INVALID_CLASS);
|
marci@1144
|
422 |
if (row_iter_map.valid(row)) { //van hasznalhato hely
|
marci@1144
|
423 |
row_iter_map.set(row, INVALID_CLASS, VALID_CLASS);
|
marci@1144
|
424 |
row_iter_map[row]=i;
|
marci@1111
|
425 |
} else { //a cucc vegere kell inzertalni mert nincs szabad hely
|
marci@1144
|
426 |
row=row_iter_map.push_back(i, VALID_CLASS);
|
marci@1031
|
427 |
}
|
marci@1144
|
428 |
int_row_map.push_back(row);
|
marci@1144
|
429 |
return row;
|
marci@1074
|
430 |
}
|
marci@1074
|
431 |
/// \e
|
marci@1144
|
432 |
void eraseCol(const Col& col) {
|
marci@1144
|
433 |
col_iter_map.set(col, VALID_CLASS, INVALID_CLASS);
|
marci@1074
|
434 |
int cols[2];
|
marci@1144
|
435 |
cols[1]=col_iter_map[col];
|
marci@1074
|
436 |
_eraseCol(cols[1]);
|
marci@1144
|
437 |
col_iter_map[col]=0; //glpk specifikus, de kell ez??
|
marci@1144
|
438 |
Col it;
|
marci@1074
|
439 |
for (col_iter_map.first(it, VALID_CLASS);
|
marci@1074
|
440 |
col_iter_map.valid(it); col_iter_map.next(it)) {
|
marci@1074
|
441 |
if (col_iter_map[it]>cols[1]) --col_iter_map[it];
|
marci@1074
|
442 |
}
|
marci@1143
|
443 |
int_col_map.erase(int_col_map.begin()+cols[1]);
|
marci@1031
|
444 |
}
|
marci@1031
|
445 |
/// \e
|
marci@1144
|
446 |
void eraseRow(const Row& row) {
|
marci@1144
|
447 |
row_iter_map.set(row, VALID_CLASS, INVALID_CLASS);
|
marci@1074
|
448 |
int rows[2];
|
marci@1144
|
449 |
rows[1]=row_iter_map[row];
|
marci@1074
|
450 |
_eraseRow(rows[1]);
|
marci@1144
|
451 |
row_iter_map[row]=0; //glpk specifikus, de kell ez??
|
marci@1144
|
452 |
Row it;
|
marci@1074
|
453 |
for (row_iter_map.first(it, VALID_CLASS);
|
marci@1074
|
454 |
row_iter_map.valid(it); row_iter_map.next(it)) {
|
marci@1074
|
455 |
if (row_iter_map[it]>rows[1]) --row_iter_map[it];
|
marci@1074
|
456 |
}
|
marci@1143
|
457 |
int_row_map.erase(int_row_map.begin()+rows[1]);
|
marci@1074
|
458 |
}
|
marci@1031
|
459 |
/// \e
|
marci@1152
|
460 |
void setCoeff(Col col, Row row, _Value value) {
|
marci@1152
|
461 |
_setCoeff(col_iter_map[col], row_iter_map[row], value);
|
marci@1152
|
462 |
}
|
marci@1152
|
463 |
/// \e
|
marci@1152
|
464 |
_Value getCoeff(Col col, Row row) {
|
marci@1152
|
465 |
return _getCoeff(col_iter_map[col], row_iter_map[row], value);
|
marci@1152
|
466 |
}
|
marci@1152
|
467 |
/// \e
|
marci@1144
|
468 |
void setColLowerBound(Col col, _Value lo) {
|
marci@1144
|
469 |
_setColLowerBound(col_iter_map[col], lo);
|
marci@1111
|
470 |
}
|
marci@1111
|
471 |
/// \e
|
marci@1144
|
472 |
_Value getColLowerBound(Col col) {
|
marci@1144
|
473 |
return _getColLowerBound(col_iter_map[col]);
|
marci@1111
|
474 |
}
|
marci@1111
|
475 |
/// \e
|
marci@1144
|
476 |
void setColUpperBound(Col col, _Value up) {
|
marci@1144
|
477 |
_setColUpperBound(col_iter_map[col], up);
|
marci@1110
|
478 |
}
|
marci@1110
|
479 |
/// \e
|
marci@1144
|
480 |
_Value getColUpperBound(Col col) {
|
marci@1144
|
481 |
return _getColUpperBound(col_iter_map[col]);
|
marci@1111
|
482 |
}
|
marci@1111
|
483 |
/// \e
|
marci@1144
|
484 |
void setRowLowerBound(Row row, _Value lo) {
|
marci@1144
|
485 |
_setRowLowerBound(row_iter_map[row], lo);
|
marci@1110
|
486 |
}
|
marci@1110
|
487 |
/// \e
|
marci@1144
|
488 |
_Value getRowLowerBound(Row row) {
|
marci@1144
|
489 |
return _getRowLowerBound(row_iter_map[row]);
|
marci@1110
|
490 |
}
|
marci@1110
|
491 |
/// \e
|
marci@1144
|
492 |
void setRowUpperBound(Row row, _Value up) {
|
marci@1144
|
493 |
_setRowUpperBound(row_iter_map[row], up);
|
marci@1081
|
494 |
}
|
marci@1031
|
495 |
/// \e
|
marci@1144
|
496 |
_Value getRowUpperBound(Row row) {
|
marci@1144
|
497 |
return _getRowUpperBound(row_iter_map[row]);
|
marci@1111
|
498 |
}
|
marci@1111
|
499 |
/// \e
|
marci@1152
|
500 |
void setObjCoeff(const Col& col, _Value obj_coef) {
|
marci@1152
|
501 |
_setObjCoeff(col_iter_map[col], obj_coef);
|
marci@1111
|
502 |
}
|
marci@1111
|
503 |
/// \e
|
marci@1152
|
504 |
_Value getObjCoeff(const Col& col) {
|
marci@1152
|
505 |
return _getObjCoeff(col_iter_map[col]);
|
marci@1081
|
506 |
}
|
marci@1081
|
507 |
|
marci@1112
|
508 |
//SOLUTION RETRIEVING FUNCTIONS
|
marci@1112
|
509 |
|
marci@1112
|
510 |
/// \e
|
marci@1144
|
511 |
_Value getPrimal(const Col& col) {
|
marci@1144
|
512 |
return _getPrimal(col_iter_map[col]);
|
marci@1112
|
513 |
}
|
marci@1112
|
514 |
|
marci@1112
|
515 |
//@}
|
marci@1112
|
516 |
|
marci@1112
|
517 |
/*! @name User friend interface
|
marci@1112
|
518 |
Problem manipulating functions in the user friend interface
|
marci@1112
|
519 |
*/
|
marci@1112
|
520 |
//@{
|
marci@1112
|
521 |
|
marci@1112
|
522 |
//EXPRESSION TYPES
|
marci@1099
|
523 |
|
marci@1099
|
524 |
/// \e
|
marci@1144
|
525 |
typedef Expr<Col, _Value> Expression;
|
marci@1099
|
526 |
/// \e
|
marci@1144
|
527 |
typedef Expr<Row, _Value> DualExpression;
|
marci@1144
|
528 |
/// \e
|
marci@1144
|
529 |
typedef Constr<Col, _Value> Constraint;
|
marci@1112
|
530 |
|
marci@1112
|
531 |
//MATRIX MANIPULATING FUNCTIONS
|
marci@1112
|
532 |
|
marci@1099
|
533 |
/// \e
|
marci@1144
|
534 |
void setRowCoeffs(Row row, const Expression& expr) {
|
marci@1099
|
535 |
std::vector<std::pair<int, _Value> > row_coeffs;
|
marci@1099
|
536 |
for(typename Expression::Data::const_iterator i=expr.data.begin();
|
marci@1099
|
537 |
i!=expr.data.end(); ++i) {
|
marci@1099
|
538 |
row_coeffs.push_back(std::make_pair
|
marci@1099
|
539 |
(col_iter_map[(*i).first], (*i).second));
|
marci@1099
|
540 |
}
|
marci@1144
|
541 |
_setRowCoeffs(row_iter_map[row], row_coeffs);
|
marci@1144
|
542 |
}
|
marci@1144
|
543 |
/// \e
|
marci@1144
|
544 |
void setRow(Row row, const Constraint& constr) {
|
marci@1144
|
545 |
setRowCoeffs(row, constr.expr);
|
marci@1144
|
546 |
setRowLowerBound(row, constr.lo);
|
marci@1144
|
547 |
setRowUpperBound(row, constr.up);
|
marci@1144
|
548 |
}
|
marci@1144
|
549 |
/// \e
|
marci@1144
|
550 |
Row addRow(const Constraint& constr) {
|
marci@1144
|
551 |
Row row=addRow();
|
marci@1144
|
552 |
setRowCoeffs(row, constr.expr);
|
marci@1144
|
553 |
setRowLowerBound(row, constr.lo);
|
marci@1144
|
554 |
setRowUpperBound(row, constr.up);
|
marci@1144
|
555 |
return row;
|
marci@1099
|
556 |
}
|
marci@1099
|
557 |
/// \e
|
marci@1143
|
558 |
/// This routine modifies \c expr by only adding to it.
|
marci@1144
|
559 |
void getRowCoeffs(Row row, Expression& expr) {
|
marci@1143
|
560 |
std::vector<std::pair<int, _Value> > row_coeffs;
|
marci@1144
|
561 |
_getRowCoeffs(row_iter_map[row], row_coeffs);
|
marci@1143
|
562 |
for(typename std::vector<std::pair<int, _Value> >::const_iterator
|
marci@1143
|
563 |
i=row_coeffs.begin(); i!=row_coeffs.end(); ++i) {
|
marci@1143
|
564 |
expr+= (*i).second*int_col_map[(*i).first];
|
marci@1143
|
565 |
}
|
marci@1143
|
566 |
}
|
marci@1143
|
567 |
/// \e
|
marci@1144
|
568 |
void setColCoeffs(Col col, const DualExpression& expr) {
|
marci@1099
|
569 |
std::vector<std::pair<int, _Value> > col_coeffs;
|
marci@1099
|
570 |
for(typename DualExpression::Data::const_iterator i=expr.data.begin();
|
marci@1099
|
571 |
i!=expr.data.end(); ++i) {
|
marci@1099
|
572 |
col_coeffs.push_back(std::make_pair
|
marci@1099
|
573 |
(row_iter_map[(*i).first], (*i).second));
|
marci@1099
|
574 |
}
|
marci@1144
|
575 |
_setColCoeffs(col_iter_map[col], col_coeffs);
|
marci@1099
|
576 |
}
|
marci@1099
|
577 |
/// \e
|
marci@1143
|
578 |
/// This routine modifies \c expr by only adding to it.
|
marci@1144
|
579 |
void getColCoeffs(Col col, DualExpression& expr) {
|
marci@1143
|
580 |
std::vector<std::pair<int, _Value> > col_coeffs;
|
marci@1144
|
581 |
_getColCoeffs(col_iter_map[col], col_coeffs);
|
marci@1143
|
582 |
for(typename std::vector<std::pair<int, _Value> >::const_iterator
|
marci@1143
|
583 |
i=col_coeffs.begin(); i!=col_coeffs.end(); ++i) {
|
marci@1143
|
584 |
expr+= (*i).second*int_row_map[(*i).first];
|
marci@1143
|
585 |
}
|
marci@1143
|
586 |
}
|
marci@1143
|
587 |
/// \e
|
marci@1099
|
588 |
void setObjCoeffs(const Expression& expr) {
|
marci@1152
|
589 |
// writing zero everywhere
|
marci@1152
|
590 |
for(Cols::ClassIt it(col_iter_map, VALID_CLASS); it!=INVALID; ++it)
|
marci@1152
|
591 |
setObjCoeff(it, 0.0);
|
marci@1152
|
592 |
// writing the data needed
|
marci@1099
|
593 |
for(typename Expression::Data::const_iterator i=expr.data.begin();
|
marci@1099
|
594 |
i!=expr.data.end(); ++i) {
|
marci@1152
|
595 |
setObjCoeff((*i).first, (*i).second);
|
marci@1099
|
596 |
}
|
marci@1099
|
597 |
}
|
marci@1143
|
598 |
/// \e
|
marci@1143
|
599 |
/// This routine modifies \c expr by only adding to it.
|
marci@1143
|
600 |
void getObjCoeffs(Expression& expr) {
|
marci@1152
|
601 |
for(Cols::ClassIt it(col_iter_map, VALID_CLASS); it!=INVALID; ++it)
|
marci@1152
|
602 |
expr+=getObjCoeff(it)*it;
|
marci@1152
|
603 |
}
|
marci@1152
|
604 |
//@}
|
marci@1152
|
605 |
|
marci@1152
|
606 |
|
marci@1152
|
607 |
/*! @name MIP functions and types (public members)
|
marci@1152
|
608 |
*/
|
marci@1152
|
609 |
//@{
|
marci@1152
|
610 |
public:
|
marci@1152
|
611 |
/// \e
|
marci@1152
|
612 |
virtual void solveBandB() = 0;
|
marci@1152
|
613 |
/// \e
|
marci@1152
|
614 |
virtual void setLP() = 0;
|
marci@1152
|
615 |
/// \e
|
marci@1152
|
616 |
virtual void setMIP() = 0;
|
marci@1152
|
617 |
protected:
|
marci@1152
|
618 |
/// \e
|
marci@1152
|
619 |
virtual void _setColCont(int i) = 0;
|
marci@1152
|
620 |
/// \e
|
marci@1152
|
621 |
virtual void _setColInt(int i) = 0;
|
marci@1152
|
622 |
public:
|
marci@1152
|
623 |
/// \e
|
marci@1152
|
624 |
void setColCont(Col col) {
|
marci@1152
|
625 |
_setColCont(col_iter_map[col]);
|
marci@1152
|
626 |
}
|
marci@1152
|
627 |
/// \e
|
marci@1152
|
628 |
void setColInt(Col col) {
|
marci@1152
|
629 |
_setColInt(col_iter_map[col]);
|
marci@1143
|
630 |
}
|
marci@1112
|
631 |
//@}
|
marci@1031
|
632 |
};
|
marci@1031
|
633 |
|
marci@1104
|
634 |
template <typename _Value>
|
marci@1104
|
635 |
const _Value LPSolverBase<_Value>::INF=std::numeric_limits<_Value>::infinity();
|
marci@1104
|
636 |
|
marci@1048
|
637 |
|
marci@1111
|
638 |
/// \brief Wrapper for GLPK solver
|
marci@1031
|
639 |
///
|
marci@1111
|
640 |
/// This class implements a lemon wrapper for GLPK.
|
marci@1081
|
641 |
class LPGLPK : public LPSolverBase<double> {
|
marci@1031
|
642 |
public:
|
marci@1048
|
643 |
typedef LPSolverBase<double> Parent;
|
marci@1031
|
644 |
|
marci@1031
|
645 |
public:
|
marci@1031
|
646 |
/// \e
|
marci@1031
|
647 |
LPX* lp;
|
marci@1031
|
648 |
|
marci@1031
|
649 |
public:
|
marci@1031
|
650 |
/// \e
|
marci@1081
|
651 |
LPGLPK() : Parent(),
|
marci@1031
|
652 |
lp(lpx_create_prob()) {
|
marci@1144
|
653 |
int_row_map.push_back(Row());
|
marci@1144
|
654 |
int_col_map.push_back(Col());
|
marci@1031
|
655 |
lpx_set_int_parm(lp, LPX_K_DUAL, 1);
|
marci@1031
|
656 |
}
|
marci@1031
|
657 |
/// \e
|
marci@1081
|
658 |
~LPGLPK() {
|
marci@1031
|
659 |
lpx_delete_prob(lp);
|
marci@1031
|
660 |
}
|
marci@1081
|
661 |
|
marci@1081
|
662 |
//MATRIX INDEPEDENT MANIPULATING FUNCTIONS
|
marci@1081
|
663 |
|
marci@1031
|
664 |
/// \e
|
marci@1031
|
665 |
void setMinimize() {
|
marci@1031
|
666 |
lpx_set_obj_dir(lp, LPX_MIN);
|
marci@1031
|
667 |
}
|
marci@1031
|
668 |
/// \e
|
marci@1031
|
669 |
void setMaximize() {
|
marci@1031
|
670 |
lpx_set_obj_dir(lp, LPX_MAX);
|
marci@1031
|
671 |
}
|
marci@1081
|
672 |
|
marci@1081
|
673 |
//LOW LEVEL INTERFACE, MATRIX MANIPULATING FUNCTIONS
|
marci@1081
|
674 |
|
marci@1074
|
675 |
protected:
|
marci@1031
|
676 |
/// \e
|
marci@1074
|
677 |
int _addCol() {
|
marci@1110
|
678 |
int i=lpx_add_cols(lp, 1);
|
marci@1110
|
679 |
_setColLowerBound(i, -INF);
|
marci@1110
|
680 |
_setColUpperBound(i, INF);
|
marci@1110
|
681 |
return i;
|
marci@1031
|
682 |
}
|
marci@1031
|
683 |
/// \e
|
marci@1074
|
684 |
int _addRow() {
|
marci@1110
|
685 |
int i=lpx_add_rows(lp, 1);
|
marci@1110
|
686 |
return i;
|
marci@1074
|
687 |
}
|
marci@1074
|
688 |
/// \e
|
marci@1081
|
689 |
virtual void _setRowCoeffs(int i,
|
marci@1104
|
690 |
const std::vector<std::pair<int, double> >& coeffs) {
|
marci@1074
|
691 |
int mem_length=1+colNum();
|
marci@1074
|
692 |
int* indices = new int[mem_length];
|
marci@1074
|
693 |
double* doubles = new double[mem_length];
|
marci@1074
|
694 |
int length=0;
|
marci@1074
|
695 |
for (std::vector<std::pair<int, double> >::
|
marci@1074
|
696 |
const_iterator it=coeffs.begin(); it!=coeffs.end(); ++it) {
|
marci@1074
|
697 |
++length;
|
marci@1074
|
698 |
indices[length]=it->first;
|
marci@1074
|
699 |
doubles[length]=it->second;
|
marci@1031
|
700 |
}
|
marci@1074
|
701 |
lpx_set_mat_row(lp, i, length, indices, doubles);
|
marci@1074
|
702 |
delete [] indices;
|
marci@1074
|
703 |
delete [] doubles;
|
marci@1031
|
704 |
}
|
marci@1074
|
705 |
/// \e
|
marci@1143
|
706 |
virtual void _getRowCoeffs(int i,
|
marci@1143
|
707 |
std::vector<std::pair<int, double> >& coeffs) {
|
marci@1143
|
708 |
int mem_length=1+colNum();
|
marci@1143
|
709 |
int* indices = new int[mem_length];
|
marci@1143
|
710 |
double* doubles = new double[mem_length];
|
marci@1143
|
711 |
int length=lpx_get_mat_row(lp, i, indices, doubles);
|
marci@1143
|
712 |
for (int i=1; i<=length; ++i) {
|
marci@1143
|
713 |
coeffs.push_back(std::make_pair(indices[i], doubles[i]));
|
marci@1143
|
714 |
}
|
marci@1143
|
715 |
delete [] indices;
|
marci@1143
|
716 |
delete [] doubles;
|
marci@1143
|
717 |
}
|
marci@1143
|
718 |
/// \e
|
marci@1081
|
719 |
virtual void _setColCoeffs(int i,
|
marci@1104
|
720 |
const std::vector<std::pair<int, double> >& coeffs) {
|
marci@1074
|
721 |
int mem_length=1+rowNum();
|
marci@1074
|
722 |
int* indices = new int[mem_length];
|
marci@1074
|
723 |
double* doubles = new double[mem_length];
|
marci@1074
|
724 |
int length=0;
|
marci@1074
|
725 |
for (std::vector<std::pair<int, double> >::
|
marci@1074
|
726 |
const_iterator it=coeffs.begin(); it!=coeffs.end(); ++it) {
|
marci@1074
|
727 |
++length;
|
marci@1074
|
728 |
indices[length]=it->first;
|
marci@1074
|
729 |
doubles[length]=it->second;
|
marci@1074
|
730 |
}
|
marci@1074
|
731 |
lpx_set_mat_col(lp, i, length, indices, doubles);
|
marci@1074
|
732 |
delete [] indices;
|
marci@1074
|
733 |
delete [] doubles;
|
marci@1031
|
734 |
}
|
marci@1031
|
735 |
/// \e
|
marci@1143
|
736 |
virtual void _getColCoeffs(int i,
|
marci@1143
|
737 |
std::vector<std::pair<int, double> >& coeffs) {
|
marci@1143
|
738 |
int mem_length=1+rowNum();
|
marci@1143
|
739 |
int* indices = new int[mem_length];
|
marci@1143
|
740 |
double* doubles = new double[mem_length];
|
marci@1143
|
741 |
int length=lpx_get_mat_col(lp, i, indices, doubles);
|
marci@1143
|
742 |
for (int i=1; i<=length; ++i) {
|
marci@1143
|
743 |
coeffs.push_back(std::make_pair(indices[i], doubles[i]));
|
marci@1143
|
744 |
}
|
marci@1143
|
745 |
delete [] indices;
|
marci@1143
|
746 |
delete [] doubles;
|
marci@1143
|
747 |
}
|
marci@1143
|
748 |
/// \e
|
marci@1074
|
749 |
virtual void _eraseCol(int i) {
|
marci@1031
|
750 |
int cols[2];
|
marci@1074
|
751 |
cols[1]=i;
|
marci@1031
|
752 |
lpx_del_cols(lp, 1, cols);
|
marci@1031
|
753 |
}
|
marci@1074
|
754 |
virtual void _eraseRow(int i) {
|
marci@1031
|
755 |
int rows[2];
|
marci@1074
|
756 |
rows[1]=i;
|
marci@1031
|
757 |
lpx_del_rows(lp, 1, rows);
|
marci@1031
|
758 |
}
|
marci@1152
|
759 |
void _setCoeff(int col, int row, double value) {
|
marci@1152
|
760 |
/// FIXME not yet implemented
|
marci@1152
|
761 |
}
|
marci@1152
|
762 |
double _getCoeff(int col, int row) {
|
marci@1152
|
763 |
/// FIXME not yet implemented
|
marci@1152
|
764 |
return 0.0;
|
marci@1152
|
765 |
}
|
marci@1110
|
766 |
virtual void _setColLowerBound(int i, double lo) {
|
marci@1110
|
767 |
if (lo==INF) {
|
marci@1110
|
768 |
//FIXME error
|
marci@1110
|
769 |
}
|
marci@1110
|
770 |
int b=lpx_get_col_type(lp, i);
|
marci@1110
|
771 |
double up=lpx_get_col_ub(lp, i);
|
marci@1110
|
772 |
if (lo==-INF) {
|
marci@1110
|
773 |
switch (b) {
|
marci@1110
|
774 |
case LPX_FR:
|
marci@1110
|
775 |
case LPX_LO:
|
marci@1110
|
776 |
lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
|
marci@1110
|
777 |
break;
|
marci@1110
|
778 |
case LPX_UP:
|
marci@1110
|
779 |
break;
|
marci@1110
|
780 |
case LPX_DB:
|
marci@1110
|
781 |
case LPX_FX:
|
marci@1110
|
782 |
lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
|
marci@1110
|
783 |
break;
|
marci@1110
|
784 |
default: ;
|
marci@1110
|
785 |
//FIXME error
|
marci@1110
|
786 |
}
|
marci@1110
|
787 |
} else {
|
marci@1110
|
788 |
switch (b) {
|
marci@1110
|
789 |
case LPX_FR:
|
marci@1110
|
790 |
case LPX_LO:
|
marci@1110
|
791 |
lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
|
marci@1110
|
792 |
break;
|
marci@1110
|
793 |
case LPX_UP:
|
marci@1110
|
794 |
case LPX_DB:
|
marci@1110
|
795 |
case LPX_FX:
|
marci@1110
|
796 |
if (lo==up)
|
marci@1110
|
797 |
lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
|
marci@1110
|
798 |
else
|
marci@1110
|
799 |
lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
|
marci@1110
|
800 |
break;
|
marci@1110
|
801 |
default: ;
|
marci@1110
|
802 |
//FIXME error
|
marci@1110
|
803 |
}
|
marci@1110
|
804 |
}
|
marci@1110
|
805 |
}
|
marci@1111
|
806 |
virtual double _getColLowerBound(int i) {
|
marci@1111
|
807 |
int b=lpx_get_col_type(lp, i);
|
marci@1111
|
808 |
switch (b) {
|
marci@1111
|
809 |
case LPX_FR:
|
marci@1111
|
810 |
return -INF;
|
marci@1111
|
811 |
case LPX_LO:
|
marci@1111
|
812 |
return lpx_get_col_lb(lp, i);
|
marci@1111
|
813 |
case LPX_UP:
|
marci@1111
|
814 |
return -INF;
|
marci@1111
|
815 |
case LPX_DB:
|
marci@1111
|
816 |
case LPX_FX:
|
marci@1111
|
817 |
return lpx_get_col_lb(lp, i);
|
marci@1111
|
818 |
default: ;
|
marci@1111
|
819 |
//FIXME error
|
marci@1111
|
820 |
return 0.0;
|
marci@1111
|
821 |
}
|
marci@1111
|
822 |
}
|
marci@1110
|
823 |
virtual void _setColUpperBound(int i, double up) {
|
marci@1110
|
824 |
if (up==-INF) {
|
marci@1110
|
825 |
//FIXME error
|
marci@1110
|
826 |
}
|
marci@1110
|
827 |
int b=lpx_get_col_type(lp, i);
|
marci@1110
|
828 |
double lo=lpx_get_col_lb(lp, i);
|
marci@1110
|
829 |
if (up==INF) {
|
marci@1110
|
830 |
switch (b) {
|
marci@1110
|
831 |
case LPX_FR:
|
marci@1110
|
832 |
case LPX_LO:
|
marci@1110
|
833 |
break;
|
marci@1110
|
834 |
case LPX_UP:
|
marci@1110
|
835 |
lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
|
marci@1110
|
836 |
break;
|
marci@1110
|
837 |
case LPX_DB:
|
marci@1110
|
838 |
case LPX_FX:
|
marci@1110
|
839 |
lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
|
marci@1110
|
840 |
break;
|
marci@1110
|
841 |
default: ;
|
marci@1110
|
842 |
//FIXME error
|
marci@1110
|
843 |
}
|
marci@1110
|
844 |
} else {
|
marci@1110
|
845 |
switch (b) {
|
marci@1110
|
846 |
case LPX_FR:
|
marci@1110
|
847 |
lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
|
marci@1110
|
848 |
case LPX_LO:
|
marci@1110
|
849 |
if (lo==up)
|
marci@1110
|
850 |
lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
|
marci@1110
|
851 |
else
|
marci@1110
|
852 |
lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
|
marci@1110
|
853 |
break;
|
marci@1110
|
854 |
case LPX_UP:
|
marci@1110
|
855 |
lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
|
marci@1110
|
856 |
break;
|
marci@1110
|
857 |
case LPX_DB:
|
marci@1110
|
858 |
case LPX_FX:
|
marci@1110
|
859 |
if (lo==up)
|
marci@1110
|
860 |
lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
|
marci@1110
|
861 |
else
|
marci@1110
|
862 |
lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
|
marci@1110
|
863 |
break;
|
marci@1110
|
864 |
default: ;
|
marci@1110
|
865 |
//FIXME error
|
marci@1110
|
866 |
}
|
marci@1110
|
867 |
}
|
marci@1110
|
868 |
}
|
marci@1110
|
869 |
virtual double _getColUpperBound(int i) {
|
marci@1110
|
870 |
int b=lpx_get_col_type(lp, i);
|
marci@1110
|
871 |
switch (b) {
|
marci@1110
|
872 |
case LPX_FR:
|
marci@1110
|
873 |
case LPX_LO:
|
marci@1110
|
874 |
return INF;
|
marci@1110
|
875 |
case LPX_UP:
|
marci@1110
|
876 |
case LPX_DB:
|
marci@1110
|
877 |
case LPX_FX:
|
marci@1110
|
878 |
return lpx_get_col_ub(lp, i);
|
marci@1110
|
879 |
default: ;
|
marci@1110
|
880 |
//FIXME error
|
marci@1110
|
881 |
return 0.0;
|
marci@1110
|
882 |
}
|
marci@1110
|
883 |
}
|
marci@1111
|
884 |
virtual void _setRowLowerBound(int i, double lo) {
|
marci@1111
|
885 |
if (lo==INF) {
|
marci@1111
|
886 |
//FIXME error
|
marci@1081
|
887 |
}
|
marci@1111
|
888 |
int b=lpx_get_row_type(lp, i);
|
marci@1111
|
889 |
double up=lpx_get_row_ub(lp, i);
|
marci@1111
|
890 |
if (lo==-INF) {
|
marci@1111
|
891 |
switch (b) {
|
marci@1111
|
892 |
case LPX_FR:
|
marci@1111
|
893 |
case LPX_LO:
|
marci@1111
|
894 |
lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
|
marci@1111
|
895 |
break;
|
marci@1111
|
896 |
case LPX_UP:
|
marci@1111
|
897 |
break;
|
marci@1111
|
898 |
case LPX_DB:
|
marci@1111
|
899 |
case LPX_FX:
|
marci@1111
|
900 |
lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
|
marci@1111
|
901 |
break;
|
marci@1111
|
902 |
default: ;
|
marci@1111
|
903 |
//FIXME error
|
marci@1111
|
904 |
}
|
marci@1111
|
905 |
} else {
|
marci@1111
|
906 |
switch (b) {
|
marci@1111
|
907 |
case LPX_FR:
|
marci@1111
|
908 |
case LPX_LO:
|
marci@1111
|
909 |
lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
|
marci@1111
|
910 |
break;
|
marci@1111
|
911 |
case LPX_UP:
|
marci@1111
|
912 |
case LPX_DB:
|
marci@1111
|
913 |
case LPX_FX:
|
marci@1111
|
914 |
if (lo==up)
|
marci@1111
|
915 |
lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
|
marci@1111
|
916 |
else
|
marci@1111
|
917 |
lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
|
marci@1111
|
918 |
break;
|
marci@1111
|
919 |
default: ;
|
marci@1111
|
920 |
//FIXME error
|
marci@1111
|
921 |
}
|
marci@1081
|
922 |
}
|
marci@1111
|
923 |
}
|
marci@1111
|
924 |
virtual double _getRowLowerBound(int i) {
|
marci@1111
|
925 |
int b=lpx_get_row_type(lp, i);
|
marci@1111
|
926 |
switch (b) {
|
marci@1111
|
927 |
case LPX_FR:
|
marci@1111
|
928 |
return -INF;
|
marci@1111
|
929 |
case LPX_LO:
|
marci@1111
|
930 |
return lpx_get_row_lb(lp, i);
|
marci@1111
|
931 |
case LPX_UP:
|
marci@1111
|
932 |
return -INF;
|
marci@1111
|
933 |
case LPX_DB:
|
marci@1111
|
934 |
case LPX_FX:
|
marci@1111
|
935 |
return lpx_get_row_lb(lp, i);
|
marci@1111
|
936 |
default: ;
|
marci@1111
|
937 |
//FIXME error
|
marci@1111
|
938 |
return 0.0;
|
marci@1111
|
939 |
}
|
marci@1111
|
940 |
}
|
marci@1111
|
941 |
virtual void _setRowUpperBound(int i, double up) {
|
marci@1111
|
942 |
if (up==-INF) {
|
marci@1111
|
943 |
//FIXME error
|
marci@1111
|
944 |
}
|
marci@1111
|
945 |
int b=lpx_get_row_type(lp, i);
|
marci@1111
|
946 |
double lo=lpx_get_row_lb(lp, i);
|
marci@1111
|
947 |
if (up==INF) {
|
marci@1111
|
948 |
switch (b) {
|
marci@1111
|
949 |
case LPX_FR:
|
marci@1111
|
950 |
case LPX_LO:
|
marci@1111
|
951 |
break;
|
marci@1111
|
952 |
case LPX_UP:
|
marci@1111
|
953 |
lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
|
marci@1111
|
954 |
break;
|
marci@1111
|
955 |
case LPX_DB:
|
marci@1111
|
956 |
case LPX_FX:
|
marci@1111
|
957 |
lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
|
marci@1111
|
958 |
break;
|
marci@1111
|
959 |
default: ;
|
marci@1111
|
960 |
//FIXME error
|
marci@1111
|
961 |
}
|
marci@1111
|
962 |
} else {
|
marci@1111
|
963 |
switch (b) {
|
marci@1111
|
964 |
case LPX_FR:
|
marci@1111
|
965 |
lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
|
marci@1111
|
966 |
case LPX_LO:
|
marci@1111
|
967 |
if (lo==up)
|
marci@1111
|
968 |
lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
|
marci@1111
|
969 |
else
|
marci@1111
|
970 |
lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
|
marci@1111
|
971 |
break;
|
marci@1111
|
972 |
case LPX_UP:
|
marci@1111
|
973 |
lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
|
marci@1111
|
974 |
break;
|
marci@1111
|
975 |
case LPX_DB:
|
marci@1111
|
976 |
case LPX_FX:
|
marci@1111
|
977 |
if (lo==up)
|
marci@1111
|
978 |
lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
|
marci@1111
|
979 |
else
|
marci@1111
|
980 |
lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
|
marci@1111
|
981 |
break;
|
marci@1111
|
982 |
default: ;
|
marci@1111
|
983 |
//FIXME error
|
marci@1111
|
984 |
}
|
marci@1111
|
985 |
}
|
marci@1111
|
986 |
}
|
marci@1111
|
987 |
virtual double _getRowUpperBound(int i) {
|
marci@1111
|
988 |
int b=lpx_get_row_type(lp, i);
|
marci@1111
|
989 |
switch (b) {
|
marci@1111
|
990 |
case LPX_FR:
|
marci@1111
|
991 |
case LPX_LO:
|
marci@1111
|
992 |
return INF;
|
marci@1111
|
993 |
case LPX_UP:
|
marci@1111
|
994 |
case LPX_DB:
|
marci@1111
|
995 |
case LPX_FX:
|
marci@1111
|
996 |
return lpx_get_row_ub(lp, i);
|
marci@1111
|
997 |
default: ;
|
marci@1111
|
998 |
//FIXME error
|
marci@1111
|
999 |
return 0.0;
|
marci@1111
|
1000 |
}
|
marci@1111
|
1001 |
}
|
marci@1031
|
1002 |
/// \e
|
marci@1152
|
1003 |
virtual double _getObjCoeff(int i) {
|
marci@1081
|
1004 |
return lpx_get_obj_coef(lp, i);
|
marci@1031
|
1005 |
}
|
marci@1031
|
1006 |
/// \e
|
marci@1152
|
1007 |
virtual void _setObjCoeff(int i, double obj_coef) {
|
marci@1081
|
1008 |
lpx_set_obj_coef(lp, i, obj_coef);
|
marci@1031
|
1009 |
}
|
marci@1081
|
1010 |
public:
|
marci@1031
|
1011 |
/// \e
|
marci@1031
|
1012 |
void solveSimplex() { lpx_simplex(lp); }
|
marci@1031
|
1013 |
/// \e
|
marci@1031
|
1014 |
void solvePrimalSimplex() { lpx_simplex(lp); }
|
marci@1031
|
1015 |
/// \e
|
marci@1031
|
1016 |
void solveDualSimplex() { lpx_simplex(lp); }
|
marci@1081
|
1017 |
protected:
|
marci@1081
|
1018 |
virtual double _getPrimal(int i) {
|
marci@1081
|
1019 |
return lpx_get_col_prim(lp, i);
|
marci@1031
|
1020 |
}
|
marci@1081
|
1021 |
public:
|
marci@1031
|
1022 |
/// \e
|
marci@1031
|
1023 |
double getObjVal() { return lpx_get_obj_val(lp); }
|
marci@1031
|
1024 |
/// \e
|
marci@1031
|
1025 |
int rowNum() const { return lpx_get_num_rows(lp); }
|
marci@1031
|
1026 |
/// \e
|
marci@1031
|
1027 |
int colNum() const { return lpx_get_num_cols(lp); }
|
marci@1031
|
1028 |
/// \e
|
marci@1031
|
1029 |
int warmUp() { return lpx_warm_up(lp); }
|
marci@1031
|
1030 |
/// \e
|
marci@1031
|
1031 |
void printWarmUpStatus(int i) {
|
marci@1031
|
1032 |
switch (i) {
|
marci@1031
|
1033 |
case LPX_E_OK: cout << "LPX_E_OK" << endl; break;
|
marci@1031
|
1034 |
case LPX_E_EMPTY: cout << "LPX_E_EMPTY" << endl; break;
|
marci@1031
|
1035 |
case LPX_E_BADB: cout << "LPX_E_BADB" << endl; break;
|
marci@1031
|
1036 |
case LPX_E_SING: cout << "LPX_E_SING" << endl; break;
|
marci@1031
|
1037 |
}
|
marci@1031
|
1038 |
}
|
marci@1031
|
1039 |
/// \e
|
marci@1031
|
1040 |
int getPrimalStatus() { return lpx_get_prim_stat(lp); }
|
marci@1031
|
1041 |
/// \e
|
marci@1031
|
1042 |
void printPrimalStatus(int i) {
|
marci@1031
|
1043 |
switch (i) {
|
marci@1031
|
1044 |
case LPX_P_UNDEF: cout << "LPX_P_UNDEF" << endl; break;
|
marci@1031
|
1045 |
case LPX_P_FEAS: cout << "LPX_P_FEAS" << endl; break;
|
marci@1031
|
1046 |
case LPX_P_INFEAS: cout << "LPX_P_INFEAS" << endl; break;
|
marci@1031
|
1047 |
case LPX_P_NOFEAS: cout << "LPX_P_NOFEAS" << endl; break;
|
marci@1031
|
1048 |
}
|
marci@1031
|
1049 |
}
|
marci@1031
|
1050 |
/// \e
|
marci@1031
|
1051 |
int getDualStatus() { return lpx_get_dual_stat(lp); }
|
marci@1031
|
1052 |
/// \e
|
marci@1031
|
1053 |
void printDualStatus(int i) {
|
marci@1031
|
1054 |
switch (i) {
|
marci@1031
|
1055 |
case LPX_D_UNDEF: cout << "LPX_D_UNDEF" << endl; break;
|
marci@1031
|
1056 |
case LPX_D_FEAS: cout << "LPX_D_FEAS" << endl; break;
|
marci@1031
|
1057 |
case LPX_D_INFEAS: cout << "LPX_D_INFEAS" << endl; break;
|
marci@1031
|
1058 |
case LPX_D_NOFEAS: cout << "LPX_D_NOFEAS" << endl; break;
|
marci@1031
|
1059 |
}
|
marci@1031
|
1060 |
}
|
marci@1144
|
1061 |
/// Returns the status of the slack variable assigned to row \c row.
|
marci@1144
|
1062 |
int getRowStat(const Row& row) {
|
marci@1144
|
1063 |
return lpx_get_row_stat(lp, row_iter_map[row]);
|
marci@1031
|
1064 |
}
|
marci@1031
|
1065 |
/// \e
|
marci@1031
|
1066 |
void printRowStatus(int i) {
|
marci@1031
|
1067 |
switch (i) {
|
marci@1031
|
1068 |
case LPX_BS: cout << "LPX_BS" << endl; break;
|
marci@1031
|
1069 |
case LPX_NL: cout << "LPX_NL" << endl; break;
|
marci@1031
|
1070 |
case LPX_NU: cout << "LPX_NU" << endl; break;
|
marci@1031
|
1071 |
case LPX_NF: cout << "LPX_NF" << endl; break;
|
marci@1031
|
1072 |
case LPX_NS: cout << "LPX_NS" << endl; break;
|
marci@1031
|
1073 |
}
|
marci@1031
|
1074 |
}
|
marci@1144
|
1075 |
/// Returns the status of the variable assigned to column \c col.
|
marci@1144
|
1076 |
int getColStat(const Col& col) {
|
marci@1144
|
1077 |
return lpx_get_col_stat(lp, col_iter_map[col]);
|
marci@1031
|
1078 |
}
|
marci@1031
|
1079 |
/// \e
|
marci@1031
|
1080 |
void printColStatus(int i) {
|
marci@1031
|
1081 |
switch (i) {
|
marci@1031
|
1082 |
case LPX_BS: cout << "LPX_BS" << endl; break;
|
marci@1031
|
1083 |
case LPX_NL: cout << "LPX_NL" << endl; break;
|
marci@1031
|
1084 |
case LPX_NU: cout << "LPX_NU" << endl; break;
|
marci@1031
|
1085 |
case LPX_NF: cout << "LPX_NF" << endl; break;
|
marci@1031
|
1086 |
case LPX_NS: cout << "LPX_NS" << endl; break;
|
marci@1031
|
1087 |
}
|
marci@1031
|
1088 |
}
|
marci@1152
|
1089 |
|
marci@1152
|
1090 |
// MIP
|
marci@1152
|
1091 |
/// \e
|
marci@1152
|
1092 |
void solveBandB() { lpx_integer(lp); }
|
marci@1152
|
1093 |
/// \e
|
marci@1152
|
1094 |
void setLP() { lpx_set_class(lp, LPX_LP); }
|
marci@1152
|
1095 |
/// \e
|
marci@1152
|
1096 |
void setMIP() { lpx_set_class(lp, LPX_MIP); }
|
marci@1152
|
1097 |
protected:
|
marci@1152
|
1098 |
/// \e
|
marci@1152
|
1099 |
void _setColCont(int i) {lpx_set_col_kind(lp, i, LPX_CV); }
|
marci@1152
|
1100 |
/// \e
|
marci@1152
|
1101 |
void _setColInt(int i) {lpx_set_col_kind(lp, i, LPX_IV); }
|
marci@1031
|
1102 |
};
|
marci@1031
|
1103 |
|
marci@1031
|
1104 |
/// @}
|
marci@1031
|
1105 |
|
marci@1031
|
1106 |
} //namespace lemon
|
marci@1031
|
1107 |
|
marci@1152
|
1108 |
#endif //LEMON_LP_SOLVER_BASE_H
|