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