Functions _eraseRow(), _eraseCol(). Not yet implemented for cplex.
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::_eraseCol(int i) {
74 lpx_del_cols(lp, 1, cols);
77 void LpGlpk::_eraseRow(int i) {
80 lpx_del_rows(lp, 1, rows);
83 void LpGlpk::_setRowCoeffs(int i,
86 const Value * values )
88 lpx_set_mat_row(lp, i, length,
89 const_cast<int * >(indices) ,
90 const_cast<Value * >(values));
93 void LpGlpk::_setColCoeffs(int i,
98 lpx_set_mat_col(lp, i, length,
99 const_cast<int * >(indices),
100 const_cast<Value * >(values));
104 void LpGlpk::_setCoeff(int row, int col, Value value)
106 ///FIXME Of course this is not efficient at all, but GLPK knows not more.
107 // First approach: get one row, apply changes and set it again
108 //(one idea to improve this: maybe it is better to do this with 1 coloumn)
110 int mem_length=2+lpx_get_num_cols(lp);
111 int* indices = new int[mem_length];
112 Value* values = new Value[mem_length];
115 int length=lpx_get_mat_row(lp, row, indices, values);
117 //The following code does not suppose that the elements of the array indices are sorted
120 while (i <= length && !found){
121 if (indices[i]==col){
130 values[length]=value;
133 lpx_set_mat_row(lp, row, length, indices, values);
139 void LpGlpk::_setColLowerBound(int i, Value lo)
144 int b=lpx_get_col_type(lp, i);
145 double up=lpx_get_col_ub(lp, i);
150 lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
156 lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
165 lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
171 lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
173 lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
182 void LpGlpk::_setColUpperBound(int i, Value up)
187 int b=lpx_get_col_type(lp, i);
188 double lo=lpx_get_col_lb(lp, i);
195 lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
199 lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
207 lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
210 lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
216 lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
218 lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
226 // void LpGlpk::_setRowLowerBound(int i, Value lo)
231 // int b=lpx_get_row_type(lp, i);
232 // double up=lpx_get_row_ub(lp, i);
237 // lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
243 // lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
252 // lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
258 // lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
260 // lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
268 // void LpGlpk::_setRowUpperBound(int i, Value up)
273 // int b=lpx_get_row_type(lp, i);
274 // double lo=lpx_get_row_lb(lp, i);
281 // lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
285 // lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
293 // lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
296 // lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
302 // lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
304 // lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
312 void LpGlpk::_setRowBounds(int i, Value lb, Value ub)
315 if (lb==INF || ub==-INF) {
321 lpx_set_row_bnds(lp, i, LPX_FR, lb, ub);
324 lpx_set_row_bnds(lp, i, LPX_UP, lb, ub);
329 lpx_set_row_bnds(lp, i, LPX_LO, lb, ub);
334 lpx_set_row_bnds(lp, i, LPX_FX, lb, ub);
337 lpx_set_row_bnds(lp, i, LPX_DB, lb, ub);
344 void LpGlpk::_setObjCoeff(int i, Value obj_coef)
346 //i=0 means the constant term (shift)
347 lpx_set_obj_coef(lp, i, obj_coef);
350 void LpGlpk::_clearObj()
352 for (int i=0;i<=lpx_get_num_cols(lp);++i){
353 lpx_set_obj_coef(lp, i, 0);
356 // void LpGlpk::_setObj(int length,
357 // int const * indices,
358 // Value const * values )
360 // Value new_values[1+lpx_num_cols()];
361 // for (i=0;i<=lpx_num_cols();++i){
364 // for (i=1;i<=length;++i){
365 // new_values[indices[i]]=values[i];
368 // for (i=0;i<=lpx_num_cols();++i){
369 // lpx_set_obj_coef(lp, i, new_values[i]);
373 LpGlpk::SolveExitStatus LpGlpk::_solve()
375 int i= lpx_simplex(lp);
385 LpGlpk::Value LpGlpk::_getPrimal(int i)
387 return lpx_get_col_prim(lp,i);
390 LpGlpk::Value LpGlpk::_getPrimalValue()
392 return lpx_get_obj_val(lp);
396 LpGlpk::SolutionStatus LpGlpk::_getPrimalStatus()
398 int stat= lpx_get_status(lp);
400 case LPX_UNDEF://Undefined (no solve has been run yet)
403 case LPX_NOFEAS://There is no feasible solution (primal, I guess)
404 case LPX_INFEAS://Infeasible
407 case LPX_UNBND://Unbounded
410 case LPX_FEAS://Feasible
413 case LPX_OPT://Feasible
417 return UNDEFINED; //to avoid gcc warning
423 void LpGlpk::_setMax()
425 lpx_set_obj_dir(lp, LPX_MAX);
428 void LpGlpk::_setMin()
430 lpx_set_obj_dir(lp, LPX_MIN);
434 void LpGlpk::messageLevel(int m)
436 lpx_set_int_parm(lp, LPX_K_MSGLEV, m);
439 void LpGlpk::presolver(bool b)
441 lpx_set_int_parm(lp, LPX_K_PRESOL, b);
445 } //END OF NAMESPACE LEMON
447 #endif //LEMON_LP_GLPK_CC