1 | /* -*- C++ -*- |
---|
2 | * src/lemon/lp_glpk.cc - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
6 | * |
---|
7 | * Permission to use, modify and distribute this software is granted |
---|
8 | * provided that this copyright notice appears in all copies. For |
---|
9 | * precise terms see the accompanying LICENSE file. |
---|
10 | * |
---|
11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
12 | * express or implied, and with no claim as to its suitability for any |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef LEMON_LP_GLPK_CC |
---|
18 | #define LEMON_LP_GLPK_CC |
---|
19 | |
---|
20 | ///\file |
---|
21 | ///\brief Implementation of the LEMON-GLPK lp solver interface. |
---|
22 | |
---|
23 | #include <lemon/lp_glpk.h> |
---|
24 | |
---|
25 | namespace lemon { |
---|
26 | |
---|
27 | /// \e |
---|
28 | int LpGlpk::_addCol() { |
---|
29 | int i=lpx_add_cols(lp, 1); |
---|
30 | _setColLowerBound(i, -INF); |
---|
31 | _setColUpperBound(i, INF); |
---|
32 | return i; |
---|
33 | } |
---|
34 | |
---|
35 | /// \e |
---|
36 | int LpGlpk::_addRow() { |
---|
37 | int i=lpx_add_rows(lp, 1); |
---|
38 | return i; |
---|
39 | } |
---|
40 | |
---|
41 | |
---|
42 | void LpGlpk::_setRowCoeffs(int i, |
---|
43 | int length, |
---|
44 | const int * indices, |
---|
45 | const Value * values ) |
---|
46 | { |
---|
47 | lpx_set_mat_row(lp, i, length, |
---|
48 | const_cast<int * >(indices) , |
---|
49 | const_cast<Value * >(values)); |
---|
50 | } |
---|
51 | |
---|
52 | void LpGlpk::_setColCoeffs(int i, |
---|
53 | int length, |
---|
54 | const int * indices, |
---|
55 | const Value * values) |
---|
56 | { |
---|
57 | lpx_set_mat_col(lp, i, length, |
---|
58 | const_cast<int * >(indices), |
---|
59 | const_cast<Value * >(values)); |
---|
60 | } |
---|
61 | |
---|
62 | void LpGlpk::_setColLowerBound(int i, Value lo) |
---|
63 | { |
---|
64 | if (lo==INF) { |
---|
65 | //FIXME error |
---|
66 | } |
---|
67 | int b=lpx_get_col_type(lp, i); |
---|
68 | double up=lpx_get_col_ub(lp, i); |
---|
69 | if (lo==-INF) { |
---|
70 | switch (b) { |
---|
71 | case LPX_FR: |
---|
72 | case LPX_LO: |
---|
73 | lpx_set_col_bnds(lp, i, LPX_FR, lo, up); |
---|
74 | break; |
---|
75 | case LPX_UP: |
---|
76 | break; |
---|
77 | case LPX_DB: |
---|
78 | case LPX_FX: |
---|
79 | lpx_set_col_bnds(lp, i, LPX_UP, lo, up); |
---|
80 | break; |
---|
81 | default: ; |
---|
82 | //FIXME error |
---|
83 | } |
---|
84 | } else { |
---|
85 | switch (b) { |
---|
86 | case LPX_FR: |
---|
87 | case LPX_LO: |
---|
88 | lpx_set_col_bnds(lp, i, LPX_LO, lo, up); |
---|
89 | break; |
---|
90 | case LPX_UP: |
---|
91 | case LPX_DB: |
---|
92 | case LPX_FX: |
---|
93 | if (lo==up) |
---|
94 | lpx_set_col_bnds(lp, i, LPX_FX, lo, up); |
---|
95 | else |
---|
96 | lpx_set_col_bnds(lp, i, LPX_DB, lo, up); |
---|
97 | break; |
---|
98 | default: ; |
---|
99 | //FIXME error |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | } |
---|
104 | |
---|
105 | void LpGlpk::_setColUpperBound(int i, Value up) |
---|
106 | { |
---|
107 | if (up==-INF) { |
---|
108 | //FIXME error |
---|
109 | } |
---|
110 | int b=lpx_get_col_type(lp, i); |
---|
111 | double lo=lpx_get_col_lb(lp, i); |
---|
112 | if (up==INF) { |
---|
113 | switch (b) { |
---|
114 | case LPX_FR: |
---|
115 | case LPX_LO: |
---|
116 | break; |
---|
117 | case LPX_UP: |
---|
118 | lpx_set_col_bnds(lp, i, LPX_FR, lo, up); |
---|
119 | break; |
---|
120 | case LPX_DB: |
---|
121 | case LPX_FX: |
---|
122 | lpx_set_col_bnds(lp, i, LPX_LO, lo, up); |
---|
123 | break; |
---|
124 | default: ; |
---|
125 | //FIXME error |
---|
126 | } |
---|
127 | } else { |
---|
128 | switch (b) { |
---|
129 | case LPX_FR: |
---|
130 | lpx_set_col_bnds(lp, i, LPX_UP, lo, up); |
---|
131 | break; |
---|
132 | case LPX_UP: |
---|
133 | lpx_set_col_bnds(lp, i, LPX_UP, lo, up); |
---|
134 | break; |
---|
135 | case LPX_LO: |
---|
136 | case LPX_DB: |
---|
137 | case LPX_FX: |
---|
138 | if (lo==up) |
---|
139 | lpx_set_col_bnds(lp, i, LPX_FX, lo, up); |
---|
140 | else |
---|
141 | lpx_set_col_bnds(lp, i, LPX_DB, lo, up); |
---|
142 | break; |
---|
143 | default: ; |
---|
144 | //FIXME error |
---|
145 | } |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | void LpGlpk::_setRowLowerBound(int i, Value lo) |
---|
150 | { |
---|
151 | if (lo==INF) { |
---|
152 | //FIXME error |
---|
153 | } |
---|
154 | int b=lpx_get_row_type(lp, i); |
---|
155 | double up=lpx_get_row_ub(lp, i); |
---|
156 | if (lo==-INF) { |
---|
157 | switch (b) { |
---|
158 | case LPX_FR: |
---|
159 | case LPX_LO: |
---|
160 | lpx_set_row_bnds(lp, i, LPX_FR, lo, up); |
---|
161 | break; |
---|
162 | case LPX_UP: |
---|
163 | break; |
---|
164 | case LPX_DB: |
---|
165 | case LPX_FX: |
---|
166 | lpx_set_row_bnds(lp, i, LPX_UP, lo, up); |
---|
167 | break; |
---|
168 | default: ; |
---|
169 | //FIXME error |
---|
170 | } |
---|
171 | } else { |
---|
172 | switch (b) { |
---|
173 | case LPX_FR: |
---|
174 | case LPX_LO: |
---|
175 | lpx_set_row_bnds(lp, i, LPX_LO, lo, up); |
---|
176 | break; |
---|
177 | case LPX_UP: |
---|
178 | case LPX_DB: |
---|
179 | case LPX_FX: |
---|
180 | if (lo==up) |
---|
181 | lpx_set_row_bnds(lp, i, LPX_FX, lo, up); |
---|
182 | else |
---|
183 | lpx_set_row_bnds(lp, i, LPX_DB, lo, up); |
---|
184 | break; |
---|
185 | default: ; |
---|
186 | //FIXME error |
---|
187 | } |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | void LpGlpk::_setRowUpperBound(int i, Value up) |
---|
192 | { |
---|
193 | if (up==-INF) { |
---|
194 | //FIXME error |
---|
195 | } |
---|
196 | int b=lpx_get_row_type(lp, i); |
---|
197 | double lo=lpx_get_row_lb(lp, i); |
---|
198 | if (up==INF) { |
---|
199 | switch (b) { |
---|
200 | case LPX_FR: |
---|
201 | case LPX_LO: |
---|
202 | break; |
---|
203 | case LPX_UP: |
---|
204 | lpx_set_row_bnds(lp, i, LPX_FR, lo, up); |
---|
205 | break; |
---|
206 | case LPX_DB: |
---|
207 | case LPX_FX: |
---|
208 | lpx_set_row_bnds(lp, i, LPX_LO, lo, up); |
---|
209 | break; |
---|
210 | default: ; |
---|
211 | //FIXME error |
---|
212 | } |
---|
213 | } else { |
---|
214 | switch (b) { |
---|
215 | case LPX_FR: |
---|
216 | lpx_set_row_bnds(lp, i, LPX_UP, lo, up); |
---|
217 | break; |
---|
218 | case LPX_UP: |
---|
219 | lpx_set_row_bnds(lp, i, LPX_UP, lo, up); |
---|
220 | break; |
---|
221 | case LPX_LO: |
---|
222 | case LPX_DB: |
---|
223 | case LPX_FX: |
---|
224 | if (lo==up) |
---|
225 | lpx_set_row_bnds(lp, i, LPX_FX, lo, up); |
---|
226 | else |
---|
227 | lpx_set_row_bnds(lp, i, LPX_DB, lo, up); |
---|
228 | break; |
---|
229 | default: ; |
---|
230 | //FIXME error |
---|
231 | } |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | void LpGlpk::_setObjCoeff(int i, Value obj_coef) |
---|
236 | { |
---|
237 | lpx_set_obj_coef(lp, i, obj_coef); |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | LpGlpk::SolveExitStatus LpGlpk::_solve() |
---|
242 | { |
---|
243 | int i= lpx_simplex(lp); |
---|
244 | switch (i) { |
---|
245 | case LPX_E_OK: |
---|
246 | return SOLVED; |
---|
247 | break; |
---|
248 | default: |
---|
249 | return UNSOLVED; |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | LpGlpk::Value LpGlpk::_getPrimal(int i) |
---|
254 | { |
---|
255 | return lpx_get_col_prim(lp,i); |
---|
256 | } |
---|
257 | |
---|
258 | |
---|
259 | LpGlpk::SolutionStatus LpGlpk::_getPrimalType() |
---|
260 | { |
---|
261 | int stat= lpx_get_status(lp); |
---|
262 | switch (stat) { |
---|
263 | case LPX_UNDEF://Undefined (no solve has been run yet) |
---|
264 | return UNDEFINED; |
---|
265 | break; |
---|
266 | case LPX_NOFEAS://There is no feasible solution (primal, I guess) |
---|
267 | case LPX_INFEAS://Infeasible |
---|
268 | return INFEASIBLE; |
---|
269 | break; |
---|
270 | case LPX_UNBND://Unbounded |
---|
271 | return INFINITE; |
---|
272 | break; |
---|
273 | case LPX_FEAS://Feasible |
---|
274 | return FEASIBLE; |
---|
275 | break; |
---|
276 | case LPX_OPT://Feasible |
---|
277 | return OPTIMAL; |
---|
278 | break; |
---|
279 | default: |
---|
280 | return UNDEFINED; //to avoid gcc warning |
---|
281 | //FIXME error |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | |
---|
286 | } //END OF NAMESPACE LEMON |
---|
287 | |
---|
288 | #endif //LEMON_LP_GLPK_CC |
---|