src/lemon/lp_glpk.cc
author deba
Tue, 19 Apr 2005 13:33:44 +0000
changeset 1374 bcfa3980b432
parent 1364 ee5959aa4410
child 1376 8de0c1aeeb32
permissions -rw-r--r--
Call the default constructor of the ObserverBase.
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
alpar@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, 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@1364
    27
  ///\e
alpar@1364
    28
alpar@1364
    29
  ///\bug Unimplemented!
alpar@1364
    30
  ///
alpar@1364
    31
  LpSolverBase &LpGlpk::_newLp()
alpar@1364
    32
  {
alpar@1368
    33
    LpSolverBase *tmp=0;
alpar@1368
    34
    return *tmp;
alpar@1364
    35
  }
alpar@1364
    36
  
alpar@1364
    37
  ///\e
alpar@1364
    38
alpar@1364
    39
  ///\bug Unimplemented!
alpar@1364
    40
  ///
alpar@1364
    41
  LpSolverBase &LpGlpk::_copyLp()
alpar@1364
    42
  {
alpar@1368
    43
    LpSolverBase *tmp=0;
alpar@1368
    44
    return *tmp;
alpar@1364
    45
  }
alpar@1364
    46
alpar@1321
    47
  LpGlpk::LpGlpk() : Parent(), 
alpar@1321
    48
		     lp(lpx_create_prob()) {
alpar@1321
    49
    ///\todo constrol function for this:
alpar@1321
    50
    lpx_set_int_parm(lp, LPX_K_DUAL, 1);
alpar@1321
    51
    messageLevel(0);
alpar@1321
    52
  }
alpar@1321
    53
  
alpar@1321
    54
  LpGlpk::~LpGlpk() {
alpar@1321
    55
    lpx_delete_prob(lp);
alpar@1321
    56
  }
alpar@1321
    57
  
alpar@1321
    58
  int LpGlpk::_addCol() { 
alpar@1321
    59
    int i=lpx_add_cols(lp, 1);
alpar@1321
    60
    _setColLowerBound(i, -INF);
alpar@1321
    61
    _setColUpperBound(i, INF);
alpar@1321
    62
    return i;
alpar@1321
    63
  }
alpar@1321
    64
alpar@1321
    65
  int LpGlpk::_addRow() { 
alpar@1321
    66
    int i=lpx_add_rows(lp, 1);
alpar@1321
    67
    return i;
alpar@1321
    68
  }
alpar@1321
    69
alpar@1321
    70
  
alpar@1321
    71
  void LpGlpk::_setRowCoeffs(int i, 
alpar@1321
    72
			     int length,
alpar@1321
    73
			     const int   * indices, 
alpar@1321
    74
			     const Value   * values )
alpar@1321
    75
  {
alpar@1321
    76
    lpx_set_mat_row(lp, i, length,
alpar@1321
    77
		    const_cast<int * >(indices) ,
alpar@1321
    78
		    const_cast<Value * >(values));
alpar@1321
    79
  }
alpar@1321
    80
  
alpar@1321
    81
  void LpGlpk::_setColCoeffs(int i, 
alpar@1321
    82
			     int length,
alpar@1321
    83
			     const int   * indices, 
alpar@1321
    84
			     const Value   * values)
alpar@1321
    85
  {
alpar@1321
    86
    lpx_set_mat_col(lp, i, length,
alpar@1321
    87
		    const_cast<int * >(indices),
alpar@1321
    88
		    const_cast<Value * >(values));
alpar@1321
    89
  }
alpar@1321
    90
  
alpar@1321
    91
  void LpGlpk::_setColLowerBound(int i, Value lo)
alpar@1321
    92
  {
alpar@1321
    93
    if (lo==INF) {
alpar@1321
    94
      //FIXME error
alpar@1321
    95
    }
alpar@1321
    96
    int b=lpx_get_col_type(lp, i);
alpar@1321
    97
    double up=lpx_get_col_ub(lp, i);	
alpar@1321
    98
    if (lo==-INF) {
alpar@1321
    99
      switch (b) {
alpar@1321
   100
      case LPX_FR:
alpar@1321
   101
      case LPX_LO:
alpar@1321
   102
	lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
alpar@1321
   103
	break;
alpar@1321
   104
      case LPX_UP:
alpar@1321
   105
	break;
alpar@1321
   106
      case LPX_DB:
alpar@1321
   107
      case LPX_FX:
alpar@1321
   108
	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
alpar@1321
   109
	break;
alpar@1321
   110
      default: ;
alpar@1321
   111
	//FIXME error
alpar@1321
   112
      }
alpar@1321
   113
    } else {
alpar@1321
   114
      switch (b) {
alpar@1321
   115
      case LPX_FR:
alpar@1321
   116
      case LPX_LO:
alpar@1321
   117
	lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
alpar@1321
   118
	break;
alpar@1321
   119
      case LPX_UP:	  
alpar@1321
   120
      case LPX_DB:
alpar@1321
   121
      case LPX_FX:
alpar@1321
   122
	if (lo==up) 
alpar@1321
   123
	  lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
alpar@1321
   124
	else 
alpar@1321
   125
	  lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
alpar@1321
   126
	break;
alpar@1321
   127
      default: ;
alpar@1321
   128
	//FIXME error
alpar@1321
   129
      }
athos@1261
   130
    }
athos@1261
   131
alpar@1321
   132
  }
alpar@1321
   133
  
alpar@1321
   134
  void LpGlpk::_setColUpperBound(int i, Value up)
alpar@1321
   135
  {
alpar@1321
   136
    if (up==-INF) {
alpar@1321
   137
      //FIXME error
athos@1261
   138
    }
alpar@1321
   139
    int b=lpx_get_col_type(lp, i);
alpar@1321
   140
    double lo=lpx_get_col_lb(lp, i);
alpar@1321
   141
    if (up==INF) {
alpar@1321
   142
      switch (b) {
alpar@1321
   143
      case LPX_FR:
alpar@1321
   144
      case LPX_LO:
alpar@1321
   145
	break;
alpar@1321
   146
      case LPX_UP:
alpar@1321
   147
	lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
alpar@1321
   148
	break;
alpar@1321
   149
      case LPX_DB:
alpar@1321
   150
      case LPX_FX:
alpar@1321
   151
	lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
alpar@1321
   152
	break;
alpar@1321
   153
      default: ;
athos@1261
   154
	//FIXME error
athos@1261
   155
      }
alpar@1321
   156
    } else {
alpar@1321
   157
      switch (b) {
alpar@1321
   158
      case LPX_FR:
alpar@1321
   159
	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
alpar@1321
   160
	break;
alpar@1321
   161
      case LPX_UP:
alpar@1321
   162
	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
alpar@1321
   163
	break;
alpar@1321
   164
      case LPX_LO:
alpar@1321
   165
      case LPX_DB:
alpar@1321
   166
      case LPX_FX:
alpar@1321
   167
	if (lo==up) 
alpar@1321
   168
	  lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
alpar@1321
   169
	else 
alpar@1321
   170
	  lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
alpar@1321
   171
	break;
alpar@1321
   172
      default: ;
athos@1261
   173
	//FIXME error
athos@1261
   174
      }
alpar@1321
   175
    }
alpar@1321
   176
  }
alpar@1321
   177
  
alpar@1321
   178
  void LpGlpk::_setRowLowerBound(int i, Value lo)
alpar@1321
   179
  {
alpar@1321
   180
    if (lo==INF) {
alpar@1321
   181
      //FIXME error
alpar@1321
   182
    }
alpar@1321
   183
    int b=lpx_get_row_type(lp, i);
alpar@1321
   184
    double up=lpx_get_row_ub(lp, i);	
alpar@1321
   185
    if (lo==-INF) {
alpar@1321
   186
      switch (b) {
alpar@1321
   187
      case LPX_FR:
alpar@1321
   188
      case LPX_LO:
alpar@1321
   189
	lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
alpar@1321
   190
	break;
alpar@1321
   191
      case LPX_UP:
alpar@1321
   192
	break;
alpar@1321
   193
      case LPX_DB:
alpar@1321
   194
      case LPX_FX:
alpar@1321
   195
	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
alpar@1321
   196
	break;
alpar@1321
   197
      default: ;
alpar@1321
   198
	//FIXME error
alpar@1321
   199
      }
alpar@1321
   200
    } else {
alpar@1321
   201
      switch (b) {
alpar@1321
   202
      case LPX_FR:
alpar@1321
   203
      case LPX_LO:
alpar@1321
   204
	lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
alpar@1321
   205
	break;
alpar@1321
   206
      case LPX_UP:	  
alpar@1321
   207
      case LPX_DB:
alpar@1321
   208
      case LPX_FX:
alpar@1321
   209
	if (lo==up) 
alpar@1321
   210
	  lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
alpar@1321
   211
	else 
alpar@1321
   212
	  lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
alpar@1321
   213
	break;
alpar@1321
   214
      default: ;
alpar@1321
   215
	//FIXME error
athos@1261
   216
      }
athos@1261
   217
    }
alpar@1321
   218
  }
athos@1261
   219
  
athos@1298
   220
  void LpGlpk::_setRowUpperBound(int i, Value up)
athos@1298
   221
  {
athos@1298
   222
    if (up==-INF) {
athos@1298
   223
      //FIXME error
athos@1298
   224
    }
athos@1298
   225
    int b=lpx_get_row_type(lp, i);
athos@1298
   226
    double lo=lpx_get_row_lb(lp, i);
athos@1298
   227
    if (up==INF) {
athos@1298
   228
      switch (b) {
athos@1298
   229
      case LPX_FR:
athos@1298
   230
      case LPX_LO:
athos@1298
   231
	break;
athos@1298
   232
      case LPX_UP:
athos@1298
   233
	lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
athos@1298
   234
	break;
athos@1298
   235
      case LPX_DB:
athos@1298
   236
      case LPX_FX:
athos@1298
   237
	lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
athos@1298
   238
	break;
athos@1298
   239
      default: ;
athos@1261
   240
	//FIXME error
athos@1261
   241
      }
athos@1298
   242
    } else {
athos@1298
   243
      switch (b) {
athos@1298
   244
      case LPX_FR:
athos@1298
   245
	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
athos@1298
   246
	break;
athos@1298
   247
      case LPX_UP:
athos@1298
   248
	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
athos@1298
   249
	break;
athos@1298
   250
      case LPX_LO:
athos@1298
   251
      case LPX_DB:
athos@1298
   252
      case LPX_FX:
athos@1298
   253
	if (lo==up) 
athos@1298
   254
	  lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
athos@1298
   255
	else 
athos@1298
   256
	  lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
athos@1298
   257
	break;
athos@1298
   258
      default: ;
athos@1298
   259
	//FIXME error
athos@1261
   260
      }
athos@1261
   261
    }
athos@1298
   262
  }
athos@1261
   263
  
athos@1298
   264
  void LpGlpk::_setObjCoeff(int i, Value obj_coef)
athos@1298
   265
  {
athos@1298
   266
    lpx_set_obj_coef(lp, i, obj_coef);
athos@1298
   267
  }
athos@1261
   268
alpar@1263
   269
alpar@1303
   270
  LpGlpk::SolveExitStatus LpGlpk::_solve()
alpar@1263
   271
  {
athos@1298
   272
    int i=  lpx_simplex(lp);
athos@1298
   273
    switch (i) {
athos@1298
   274
    case LPX_E_OK: 
athos@1298
   275
      return SOLVED;
athos@1298
   276
      break;
athos@1298
   277
    default:
athos@1298
   278
      return UNSOLVED;
athos@1298
   279
    }
alpar@1263
   280
  }
alpar@1263
   281
alpar@1293
   282
  LpGlpk::Value LpGlpk::_getPrimal(int i)
alpar@1263
   283
  {
athos@1298
   284
    return lpx_get_col_prim(lp,i);
alpar@1263
   285
  }
alpar@1263
   286
  
alpar@1312
   287
  LpGlpk::Value LpGlpk::_getPrimalValue()
alpar@1312
   288
  {
athos@1314
   289
    return lpx_get_obj_val(lp);
alpar@1312
   290
  }
alpar@1312
   291
  
athos@1298
   292
 
alpar@1312
   293
  LpGlpk::SolutionStatus LpGlpk::_getPrimalStatus()
alpar@1294
   294
  {
athos@1298
   295
    int stat=  lpx_get_status(lp);
athos@1298
   296
    switch (stat) {
athos@1298
   297
    case LPX_UNDEF://Undefined (no solve has been run yet)
athos@1298
   298
      return UNDEFINED;
athos@1298
   299
      break;
athos@1298
   300
    case LPX_NOFEAS://There is no feasible solution (primal, I guess)
athos@1298
   301
    case LPX_INFEAS://Infeasible 
athos@1298
   302
      return INFEASIBLE;
athos@1298
   303
      break;
athos@1298
   304
    case LPX_UNBND://Unbounded
athos@1298
   305
      return INFINITE;
athos@1298
   306
      break;
athos@1298
   307
    case LPX_FEAS://Feasible
alpar@1300
   308
      return FEASIBLE;
athos@1298
   309
      break;
athos@1298
   310
    case LPX_OPT://Feasible
athos@1298
   311
      return OPTIMAL;
athos@1298
   312
      break;
alpar@1300
   313
    default:
alpar@1300
   314
      return UNDEFINED; //to avoid gcc warning
athos@1298
   315
      //FIXME error
athos@1298
   316
    }
alpar@1294
   317
  }
alpar@1263
   318
alpar@1263
   319
alpar@1312
   320
  void LpGlpk::_setMax()
alpar@1312
   321
  {
alpar@1321
   322
    lpx_set_obj_dir(lp, LPX_MAX);
alpar@1321
   323
  }
alpar@1321
   324
alpar@1312
   325
  void LpGlpk::_setMin()
alpar@1312
   326
  {
alpar@1321
   327
    lpx_set_obj_dir(lp, LPX_MIN);
alpar@1321
   328
  }
alpar@1321
   329
alpar@1321
   330
 
alpar@1321
   331
  void LpGlpk::messageLevel(int m)
alpar@1321
   332
  {
alpar@1321
   333
    lpx_set_int_parm(lp, LPX_K_MSGLEV, m);
alpar@1321
   334
  }
alpar@1312
   335
alpar@1326
   336
  void LpGlpk::presolver(bool b)
alpar@1326
   337
  {
alpar@1326
   338
    lpx_set_int_parm(lp, LPX_K_PRESOL, b);
alpar@1326
   339
  }
alpar@1326
   340
alpar@1312
   341
 
athos@1261
   342
} //END OF NAMESPACE LEMON
athos@1261
   343
athos@1261
   344
#endif //LEMON_LP_GLPK_CC