src/lemon/lp_glpk.cc
author athos
Fri, 20 May 2005 09:31:25 +0000
changeset 1431 ad44b1dd8013
parent 1405 3626c7f10f14
child 1432 46b088b01f88
permissions -rw-r--r--
Added function _setCoeff().
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
  }
athos@1431
    90
athos@1431
    91
athos@1431
    92
  void LpGlpk::_setCoeff(int row, int col, Value value) 
athos@1431
    93
  {
athos@1431
    94
    ///FIXME Of course this is not efficient at all, but GLPK knows not more.
athos@1431
    95
    // First approach: get one row, apply changes and set it again
athos@1431
    96
    //(one idea to improve this: maybe it is better to do this with 1 coloumn)
athos@1431
    97
    
athos@1431
    98
    int mem_length=2+lpx_get_num_cols(lp);
athos@1431
    99
    int* indices = new int[mem_length];
athos@1431
   100
    Value* values = new Value[mem_length];
athos@1431
   101
    
athos@1431
   102
athos@1431
   103
    int length=lpx_get_mat_row(lp, row, indices, values);
athos@1431
   104
athos@1431
   105
    //The following code does not suppose that the elements of the array indices are sorted
athos@1431
   106
    int i=1;
athos@1431
   107
    bool found=false;
athos@1431
   108
    while (i <= length && !found){
athos@1431
   109
      if (indices[i]==col){
athos@1431
   110
	found = true;
athos@1431
   111
	values[i]=value;
athos@1431
   112
      }
athos@1431
   113
      ++i;
athos@1431
   114
    }
athos@1431
   115
    if (!found){
athos@1431
   116
      ++length;
athos@1431
   117
      indices[length]=col;
athos@1431
   118
      values[length]=value;
athos@1431
   119
    }
athos@1431
   120
    
athos@1431
   121
    lpx_set_mat_row(lp, row, length, indices, values);
athos@1431
   122
    delete [] indices;
athos@1431
   123
    delete [] values;
athos@1431
   124
    
athos@1431
   125
  }
athos@1431
   126
alpar@1321
   127
  void LpGlpk::_setColLowerBound(int i, Value lo)
alpar@1321
   128
  {
alpar@1321
   129
    if (lo==INF) {
alpar@1321
   130
      //FIXME error
alpar@1321
   131
    }
alpar@1321
   132
    int b=lpx_get_col_type(lp, i);
alpar@1321
   133
    double up=lpx_get_col_ub(lp, i);	
alpar@1321
   134
    if (lo==-INF) {
alpar@1321
   135
      switch (b) {
alpar@1321
   136
      case LPX_FR:
alpar@1321
   137
      case LPX_LO:
alpar@1321
   138
	lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
alpar@1321
   139
	break;
alpar@1321
   140
      case LPX_UP:
alpar@1321
   141
	break;
alpar@1321
   142
      case LPX_DB:
alpar@1321
   143
      case LPX_FX:
alpar@1321
   144
	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
alpar@1321
   145
	break;
alpar@1321
   146
      default: ;
alpar@1321
   147
	//FIXME error
alpar@1321
   148
      }
alpar@1321
   149
    } else {
alpar@1321
   150
      switch (b) {
alpar@1321
   151
      case LPX_FR:
alpar@1321
   152
      case LPX_LO:
alpar@1321
   153
	lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
alpar@1321
   154
	break;
alpar@1321
   155
      case LPX_UP:	  
alpar@1321
   156
      case LPX_DB:
alpar@1321
   157
      case LPX_FX:
alpar@1321
   158
	if (lo==up) 
alpar@1321
   159
	  lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
alpar@1321
   160
	else 
alpar@1321
   161
	  lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
alpar@1321
   162
	break;
alpar@1321
   163
      default: ;
alpar@1321
   164
	//FIXME error
alpar@1321
   165
      }
athos@1261
   166
    }
athos@1261
   167
alpar@1321
   168
  }
alpar@1321
   169
  
alpar@1321
   170
  void LpGlpk::_setColUpperBound(int i, Value up)
alpar@1321
   171
  {
alpar@1321
   172
    if (up==-INF) {
alpar@1321
   173
      //FIXME error
athos@1261
   174
    }
alpar@1321
   175
    int b=lpx_get_col_type(lp, i);
alpar@1321
   176
    double lo=lpx_get_col_lb(lp, i);
alpar@1321
   177
    if (up==INF) {
alpar@1321
   178
      switch (b) {
alpar@1321
   179
      case LPX_FR:
alpar@1321
   180
      case LPX_LO:
alpar@1321
   181
	break;
alpar@1321
   182
      case LPX_UP:
alpar@1321
   183
	lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
alpar@1321
   184
	break;
alpar@1321
   185
      case LPX_DB:
alpar@1321
   186
      case LPX_FX:
alpar@1321
   187
	lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
alpar@1321
   188
	break;
alpar@1321
   189
      default: ;
athos@1261
   190
	//FIXME error
athos@1261
   191
      }
alpar@1321
   192
    } else {
alpar@1321
   193
      switch (b) {
alpar@1321
   194
      case LPX_FR:
alpar@1321
   195
	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
alpar@1321
   196
	break;
alpar@1321
   197
      case LPX_UP:
alpar@1321
   198
	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
alpar@1321
   199
	break;
alpar@1321
   200
      case LPX_LO:
alpar@1321
   201
      case LPX_DB:
alpar@1321
   202
      case LPX_FX:
alpar@1321
   203
	if (lo==up) 
alpar@1321
   204
	  lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
alpar@1321
   205
	else 
alpar@1321
   206
	  lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
alpar@1321
   207
	break;
alpar@1321
   208
      default: ;
athos@1261
   209
	//FIXME error
athos@1261
   210
      }
alpar@1321
   211
    }
alpar@1321
   212
  }
alpar@1321
   213
  
athos@1405
   214
//   void LpGlpk::_setRowLowerBound(int i, Value lo)
athos@1405
   215
//   {
athos@1405
   216
//     if (lo==INF) {
athos@1405
   217
//       //FIXME error
athos@1405
   218
//     }
athos@1405
   219
//     int b=lpx_get_row_type(lp, i);
athos@1405
   220
//     double up=lpx_get_row_ub(lp, i);	
athos@1405
   221
//     if (lo==-INF) {
athos@1405
   222
//       switch (b) {
athos@1405
   223
//       case LPX_FR:
athos@1405
   224
//       case LPX_LO:
athos@1405
   225
// 	lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
athos@1405
   226
// 	break;
athos@1405
   227
//       case LPX_UP:
athos@1405
   228
// 	break;
athos@1405
   229
//       case LPX_DB:
athos@1405
   230
//       case LPX_FX:
athos@1405
   231
// 	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
athos@1405
   232
// 	break;
athos@1405
   233
//       default: ;
athos@1405
   234
// 	//FIXME error
athos@1405
   235
//       }
athos@1405
   236
//     } else {
athos@1405
   237
//       switch (b) {
athos@1405
   238
//       case LPX_FR:
athos@1405
   239
//       case LPX_LO:
athos@1405
   240
// 	lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
athos@1405
   241
// 	break;
athos@1405
   242
//       case LPX_UP:	  
athos@1405
   243
//       case LPX_DB:
athos@1405
   244
//       case LPX_FX:
athos@1405
   245
// 	if (lo==up) 
athos@1405
   246
// 	  lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
athos@1405
   247
// 	else 
athos@1405
   248
// 	  lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
athos@1405
   249
// 	break;
athos@1405
   250
//       default: ;
athos@1405
   251
// 	//FIXME error
athos@1405
   252
//       }
athos@1405
   253
//     }
athos@1405
   254
//   }
athos@1261
   255
  
athos@1405
   256
//   void LpGlpk::_setRowUpperBound(int i, Value up)
athos@1405
   257
//   {
athos@1405
   258
//     if (up==-INF) {
athos@1405
   259
//       //FIXME error
athos@1405
   260
//     }
athos@1405
   261
//     int b=lpx_get_row_type(lp, i);
athos@1405
   262
//     double lo=lpx_get_row_lb(lp, i);
athos@1405
   263
//     if (up==INF) {
athos@1405
   264
//       switch (b) {
athos@1405
   265
//       case LPX_FR:
athos@1405
   266
//       case LPX_LO:
athos@1405
   267
// 	break;
athos@1405
   268
//       case LPX_UP:
athos@1405
   269
// 	lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
athos@1405
   270
// 	break;
athos@1405
   271
//       case LPX_DB:
athos@1405
   272
//       case LPX_FX:
athos@1405
   273
// 	lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
athos@1405
   274
// 	break;
athos@1405
   275
//       default: ;
athos@1405
   276
// 	//FIXME error
athos@1405
   277
//       }
athos@1405
   278
//     } else {
athos@1405
   279
//       switch (b) {
athos@1405
   280
//       case LPX_FR:
athos@1405
   281
// 	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
athos@1405
   282
// 	break;
athos@1405
   283
//       case LPX_UP:
athos@1405
   284
// 	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
athos@1405
   285
// 	break;
athos@1405
   286
//       case LPX_LO:
athos@1405
   287
//       case LPX_DB:
athos@1405
   288
//       case LPX_FX:
athos@1405
   289
// 	if (lo==up) 
athos@1405
   290
// 	  lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
athos@1405
   291
// 	else 
athos@1405
   292
// 	  lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
athos@1405
   293
// 	break;
athos@1405
   294
//       default: ;
athos@1405
   295
// 	//FIXME error
athos@1405
   296
//       }
athos@1405
   297
//     }
athos@1405
   298
//   }
athos@1379
   299
athos@1379
   300
  void LpGlpk::_setRowBounds(int i, Value lb, Value ub)
athos@1379
   301
  {
athos@1379
   302
    //Bad parameter
athos@1379
   303
    if (lb==INF || ub==-INF) {
athos@1379
   304
      //FIXME error
athos@1379
   305
    }
athos@1379
   306
athos@1379
   307
    if (lb == -INF){
athos@1379
   308
      if (ub == INF){
athos@1379
   309
	lpx_set_row_bnds(lp, i, LPX_FR, lb, ub);
athos@1379
   310
      }
athos@1379
   311
      else{
athos@1379
   312
	lpx_set_row_bnds(lp, i, LPX_UP, lb, ub);
athos@1379
   313
      }
athos@1379
   314
    }
athos@1379
   315
    else{
athos@1379
   316
      if (ub==INF){
athos@1379
   317
	lpx_set_row_bnds(lp, i, LPX_LO, lb, ub);
athos@1379
   318
athos@1379
   319
      }
athos@1379
   320
      else{
athos@1379
   321
	if (lb == ub){
athos@1379
   322
	  lpx_set_row_bnds(lp, i, LPX_FX, lb, ub);
athos@1379
   323
	}
athos@1379
   324
	else{
athos@1379
   325
	  lpx_set_row_bnds(lp, i, LPX_DB, lb, ub);
athos@1379
   326
	}
athos@1379
   327
      }
athos@1379
   328
    }
athos@1379
   329
athos@1379
   330
  }
athos@1261
   331
  
athos@1298
   332
  void LpGlpk::_setObjCoeff(int i, Value obj_coef)
athos@1298
   333
  {
athos@1376
   334
    //i=0 means the constant term (shift)
athos@1298
   335
    lpx_set_obj_coef(lp, i, obj_coef);
athos@1298
   336
  }
athos@1261
   337
athos@1377
   338
  void LpGlpk::_clearObj()
athos@1376
   339
  {
athos@1377
   340
    for (int i=0;i<=lpx_get_num_cols(lp);++i){
athos@1377
   341
      lpx_set_obj_coef(lp, i, 0);
athos@1376
   342
    }
athos@1376
   343
  }
athos@1377
   344
//   void LpGlpk::_setObj(int length,
athos@1377
   345
// 		       int  const * indices, 
athos@1377
   346
// 		       Value  const * values )
athos@1377
   347
//   {
athos@1377
   348
//     Value new_values[1+lpx_num_cols()];
athos@1377
   349
//     for (i=0;i<=lpx_num_cols();++i){
athos@1377
   350
//       new_values[i]=0;
athos@1377
   351
//     }
athos@1377
   352
//     for (i=1;i<=length;++i){
athos@1377
   353
//       new_values[indices[i]]=values[i];
athos@1377
   354
//     }
athos@1377
   355
    
athos@1377
   356
//     for (i=0;i<=lpx_num_cols();++i){
athos@1377
   357
//       lpx_set_obj_coef(lp, i, new_values[i]);
athos@1377
   358
//     }
athos@1377
   359
//   }
alpar@1263
   360
alpar@1303
   361
  LpGlpk::SolveExitStatus LpGlpk::_solve()
alpar@1263
   362
  {
athos@1298
   363
    int i=  lpx_simplex(lp);
athos@1298
   364
    switch (i) {
athos@1298
   365
    case LPX_E_OK: 
athos@1298
   366
      return SOLVED;
athos@1298
   367
      break;
athos@1298
   368
    default:
athos@1298
   369
      return UNSOLVED;
athos@1298
   370
    }
alpar@1263
   371
  }
alpar@1263
   372
alpar@1293
   373
  LpGlpk::Value LpGlpk::_getPrimal(int i)
alpar@1263
   374
  {
athos@1298
   375
    return lpx_get_col_prim(lp,i);
alpar@1263
   376
  }
alpar@1263
   377
  
alpar@1312
   378
  LpGlpk::Value LpGlpk::_getPrimalValue()
alpar@1312
   379
  {
athos@1314
   380
    return lpx_get_obj_val(lp);
alpar@1312
   381
  }
alpar@1312
   382
  
athos@1298
   383
 
alpar@1312
   384
  LpGlpk::SolutionStatus LpGlpk::_getPrimalStatus()
alpar@1294
   385
  {
athos@1298
   386
    int stat=  lpx_get_status(lp);
athos@1298
   387
    switch (stat) {
athos@1298
   388
    case LPX_UNDEF://Undefined (no solve has been run yet)
athos@1298
   389
      return UNDEFINED;
athos@1298
   390
      break;
athos@1298
   391
    case LPX_NOFEAS://There is no feasible solution (primal, I guess)
athos@1298
   392
    case LPX_INFEAS://Infeasible 
athos@1298
   393
      return INFEASIBLE;
athos@1298
   394
      break;
athos@1298
   395
    case LPX_UNBND://Unbounded
athos@1298
   396
      return INFINITE;
athos@1298
   397
      break;
athos@1298
   398
    case LPX_FEAS://Feasible
alpar@1300
   399
      return FEASIBLE;
athos@1298
   400
      break;
athos@1298
   401
    case LPX_OPT://Feasible
athos@1298
   402
      return OPTIMAL;
athos@1298
   403
      break;
alpar@1300
   404
    default:
alpar@1300
   405
      return UNDEFINED; //to avoid gcc warning
athos@1298
   406
      //FIXME error
athos@1298
   407
    }
alpar@1294
   408
  }
alpar@1263
   409
alpar@1263
   410
alpar@1312
   411
  void LpGlpk::_setMax()
alpar@1312
   412
  {
alpar@1321
   413
    lpx_set_obj_dir(lp, LPX_MAX);
alpar@1321
   414
  }
alpar@1321
   415
alpar@1312
   416
  void LpGlpk::_setMin()
alpar@1312
   417
  {
alpar@1321
   418
    lpx_set_obj_dir(lp, LPX_MIN);
alpar@1321
   419
  }
alpar@1321
   420
alpar@1321
   421
 
alpar@1321
   422
  void LpGlpk::messageLevel(int m)
alpar@1321
   423
  {
alpar@1321
   424
    lpx_set_int_parm(lp, LPX_K_MSGLEV, m);
alpar@1321
   425
  }
alpar@1312
   426
alpar@1326
   427
  void LpGlpk::presolver(bool b)
alpar@1326
   428
  {
alpar@1326
   429
    lpx_set_int_parm(lp, LPX_K_PRESOL, b);
alpar@1326
   430
  }
alpar@1326
   431
alpar@1312
   432
 
athos@1261
   433
} //END OF NAMESPACE LEMON
athos@1261
   434
athos@1261
   435
#endif //LEMON_LP_GLPK_CC