Added function _setCoeff().
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));
92 void LpGlpk::_setCoeff(int row, int col, Value value)
94 ///FIXME Of course this is not efficient at all, but GLPK knows not more.
95 // First approach: get one row, apply changes and set it again
96 //(one idea to improve this: maybe it is better to do this with 1 coloumn)
98 int mem_length=2+lpx_get_num_cols(lp);
99 int* indices = new int[mem_length];
100 Value* values = new Value[mem_length];
103 int length=lpx_get_mat_row(lp, row, indices, values);
105 //The following code does not suppose that the elements of the array indices are sorted
108 while (i <= length && !found){
109 if (indices[i]==col){
118 values[length]=value;
121 lpx_set_mat_row(lp, row, length, indices, values);
127 void LpGlpk::_setColLowerBound(int i, Value lo)
132 int b=lpx_get_col_type(lp, i);
133 double up=lpx_get_col_ub(lp, i);
138 lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
144 lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
153 lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
159 lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
161 lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
170 void LpGlpk::_setColUpperBound(int i, Value up)
175 int b=lpx_get_col_type(lp, i);
176 double lo=lpx_get_col_lb(lp, i);
183 lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
187 lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
195 lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
198 lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
204 lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
206 lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
214 // void LpGlpk::_setRowLowerBound(int i, Value lo)
219 // int b=lpx_get_row_type(lp, i);
220 // double up=lpx_get_row_ub(lp, i);
225 // lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
231 // lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
240 // lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
246 // lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
248 // lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
256 // void LpGlpk::_setRowUpperBound(int i, Value up)
261 // int b=lpx_get_row_type(lp, i);
262 // double lo=lpx_get_row_lb(lp, i);
269 // lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
273 // lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
281 // lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
284 // lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
290 // lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
292 // lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
300 void LpGlpk::_setRowBounds(int i, Value lb, Value ub)
303 if (lb==INF || ub==-INF) {
309 lpx_set_row_bnds(lp, i, LPX_FR, lb, ub);
312 lpx_set_row_bnds(lp, i, LPX_UP, lb, ub);
317 lpx_set_row_bnds(lp, i, LPX_LO, lb, ub);
322 lpx_set_row_bnds(lp, i, LPX_FX, lb, ub);
325 lpx_set_row_bnds(lp, i, LPX_DB, lb, ub);
332 void LpGlpk::_setObjCoeff(int i, Value obj_coef)
334 //i=0 means the constant term (shift)
335 lpx_set_obj_coef(lp, i, obj_coef);
338 void LpGlpk::_clearObj()
340 for (int i=0;i<=lpx_get_num_cols(lp);++i){
341 lpx_set_obj_coef(lp, i, 0);
344 // void LpGlpk::_setObj(int length,
345 // int const * indices,
346 // Value const * values )
348 // Value new_values[1+lpx_num_cols()];
349 // for (i=0;i<=lpx_num_cols();++i){
352 // for (i=1;i<=length;++i){
353 // new_values[indices[i]]=values[i];
356 // for (i=0;i<=lpx_num_cols();++i){
357 // lpx_set_obj_coef(lp, i, new_values[i]);
361 LpGlpk::SolveExitStatus LpGlpk::_solve()
363 int i= lpx_simplex(lp);
373 LpGlpk::Value LpGlpk::_getPrimal(int i)
375 return lpx_get_col_prim(lp,i);
378 LpGlpk::Value LpGlpk::_getPrimalValue()
380 return lpx_get_obj_val(lp);
384 LpGlpk::SolutionStatus LpGlpk::_getPrimalStatus()
386 int stat= lpx_get_status(lp);
388 case LPX_UNDEF://Undefined (no solve has been run yet)
391 case LPX_NOFEAS://There is no feasible solution (primal, I guess)
392 case LPX_INFEAS://Infeasible
395 case LPX_UNBND://Unbounded
398 case LPX_FEAS://Feasible
401 case LPX_OPT://Feasible
405 return UNDEFINED; //to avoid gcc warning
411 void LpGlpk::_setMax()
413 lpx_set_obj_dir(lp, LPX_MAX);
416 void LpGlpk::_setMin()
418 lpx_set_obj_dir(lp, LPX_MIN);
422 void LpGlpk::messageLevel(int m)
424 lpx_set_int_parm(lp, LPX_K_MSGLEV, m);
427 void LpGlpk::presolver(bool b)
429 lpx_set_int_parm(lp, LPX_K_PRESOL, b);
433 } //END OF NAMESPACE LEMON
435 #endif //LEMON_LP_GLPK_CC