lemon/lp_glpk.cc
author athos
Mon, 04 Dec 2006 16:48:13 +0000
changeset 2324 18fc834761d9
parent 2321 e23a610bed51
child 2326 af8c695372be
permissions -rw-r--r--
Some query functions got implemented, but only for GLPK.
athos@1261
     1
/* -*- C++ -*-
athos@1261
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
athos@1261
     8
 *
athos@1261
     9
 * Permission to use, modify and distribute this software is granted
athos@1261
    10
 * provided that this copyright notice appears in all copies. For
athos@1261
    11
 * precise terms see the accompanying LICENSE file.
athos@1261
    12
 *
athos@1261
    13
 * This software is provided "AS IS" with no warranty of any kind,
athos@1261
    14
 * express or implied, and with no claim as to its suitability for any
athos@1261
    15
 * purpose.
athos@1261
    16
 *
athos@1261
    17
 */
athos@1261
    18
athos@1261
    19
///\file
athos@1261
    20
///\brief Implementation of the LEMON-GLPK lp solver interface.
athos@1261
    21
ladanyi@1305
    22
#include <lemon/lp_glpk.h>
athos@1473
    23
//#include <iostream>
athos@1261
    24
namespace lemon {
athos@1261
    25
alpar@1364
    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@2321
    34
  LpGlpk::LpGlpk(const LpGlpk &glp) : Parent(glp), 
alpar@2321
    35
				      lp(lpx_create_prob()) {
alpar@2321
    36
    ///\todo constrol function for this:
alpar@2321
    37
    lpx_set_int_parm(lp, LPX_K_DUAL, 1);
alpar@2321
    38
    messageLevel(0);
alpar@2321
    39
    //Coefficient matrix, row bounds
alpar@2321
    40
    lpx_add_rows(lp, lpx_get_num_rows(glp.lp));
alpar@2321
    41
    lpx_add_cols(lp, lpx_get_num_cols(glp.lp));
alpar@2321
    42
    int len;
alpar@2321
    43
    int ind[1+lpx_get_num_cols(glp.lp)];
alpar@2321
    44
    Value val[1+lpx_get_num_cols(glp.lp)];
alpar@2321
    45
    for (int i=1;i<=lpx_get_num_rows(glp.lp);++i)
alpar@2321
    46
      {
alpar@2321
    47
	len=lpx_get_mat_row(glp.lp,i,ind,val);
alpar@2321
    48
	lpx_set_mat_row(lp, i,len,ind,val);
alpar@2321
    49
	lpx_set_row_bnds(lp,i,lpx_get_row_type(glp.lp,i),
alpar@2321
    50
			 lpx_get_row_lb(glp.lp,i),lpx_get_row_ub(glp.lp,i));
alpar@2321
    51
      }
alpar@2321
    52
alpar@2321
    53
    //Objective function, coloumn bounds
alpar@2321
    54
    lpx_set_obj_dir(lp, lpx_get_obj_dir(glp.lp));
alpar@2321
    55
    //Objectif function's constant term treated separately
alpar@2321
    56
    lpx_set_obj_coef(lp,0,lpx_get_obj_coef(glp.lp,0));
alpar@2321
    57
    for (int i=1;i<=lpx_get_num_cols(glp.lp);++i)
alpar@2321
    58
      {
alpar@2321
    59
	lpx_set_obj_coef(lp,i,lpx_get_obj_coef(glp.lp,i));
alpar@2321
    60
	lpx_set_col_bnds(lp,i,lpx_get_col_type(glp.lp,i),
alpar@2321
    61
			 lpx_get_col_lb(glp.lp,i),lpx_get_col_ub(glp.lp,i));
alpar@2321
    62
      }
alpar@2321
    63
  }
alpar@2321
    64
  
alpar@1321
    65
  LpGlpk::~LpGlpk() {
alpar@1321
    66
    lpx_delete_prob(lp);
alpar@1321
    67
  }
alpar@1321
    68
  
alpar@1321
    69
  int LpGlpk::_addCol() { 
alpar@1321
    70
    int i=lpx_add_cols(lp, 1);
alpar@1321
    71
    _setColLowerBound(i, -INF);
alpar@1321
    72
    _setColUpperBound(i, INF);
alpar@1321
    73
    return i;
alpar@1321
    74
  }
alpar@1321
    75
athos@1436
    76
  ///\e
athos@1436
    77
athos@1436
    78
athos@1436
    79
  LpSolverBase &LpGlpk::_newLp()
athos@1436
    80
  {
alpar@2321
    81
    LpGlpk* newlp=new LpGlpk;
athos@1436
    82
    return *newlp;
athos@1436
    83
  }
athos@1436
    84
  
athos@1436
    85
  ///\e
athos@1436
    86
athos@1436
    87
  LpSolverBase &LpGlpk::_copyLp()
athos@1436
    88
  {
alpar@2321
    89
    LpGlpk* newlp=new LpGlpk(*this);
athos@1436
    90
    return *newlp;
athos@1436
    91
  }
athos@1436
    92
alpar@1321
    93
  int LpGlpk::_addRow() { 
alpar@1321
    94
    int i=lpx_add_rows(lp, 1);
alpar@1321
    95
    return i;
alpar@1321
    96
  }
alpar@1321
    97
alpar@1321
    98
  
athos@1432
    99
  void LpGlpk::_eraseCol(int i) {
athos@1432
   100
    int cols[2];
athos@1432
   101
    cols[1]=i;
athos@1432
   102
    lpx_del_cols(lp, 1, cols);
athos@1432
   103
  }
athos@1432
   104
  
athos@1432
   105
  void LpGlpk::_eraseRow(int i) {
athos@1432
   106
    int rows[2];
athos@1432
   107
    rows[1]=i;
athos@1432
   108
    lpx_del_rows(lp, 1, rows);
athos@1432
   109
  }
athos@1432
   110
alpar@1895
   111
  void LpGlpk::_getColName(int col, std::string & name)
alpar@1895
   112
  {
alpar@1895
   113
    
alpar@1895
   114
    char *n = lpx_get_col_name(lp,col);
alpar@1895
   115
    name = n?n:"";
alpar@1895
   116
  }
alpar@1895
   117
  
alpar@1895
   118
  
alpar@1895
   119
  void LpGlpk::_setColName(int col, const std::string & name)
alpar@1895
   120
  {
alpar@1895
   121
    lpx_set_col_name(lp,col,const_cast<char*>(name.c_str()));
alpar@1895
   122
  }
alpar@1895
   123
  
deba@2312
   124
  void LpGlpk::_setRowCoeffs(int i, LpRowIterator b, LpRowIterator e) 
alpar@1321
   125
  {
deba@2312
   126
    std::vector<int> indices;
deba@2312
   127
    std::vector<Value> values;
deba@2312
   128
deba@2312
   129
    indices.push_back(0);
deba@2312
   130
    values.push_back(0);
deba@2312
   131
deba@2312
   132
    for(LpRowIterator it=b; it!=e; ++it) {
deba@2312
   133
      indices.push_back(it->first);
deba@2312
   134
      values.push_back(it->second);
deba@2312
   135
    }
deba@2312
   136
deba@2312
   137
    lpx_set_mat_row(lp, i, values.size() - 1, &indices[0], &values[0]);
alpar@1321
   138
  }
alpar@1321
   139
  
deba@2312
   140
  void LpGlpk::_setColCoeffs(int i, LpColIterator b, LpColIterator e) {
deba@2312
   141
deba@2312
   142
    std::vector<int> indices;
deba@2312
   143
    std::vector<Value> values;
deba@2312
   144
deba@2312
   145
    indices.push_back(0);
deba@2312
   146
    values.push_back(0);
deba@2312
   147
deba@2312
   148
    for(LpColIterator it=b; it!=e; ++it) {
deba@2312
   149
      indices.push_back(it->first);
deba@2312
   150
      values.push_back(it->second);
deba@2312
   151
    }
deba@2312
   152
    
deba@2312
   153
    lpx_set_mat_col(lp, i, values.size() - 1, &indices[0], &values[0]);
alpar@1321
   154
  }
athos@1431
   155
athos@1431
   156
athos@1431
   157
  void LpGlpk::_setCoeff(int row, int col, Value value) 
athos@1431
   158
  {
deba@2312
   159
deba@2312
   160
    if (lpx_get_num_cols(lp) < lpx_get_num_rows(lp)) {
deba@2312
   161
deba@2312
   162
      int length=lpx_get_mat_row(lp, row, 0, 0);
deba@2312
   163
      
deba@2312
   164
      std::vector<int> indices(length + 2);
deba@2312
   165
      std::vector<Value> values(length + 2);
deba@2312
   166
      
deba@2312
   167
      lpx_get_mat_row(lp, row, &indices[0], &values[0]);
deba@2312
   168
      
deba@2312
   169
      //The following code does not suppose that the elements of the
deba@2312
   170
      //array indices are sorted
deba@2312
   171
      bool found=false;
deba@2312
   172
      for (int i = 1; i <= length; ++i) {
deba@2312
   173
        if (indices[i]==col){
deba@2312
   174
          found=true;
deba@2312
   175
          values[i]=value;
deba@2312
   176
          break;
deba@2312
   177
        }
deba@2312
   178
      }
deba@2312
   179
      if (!found){
deba@2312
   180
        ++length;
deba@2312
   181
        indices[length]=col;
deba@2312
   182
        values[length]=value;
deba@2312
   183
      }
athos@1431
   184
    
deba@2312
   185
      lpx_set_mat_row(lp, row, length, &indices[0], &values[0]);
deba@2312
   186
deba@2312
   187
    } else {
deba@2312
   188
deba@2312
   189
      int length=lpx_get_mat_col(lp, col, 0, 0);
deba@2312
   190
      
deba@2312
   191
      std::vector<int> indices(length + 2);
deba@2312
   192
      std::vector<Value> values(length + 2);
deba@2312
   193
      
deba@2312
   194
      lpx_get_mat_col(lp, col, &indices[0], &values[0]);
deba@2312
   195
      
deba@2312
   196
      //The following code does not suppose that the elements of the
deba@2312
   197
      //array indices are sorted
deba@2312
   198
      bool found=false;
deba@2312
   199
      for (int i = 1; i <= length; ++i) {
deba@2312
   200
        if (indices[i]==col){
deba@2312
   201
          found=true;
deba@2312
   202
          values[i]=value;
deba@2312
   203
          break;
deba@2312
   204
        }
deba@2312
   205
      }
deba@2312
   206
      if (!found){
deba@2312
   207
        ++length;
deba@2312
   208
        indices[length]=row;
deba@2312
   209
        values[length]=value;
deba@2312
   210
      }
athos@1431
   211
    
deba@2312
   212
      lpx_set_mat_col(lp, col, length, &indices[0], &values[0]);
athos@1431
   213
    }
athos@1431
   214
  }
athos@1431
   215
athos@2324
   216
  LpGlpk::Value LpGlpk::_getCoeff(int row, int col)
athos@2324
   217
  {
athos@2324
   218
    ///\todo This is not yet implemented!!!
athos@2324
   219
    return 0;
athos@2324
   220
  }
athos@2324
   221
athos@2324
   222
alpar@1321
   223
  void LpGlpk::_setColLowerBound(int i, Value lo)
alpar@1321
   224
  {
alpar@1321
   225
    if (lo==INF) {
alpar@1321
   226
      //FIXME error
alpar@1321
   227
    }
alpar@1321
   228
    int b=lpx_get_col_type(lp, i);
alpar@1321
   229
    double up=lpx_get_col_ub(lp, i);	
alpar@1321
   230
    if (lo==-INF) {
alpar@1321
   231
      switch (b) {
alpar@1321
   232
      case LPX_FR:
alpar@1321
   233
      case LPX_LO:
alpar@1321
   234
	lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
alpar@1321
   235
	break;
alpar@1321
   236
      case LPX_UP:
alpar@1321
   237
	break;
alpar@1321
   238
      case LPX_DB:
alpar@1321
   239
      case LPX_FX:
alpar@1321
   240
	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
alpar@1321
   241
	break;
alpar@1321
   242
      default: ;
alpar@1321
   243
	//FIXME error
alpar@1321
   244
      }
alpar@1321
   245
    } else {
alpar@1321
   246
      switch (b) {
alpar@1321
   247
      case LPX_FR:
alpar@1321
   248
      case LPX_LO:
alpar@1321
   249
	lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
alpar@1321
   250
	break;
alpar@1321
   251
      case LPX_UP:	  
alpar@1321
   252
      case LPX_DB:
alpar@1321
   253
      case LPX_FX:
alpar@1321
   254
	if (lo==up) 
alpar@1321
   255
	  lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
alpar@1321
   256
	else 
alpar@1321
   257
	  lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
alpar@1321
   258
	break;
alpar@1321
   259
      default: ;
alpar@1321
   260
	//FIXME error
alpar@1321
   261
      }
athos@1261
   262
    }
athos@1261
   263
alpar@1321
   264
  }
alpar@1321
   265
  
alpar@1321
   266
  void LpGlpk::_setColUpperBound(int i, Value up)
alpar@1321
   267
  {
alpar@1321
   268
    if (up==-INF) {
alpar@1321
   269
      //FIXME error
athos@1261
   270
    }
alpar@1321
   271
    int b=lpx_get_col_type(lp, i);
alpar@1321
   272
    double lo=lpx_get_col_lb(lp, i);
alpar@1321
   273
    if (up==INF) {
alpar@1321
   274
      switch (b) {
alpar@1321
   275
      case LPX_FR:
alpar@1321
   276
      case LPX_LO:
alpar@1321
   277
	break;
alpar@1321
   278
      case LPX_UP:
alpar@1321
   279
	lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
alpar@1321
   280
	break;
alpar@1321
   281
      case LPX_DB:
alpar@1321
   282
      case LPX_FX:
alpar@1321
   283
	lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
alpar@1321
   284
	break;
alpar@1321
   285
      default: ;
athos@1261
   286
	//FIXME error
athos@1261
   287
      }
alpar@1321
   288
    } else {
alpar@1321
   289
      switch (b) {
alpar@1321
   290
      case LPX_FR:
alpar@1321
   291
	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
alpar@1321
   292
	break;
alpar@1321
   293
      case LPX_UP:
alpar@1321
   294
	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
alpar@1321
   295
	break;
alpar@1321
   296
      case LPX_LO:
alpar@1321
   297
      case LPX_DB:
alpar@1321
   298
      case LPX_FX:
alpar@1321
   299
	if (lo==up) 
alpar@1321
   300
	  lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
alpar@1321
   301
	else 
alpar@1321
   302
	  lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
alpar@1321
   303
	break;
alpar@1321
   304
      default: ;
athos@1261
   305
	//FIXME error
athos@1261
   306
      }
alpar@1321
   307
    }
alpar@1321
   308
  }
alpar@1321
   309
  
athos@1405
   310
//   void LpGlpk::_setRowLowerBound(int i, Value lo)
athos@1405
   311
//   {
athos@1405
   312
//     if (lo==INF) {
athos@1405
   313
//       //FIXME error
athos@1405
   314
//     }
athos@1405
   315
//     int b=lpx_get_row_type(lp, i);
athos@1405
   316
//     double up=lpx_get_row_ub(lp, i);	
athos@1405
   317
//     if (lo==-INF) {
athos@1405
   318
//       switch (b) {
athos@1405
   319
//       case LPX_FR:
athos@1405
   320
//       case LPX_LO:
athos@1405
   321
// 	lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
athos@1405
   322
// 	break;
athos@1405
   323
//       case LPX_UP:
athos@1405
   324
// 	break;
athos@1405
   325
//       case LPX_DB:
athos@1405
   326
//       case LPX_FX:
athos@1405
   327
// 	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
athos@1405
   328
// 	break;
athos@1405
   329
//       default: ;
athos@1405
   330
// 	//FIXME error
athos@1405
   331
//       }
athos@1405
   332
//     } else {
athos@1405
   333
//       switch (b) {
athos@1405
   334
//       case LPX_FR:
athos@1405
   335
//       case LPX_LO:
athos@1405
   336
// 	lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
athos@1405
   337
// 	break;
athos@1405
   338
//       case LPX_UP:	  
athos@1405
   339
//       case LPX_DB:
athos@1405
   340
//       case LPX_FX:
athos@1405
   341
// 	if (lo==up) 
athos@1405
   342
// 	  lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
athos@1405
   343
// 	else 
athos@1405
   344
// 	  lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
athos@1405
   345
// 	break;
athos@1405
   346
//       default: ;
athos@1405
   347
// 	//FIXME error
athos@1405
   348
//       }
athos@1405
   349
//     }
athos@1405
   350
//   }
athos@1261
   351
  
athos@1405
   352
//   void LpGlpk::_setRowUpperBound(int i, Value up)
athos@1405
   353
//   {
athos@1405
   354
//     if (up==-INF) {
athos@1405
   355
//       //FIXME error
athos@1405
   356
//     }
athos@1405
   357
//     int b=lpx_get_row_type(lp, i);
athos@1405
   358
//     double lo=lpx_get_row_lb(lp, i);
athos@1405
   359
//     if (up==INF) {
athos@1405
   360
//       switch (b) {
athos@1405
   361
//       case LPX_FR:
athos@1405
   362
//       case LPX_LO:
athos@1405
   363
// 	break;
athos@1405
   364
//       case LPX_UP:
athos@1405
   365
// 	lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
athos@1405
   366
// 	break;
athos@1405
   367
//       case LPX_DB:
athos@1405
   368
//       case LPX_FX:
athos@1405
   369
// 	lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
athos@1405
   370
// 	break;
athos@1405
   371
//       default: ;
athos@1405
   372
// 	//FIXME error
athos@1405
   373
//       }
athos@1405
   374
//     } else {
athos@1405
   375
//       switch (b) {
athos@1405
   376
//       case LPX_FR:
athos@1405
   377
// 	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
athos@1405
   378
// 	break;
athos@1405
   379
//       case LPX_UP:
athos@1405
   380
// 	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
athos@1405
   381
// 	break;
athos@1405
   382
//       case LPX_LO:
athos@1405
   383
//       case LPX_DB:
athos@1405
   384
//       case LPX_FX:
athos@1405
   385
// 	if (lo==up) 
athos@1405
   386
// 	  lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
athos@1405
   387
// 	else 
athos@1405
   388
// 	  lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
athos@1405
   389
// 	break;
athos@1405
   390
//       default: ;
athos@1405
   391
// 	//FIXME error
athos@1405
   392
//       }
athos@1405
   393
//     }
athos@1405
   394
//   }
athos@1379
   395
athos@1379
   396
  void LpGlpk::_setRowBounds(int i, Value lb, Value ub)
athos@1379
   397
  {
athos@1379
   398
    //Bad parameter
athos@1379
   399
    if (lb==INF || ub==-INF) {
athos@1379
   400
      //FIXME error
athos@1379
   401
    }
athos@1379
   402
athos@1379
   403
    if (lb == -INF){
athos@1379
   404
      if (ub == INF){
athos@1379
   405
	lpx_set_row_bnds(lp, i, LPX_FR, lb, ub);
athos@1379
   406
      }
athos@1379
   407
      else{
athos@1379
   408
	lpx_set_row_bnds(lp, i, LPX_UP, lb, ub);
athos@1379
   409
      }
athos@1379
   410
    }
athos@1379
   411
    else{
athos@1379
   412
      if (ub==INF){
athos@1379
   413
	lpx_set_row_bnds(lp, i, LPX_LO, lb, ub);
athos@1379
   414
athos@1379
   415
      }
athos@1379
   416
      else{
athos@1379
   417
	if (lb == ub){
athos@1379
   418
	  lpx_set_row_bnds(lp, i, LPX_FX, lb, ub);
athos@1379
   419
	}
athos@1379
   420
	else{
athos@1379
   421
	  lpx_set_row_bnds(lp, i, LPX_DB, lb, ub);
athos@1379
   422
	}
athos@1379
   423
      }
athos@1379
   424
    }
athos@1379
   425
athos@1379
   426
  }
athos@1261
   427
  
athos@1298
   428
  void LpGlpk::_setObjCoeff(int i, Value obj_coef)
athos@1298
   429
  {
athos@1376
   430
    //i=0 means the constant term (shift)
athos@1298
   431
    lpx_set_obj_coef(lp, i, obj_coef);
athos@1298
   432
  }
athos@1261
   433
athos@2324
   434
  LpGlpk::Value LpGlpk::_getObjCoeff(int i){
athos@2324
   435
    //i=0 means the constant term (shift)
athos@2324
   436
    return lpx_get_obj_coef(lp, i);
athos@2324
   437
  }
athos@2324
   438
athos@1377
   439
  void LpGlpk::_clearObj()
athos@1376
   440
  {
athos@1377
   441
    for (int i=0;i<=lpx_get_num_cols(lp);++i){
athos@1377
   442
      lpx_set_obj_coef(lp, i, 0);
athos@1376
   443
    }
athos@1376
   444
  }
athos@1377
   445
//   void LpGlpk::_setObj(int length,
athos@1377
   446
// 		       int  const * indices, 
athos@1377
   447
// 		       Value  const * values )
athos@1377
   448
//   {
athos@1377
   449
//     Value new_values[1+lpx_num_cols()];
athos@1377
   450
//     for (i=0;i<=lpx_num_cols();++i){
athos@1377
   451
//       new_values[i]=0;
athos@1377
   452
//     }
athos@1377
   453
//     for (i=1;i<=length;++i){
athos@1377
   454
//       new_values[indices[i]]=values[i];
athos@1377
   455
//     }
athos@1377
   456
    
athos@1377
   457
//     for (i=0;i<=lpx_num_cols();++i){
athos@1377
   458
//       lpx_set_obj_coef(lp, i, new_values[i]);
athos@1377
   459
//     }
athos@1377
   460
//   }
alpar@1263
   461
alpar@1303
   462
  LpGlpk::SolveExitStatus LpGlpk::_solve()
alpar@1263
   463
  {
athos@1458
   464
    int i =  lpx_simplex(lp);
athos@1298
   465
    switch (i) {
athos@1298
   466
    case LPX_E_OK: 
athos@1298
   467
      return SOLVED;
athos@1298
   468
    default:
athos@1298
   469
      return UNSOLVED;
athos@1298
   470
    }
alpar@1263
   471
  }
alpar@1263
   472
alpar@1293
   473
  LpGlpk::Value LpGlpk::_getPrimal(int i)
alpar@1263
   474
  {
athos@1298
   475
    return lpx_get_col_prim(lp,i);
alpar@1263
   476
  }
marci@1787
   477
marci@1787
   478
  LpGlpk::Value LpGlpk::_getDual(int i)
marci@1787
   479
  {
marci@1787
   480
    return lpx_get_row_dual(lp,i);
marci@1787
   481
  }
alpar@1263
   482
  
alpar@1312
   483
  LpGlpk::Value LpGlpk::_getPrimalValue()
alpar@1312
   484
  {
athos@1314
   485
    return lpx_get_obj_val(lp);
alpar@1312
   486
  }
marci@1840
   487
  bool LpGlpk::_isBasicCol(int i) {
marci@1840
   488
    return (lpx_get_col_stat(lp, i)==LPX_BS);
marci@1840
   489
  }
alpar@1312
   490
  
athos@1298
   491
 
alpar@1312
   492
  LpGlpk::SolutionStatus LpGlpk::_getPrimalStatus()
alpar@1294
   493
  {
athos@1298
   494
    int stat=  lpx_get_status(lp);
athos@1298
   495
    switch (stat) {
athos@1298
   496
    case LPX_UNDEF://Undefined (no solve has been run yet)
athos@1298
   497
      return UNDEFINED;
athos@1458
   498
    case LPX_NOFEAS://There is no feasible solution (primal, I guess)
athos@1458
   499
    case LPX_INFEAS://Infeasible 
athos@1458
   500
      return INFEASIBLE;
athos@1458
   501
    case LPX_UNBND://Unbounded
athos@1458
   502
      return INFINITE;
athos@1458
   503
    case LPX_FEAS://Feasible
athos@1458
   504
      return FEASIBLE;
athos@1458
   505
    case LPX_OPT://Feasible
athos@1458
   506
      return OPTIMAL;
athos@1458
   507
    default:
athos@1458
   508
      return UNDEFINED; //to avoid gcc warning
athos@1458
   509
      //FIXME error
athos@1458
   510
    }
athos@1458
   511
  }
athos@1458
   512
athos@1458
   513
  LpGlpk::SolutionStatus LpGlpk::_getDualStatus()
athos@1458
   514
  {
athos@1473
   515
//     std::cout<<"Itt megy: "<<lpx_get_dual_stat(lp)<<std::endl;
athos@1473
   516
//     std::cout<<"Itt a primal: "<<lpx_get_prim_stat(lp)<<std::endl;
athos@1473
   517
alpar@1466
   518
    switch (lpx_get_dual_stat(lp)) {
athos@1458
   519
    case LPX_D_UNDEF://Undefined (no solve has been run yet)
athos@1458
   520
      return UNDEFINED;
athos@1540
   521
    case LPX_D_NOFEAS://There is no dual feasible solution 
athos@1460
   522
//    case LPX_D_INFEAS://Infeasible 
athos@1458
   523
      return INFEASIBLE;
athos@1473
   524
    case LPX_D_FEAS://Feasible    
athos@1473
   525
      switch (lpx_get_status(lp)) {
athos@1473
   526
      case LPX_NOFEAS:
athos@1458
   527
	return INFINITE;
athos@1458
   528
      case LPX_OPT:
athos@1458
   529
	return OPTIMAL;
athos@1458
   530
      default:
athos@1458
   531
	return FEASIBLE;
athos@1458
   532
      }
athos@1458
   533
    default:
athos@1458
   534
      return UNDEFINED; //to avoid gcc warning
athos@1458
   535
      //FIXME error
athos@1458
   536
    }
athos@1458
   537
  }
athos@1458
   538
athos@1463
   539
  LpGlpk::ProblemTypes LpGlpk::_getProblemType()
athos@1458
   540
  {
athos@1460
   541
      //int stat=  lpx_get_status(lp);
athos@1458
   542
    int statp=  lpx_get_prim_stat(lp);
athos@1458
   543
    int statd=  lpx_get_dual_stat(lp);
athos@1464
   544
    if (statp==LPX_P_FEAS && statd==LPX_D_FEAS)
athos@1460
   545
	return PRIMAL_DUAL_FEASIBLE;
athos@1464
   546
    if (statp==LPX_P_FEAS && statd==LPX_D_NOFEAS)
athos@1460
   547
	return PRIMAL_FEASIBLE_DUAL_INFEASIBLE;
athos@1464
   548
    if (statp==LPX_P_NOFEAS && statd==LPX_D_FEAS)
athos@1460
   549
	return PRIMAL_INFEASIBLE_DUAL_FEASIBLE;
athos@1464
   550
    if (statp==LPX_P_NOFEAS && statd==LPX_D_NOFEAS)
athos@1460
   551
	return PRIMAL_DUAL_INFEASIBLE;
athos@1460
   552
    //In all other cases
athos@1460
   553
    return UNKNOWN;
alpar@1294
   554
  }
alpar@1263
   555
alpar@1312
   556
  void LpGlpk::_setMax()
alpar@1312
   557
  {
alpar@1321
   558
    lpx_set_obj_dir(lp, LPX_MAX);
alpar@1321
   559
  }
alpar@1321
   560
alpar@1312
   561
  void LpGlpk::_setMin()
alpar@1312
   562
  {
alpar@1321
   563
    lpx_set_obj_dir(lp, LPX_MIN);
alpar@1321
   564
  }
alpar@1321
   565
athos@2324
   566
  bool LpGlpk::_isMax()
athos@2324
   567
  {
athos@2324
   568
    return (lpx_get_obj_dir(lp)==LPX_MAX);
athos@2324
   569
  }
athos@2324
   570
alpar@1321
   571
 
athos@2324
   572
alpar@1321
   573
  void LpGlpk::messageLevel(int m)
alpar@1321
   574
  {
alpar@1321
   575
    lpx_set_int_parm(lp, LPX_K_MSGLEV, m);
alpar@1321
   576
  }
alpar@1312
   577
alpar@1326
   578
  void LpGlpk::presolver(bool b)
alpar@1326
   579
  {
alpar@1326
   580
    lpx_set_int_parm(lp, LPX_K_PRESOL, b);
alpar@1326
   581
  }
alpar@1326
   582
alpar@1312
   583
 
athos@1261
   584
} //END OF NAMESPACE LEMON