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