2 * src/lemon/lp_glpk.cc - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
17 #ifndef LEMON_LP_GLPK_CC
18 #define LEMON_LP_GLPK_CC
21 ///\brief Implementation of the LEMON-GLPK lp solver interface.
23 #include <lemon/lp_glpk.h>
29 ///\bug Unimplemented!
31 LpSolverBase &LpGlpk::_newLp()
39 ///\bug Unimplemented!
41 LpSolverBase &LpGlpk::_copyLp()
47 LpGlpk::LpGlpk() : Parent(),
48 lp(lpx_create_prob()) {
49 ///\todo constrol function for this:
50 lpx_set_int_parm(lp, LPX_K_DUAL, 1);
58 int LpGlpk::_addCol() {
59 int i=lpx_add_cols(lp, 1);
60 _setColLowerBound(i, -INF);
61 _setColUpperBound(i, INF);
65 int LpGlpk::_addRow() {
66 int i=lpx_add_rows(lp, 1);
71 void LpGlpk::_setRowCoeffs(int i,
74 const Value * values )
76 lpx_set_mat_row(lp, i, length,
77 const_cast<int * >(indices) ,
78 const_cast<Value * >(values));
81 void LpGlpk::_setColCoeffs(int i,
86 lpx_set_mat_col(lp, i, length,
87 const_cast<int * >(indices),
88 const_cast<Value * >(values));
91 void LpGlpk::_setColLowerBound(int i, Value lo)
96 int b=lpx_get_col_type(lp, i);
97 double up=lpx_get_col_ub(lp, i);
102 lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
108 lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
117 lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
123 lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
125 lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
134 void LpGlpk::_setColUpperBound(int i, Value up)
139 int b=lpx_get_col_type(lp, i);
140 double lo=lpx_get_col_lb(lp, i);
147 lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
151 lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
159 lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
162 lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
168 lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
170 lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
178 void LpGlpk::_setRowLowerBound(int i, Value lo)
183 int b=lpx_get_row_type(lp, i);
184 double up=lpx_get_row_ub(lp, i);
189 lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
195 lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
204 lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
210 lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
212 lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
220 void LpGlpk::_setRowUpperBound(int i, Value up)
225 int b=lpx_get_row_type(lp, i);
226 double lo=lpx_get_row_lb(lp, i);
233 lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
237 lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
245 lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
248 lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
254 lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
256 lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
264 void LpGlpk::_setRowBounds(int i, Value lb, Value ub)
267 if (lb==INF || ub==-INF) {
273 lpx_set_row_bnds(lp, i, LPX_FR, lb, ub);
276 lpx_set_row_bnds(lp, i, LPX_UP, lb, ub);
281 lpx_set_row_bnds(lp, i, LPX_LO, lb, ub);
286 lpx_set_row_bnds(lp, i, LPX_FX, lb, ub);
289 lpx_set_row_bnds(lp, i, LPX_DB, lb, ub);
296 void LpGlpk::_setObjCoeff(int i, Value obj_coef)
298 //i=0 means the constant term (shift)
299 lpx_set_obj_coef(lp, i, obj_coef);
302 void LpGlpk::_clearObj()
304 for (int i=0;i<=lpx_get_num_cols(lp);++i){
305 lpx_set_obj_coef(lp, i, 0);
308 // void LpGlpk::_setObj(int length,
309 // int const * indices,
310 // Value const * values )
312 // Value new_values[1+lpx_num_cols()];
313 // for (i=0;i<=lpx_num_cols();++i){
316 // for (i=1;i<=length;++i){
317 // new_values[indices[i]]=values[i];
320 // for (i=0;i<=lpx_num_cols();++i){
321 // lpx_set_obj_coef(lp, i, new_values[i]);
325 LpGlpk::SolveExitStatus LpGlpk::_solve()
327 int i= lpx_simplex(lp);
337 LpGlpk::Value LpGlpk::_getPrimal(int i)
339 return lpx_get_col_prim(lp,i);
342 LpGlpk::Value LpGlpk::_getPrimalValue()
344 return lpx_get_obj_val(lp);
348 LpGlpk::SolutionStatus LpGlpk::_getPrimalStatus()
350 int stat= lpx_get_status(lp);
352 case LPX_UNDEF://Undefined (no solve has been run yet)
355 case LPX_NOFEAS://There is no feasible solution (primal, I guess)
356 case LPX_INFEAS://Infeasible
359 case LPX_UNBND://Unbounded
362 case LPX_FEAS://Feasible
365 case LPX_OPT://Feasible
369 return UNDEFINED; //to avoid gcc warning
375 void LpGlpk::_setMax()
377 lpx_set_obj_dir(lp, LPX_MAX);
380 void LpGlpk::_setMin()
382 lpx_set_obj_dir(lp, LPX_MIN);
386 void LpGlpk::messageLevel(int m)
388 lpx_set_int_parm(lp, LPX_K_MSGLEV, m);
391 void LpGlpk::presolver(bool b)
393 lpx_set_int_parm(lp, LPX_K_PRESOL, b);
397 } //END OF NAMESPACE LEMON
399 #endif //LEMON_LP_GLPK_CC