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