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