athos@1261
|
1 |
/* -*- C++ -*-
|
athos@1261
|
2 |
*
|
alpar@1956
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
alpar@1956
|
4 |
*
|
alpar@1956
|
5 |
* Copyright (C) 2003-2006
|
alpar@1956
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@1359
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
athos@1261
|
8 |
*
|
athos@1261
|
9 |
* Permission to use, modify and distribute this software is granted
|
athos@1261
|
10 |
* provided that this copyright notice appears in all copies. For
|
athos@1261
|
11 |
* precise terms see the accompanying LICENSE file.
|
athos@1261
|
12 |
*
|
athos@1261
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
athos@1261
|
14 |
* express or implied, and with no claim as to its suitability for any
|
athos@1261
|
15 |
* purpose.
|
athos@1261
|
16 |
*
|
athos@1261
|
17 |
*/
|
athos@1261
|
18 |
|
athos@1261
|
19 |
#ifndef LEMON_LP_GLPK_CC
|
athos@1261
|
20 |
#define LEMON_LP_GLPK_CC
|
athos@1261
|
21 |
|
athos@1261
|
22 |
///\file
|
athos@1261
|
23 |
///\brief Implementation of the LEMON-GLPK lp solver interface.
|
athos@1261
|
24 |
|
ladanyi@1305
|
25 |
#include <lemon/lp_glpk.h>
|
athos@1473
|
26 |
//#include <iostream>
|
athos@1261
|
27 |
namespace lemon {
|
athos@1261
|
28 |
|
alpar@1364
|
29 |
|
alpar@1321
|
30 |
LpGlpk::LpGlpk() : Parent(),
|
alpar@1321
|
31 |
lp(lpx_create_prob()) {
|
alpar@1321
|
32 |
///\todo constrol function for this:
|
alpar@1321
|
33 |
lpx_set_int_parm(lp, LPX_K_DUAL, 1);
|
alpar@1321
|
34 |
messageLevel(0);
|
alpar@1321
|
35 |
}
|
alpar@1321
|
36 |
|
alpar@1321
|
37 |
LpGlpk::~LpGlpk() {
|
alpar@1321
|
38 |
lpx_delete_prob(lp);
|
alpar@1321
|
39 |
}
|
alpar@1321
|
40 |
|
alpar@1321
|
41 |
int LpGlpk::_addCol() {
|
alpar@1321
|
42 |
int i=lpx_add_cols(lp, 1);
|
alpar@1321
|
43 |
_setColLowerBound(i, -INF);
|
alpar@1321
|
44 |
_setColUpperBound(i, INF);
|
alpar@1321
|
45 |
return i;
|
alpar@1321
|
46 |
}
|
alpar@1321
|
47 |
|
athos@1436
|
48 |
///\e
|
athos@1436
|
49 |
|
athos@1436
|
50 |
|
athos@1436
|
51 |
LpSolverBase &LpGlpk::_newLp()
|
athos@1436
|
52 |
{
|
athos@1436
|
53 |
LpGlpk* newlp=new LpGlpk();
|
athos@1436
|
54 |
return *newlp;
|
athos@1436
|
55 |
}
|
athos@1436
|
56 |
|
athos@1436
|
57 |
///\e
|
athos@1436
|
58 |
|
athos@1436
|
59 |
LpSolverBase &LpGlpk::_copyLp()
|
athos@1436
|
60 |
{
|
athos@1436
|
61 |
LpGlpk* newlp=new LpGlpk();
|
athos@1436
|
62 |
|
athos@1436
|
63 |
//Coefficient matrix, row bounds
|
athos@1436
|
64 |
lpx_add_rows(newlp->lp, lpx_get_num_rows(lp));
|
athos@1436
|
65 |
lpx_add_cols(newlp->lp, lpx_get_num_cols(lp));
|
athos@1436
|
66 |
int len;
|
athos@1436
|
67 |
int ind[1+lpx_get_num_cols(lp)];
|
athos@1436
|
68 |
Value val[1+lpx_get_num_cols(lp)];
|
athos@1436
|
69 |
for (int i=1;i<=lpx_get_num_rows(lp);++i){
|
athos@1436
|
70 |
len=lpx_get_mat_row(lp,i,ind,val);
|
athos@1436
|
71 |
lpx_set_mat_row(newlp->lp, i,len,ind,val);
|
athos@1436
|
72 |
lpx_set_row_bnds(newlp->lp,i,lpx_get_row_type(lp,i),
|
athos@1436
|
73 |
lpx_get_row_lb(lp,i),lpx_get_row_ub(lp,i));
|
athos@1436
|
74 |
}
|
athos@1436
|
75 |
|
athos@1436
|
76 |
//Objective function, coloumn bounds
|
athos@1436
|
77 |
lpx_set_obj_dir(newlp->lp, lpx_get_obj_dir(lp));
|
athos@1436
|
78 |
//Objectif function's constant term treated separately
|
athos@1436
|
79 |
lpx_set_obj_coef(newlp->lp,0,lpx_get_obj_coef(lp,0));
|
athos@1436
|
80 |
for (int i=1;i<=lpx_get_num_cols(lp);++i){
|
athos@1436
|
81 |
lpx_set_obj_coef(newlp->lp,i,lpx_get_obj_coef(lp,i));
|
athos@1436
|
82 |
lpx_set_col_bnds(newlp->lp,i,lpx_get_col_type(lp,i),
|
athos@1436
|
83 |
lpx_get_col_lb(lp,i),lpx_get_col_ub(lp,i));
|
athos@1436
|
84 |
|
athos@1436
|
85 |
|
athos@1436
|
86 |
}
|
athos@1436
|
87 |
|
athos@1436
|
88 |
return *newlp;
|
athos@1436
|
89 |
}
|
athos@1436
|
90 |
|
alpar@1321
|
91 |
int LpGlpk::_addRow() {
|
alpar@1321
|
92 |
int i=lpx_add_rows(lp, 1);
|
alpar@1321
|
93 |
return i;
|
alpar@1321
|
94 |
}
|
alpar@1321
|
95 |
|
alpar@1321
|
96 |
|
athos@1432
|
97 |
void LpGlpk::_eraseCol(int i) {
|
athos@1432
|
98 |
int cols[2];
|
athos@1432
|
99 |
cols[1]=i;
|
athos@1432
|
100 |
lpx_del_cols(lp, 1, cols);
|
athos@1432
|
101 |
}
|
athos@1432
|
102 |
|
athos@1432
|
103 |
void LpGlpk::_eraseRow(int i) {
|
athos@1432
|
104 |
int rows[2];
|
athos@1432
|
105 |
rows[1]=i;
|
athos@1432
|
106 |
lpx_del_rows(lp, 1, rows);
|
athos@1432
|
107 |
}
|
athos@1432
|
108 |
|
alpar@1895
|
109 |
void LpGlpk::_getColName(int col, std::string & name)
|
alpar@1895
|
110 |
{
|
alpar@1895
|
111 |
|
alpar@1895
|
112 |
char *n = lpx_get_col_name(lp,col);
|
alpar@1895
|
113 |
name = n?n:"";
|
alpar@1895
|
114 |
}
|
alpar@1895
|
115 |
|
alpar@1895
|
116 |
|
alpar@1895
|
117 |
void LpGlpk::_setColName(int col, const std::string & name)
|
alpar@1895
|
118 |
{
|
alpar@1895
|
119 |
lpx_set_col_name(lp,col,const_cast<char*>(name.c_str()));
|
alpar@1895
|
120 |
}
|
alpar@1895
|
121 |
|
alpar@1321
|
122 |
void LpGlpk::_setRowCoeffs(int i,
|
alpar@1321
|
123 |
int length,
|
alpar@1321
|
124 |
const int * indices,
|
alpar@1321
|
125 |
const Value * values )
|
alpar@1321
|
126 |
{
|
alpar@1321
|
127 |
lpx_set_mat_row(lp, i, length,
|
alpar@1321
|
128 |
const_cast<int * >(indices) ,
|
alpar@1321
|
129 |
const_cast<Value * >(values));
|
alpar@1321
|
130 |
}
|
alpar@1321
|
131 |
|
alpar@1321
|
132 |
void LpGlpk::_setColCoeffs(int i,
|
alpar@1321
|
133 |
int length,
|
alpar@1321
|
134 |
const int * indices,
|
alpar@1321
|
135 |
const Value * values)
|
alpar@1321
|
136 |
{
|
alpar@1321
|
137 |
lpx_set_mat_col(lp, i, length,
|
alpar@1321
|
138 |
const_cast<int * >(indices),
|
alpar@1321
|
139 |
const_cast<Value * >(values));
|
alpar@1321
|
140 |
}
|
athos@1431
|
141 |
|
athos@1431
|
142 |
|
athos@1431
|
143 |
void LpGlpk::_setCoeff(int row, int col, Value value)
|
athos@1431
|
144 |
{
|
athos@1431
|
145 |
///FIXME Of course this is not efficient at all, but GLPK knows not more.
|
athos@1431
|
146 |
// First approach: get one row, apply changes and set it again
|
athos@1431
|
147 |
//(one idea to improve this: maybe it is better to do this with 1 coloumn)
|
athos@1431
|
148 |
|
athos@1431
|
149 |
int mem_length=2+lpx_get_num_cols(lp);
|
athos@1431
|
150 |
int* indices = new int[mem_length];
|
athos@1431
|
151 |
Value* values = new Value[mem_length];
|
athos@1431
|
152 |
|
athos@1431
|
153 |
|
athos@1431
|
154 |
int length=lpx_get_mat_row(lp, row, indices, values);
|
athos@1431
|
155 |
|
athos@1431
|
156 |
//The following code does not suppose that the elements of the array indices are sorted
|
athos@1431
|
157 |
int i=1;
|
athos@1431
|
158 |
bool found=false;
|
athos@1431
|
159 |
while (i <= length && !found){
|
athos@1431
|
160 |
if (indices[i]==col){
|
athos@1431
|
161 |
found = true;
|
athos@1431
|
162 |
values[i]=value;
|
athos@1431
|
163 |
}
|
athos@1431
|
164 |
++i;
|
athos@1431
|
165 |
}
|
athos@1431
|
166 |
if (!found){
|
athos@1431
|
167 |
++length;
|
athos@1431
|
168 |
indices[length]=col;
|
athos@1431
|
169 |
values[length]=value;
|
athos@1431
|
170 |
}
|
athos@1431
|
171 |
|
athos@1431
|
172 |
lpx_set_mat_row(lp, row, length, indices, values);
|
athos@1431
|
173 |
delete [] indices;
|
athos@1431
|
174 |
delete [] values;
|
athos@1431
|
175 |
|
athos@1431
|
176 |
}
|
athos@1431
|
177 |
|
alpar@1321
|
178 |
void LpGlpk::_setColLowerBound(int i, Value lo)
|
alpar@1321
|
179 |
{
|
alpar@1321
|
180 |
if (lo==INF) {
|
alpar@1321
|
181 |
//FIXME error
|
alpar@1321
|
182 |
}
|
alpar@1321
|
183 |
int b=lpx_get_col_type(lp, i);
|
alpar@1321
|
184 |
double up=lpx_get_col_ub(lp, i);
|
alpar@1321
|
185 |
if (lo==-INF) {
|
alpar@1321
|
186 |
switch (b) {
|
alpar@1321
|
187 |
case LPX_FR:
|
alpar@1321
|
188 |
case LPX_LO:
|
alpar@1321
|
189 |
lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
|
alpar@1321
|
190 |
break;
|
alpar@1321
|
191 |
case LPX_UP:
|
alpar@1321
|
192 |
break;
|
alpar@1321
|
193 |
case LPX_DB:
|
alpar@1321
|
194 |
case LPX_FX:
|
alpar@1321
|
195 |
lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
|
alpar@1321
|
196 |
break;
|
alpar@1321
|
197 |
default: ;
|
alpar@1321
|
198 |
//FIXME error
|
alpar@1321
|
199 |
}
|
alpar@1321
|
200 |
} else {
|
alpar@1321
|
201 |
switch (b) {
|
alpar@1321
|
202 |
case LPX_FR:
|
alpar@1321
|
203 |
case LPX_LO:
|
alpar@1321
|
204 |
lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
|
alpar@1321
|
205 |
break;
|
alpar@1321
|
206 |
case LPX_UP:
|
alpar@1321
|
207 |
case LPX_DB:
|
alpar@1321
|
208 |
case LPX_FX:
|
alpar@1321
|
209 |
if (lo==up)
|
alpar@1321
|
210 |
lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
|
alpar@1321
|
211 |
else
|
alpar@1321
|
212 |
lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
|
alpar@1321
|
213 |
break;
|
alpar@1321
|
214 |
default: ;
|
alpar@1321
|
215 |
//FIXME error
|
alpar@1321
|
216 |
}
|
athos@1261
|
217 |
}
|
athos@1261
|
218 |
|
alpar@1321
|
219 |
}
|
alpar@1321
|
220 |
|
alpar@1321
|
221 |
void LpGlpk::_setColUpperBound(int i, Value up)
|
alpar@1321
|
222 |
{
|
alpar@1321
|
223 |
if (up==-INF) {
|
alpar@1321
|
224 |
//FIXME error
|
athos@1261
|
225 |
}
|
alpar@1321
|
226 |
int b=lpx_get_col_type(lp, i);
|
alpar@1321
|
227 |
double lo=lpx_get_col_lb(lp, i);
|
alpar@1321
|
228 |
if (up==INF) {
|
alpar@1321
|
229 |
switch (b) {
|
alpar@1321
|
230 |
case LPX_FR:
|
alpar@1321
|
231 |
case LPX_LO:
|
alpar@1321
|
232 |
break;
|
alpar@1321
|
233 |
case LPX_UP:
|
alpar@1321
|
234 |
lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
|
alpar@1321
|
235 |
break;
|
alpar@1321
|
236 |
case LPX_DB:
|
alpar@1321
|
237 |
case LPX_FX:
|
alpar@1321
|
238 |
lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
|
alpar@1321
|
239 |
break;
|
alpar@1321
|
240 |
default: ;
|
athos@1261
|
241 |
//FIXME error
|
athos@1261
|
242 |
}
|
alpar@1321
|
243 |
} else {
|
alpar@1321
|
244 |
switch (b) {
|
alpar@1321
|
245 |
case LPX_FR:
|
alpar@1321
|
246 |
lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
|
alpar@1321
|
247 |
break;
|
alpar@1321
|
248 |
case LPX_UP:
|
alpar@1321
|
249 |
lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
|
alpar@1321
|
250 |
break;
|
alpar@1321
|
251 |
case LPX_LO:
|
alpar@1321
|
252 |
case LPX_DB:
|
alpar@1321
|
253 |
case LPX_FX:
|
alpar@1321
|
254 |
if (lo==up)
|
alpar@1321
|
255 |
lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
|
alpar@1321
|
256 |
else
|
alpar@1321
|
257 |
lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
|
alpar@1321
|
258 |
break;
|
alpar@1321
|
259 |
default: ;
|
athos@1261
|
260 |
//FIXME error
|
athos@1261
|
261 |
}
|
alpar@1321
|
262 |
}
|
alpar@1321
|
263 |
}
|
alpar@1321
|
264 |
|
athos@1405
|
265 |
// void LpGlpk::_setRowLowerBound(int i, Value lo)
|
athos@1405
|
266 |
// {
|
athos@1405
|
267 |
// if (lo==INF) {
|
athos@1405
|
268 |
// //FIXME error
|
athos@1405
|
269 |
// }
|
athos@1405
|
270 |
// int b=lpx_get_row_type(lp, i);
|
athos@1405
|
271 |
// double up=lpx_get_row_ub(lp, i);
|
athos@1405
|
272 |
// if (lo==-INF) {
|
athos@1405
|
273 |
// switch (b) {
|
athos@1405
|
274 |
// case LPX_FR:
|
athos@1405
|
275 |
// case LPX_LO:
|
athos@1405
|
276 |
// lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
|
athos@1405
|
277 |
// break;
|
athos@1405
|
278 |
// case LPX_UP:
|
athos@1405
|
279 |
// break;
|
athos@1405
|
280 |
// case LPX_DB:
|
athos@1405
|
281 |
// case LPX_FX:
|
athos@1405
|
282 |
// lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
|
athos@1405
|
283 |
// break;
|
athos@1405
|
284 |
// default: ;
|
athos@1405
|
285 |
// //FIXME error
|
athos@1405
|
286 |
// }
|
athos@1405
|
287 |
// } else {
|
athos@1405
|
288 |
// switch (b) {
|
athos@1405
|
289 |
// case LPX_FR:
|
athos@1405
|
290 |
// case LPX_LO:
|
athos@1405
|
291 |
// lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
|
athos@1405
|
292 |
// break;
|
athos@1405
|
293 |
// case LPX_UP:
|
athos@1405
|
294 |
// case LPX_DB:
|
athos@1405
|
295 |
// case LPX_FX:
|
athos@1405
|
296 |
// if (lo==up)
|
athos@1405
|
297 |
// lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
|
athos@1405
|
298 |
// else
|
athos@1405
|
299 |
// lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
|
athos@1405
|
300 |
// break;
|
athos@1405
|
301 |
// default: ;
|
athos@1405
|
302 |
// //FIXME error
|
athos@1405
|
303 |
// }
|
athos@1405
|
304 |
// }
|
athos@1405
|
305 |
// }
|
athos@1261
|
306 |
|
athos@1405
|
307 |
// void LpGlpk::_setRowUpperBound(int i, Value up)
|
athos@1405
|
308 |
// {
|
athos@1405
|
309 |
// if (up==-INF) {
|
athos@1405
|
310 |
// //FIXME error
|
athos@1405
|
311 |
// }
|
athos@1405
|
312 |
// int b=lpx_get_row_type(lp, i);
|
athos@1405
|
313 |
// double lo=lpx_get_row_lb(lp, i);
|
athos@1405
|
314 |
// if (up==INF) {
|
athos@1405
|
315 |
// switch (b) {
|
athos@1405
|
316 |
// case LPX_FR:
|
athos@1405
|
317 |
// case LPX_LO:
|
athos@1405
|
318 |
// break;
|
athos@1405
|
319 |
// case LPX_UP:
|
athos@1405
|
320 |
// lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
|
athos@1405
|
321 |
// break;
|
athos@1405
|
322 |
// case LPX_DB:
|
athos@1405
|
323 |
// case LPX_FX:
|
athos@1405
|
324 |
// lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
|
athos@1405
|
325 |
// break;
|
athos@1405
|
326 |
// default: ;
|
athos@1405
|
327 |
// //FIXME error
|
athos@1405
|
328 |
// }
|
athos@1405
|
329 |
// } else {
|
athos@1405
|
330 |
// switch (b) {
|
athos@1405
|
331 |
// case LPX_FR:
|
athos@1405
|
332 |
// lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
|
athos@1405
|
333 |
// break;
|
athos@1405
|
334 |
// case LPX_UP:
|
athos@1405
|
335 |
// lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
|
athos@1405
|
336 |
// break;
|
athos@1405
|
337 |
// case LPX_LO:
|
athos@1405
|
338 |
// case LPX_DB:
|
athos@1405
|
339 |
// case LPX_FX:
|
athos@1405
|
340 |
// if (lo==up)
|
athos@1405
|
341 |
// lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
|
athos@1405
|
342 |
// else
|
athos@1405
|
343 |
// lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
|
athos@1405
|
344 |
// break;
|
athos@1405
|
345 |
// default: ;
|
athos@1405
|
346 |
// //FIXME error
|
athos@1405
|
347 |
// }
|
athos@1405
|
348 |
// }
|
athos@1405
|
349 |
// }
|
athos@1379
|
350 |
|
athos@1379
|
351 |
void LpGlpk::_setRowBounds(int i, Value lb, Value ub)
|
athos@1379
|
352 |
{
|
athos@1379
|
353 |
//Bad parameter
|
athos@1379
|
354 |
if (lb==INF || ub==-INF) {
|
athos@1379
|
355 |
//FIXME error
|
athos@1379
|
356 |
}
|
athos@1379
|
357 |
|
athos@1379
|
358 |
if (lb == -INF){
|
athos@1379
|
359 |
if (ub == INF){
|
athos@1379
|
360 |
lpx_set_row_bnds(lp, i, LPX_FR, lb, ub);
|
athos@1379
|
361 |
}
|
athos@1379
|
362 |
else{
|
athos@1379
|
363 |
lpx_set_row_bnds(lp, i, LPX_UP, lb, ub);
|
athos@1379
|
364 |
}
|
athos@1379
|
365 |
}
|
athos@1379
|
366 |
else{
|
athos@1379
|
367 |
if (ub==INF){
|
athos@1379
|
368 |
lpx_set_row_bnds(lp, i, LPX_LO, lb, ub);
|
athos@1379
|
369 |
|
athos@1379
|
370 |
}
|
athos@1379
|
371 |
else{
|
athos@1379
|
372 |
if (lb == ub){
|
athos@1379
|
373 |
lpx_set_row_bnds(lp, i, LPX_FX, lb, ub);
|
athos@1379
|
374 |
}
|
athos@1379
|
375 |
else{
|
athos@1379
|
376 |
lpx_set_row_bnds(lp, i, LPX_DB, lb, ub);
|
athos@1379
|
377 |
}
|
athos@1379
|
378 |
}
|
athos@1379
|
379 |
}
|
athos@1379
|
380 |
|
athos@1379
|
381 |
}
|
athos@1261
|
382 |
|
athos@1298
|
383 |
void LpGlpk::_setObjCoeff(int i, Value obj_coef)
|
athos@1298
|
384 |
{
|
athos@1376
|
385 |
//i=0 means the constant term (shift)
|
athos@1298
|
386 |
lpx_set_obj_coef(lp, i, obj_coef);
|
athos@1298
|
387 |
}
|
athos@1261
|
388 |
|
athos@1377
|
389 |
void LpGlpk::_clearObj()
|
athos@1376
|
390 |
{
|
athos@1377
|
391 |
for (int i=0;i<=lpx_get_num_cols(lp);++i){
|
athos@1377
|
392 |
lpx_set_obj_coef(lp, i, 0);
|
athos@1376
|
393 |
}
|
athos@1376
|
394 |
}
|
athos@1377
|
395 |
// void LpGlpk::_setObj(int length,
|
athos@1377
|
396 |
// int const * indices,
|
athos@1377
|
397 |
// Value const * values )
|
athos@1377
|
398 |
// {
|
athos@1377
|
399 |
// Value new_values[1+lpx_num_cols()];
|
athos@1377
|
400 |
// for (i=0;i<=lpx_num_cols();++i){
|
athos@1377
|
401 |
// new_values[i]=0;
|
athos@1377
|
402 |
// }
|
athos@1377
|
403 |
// for (i=1;i<=length;++i){
|
athos@1377
|
404 |
// new_values[indices[i]]=values[i];
|
athos@1377
|
405 |
// }
|
athos@1377
|
406 |
|
athos@1377
|
407 |
// for (i=0;i<=lpx_num_cols();++i){
|
athos@1377
|
408 |
// lpx_set_obj_coef(lp, i, new_values[i]);
|
athos@1377
|
409 |
// }
|
athos@1377
|
410 |
// }
|
alpar@1263
|
411 |
|
alpar@1303
|
412 |
LpGlpk::SolveExitStatus LpGlpk::_solve()
|
alpar@1263
|
413 |
{
|
athos@1458
|
414 |
int i = lpx_simplex(lp);
|
athos@1298
|
415 |
switch (i) {
|
athos@1298
|
416 |
case LPX_E_OK:
|
athos@1298
|
417 |
return SOLVED;
|
athos@1298
|
418 |
default:
|
athos@1298
|
419 |
return UNSOLVED;
|
athos@1298
|
420 |
}
|
alpar@1263
|
421 |
}
|
alpar@1263
|
422 |
|
alpar@1293
|
423 |
LpGlpk::Value LpGlpk::_getPrimal(int i)
|
alpar@1263
|
424 |
{
|
athos@1298
|
425 |
return lpx_get_col_prim(lp,i);
|
alpar@1263
|
426 |
}
|
marci@1787
|
427 |
|
marci@1787
|
428 |
LpGlpk::Value LpGlpk::_getDual(int i)
|
marci@1787
|
429 |
{
|
marci@1787
|
430 |
return lpx_get_row_dual(lp,i);
|
marci@1787
|
431 |
}
|
alpar@1263
|
432 |
|
alpar@1312
|
433 |
LpGlpk::Value LpGlpk::_getPrimalValue()
|
alpar@1312
|
434 |
{
|
athos@1314
|
435 |
return lpx_get_obj_val(lp);
|
alpar@1312
|
436 |
}
|
marci@1840
|
437 |
bool LpGlpk::_isBasicCol(int i) {
|
marci@1840
|
438 |
return (lpx_get_col_stat(lp, i)==LPX_BS);
|
marci@1840
|
439 |
}
|
alpar@1312
|
440 |
|
athos@1298
|
441 |
|
alpar@1312
|
442 |
LpGlpk::SolutionStatus LpGlpk::_getPrimalStatus()
|
alpar@1294
|
443 |
{
|
athos@1298
|
444 |
int stat= lpx_get_status(lp);
|
athos@1298
|
445 |
switch (stat) {
|
athos@1298
|
446 |
case LPX_UNDEF://Undefined (no solve has been run yet)
|
athos@1298
|
447 |
return UNDEFINED;
|
athos@1458
|
448 |
case LPX_NOFEAS://There is no feasible solution (primal, I guess)
|
athos@1458
|
449 |
case LPX_INFEAS://Infeasible
|
athos@1458
|
450 |
return INFEASIBLE;
|
athos@1458
|
451 |
case LPX_UNBND://Unbounded
|
athos@1458
|
452 |
return INFINITE;
|
athos@1458
|
453 |
case LPX_FEAS://Feasible
|
athos@1458
|
454 |
return FEASIBLE;
|
athos@1458
|
455 |
case LPX_OPT://Feasible
|
athos@1458
|
456 |
return OPTIMAL;
|
athos@1458
|
457 |
default:
|
athos@1458
|
458 |
return UNDEFINED; //to avoid gcc warning
|
athos@1458
|
459 |
//FIXME error
|
athos@1458
|
460 |
}
|
athos@1458
|
461 |
}
|
athos@1458
|
462 |
|
athos@1458
|
463 |
LpGlpk::SolutionStatus LpGlpk::_getDualStatus()
|
athos@1458
|
464 |
{
|
athos@1473
|
465 |
// std::cout<<"Itt megy: "<<lpx_get_dual_stat(lp)<<std::endl;
|
athos@1473
|
466 |
// std::cout<<"Itt a primal: "<<lpx_get_prim_stat(lp)<<std::endl;
|
athos@1473
|
467 |
|
alpar@1466
|
468 |
switch (lpx_get_dual_stat(lp)) {
|
athos@1458
|
469 |
case LPX_D_UNDEF://Undefined (no solve has been run yet)
|
athos@1458
|
470 |
return UNDEFINED;
|
athos@1540
|
471 |
case LPX_D_NOFEAS://There is no dual feasible solution
|
athos@1460
|
472 |
// case LPX_D_INFEAS://Infeasible
|
athos@1458
|
473 |
return INFEASIBLE;
|
athos@1473
|
474 |
case LPX_D_FEAS://Feasible
|
athos@1473
|
475 |
switch (lpx_get_status(lp)) {
|
athos@1473
|
476 |
case LPX_NOFEAS:
|
athos@1458
|
477 |
return INFINITE;
|
athos@1458
|
478 |
case LPX_OPT:
|
athos@1458
|
479 |
return OPTIMAL;
|
athos@1458
|
480 |
default:
|
athos@1458
|
481 |
return FEASIBLE;
|
athos@1458
|
482 |
}
|
athos@1458
|
483 |
default:
|
athos@1458
|
484 |
return UNDEFINED; //to avoid gcc warning
|
athos@1458
|
485 |
//FIXME error
|
athos@1458
|
486 |
}
|
athos@1458
|
487 |
}
|
athos@1458
|
488 |
|
athos@1463
|
489 |
LpGlpk::ProblemTypes LpGlpk::_getProblemType()
|
athos@1458
|
490 |
{
|
athos@1460
|
491 |
//int stat= lpx_get_status(lp);
|
athos@1458
|
492 |
int statp= lpx_get_prim_stat(lp);
|
athos@1458
|
493 |
int statd= lpx_get_dual_stat(lp);
|
athos@1464
|
494 |
if (statp==LPX_P_FEAS && statd==LPX_D_FEAS)
|
athos@1460
|
495 |
return PRIMAL_DUAL_FEASIBLE;
|
athos@1464
|
496 |
if (statp==LPX_P_FEAS && statd==LPX_D_NOFEAS)
|
athos@1460
|
497 |
return PRIMAL_FEASIBLE_DUAL_INFEASIBLE;
|
athos@1464
|
498 |
if (statp==LPX_P_NOFEAS && statd==LPX_D_FEAS)
|
athos@1460
|
499 |
return PRIMAL_INFEASIBLE_DUAL_FEASIBLE;
|
athos@1464
|
500 |
if (statp==LPX_P_NOFEAS && statd==LPX_D_NOFEAS)
|
athos@1460
|
501 |
return PRIMAL_DUAL_INFEASIBLE;
|
athos@1460
|
502 |
//In all other cases
|
athos@1460
|
503 |
return UNKNOWN;
|
alpar@1294
|
504 |
}
|
alpar@1263
|
505 |
|
alpar@1312
|
506 |
void LpGlpk::_setMax()
|
alpar@1312
|
507 |
{
|
alpar@1321
|
508 |
lpx_set_obj_dir(lp, LPX_MAX);
|
alpar@1321
|
509 |
}
|
alpar@1321
|
510 |
|
alpar@1312
|
511 |
void LpGlpk::_setMin()
|
alpar@1312
|
512 |
{
|
alpar@1321
|
513 |
lpx_set_obj_dir(lp, LPX_MIN);
|
alpar@1321
|
514 |
}
|
alpar@1321
|
515 |
|
alpar@1321
|
516 |
|
alpar@1321
|
517 |
void LpGlpk::messageLevel(int m)
|
alpar@1321
|
518 |
{
|
alpar@1321
|
519 |
lpx_set_int_parm(lp, LPX_K_MSGLEV, m);
|
alpar@1321
|
520 |
}
|
alpar@1312
|
521 |
|
alpar@1326
|
522 |
void LpGlpk::presolver(bool b)
|
alpar@1326
|
523 |
{
|
alpar@1326
|
524 |
lpx_set_int_parm(lp, LPX_K_PRESOL, b);
|
alpar@1326
|
525 |
}
|
alpar@1326
|
526 |
|
alpar@1312
|
527 |
|
athos@1261
|
528 |
} //END OF NAMESPACE LEMON
|
athos@1261
|
529 |
|
athos@1261
|
530 |
#endif //LEMON_LP_GLPK_CC
|