alpar@484
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
alpar@484
|
2 |
*
|
alpar@484
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
alpar@484
|
4 |
*
|
alpar@1270
|
5 |
* Copyright (C) 2003-2013
|
alpar@484
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@484
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@484
|
8 |
*
|
alpar@484
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@484
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@484
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@484
|
12 |
*
|
alpar@484
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@484
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@484
|
15 |
* purpose.
|
alpar@484
|
16 |
*
|
alpar@484
|
17 |
*/
|
alpar@484
|
18 |
|
alpar@484
|
19 |
///\file
|
alpar@484
|
20 |
///\brief Implementation of the LEMON GLPK LP and MIP solver interface.
|
alpar@484
|
21 |
|
alpar@484
|
22 |
#include <lemon/glpk.h>
|
alpar@484
|
23 |
#include <glpk.h>
|
alpar@484
|
24 |
|
alpar@484
|
25 |
#include <lemon/assert.h>
|
alpar@484
|
26 |
|
alpar@484
|
27 |
namespace lemon {
|
alpar@484
|
28 |
|
alpar@484
|
29 |
// GlpkBase members
|
alpar@484
|
30 |
|
alpar@484
|
31 |
GlpkBase::GlpkBase() : LpBase() {
|
alpar@484
|
32 |
lp = glp_create_prob();
|
alpar@484
|
33 |
glp_create_index(lp);
|
deba@623
|
34 |
messageLevel(MESSAGE_NOTHING);
|
alpar@484
|
35 |
}
|
alpar@484
|
36 |
|
alpar@484
|
37 |
GlpkBase::GlpkBase(const GlpkBase &other) : LpBase() {
|
alpar@484
|
38 |
lp = glp_create_prob();
|
alpar@484
|
39 |
glp_copy_prob(lp, other.lp, GLP_ON);
|
alpar@484
|
40 |
glp_create_index(lp);
|
alpar@484
|
41 |
rows = other.rows;
|
alpar@484
|
42 |
cols = other.cols;
|
deba@623
|
43 |
messageLevel(MESSAGE_NOTHING);
|
alpar@484
|
44 |
}
|
alpar@484
|
45 |
|
alpar@484
|
46 |
GlpkBase::~GlpkBase() {
|
alpar@484
|
47 |
glp_delete_prob(lp);
|
alpar@484
|
48 |
}
|
alpar@484
|
49 |
|
alpar@484
|
50 |
int GlpkBase::_addCol() {
|
alpar@484
|
51 |
int i = glp_add_cols(lp, 1);
|
alpar@484
|
52 |
glp_set_col_bnds(lp, i, GLP_FR, 0.0, 0.0);
|
alpar@484
|
53 |
return i;
|
alpar@484
|
54 |
}
|
alpar@484
|
55 |
|
alpar@484
|
56 |
int GlpkBase::_addRow() {
|
alpar@484
|
57 |
int i = glp_add_rows(lp, 1);
|
alpar@484
|
58 |
glp_set_row_bnds(lp, i, GLP_FR, 0.0, 0.0);
|
alpar@484
|
59 |
return i;
|
alpar@484
|
60 |
}
|
alpar@484
|
61 |
|
alpar@956
|
62 |
int GlpkBase::_addRow(Value lo, ExprIterator b,
|
deba@793
|
63 |
ExprIterator e, Value up) {
|
deba@793
|
64 |
int i = glp_add_rows(lp, 1);
|
deba@793
|
65 |
|
deba@793
|
66 |
if (lo == -INF) {
|
deba@793
|
67 |
if (up == INF) {
|
deba@793
|
68 |
glp_set_row_bnds(lp, i, GLP_FR, lo, up);
|
deba@793
|
69 |
} else {
|
deba@793
|
70 |
glp_set_row_bnds(lp, i, GLP_UP, lo, up);
|
alpar@956
|
71 |
}
|
deba@793
|
72 |
} else {
|
deba@793
|
73 |
if (up == INF) {
|
deba@793
|
74 |
glp_set_row_bnds(lp, i, GLP_LO, lo, up);
|
alpar@956
|
75 |
} else if (lo != up) {
|
deba@793
|
76 |
glp_set_row_bnds(lp, i, GLP_DB, lo, up);
|
deba@793
|
77 |
} else {
|
deba@793
|
78 |
glp_set_row_bnds(lp, i, GLP_FX, lo, up);
|
deba@793
|
79 |
}
|
deba@793
|
80 |
}
|
deba@793
|
81 |
|
deba@793
|
82 |
std::vector<int> indexes;
|
deba@793
|
83 |
std::vector<Value> values;
|
deba@793
|
84 |
|
deba@793
|
85 |
indexes.push_back(0);
|
deba@793
|
86 |
values.push_back(0);
|
deba@793
|
87 |
|
deba@793
|
88 |
for(ExprIterator it = b; it != e; ++it) {
|
deba@793
|
89 |
indexes.push_back(it->first);
|
deba@793
|
90 |
values.push_back(it->second);
|
deba@793
|
91 |
}
|
deba@793
|
92 |
|
deba@793
|
93 |
glp_set_mat_row(lp, i, values.size() - 1,
|
deba@793
|
94 |
&indexes.front(), &values.front());
|
deba@793
|
95 |
return i;
|
deba@793
|
96 |
}
|
deba@793
|
97 |
|
alpar@484
|
98 |
void GlpkBase::_eraseCol(int i) {
|
alpar@484
|
99 |
int ca[2];
|
alpar@484
|
100 |
ca[1] = i;
|
alpar@484
|
101 |
glp_del_cols(lp, 1, ca);
|
alpar@484
|
102 |
}
|
alpar@484
|
103 |
|
alpar@484
|
104 |
void GlpkBase::_eraseRow(int i) {
|
alpar@484
|
105 |
int ra[2];
|
alpar@484
|
106 |
ra[1] = i;
|
alpar@484
|
107 |
glp_del_rows(lp, 1, ra);
|
alpar@484
|
108 |
}
|
alpar@484
|
109 |
|
alpar@484
|
110 |
void GlpkBase::_eraseColId(int i) {
|
alpar@484
|
111 |
cols.eraseIndex(i);
|
alpar@484
|
112 |
cols.shiftIndices(i);
|
alpar@484
|
113 |
}
|
alpar@484
|
114 |
|
alpar@484
|
115 |
void GlpkBase::_eraseRowId(int i) {
|
alpar@484
|
116 |
rows.eraseIndex(i);
|
alpar@484
|
117 |
rows.shiftIndices(i);
|
alpar@484
|
118 |
}
|
alpar@484
|
119 |
|
alpar@484
|
120 |
void GlpkBase::_getColName(int c, std::string& name) const {
|
alpar@484
|
121 |
const char *str = glp_get_col_name(lp, c);
|
alpar@484
|
122 |
if (str) name = str;
|
alpar@484
|
123 |
else name.clear();
|
alpar@484
|
124 |
}
|
alpar@484
|
125 |
|
alpar@484
|
126 |
void GlpkBase::_setColName(int c, const std::string & name) {
|
alpar@484
|
127 |
glp_set_col_name(lp, c, const_cast<char*>(name.c_str()));
|
alpar@484
|
128 |
|
alpar@484
|
129 |
}
|
alpar@484
|
130 |
|
alpar@484
|
131 |
int GlpkBase::_colByName(const std::string& name) const {
|
alpar@484
|
132 |
int k = glp_find_col(lp, const_cast<char*>(name.c_str()));
|
alpar@484
|
133 |
return k > 0 ? k : -1;
|
alpar@484
|
134 |
}
|
alpar@484
|
135 |
|
alpar@484
|
136 |
void GlpkBase::_getRowName(int r, std::string& name) const {
|
alpar@484
|
137 |
const char *str = glp_get_row_name(lp, r);
|
alpar@484
|
138 |
if (str) name = str;
|
alpar@484
|
139 |
else name.clear();
|
alpar@484
|
140 |
}
|
alpar@484
|
141 |
|
alpar@484
|
142 |
void GlpkBase::_setRowName(int r, const std::string & name) {
|
alpar@484
|
143 |
glp_set_row_name(lp, r, const_cast<char*>(name.c_str()));
|
alpar@484
|
144 |
|
alpar@484
|
145 |
}
|
alpar@484
|
146 |
|
alpar@484
|
147 |
int GlpkBase::_rowByName(const std::string& name) const {
|
alpar@484
|
148 |
int k = glp_find_row(lp, const_cast<char*>(name.c_str()));
|
alpar@484
|
149 |
return k > 0 ? k : -1;
|
alpar@484
|
150 |
}
|
alpar@484
|
151 |
|
alpar@484
|
152 |
void GlpkBase::_setRowCoeffs(int i, ExprIterator b, ExprIterator e) {
|
alpar@484
|
153 |
std::vector<int> indexes;
|
alpar@484
|
154 |
std::vector<Value> values;
|
alpar@484
|
155 |
|
alpar@484
|
156 |
indexes.push_back(0);
|
alpar@484
|
157 |
values.push_back(0);
|
alpar@484
|
158 |
|
alpar@484
|
159 |
for(ExprIterator it = b; it != e; ++it) {
|
alpar@484
|
160 |
indexes.push_back(it->first);
|
alpar@484
|
161 |
values.push_back(it->second);
|
alpar@484
|
162 |
}
|
alpar@484
|
163 |
|
alpar@484
|
164 |
glp_set_mat_row(lp, i, values.size() - 1,
|
alpar@484
|
165 |
&indexes.front(), &values.front());
|
alpar@484
|
166 |
}
|
alpar@484
|
167 |
|
alpar@484
|
168 |
void GlpkBase::_getRowCoeffs(int ix, InsertIterator b) const {
|
alpar@484
|
169 |
int length = glp_get_mat_row(lp, ix, 0, 0);
|
alpar@484
|
170 |
|
alpar@484
|
171 |
std::vector<int> indexes(length + 1);
|
alpar@484
|
172 |
std::vector<Value> values(length + 1);
|
alpar@484
|
173 |
|
alpar@484
|
174 |
glp_get_mat_row(lp, ix, &indexes.front(), &values.front());
|
alpar@484
|
175 |
|
alpar@484
|
176 |
for (int i = 1; i <= length; ++i) {
|
alpar@484
|
177 |
*b = std::make_pair(indexes[i], values[i]);
|
alpar@484
|
178 |
++b;
|
alpar@484
|
179 |
}
|
alpar@484
|
180 |
}
|
alpar@484
|
181 |
|
alpar@484
|
182 |
void GlpkBase::_setColCoeffs(int ix, ExprIterator b,
|
alpar@484
|
183 |
ExprIterator e) {
|
alpar@484
|
184 |
|
alpar@484
|
185 |
std::vector<int> indexes;
|
alpar@484
|
186 |
std::vector<Value> values;
|
alpar@484
|
187 |
|
alpar@484
|
188 |
indexes.push_back(0);
|
alpar@484
|
189 |
values.push_back(0);
|
alpar@484
|
190 |
|
alpar@484
|
191 |
for(ExprIterator it = b; it != e; ++it) {
|
alpar@484
|
192 |
indexes.push_back(it->first);
|
alpar@484
|
193 |
values.push_back(it->second);
|
alpar@484
|
194 |
}
|
alpar@484
|
195 |
|
alpar@484
|
196 |
glp_set_mat_col(lp, ix, values.size() - 1,
|
alpar@484
|
197 |
&indexes.front(), &values.front());
|
alpar@484
|
198 |
}
|
alpar@484
|
199 |
|
alpar@484
|
200 |
void GlpkBase::_getColCoeffs(int ix, InsertIterator b) const {
|
alpar@484
|
201 |
int length = glp_get_mat_col(lp, ix, 0, 0);
|
alpar@484
|
202 |
|
alpar@484
|
203 |
std::vector<int> indexes(length + 1);
|
alpar@484
|
204 |
std::vector<Value> values(length + 1);
|
alpar@484
|
205 |
|
alpar@484
|
206 |
glp_get_mat_col(lp, ix, &indexes.front(), &values.front());
|
alpar@484
|
207 |
|
alpar@484
|
208 |
for (int i = 1; i <= length; ++i) {
|
alpar@484
|
209 |
*b = std::make_pair(indexes[i], values[i]);
|
alpar@484
|
210 |
++b;
|
alpar@484
|
211 |
}
|
alpar@484
|
212 |
}
|
alpar@484
|
213 |
|
alpar@484
|
214 |
void GlpkBase::_setCoeff(int ix, int jx, Value value) {
|
alpar@484
|
215 |
|
alpar@484
|
216 |
if (glp_get_num_cols(lp) < glp_get_num_rows(lp)) {
|
alpar@484
|
217 |
|
alpar@484
|
218 |
int length = glp_get_mat_row(lp, ix, 0, 0);
|
alpar@484
|
219 |
|
alpar@484
|
220 |
std::vector<int> indexes(length + 2);
|
alpar@484
|
221 |
std::vector<Value> values(length + 2);
|
alpar@484
|
222 |
|
alpar@484
|
223 |
glp_get_mat_row(lp, ix, &indexes.front(), &values.front());
|
alpar@484
|
224 |
|
alpar@484
|
225 |
//The following code does not suppose that the elements of the
|
alpar@484
|
226 |
//array indexes are sorted
|
alpar@484
|
227 |
bool found = false;
|
alpar@484
|
228 |
for (int i = 1; i <= length; ++i) {
|
alpar@484
|
229 |
if (indexes[i] == jx) {
|
alpar@484
|
230 |
found = true;
|
alpar@484
|
231 |
values[i] = value;
|
alpar@484
|
232 |
break;
|
alpar@484
|
233 |
}
|
alpar@484
|
234 |
}
|
alpar@484
|
235 |
if (!found) {
|
alpar@484
|
236 |
++length;
|
alpar@484
|
237 |
indexes[length] = jx;
|
alpar@484
|
238 |
values[length] = value;
|
alpar@484
|
239 |
}
|
alpar@484
|
240 |
|
alpar@484
|
241 |
glp_set_mat_row(lp, ix, length, &indexes.front(), &values.front());
|
alpar@484
|
242 |
|
alpar@484
|
243 |
} else {
|
alpar@484
|
244 |
|
alpar@484
|
245 |
int length = glp_get_mat_col(lp, jx, 0, 0);
|
alpar@484
|
246 |
|
alpar@484
|
247 |
std::vector<int> indexes(length + 2);
|
alpar@484
|
248 |
std::vector<Value> values(length + 2);
|
alpar@484
|
249 |
|
alpar@484
|
250 |
glp_get_mat_col(lp, jx, &indexes.front(), &values.front());
|
alpar@484
|
251 |
|
alpar@484
|
252 |
//The following code does not suppose that the elements of the
|
alpar@484
|
253 |
//array indexes are sorted
|
alpar@484
|
254 |
bool found = false;
|
alpar@484
|
255 |
for (int i = 1; i <= length; ++i) {
|
alpar@484
|
256 |
if (indexes[i] == ix) {
|
alpar@484
|
257 |
found = true;
|
alpar@484
|
258 |
values[i] = value;
|
alpar@484
|
259 |
break;
|
alpar@484
|
260 |
}
|
alpar@484
|
261 |
}
|
alpar@484
|
262 |
if (!found) {
|
alpar@484
|
263 |
++length;
|
alpar@484
|
264 |
indexes[length] = ix;
|
alpar@484
|
265 |
values[length] = value;
|
alpar@484
|
266 |
}
|
alpar@484
|
267 |
|
alpar@484
|
268 |
glp_set_mat_col(lp, jx, length, &indexes.front(), &values.front());
|
alpar@484
|
269 |
}
|
alpar@484
|
270 |
|
alpar@484
|
271 |
}
|
alpar@484
|
272 |
|
alpar@484
|
273 |
GlpkBase::Value GlpkBase::_getCoeff(int ix, int jx) const {
|
alpar@484
|
274 |
|
alpar@484
|
275 |
int length = glp_get_mat_row(lp, ix, 0, 0);
|
alpar@484
|
276 |
|
alpar@484
|
277 |
std::vector<int> indexes(length + 1);
|
alpar@484
|
278 |
std::vector<Value> values(length + 1);
|
alpar@484
|
279 |
|
alpar@484
|
280 |
glp_get_mat_row(lp, ix, &indexes.front(), &values.front());
|
alpar@484
|
281 |
|
alpar@484
|
282 |
for (int i = 1; i <= length; ++i) {
|
alpar@484
|
283 |
if (indexes[i] == jx) {
|
alpar@484
|
284 |
return values[i];
|
alpar@484
|
285 |
}
|
alpar@484
|
286 |
}
|
alpar@484
|
287 |
|
alpar@484
|
288 |
return 0;
|
alpar@484
|
289 |
}
|
alpar@484
|
290 |
|
alpar@484
|
291 |
void GlpkBase::_setColLowerBound(int i, Value lo) {
|
alpar@484
|
292 |
LEMON_ASSERT(lo != INF, "Invalid bound");
|
alpar@484
|
293 |
|
alpar@484
|
294 |
int b = glp_get_col_type(lp, i);
|
alpar@484
|
295 |
double up = glp_get_col_ub(lp, i);
|
alpar@484
|
296 |
if (lo == -INF) {
|
alpar@484
|
297 |
switch (b) {
|
alpar@484
|
298 |
case GLP_FR:
|
alpar@484
|
299 |
case GLP_LO:
|
alpar@484
|
300 |
glp_set_col_bnds(lp, i, GLP_FR, lo, up);
|
alpar@484
|
301 |
break;
|
alpar@484
|
302 |
case GLP_UP:
|
alpar@484
|
303 |
break;
|
alpar@484
|
304 |
case GLP_DB:
|
alpar@484
|
305 |
case GLP_FX:
|
alpar@484
|
306 |
glp_set_col_bnds(lp, i, GLP_UP, lo, up);
|
alpar@484
|
307 |
break;
|
alpar@484
|
308 |
default:
|
alpar@484
|
309 |
break;
|
alpar@484
|
310 |
}
|
alpar@484
|
311 |
} else {
|
alpar@484
|
312 |
switch (b) {
|
alpar@484
|
313 |
case GLP_FR:
|
alpar@484
|
314 |
case GLP_LO:
|
alpar@484
|
315 |
glp_set_col_bnds(lp, i, GLP_LO, lo, up);
|
alpar@484
|
316 |
break;
|
alpar@484
|
317 |
case GLP_UP:
|
alpar@484
|
318 |
case GLP_DB:
|
alpar@484
|
319 |
case GLP_FX:
|
alpar@484
|
320 |
if (lo == up)
|
alpar@484
|
321 |
glp_set_col_bnds(lp, i, GLP_FX, lo, up);
|
alpar@484
|
322 |
else
|
alpar@484
|
323 |
glp_set_col_bnds(lp, i, GLP_DB, lo, up);
|
alpar@484
|
324 |
break;
|
alpar@484
|
325 |
default:
|
alpar@484
|
326 |
break;
|
alpar@484
|
327 |
}
|
alpar@484
|
328 |
}
|
alpar@484
|
329 |
}
|
alpar@484
|
330 |
|
alpar@484
|
331 |
GlpkBase::Value GlpkBase::_getColLowerBound(int i) const {
|
alpar@484
|
332 |
int b = glp_get_col_type(lp, i);
|
alpar@484
|
333 |
switch (b) {
|
alpar@484
|
334 |
case GLP_LO:
|
alpar@484
|
335 |
case GLP_DB:
|
alpar@484
|
336 |
case GLP_FX:
|
alpar@484
|
337 |
return glp_get_col_lb(lp, i);
|
alpar@484
|
338 |
default:
|
alpar@484
|
339 |
return -INF;
|
alpar@484
|
340 |
}
|
alpar@484
|
341 |
}
|
alpar@484
|
342 |
|
alpar@484
|
343 |
void GlpkBase::_setColUpperBound(int i, Value up) {
|
alpar@484
|
344 |
LEMON_ASSERT(up != -INF, "Invalid bound");
|
alpar@484
|
345 |
|
alpar@484
|
346 |
int b = glp_get_col_type(lp, i);
|
alpar@484
|
347 |
double lo = glp_get_col_lb(lp, i);
|
alpar@484
|
348 |
if (up == INF) {
|
alpar@484
|
349 |
switch (b) {
|
alpar@484
|
350 |
case GLP_FR:
|
alpar@484
|
351 |
case GLP_LO:
|
alpar@484
|
352 |
break;
|
alpar@484
|
353 |
case GLP_UP:
|
alpar@484
|
354 |
glp_set_col_bnds(lp, i, GLP_FR, lo, up);
|
alpar@484
|
355 |
break;
|
alpar@484
|
356 |
case GLP_DB:
|
alpar@484
|
357 |
case GLP_FX:
|
alpar@484
|
358 |
glp_set_col_bnds(lp, i, GLP_LO, lo, up);
|
alpar@484
|
359 |
break;
|
alpar@484
|
360 |
default:
|
alpar@484
|
361 |
break;
|
alpar@484
|
362 |
}
|
alpar@484
|
363 |
} else {
|
alpar@484
|
364 |
switch (b) {
|
alpar@484
|
365 |
case GLP_FR:
|
alpar@484
|
366 |
glp_set_col_bnds(lp, i, GLP_UP, lo, up);
|
alpar@484
|
367 |
break;
|
alpar@484
|
368 |
case GLP_UP:
|
alpar@484
|
369 |
glp_set_col_bnds(lp, i, GLP_UP, lo, up);
|
alpar@484
|
370 |
break;
|
alpar@484
|
371 |
case GLP_LO:
|
alpar@484
|
372 |
case GLP_DB:
|
alpar@484
|
373 |
case GLP_FX:
|
alpar@484
|
374 |
if (lo == up)
|
alpar@484
|
375 |
glp_set_col_bnds(lp, i, GLP_FX, lo, up);
|
alpar@484
|
376 |
else
|
alpar@484
|
377 |
glp_set_col_bnds(lp, i, GLP_DB, lo, up);
|
alpar@484
|
378 |
break;
|
alpar@484
|
379 |
default:
|
alpar@484
|
380 |
break;
|
alpar@484
|
381 |
}
|
alpar@484
|
382 |
}
|
alpar@484
|
383 |
|
alpar@484
|
384 |
}
|
alpar@484
|
385 |
|
alpar@484
|
386 |
GlpkBase::Value GlpkBase::_getColUpperBound(int i) const {
|
alpar@484
|
387 |
int b = glp_get_col_type(lp, i);
|
alpar@484
|
388 |
switch (b) {
|
alpar@484
|
389 |
case GLP_UP:
|
alpar@484
|
390 |
case GLP_DB:
|
alpar@484
|
391 |
case GLP_FX:
|
alpar@484
|
392 |
return glp_get_col_ub(lp, i);
|
alpar@484
|
393 |
default:
|
alpar@484
|
394 |
return INF;
|
alpar@484
|
395 |
}
|
alpar@484
|
396 |
}
|
alpar@484
|
397 |
|
alpar@484
|
398 |
void GlpkBase::_setRowLowerBound(int i, Value lo) {
|
alpar@484
|
399 |
LEMON_ASSERT(lo != INF, "Invalid bound");
|
alpar@484
|
400 |
|
alpar@484
|
401 |
int b = glp_get_row_type(lp, i);
|
alpar@484
|
402 |
double up = glp_get_row_ub(lp, i);
|
alpar@484
|
403 |
if (lo == -INF) {
|
alpar@484
|
404 |
switch (b) {
|
alpar@484
|
405 |
case GLP_FR:
|
alpar@484
|
406 |
case GLP_LO:
|
alpar@484
|
407 |
glp_set_row_bnds(lp, i, GLP_FR, lo, up);
|
alpar@484
|
408 |
break;
|
alpar@484
|
409 |
case GLP_UP:
|
alpar@484
|
410 |
break;
|
alpar@484
|
411 |
case GLP_DB:
|
alpar@484
|
412 |
case GLP_FX:
|
alpar@484
|
413 |
glp_set_row_bnds(lp, i, GLP_UP, lo, up);
|
alpar@484
|
414 |
break;
|
alpar@484
|
415 |
default:
|
alpar@484
|
416 |
break;
|
alpar@484
|
417 |
}
|
alpar@484
|
418 |
} else {
|
alpar@484
|
419 |
switch (b) {
|
alpar@484
|
420 |
case GLP_FR:
|
alpar@484
|
421 |
case GLP_LO:
|
alpar@484
|
422 |
glp_set_row_bnds(lp, i, GLP_LO, lo, up);
|
alpar@484
|
423 |
break;
|
alpar@484
|
424 |
case GLP_UP:
|
alpar@484
|
425 |
case GLP_DB:
|
alpar@484
|
426 |
case GLP_FX:
|
alpar@484
|
427 |
if (lo == up)
|
alpar@484
|
428 |
glp_set_row_bnds(lp, i, GLP_FX, lo, up);
|
alpar@484
|
429 |
else
|
alpar@484
|
430 |
glp_set_row_bnds(lp, i, GLP_DB, lo, up);
|
alpar@484
|
431 |
break;
|
alpar@484
|
432 |
default:
|
alpar@484
|
433 |
break;
|
alpar@484
|
434 |
}
|
alpar@484
|
435 |
}
|
alpar@484
|
436 |
|
alpar@484
|
437 |
}
|
alpar@484
|
438 |
|
alpar@484
|
439 |
GlpkBase::Value GlpkBase::_getRowLowerBound(int i) const {
|
alpar@484
|
440 |
int b = glp_get_row_type(lp, i);
|
alpar@484
|
441 |
switch (b) {
|
alpar@484
|
442 |
case GLP_LO:
|
alpar@484
|
443 |
case GLP_DB:
|
alpar@484
|
444 |
case GLP_FX:
|
alpar@484
|
445 |
return glp_get_row_lb(lp, i);
|
alpar@484
|
446 |
default:
|
alpar@484
|
447 |
return -INF;
|
alpar@484
|
448 |
}
|
alpar@484
|
449 |
}
|
alpar@484
|
450 |
|
alpar@484
|
451 |
void GlpkBase::_setRowUpperBound(int i, Value up) {
|
alpar@484
|
452 |
LEMON_ASSERT(up != -INF, "Invalid bound");
|
alpar@484
|
453 |
|
alpar@484
|
454 |
int b = glp_get_row_type(lp, i);
|
alpar@484
|
455 |
double lo = glp_get_row_lb(lp, i);
|
alpar@484
|
456 |
if (up == INF) {
|
alpar@484
|
457 |
switch (b) {
|
alpar@484
|
458 |
case GLP_FR:
|
alpar@484
|
459 |
case GLP_LO:
|
alpar@484
|
460 |
break;
|
alpar@484
|
461 |
case GLP_UP:
|
alpar@484
|
462 |
glp_set_row_bnds(lp, i, GLP_FR, lo, up);
|
alpar@484
|
463 |
break;
|
alpar@484
|
464 |
case GLP_DB:
|
alpar@484
|
465 |
case GLP_FX:
|
alpar@484
|
466 |
glp_set_row_bnds(lp, i, GLP_LO, lo, up);
|
alpar@484
|
467 |
break;
|
alpar@484
|
468 |
default:
|
alpar@484
|
469 |
break;
|
alpar@484
|
470 |
}
|
alpar@484
|
471 |
} else {
|
alpar@484
|
472 |
switch (b) {
|
alpar@484
|
473 |
case GLP_FR:
|
alpar@484
|
474 |
glp_set_row_bnds(lp, i, GLP_UP, lo, up);
|
alpar@484
|
475 |
break;
|
alpar@484
|
476 |
case GLP_UP:
|
alpar@484
|
477 |
glp_set_row_bnds(lp, i, GLP_UP, lo, up);
|
alpar@484
|
478 |
break;
|
alpar@484
|
479 |
case GLP_LO:
|
alpar@484
|
480 |
case GLP_DB:
|
alpar@484
|
481 |
case GLP_FX:
|
alpar@484
|
482 |
if (lo == up)
|
alpar@484
|
483 |
glp_set_row_bnds(lp, i, GLP_FX, lo, up);
|
alpar@484
|
484 |
else
|
alpar@484
|
485 |
glp_set_row_bnds(lp, i, GLP_DB, lo, up);
|
alpar@484
|
486 |
break;
|
alpar@484
|
487 |
default:
|
alpar@484
|
488 |
break;
|
alpar@484
|
489 |
}
|
alpar@484
|
490 |
}
|
alpar@484
|
491 |
}
|
alpar@484
|
492 |
|
alpar@484
|
493 |
GlpkBase::Value GlpkBase::_getRowUpperBound(int i) const {
|
alpar@484
|
494 |
int b = glp_get_row_type(lp, i);
|
alpar@484
|
495 |
switch (b) {
|
alpar@484
|
496 |
case GLP_UP:
|
alpar@484
|
497 |
case GLP_DB:
|
alpar@484
|
498 |
case GLP_FX:
|
alpar@484
|
499 |
return glp_get_row_ub(lp, i);
|
alpar@484
|
500 |
default:
|
alpar@484
|
501 |
return INF;
|
alpar@484
|
502 |
}
|
alpar@484
|
503 |
}
|
alpar@484
|
504 |
|
alpar@484
|
505 |
void GlpkBase::_setObjCoeffs(ExprIterator b, ExprIterator e) {
|
alpar@484
|
506 |
for (int i = 1; i <= glp_get_num_cols(lp); ++i) {
|
alpar@484
|
507 |
glp_set_obj_coef(lp, i, 0.0);
|
alpar@484
|
508 |
}
|
alpar@484
|
509 |
for (ExprIterator it = b; it != e; ++it) {
|
alpar@484
|
510 |
glp_set_obj_coef(lp, it->first, it->second);
|
alpar@484
|
511 |
}
|
alpar@484
|
512 |
}
|
alpar@484
|
513 |
|
alpar@484
|
514 |
void GlpkBase::_getObjCoeffs(InsertIterator b) const {
|
alpar@484
|
515 |
for (int i = 1; i <= glp_get_num_cols(lp); ++i) {
|
alpar@484
|
516 |
Value val = glp_get_obj_coef(lp, i);
|
alpar@484
|
517 |
if (val != 0.0) {
|
alpar@484
|
518 |
*b = std::make_pair(i, val);
|
alpar@484
|
519 |
++b;
|
alpar@484
|
520 |
}
|
alpar@484
|
521 |
}
|
alpar@484
|
522 |
}
|
alpar@484
|
523 |
|
alpar@484
|
524 |
void GlpkBase::_setObjCoeff(int i, Value obj_coef) {
|
alpar@484
|
525 |
//i = 0 means the constant term (shift)
|
alpar@484
|
526 |
glp_set_obj_coef(lp, i, obj_coef);
|
alpar@484
|
527 |
}
|
alpar@484
|
528 |
|
alpar@484
|
529 |
GlpkBase::Value GlpkBase::_getObjCoeff(int i) const {
|
alpar@484
|
530 |
//i = 0 means the constant term (shift)
|
alpar@484
|
531 |
return glp_get_obj_coef(lp, i);
|
alpar@484
|
532 |
}
|
alpar@484
|
533 |
|
alpar@484
|
534 |
void GlpkBase::_setSense(GlpkBase::Sense sense) {
|
alpar@484
|
535 |
switch (sense) {
|
alpar@484
|
536 |
case MIN:
|
alpar@484
|
537 |
glp_set_obj_dir(lp, GLP_MIN);
|
alpar@484
|
538 |
break;
|
alpar@484
|
539 |
case MAX:
|
alpar@484
|
540 |
glp_set_obj_dir(lp, GLP_MAX);
|
alpar@484
|
541 |
break;
|
alpar@484
|
542 |
}
|
alpar@484
|
543 |
}
|
alpar@484
|
544 |
|
alpar@484
|
545 |
GlpkBase::Sense GlpkBase::_getSense() const {
|
alpar@484
|
546 |
switch(glp_get_obj_dir(lp)) {
|
alpar@484
|
547 |
case GLP_MIN:
|
alpar@484
|
548 |
return MIN;
|
alpar@484
|
549 |
case GLP_MAX:
|
alpar@484
|
550 |
return MAX;
|
alpar@484
|
551 |
default:
|
alpar@484
|
552 |
LEMON_ASSERT(false, "Wrong sense");
|
alpar@484
|
553 |
return GlpkBase::Sense();
|
alpar@484
|
554 |
}
|
alpar@484
|
555 |
}
|
alpar@484
|
556 |
|
alpar@484
|
557 |
void GlpkBase::_clear() {
|
alpar@484
|
558 |
glp_erase_prob(lp);
|
alpar@484
|
559 |
}
|
alpar@484
|
560 |
|
deba@584
|
561 |
void GlpkBase::freeEnv() {
|
deba@584
|
562 |
glp_free_env();
|
deba@584
|
563 |
}
|
deba@584
|
564 |
|
deba@623
|
565 |
void GlpkBase::_messageLevel(MessageLevel level) {
|
deba@623
|
566 |
switch (level) {
|
deba@623
|
567 |
case MESSAGE_NOTHING:
|
deba@623
|
568 |
_message_level = GLP_MSG_OFF;
|
deba@623
|
569 |
break;
|
deba@623
|
570 |
case MESSAGE_ERROR:
|
deba@623
|
571 |
_message_level = GLP_MSG_ERR;
|
deba@623
|
572 |
break;
|
deba@623
|
573 |
case MESSAGE_WARNING:
|
deba@623
|
574 |
_message_level = GLP_MSG_ERR;
|
deba@623
|
575 |
break;
|
deba@623
|
576 |
case MESSAGE_NORMAL:
|
deba@623
|
577 |
_message_level = GLP_MSG_ON;
|
deba@623
|
578 |
break;
|
deba@623
|
579 |
case MESSAGE_VERBOSE:
|
deba@623
|
580 |
_message_level = GLP_MSG_ALL;
|
deba@623
|
581 |
break;
|
deba@623
|
582 |
}
|
deba@623
|
583 |
}
|
deba@623
|
584 |
|
alpar@1231
|
585 |
void GlpkBase::_write(std::string file, std::string format) const
|
alpar@1231
|
586 |
{
|
alpar@1231
|
587 |
if(format == "MPS")
|
alpar@1231
|
588 |
glp_write_mps(lp, GLP_MPS_FILE, 0, file.c_str());
|
alpar@1231
|
589 |
else if(format == "LP")
|
alpar@1231
|
590 |
glp_write_lp(lp, 0, file.c_str());
|
alpar@1231
|
591 |
else throw UnsupportedFormatError(format);
|
alpar@1231
|
592 |
}
|
alpar@1231
|
593 |
|
deba@585
|
594 |
GlpkBase::FreeEnvHelper GlpkBase::freeEnvHelper;
|
deba@585
|
595 |
|
alpar@485
|
596 |
// GlpkLp members
|
alpar@484
|
597 |
|
alpar@485
|
598 |
GlpkLp::GlpkLp()
|
deba@598
|
599 |
: LpBase(), LpSolver(), GlpkBase() {
|
deba@612
|
600 |
presolver(false);
|
alpar@484
|
601 |
}
|
alpar@484
|
602 |
|
alpar@485
|
603 |
GlpkLp::GlpkLp(const GlpkLp& other)
|
deba@598
|
604 |
: LpBase(other), LpSolver(other), GlpkBase(other) {
|
deba@612
|
605 |
presolver(false);
|
alpar@484
|
606 |
}
|
alpar@484
|
607 |
|
alpar@587
|
608 |
GlpkLp* GlpkLp::newSolver() const { return new GlpkLp; }
|
alpar@587
|
609 |
GlpkLp* GlpkLp::cloneSolver() const { return new GlpkLp(*this); }
|
alpar@484
|
610 |
|
alpar@485
|
611 |
const char* GlpkLp::_solverName() const { return "GlpkLp"; }
|
alpar@484
|
612 |
|
alpar@485
|
613 |
void GlpkLp::_clear_temporals() {
|
alpar@484
|
614 |
_primal_ray.clear();
|
alpar@484
|
615 |
_dual_ray.clear();
|
alpar@484
|
616 |
}
|
alpar@484
|
617 |
|
alpar@485
|
618 |
GlpkLp::SolveExitStatus GlpkLp::_solve() {
|
alpar@484
|
619 |
return solvePrimal();
|
alpar@484
|
620 |
}
|
alpar@484
|
621 |
|
alpar@485
|
622 |
GlpkLp::SolveExitStatus GlpkLp::solvePrimal() {
|
alpar@484
|
623 |
_clear_temporals();
|
alpar@484
|
624 |
|
alpar@484
|
625 |
glp_smcp smcp;
|
alpar@484
|
626 |
glp_init_smcp(&smcp);
|
alpar@484
|
627 |
|
deba@623
|
628 |
smcp.msg_lev = _message_level;
|
deba@612
|
629 |
smcp.presolve = _presolve;
|
alpar@484
|
630 |
|
deba@612
|
631 |
// If the basis is not valid we get an error return value.
|
deba@612
|
632 |
// In this case we can try to create a new basis.
|
deba@612
|
633 |
switch (glp_simplex(lp, &smcp)) {
|
deba@612
|
634 |
case 0:
|
deba@612
|
635 |
break;
|
deba@612
|
636 |
case GLP_EBADB:
|
deba@612
|
637 |
case GLP_ESING:
|
deba@612
|
638 |
case GLP_ECOND:
|
deba@613
|
639 |
glp_term_out(false);
|
deba@612
|
640 |
glp_adv_basis(lp, 0);
|
deba@613
|
641 |
glp_term_out(true);
|
deba@612
|
642 |
if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
|
deba@612
|
643 |
break;
|
deba@612
|
644 |
default:
|
deba@612
|
645 |
return UNSOLVED;
|
deba@612
|
646 |
}
|
deba@612
|
647 |
|
alpar@484
|
648 |
return SOLVED;
|
alpar@484
|
649 |
}
|
alpar@484
|
650 |
|
alpar@485
|
651 |
GlpkLp::SolveExitStatus GlpkLp::solveDual() {
|
alpar@484
|
652 |
_clear_temporals();
|
alpar@484
|
653 |
|
alpar@484
|
654 |
glp_smcp smcp;
|
alpar@484
|
655 |
glp_init_smcp(&smcp);
|
alpar@484
|
656 |
|
deba@623
|
657 |
smcp.msg_lev = _message_level;
|
alpar@484
|
658 |
smcp.meth = GLP_DUAL;
|
deba@612
|
659 |
smcp.presolve = _presolve;
|
alpar@484
|
660 |
|
deba@612
|
661 |
// If the basis is not valid we get an error return value.
|
deba@612
|
662 |
// In this case we can try to create a new basis.
|
deba@612
|
663 |
switch (glp_simplex(lp, &smcp)) {
|
deba@612
|
664 |
case 0:
|
deba@612
|
665 |
break;
|
deba@612
|
666 |
case GLP_EBADB:
|
deba@612
|
667 |
case GLP_ESING:
|
deba@612
|
668 |
case GLP_ECOND:
|
deba@613
|
669 |
glp_term_out(false);
|
deba@612
|
670 |
glp_adv_basis(lp, 0);
|
deba@613
|
671 |
glp_term_out(true);
|
deba@612
|
672 |
if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
|
deba@612
|
673 |
break;
|
deba@612
|
674 |
default:
|
deba@612
|
675 |
return UNSOLVED;
|
deba@612
|
676 |
}
|
alpar@484
|
677 |
return SOLVED;
|
alpar@484
|
678 |
}
|
alpar@484
|
679 |
|
alpar@485
|
680 |
GlpkLp::Value GlpkLp::_getPrimal(int i) const {
|
alpar@484
|
681 |
return glp_get_col_prim(lp, i);
|
alpar@484
|
682 |
}
|
alpar@484
|
683 |
|
alpar@485
|
684 |
GlpkLp::Value GlpkLp::_getDual(int i) const {
|
alpar@484
|
685 |
return glp_get_row_dual(lp, i);
|
alpar@484
|
686 |
}
|
alpar@484
|
687 |
|
alpar@485
|
688 |
GlpkLp::Value GlpkLp::_getPrimalValue() const {
|
alpar@484
|
689 |
return glp_get_obj_val(lp);
|
alpar@484
|
690 |
}
|
alpar@484
|
691 |
|
alpar@485
|
692 |
GlpkLp::VarStatus GlpkLp::_getColStatus(int i) const {
|
alpar@484
|
693 |
switch (glp_get_col_stat(lp, i)) {
|
alpar@484
|
694 |
case GLP_BS:
|
alpar@484
|
695 |
return BASIC;
|
alpar@484
|
696 |
case GLP_UP:
|
alpar@484
|
697 |
return UPPER;
|
alpar@484
|
698 |
case GLP_LO:
|
alpar@484
|
699 |
return LOWER;
|
alpar@484
|
700 |
case GLP_NF:
|
alpar@484
|
701 |
return FREE;
|
alpar@484
|
702 |
case GLP_NS:
|
alpar@484
|
703 |
return FIXED;
|
alpar@484
|
704 |
default:
|
alpar@484
|
705 |
LEMON_ASSERT(false, "Wrong column status");
|
alpar@485
|
706 |
return GlpkLp::VarStatus();
|
alpar@484
|
707 |
}
|
alpar@484
|
708 |
}
|
alpar@484
|
709 |
|
alpar@485
|
710 |
GlpkLp::VarStatus GlpkLp::_getRowStatus(int i) const {
|
alpar@484
|
711 |
switch (glp_get_row_stat(lp, i)) {
|
alpar@484
|
712 |
case GLP_BS:
|
alpar@484
|
713 |
return BASIC;
|
alpar@484
|
714 |
case GLP_UP:
|
alpar@484
|
715 |
return UPPER;
|
alpar@484
|
716 |
case GLP_LO:
|
alpar@484
|
717 |
return LOWER;
|
alpar@484
|
718 |
case GLP_NF:
|
alpar@484
|
719 |
return FREE;
|
alpar@484
|
720 |
case GLP_NS:
|
alpar@484
|
721 |
return FIXED;
|
alpar@484
|
722 |
default:
|
alpar@484
|
723 |
LEMON_ASSERT(false, "Wrong row status");
|
alpar@485
|
724 |
return GlpkLp::VarStatus();
|
alpar@484
|
725 |
}
|
alpar@484
|
726 |
}
|
alpar@484
|
727 |
|
alpar@485
|
728 |
GlpkLp::Value GlpkLp::_getPrimalRay(int i) const {
|
alpar@484
|
729 |
if (_primal_ray.empty()) {
|
alpar@484
|
730 |
int row_num = glp_get_num_rows(lp);
|
alpar@484
|
731 |
int col_num = glp_get_num_cols(lp);
|
alpar@484
|
732 |
|
alpar@484
|
733 |
_primal_ray.resize(col_num + 1, 0.0);
|
alpar@484
|
734 |
|
alpar@484
|
735 |
int index = glp_get_unbnd_ray(lp);
|
alpar@484
|
736 |
if (index != 0) {
|
alpar@484
|
737 |
// The primal ray is found in primal simplex second phase
|
alpar@484
|
738 |
LEMON_ASSERT((index <= row_num ? glp_get_row_stat(lp, index) :
|
alpar@484
|
739 |
glp_get_col_stat(lp, index - row_num)) != GLP_BS,
|
alpar@484
|
740 |
"Wrong primal ray");
|
alpar@484
|
741 |
|
alpar@484
|
742 |
bool negate = glp_get_obj_dir(lp) == GLP_MAX;
|
alpar@484
|
743 |
|
alpar@484
|
744 |
if (index > row_num) {
|
alpar@484
|
745 |
_primal_ray[index - row_num] = 1.0;
|
alpar@484
|
746 |
if (glp_get_col_dual(lp, index - row_num) > 0) {
|
alpar@484
|
747 |
negate = !negate;
|
alpar@484
|
748 |
}
|
alpar@484
|
749 |
} else {
|
alpar@484
|
750 |
if (glp_get_row_dual(lp, index) > 0) {
|
alpar@484
|
751 |
negate = !negate;
|
alpar@484
|
752 |
}
|
alpar@484
|
753 |
}
|
alpar@484
|
754 |
|
alpar@484
|
755 |
std::vector<int> ray_indexes(row_num + 1);
|
alpar@484
|
756 |
std::vector<Value> ray_values(row_num + 1);
|
alpar@484
|
757 |
int ray_length = glp_eval_tab_col(lp, index, &ray_indexes.front(),
|
alpar@484
|
758 |
&ray_values.front());
|
alpar@484
|
759 |
|
alpar@484
|
760 |
for (int i = 1; i <= ray_length; ++i) {
|
alpar@484
|
761 |
if (ray_indexes[i] > row_num) {
|
alpar@484
|
762 |
_primal_ray[ray_indexes[i] - row_num] = ray_values[i];
|
alpar@484
|
763 |
}
|
alpar@484
|
764 |
}
|
alpar@484
|
765 |
|
alpar@484
|
766 |
if (negate) {
|
alpar@484
|
767 |
for (int i = 1; i <= col_num; ++i) {
|
alpar@484
|
768 |
_primal_ray[i] = - _primal_ray[i];
|
alpar@484
|
769 |
}
|
alpar@484
|
770 |
}
|
alpar@484
|
771 |
} else {
|
alpar@484
|
772 |
for (int i = 1; i <= col_num; ++i) {
|
alpar@484
|
773 |
_primal_ray[i] = glp_get_col_prim(lp, i);
|
alpar@484
|
774 |
}
|
alpar@484
|
775 |
}
|
alpar@484
|
776 |
}
|
alpar@484
|
777 |
return _primal_ray[i];
|
alpar@484
|
778 |
}
|
alpar@484
|
779 |
|
alpar@485
|
780 |
GlpkLp::Value GlpkLp::_getDualRay(int i) const {
|
alpar@484
|
781 |
if (_dual_ray.empty()) {
|
alpar@484
|
782 |
int row_num = glp_get_num_rows(lp);
|
alpar@484
|
783 |
|
alpar@484
|
784 |
_dual_ray.resize(row_num + 1, 0.0);
|
alpar@484
|
785 |
|
alpar@484
|
786 |
int index = glp_get_unbnd_ray(lp);
|
alpar@484
|
787 |
if (index != 0) {
|
alpar@484
|
788 |
// The dual ray is found in dual simplex second phase
|
alpar@484
|
789 |
LEMON_ASSERT((index <= row_num ? glp_get_row_stat(lp, index) :
|
alpar@484
|
790 |
glp_get_col_stat(lp, index - row_num)) == GLP_BS,
|
alpar@484
|
791 |
|
alpar@484
|
792 |
"Wrong dual ray");
|
alpar@484
|
793 |
|
alpar@484
|
794 |
int idx;
|
alpar@484
|
795 |
bool negate = false;
|
alpar@484
|
796 |
|
alpar@484
|
797 |
if (index > row_num) {
|
alpar@484
|
798 |
idx = glp_get_col_bind(lp, index - row_num);
|
alpar@484
|
799 |
if (glp_get_col_prim(lp, index - row_num) >
|
alpar@484
|
800 |
glp_get_col_ub(lp, index - row_num)) {
|
alpar@484
|
801 |
negate = true;
|
alpar@484
|
802 |
}
|
alpar@484
|
803 |
} else {
|
alpar@484
|
804 |
idx = glp_get_row_bind(lp, index);
|
alpar@484
|
805 |
if (glp_get_row_prim(lp, index) > glp_get_row_ub(lp, index)) {
|
alpar@484
|
806 |
negate = true;
|
alpar@484
|
807 |
}
|
alpar@484
|
808 |
}
|
alpar@484
|
809 |
|
alpar@484
|
810 |
_dual_ray[idx] = negate ? - 1.0 : 1.0;
|
alpar@484
|
811 |
|
alpar@484
|
812 |
glp_btran(lp, &_dual_ray.front());
|
alpar@484
|
813 |
} else {
|
alpar@484
|
814 |
double eps = 1e-7;
|
alpar@484
|
815 |
// The dual ray is found in primal simplex first phase
|
alpar@484
|
816 |
// We assume that the glpk minimizes the slack to get feasible solution
|
alpar@484
|
817 |
for (int i = 1; i <= row_num; ++i) {
|
alpar@484
|
818 |
int index = glp_get_bhead(lp, i);
|
alpar@484
|
819 |
if (index <= row_num) {
|
alpar@484
|
820 |
double res = glp_get_row_prim(lp, index);
|
alpar@484
|
821 |
if (res > glp_get_row_ub(lp, index) + eps) {
|
alpar@484
|
822 |
_dual_ray[i] = -1;
|
alpar@484
|
823 |
} else if (res < glp_get_row_lb(lp, index) - eps) {
|
alpar@484
|
824 |
_dual_ray[i] = 1;
|
alpar@484
|
825 |
} else {
|
alpar@484
|
826 |
_dual_ray[i] = 0;
|
alpar@484
|
827 |
}
|
alpar@484
|
828 |
_dual_ray[i] *= glp_get_rii(lp, index);
|
alpar@484
|
829 |
} else {
|
alpar@484
|
830 |
double res = glp_get_col_prim(lp, index - row_num);
|
alpar@484
|
831 |
if (res > glp_get_col_ub(lp, index - row_num) + eps) {
|
alpar@484
|
832 |
_dual_ray[i] = -1;
|
alpar@484
|
833 |
} else if (res < glp_get_col_lb(lp, index - row_num) - eps) {
|
alpar@484
|
834 |
_dual_ray[i] = 1;
|
alpar@484
|
835 |
} else {
|
alpar@484
|
836 |
_dual_ray[i] = 0;
|
alpar@484
|
837 |
}
|
alpar@484
|
838 |
_dual_ray[i] /= glp_get_sjj(lp, index - row_num);
|
alpar@484
|
839 |
}
|
alpar@484
|
840 |
}
|
alpar@484
|
841 |
|
alpar@484
|
842 |
glp_btran(lp, &_dual_ray.front());
|
alpar@484
|
843 |
|
alpar@484
|
844 |
for (int i = 1; i <= row_num; ++i) {
|
alpar@484
|
845 |
_dual_ray[i] /= glp_get_rii(lp, i);
|
alpar@484
|
846 |
}
|
alpar@484
|
847 |
}
|
alpar@484
|
848 |
}
|
alpar@484
|
849 |
return _dual_ray[i];
|
alpar@484
|
850 |
}
|
alpar@484
|
851 |
|
alpar@485
|
852 |
GlpkLp::ProblemType GlpkLp::_getPrimalType() const {
|
alpar@484
|
853 |
if (glp_get_status(lp) == GLP_OPT)
|
alpar@484
|
854 |
return OPTIMAL;
|
alpar@484
|
855 |
switch (glp_get_prim_stat(lp)) {
|
alpar@484
|
856 |
case GLP_UNDEF:
|
alpar@484
|
857 |
return UNDEFINED;
|
alpar@484
|
858 |
case GLP_FEAS:
|
alpar@484
|
859 |
case GLP_INFEAS:
|
alpar@484
|
860 |
if (glp_get_dual_stat(lp) == GLP_NOFEAS) {
|
alpar@484
|
861 |
return UNBOUNDED;
|
alpar@484
|
862 |
} else {
|
alpar@484
|
863 |
return UNDEFINED;
|
alpar@484
|
864 |
}
|
alpar@484
|
865 |
case GLP_NOFEAS:
|
alpar@484
|
866 |
return INFEASIBLE;
|
alpar@484
|
867 |
default:
|
alpar@484
|
868 |
LEMON_ASSERT(false, "Wrong primal type");
|
alpar@485
|
869 |
return GlpkLp::ProblemType();
|
alpar@484
|
870 |
}
|
alpar@484
|
871 |
}
|
alpar@484
|
872 |
|
alpar@485
|
873 |
GlpkLp::ProblemType GlpkLp::_getDualType() const {
|
alpar@484
|
874 |
if (glp_get_status(lp) == GLP_OPT)
|
alpar@484
|
875 |
return OPTIMAL;
|
alpar@484
|
876 |
switch (glp_get_dual_stat(lp)) {
|
alpar@484
|
877 |
case GLP_UNDEF:
|
alpar@484
|
878 |
return UNDEFINED;
|
alpar@484
|
879 |
case GLP_FEAS:
|
alpar@484
|
880 |
case GLP_INFEAS:
|
alpar@484
|
881 |
if (glp_get_prim_stat(lp) == GLP_NOFEAS) {
|
alpar@484
|
882 |
return UNBOUNDED;
|
alpar@484
|
883 |
} else {
|
alpar@484
|
884 |
return UNDEFINED;
|
alpar@484
|
885 |
}
|
alpar@484
|
886 |
case GLP_NOFEAS:
|
alpar@484
|
887 |
return INFEASIBLE;
|
alpar@484
|
888 |
default:
|
alpar@484
|
889 |
LEMON_ASSERT(false, "Wrong primal type");
|
alpar@485
|
890 |
return GlpkLp::ProblemType();
|
alpar@484
|
891 |
}
|
alpar@484
|
892 |
}
|
alpar@484
|
893 |
|
deba@612
|
894 |
void GlpkLp::presolver(bool presolve) {
|
deba@612
|
895 |
_presolve = presolve;
|
alpar@484
|
896 |
}
|
alpar@484
|
897 |
|
alpar@485
|
898 |
// GlpkMip members
|
alpar@484
|
899 |
|
alpar@485
|
900 |
GlpkMip::GlpkMip()
|
deba@598
|
901 |
: LpBase(), MipSolver(), GlpkBase() {
|
alpar@484
|
902 |
}
|
alpar@484
|
903 |
|
alpar@485
|
904 |
GlpkMip::GlpkMip(const GlpkMip& other)
|
deba@598
|
905 |
: LpBase(), MipSolver(), GlpkBase(other) {
|
alpar@484
|
906 |
}
|
alpar@484
|
907 |
|
alpar@485
|
908 |
void GlpkMip::_setColType(int i, GlpkMip::ColTypes col_type) {
|
alpar@484
|
909 |
switch (col_type) {
|
alpar@484
|
910 |
case INTEGER:
|
alpar@484
|
911 |
glp_set_col_kind(lp, i, GLP_IV);
|
alpar@484
|
912 |
break;
|
alpar@484
|
913 |
case REAL:
|
alpar@484
|
914 |
glp_set_col_kind(lp, i, GLP_CV);
|
alpar@484
|
915 |
break;
|
alpar@484
|
916 |
}
|
alpar@484
|
917 |
}
|
alpar@484
|
918 |
|
alpar@485
|
919 |
GlpkMip::ColTypes GlpkMip::_getColType(int i) const {
|
alpar@484
|
920 |
switch (glp_get_col_kind(lp, i)) {
|
alpar@484
|
921 |
case GLP_IV:
|
alpar@484
|
922 |
case GLP_BV:
|
alpar@484
|
923 |
return INTEGER;
|
alpar@484
|
924 |
default:
|
alpar@484
|
925 |
return REAL;
|
alpar@484
|
926 |
}
|
alpar@484
|
927 |
|
alpar@484
|
928 |
}
|
alpar@484
|
929 |
|
alpar@485
|
930 |
GlpkMip::SolveExitStatus GlpkMip::_solve() {
|
alpar@484
|
931 |
glp_smcp smcp;
|
alpar@484
|
932 |
glp_init_smcp(&smcp);
|
alpar@484
|
933 |
|
deba@623
|
934 |
smcp.msg_lev = _message_level;
|
alpar@484
|
935 |
smcp.meth = GLP_DUAL;
|
alpar@484
|
936 |
|
deba@612
|
937 |
// If the basis is not valid we get an error return value.
|
deba@612
|
938 |
// In this case we can try to create a new basis.
|
deba@612
|
939 |
switch (glp_simplex(lp, &smcp)) {
|
deba@612
|
940 |
case 0:
|
deba@612
|
941 |
break;
|
deba@612
|
942 |
case GLP_EBADB:
|
deba@612
|
943 |
case GLP_ESING:
|
deba@612
|
944 |
case GLP_ECOND:
|
deba@613
|
945 |
glp_term_out(false);
|
deba@612
|
946 |
glp_adv_basis(lp, 0);
|
deba@613
|
947 |
glp_term_out(true);
|
deba@612
|
948 |
if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
|
deba@612
|
949 |
break;
|
deba@612
|
950 |
default:
|
deba@612
|
951 |
return UNSOLVED;
|
deba@612
|
952 |
}
|
deba@612
|
953 |
|
alpar@484
|
954 |
if (glp_get_status(lp) != GLP_OPT) return SOLVED;
|
alpar@484
|
955 |
|
alpar@484
|
956 |
glp_iocp iocp;
|
alpar@484
|
957 |
glp_init_iocp(&iocp);
|
alpar@484
|
958 |
|
deba@623
|
959 |
iocp.msg_lev = _message_level;
|
alpar@484
|
960 |
|
alpar@484
|
961 |
if (glp_intopt(lp, &iocp) != 0) return UNSOLVED;
|
alpar@484
|
962 |
return SOLVED;
|
alpar@484
|
963 |
}
|
alpar@484
|
964 |
|
alpar@484
|
965 |
|
alpar@485
|
966 |
GlpkMip::ProblemType GlpkMip::_getType() const {
|
alpar@484
|
967 |
switch (glp_get_status(lp)) {
|
alpar@484
|
968 |
case GLP_OPT:
|
alpar@484
|
969 |
switch (glp_mip_status(lp)) {
|
alpar@484
|
970 |
case GLP_UNDEF:
|
alpar@484
|
971 |
return UNDEFINED;
|
alpar@484
|
972 |
case GLP_NOFEAS:
|
alpar@484
|
973 |
return INFEASIBLE;
|
alpar@484
|
974 |
case GLP_FEAS:
|
alpar@484
|
975 |
return FEASIBLE;
|
alpar@484
|
976 |
case GLP_OPT:
|
alpar@484
|
977 |
return OPTIMAL;
|
alpar@484
|
978 |
default:
|
alpar@484
|
979 |
LEMON_ASSERT(false, "Wrong problem type.");
|
alpar@485
|
980 |
return GlpkMip::ProblemType();
|
alpar@484
|
981 |
}
|
alpar@484
|
982 |
case GLP_NOFEAS:
|
alpar@484
|
983 |
return INFEASIBLE;
|
alpar@484
|
984 |
case GLP_INFEAS:
|
alpar@484
|
985 |
case GLP_FEAS:
|
alpar@484
|
986 |
if (glp_get_dual_stat(lp) == GLP_NOFEAS) {
|
alpar@484
|
987 |
return UNBOUNDED;
|
alpar@484
|
988 |
} else {
|
alpar@484
|
989 |
return UNDEFINED;
|
alpar@484
|
990 |
}
|
alpar@484
|
991 |
default:
|
alpar@484
|
992 |
LEMON_ASSERT(false, "Wrong problem type.");
|
alpar@485
|
993 |
return GlpkMip::ProblemType();
|
alpar@484
|
994 |
}
|
alpar@484
|
995 |
}
|
alpar@484
|
996 |
|
alpar@485
|
997 |
GlpkMip::Value GlpkMip::_getSol(int i) const {
|
alpar@484
|
998 |
return glp_mip_col_val(lp, i);
|
alpar@484
|
999 |
}
|
alpar@484
|
1000 |
|
alpar@485
|
1001 |
GlpkMip::Value GlpkMip::_getSolValue() const {
|
alpar@484
|
1002 |
return glp_mip_obj_val(lp);
|
alpar@484
|
1003 |
}
|
alpar@484
|
1004 |
|
alpar@587
|
1005 |
GlpkMip* GlpkMip::newSolver() const { return new GlpkMip; }
|
alpar@587
|
1006 |
GlpkMip* GlpkMip::cloneSolver() const {return new GlpkMip(*this); }
|
alpar@484
|
1007 |
|
alpar@485
|
1008 |
const char* GlpkMip::_solverName() const { return "GlpkMip"; }
|
alpar@484
|
1009 |
|
alpar@1231
|
1010 |
|
alpar@1231
|
1011 |
|
alpar@484
|
1012 |
} //END OF NAMESPACE LEMON
|