lemon/lp_glpk.cc
author deba
Tue, 19 Dec 2006 15:53:42 +0000
changeset 2333 8070a099ffb6
parent 2326 af8c695372be
child 2345 bfcaad2b84e8
permissions -rw-r--r--
MACROS for debug map usage
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@2328
   216
  LpGlpk::Value LpGlpk::_getCoeff(int row, int col)
athos@2324
   217
  {
athos@2328
   218
athos@2328
   219
    int length=lpx_get_mat_row(lp, row, 0, 0);
athos@2328
   220
    
athos@2328
   221
    std::vector<int> indices(length + 2);
athos@2328
   222
    std::vector<Value> values(length + 2);
athos@2328
   223
    
athos@2328
   224
    lpx_get_mat_row(lp, row, &indices[0], &values[0]);
athos@2328
   225
    
athos@2328
   226
    //The following code does not suppose that the elements of the
athos@2328
   227
    //array indices are sorted
athos@2328
   228
    for (int i = 1; i <= length; ++i) {
athos@2328
   229
      if (indices[i]==col){
athos@2328
   230
	return values[i];
athos@2328
   231
      }
athos@2328
   232
    }
athos@2324
   233
    return 0;
athos@2328
   234
athos@2324
   235
  }
athos@2324
   236
athos@2324
   237
alpar@1321
   238
  void LpGlpk::_setColLowerBound(int i, Value lo)
alpar@1321
   239
  {
alpar@1321
   240
    if (lo==INF) {
alpar@1321
   241
      //FIXME error
alpar@1321
   242
    }
alpar@1321
   243
    int b=lpx_get_col_type(lp, i);
alpar@1321
   244
    double up=lpx_get_col_ub(lp, i);	
alpar@1321
   245
    if (lo==-INF) {
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_FR, lo, up);
alpar@1321
   250
	break;
alpar@1321
   251
      case LPX_UP:
alpar@1321
   252
	break;
alpar@1321
   253
      case LPX_DB:
alpar@1321
   254
      case LPX_FX:
alpar@1321
   255
	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
alpar@1321
   256
	break;
alpar@1321
   257
      default: ;
alpar@1321
   258
	//FIXME error
alpar@1321
   259
      }
alpar@1321
   260
    } else {
alpar@1321
   261
      switch (b) {
alpar@1321
   262
      case LPX_FR:
alpar@1321
   263
      case LPX_LO:
alpar@1321
   264
	lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
alpar@1321
   265
	break;
alpar@1321
   266
      case LPX_UP:	  
alpar@1321
   267
      case LPX_DB:
alpar@1321
   268
      case LPX_FX:
alpar@1321
   269
	if (lo==up) 
alpar@1321
   270
	  lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
alpar@1321
   271
	else 
alpar@1321
   272
	  lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
alpar@1321
   273
	break;
alpar@1321
   274
      default: ;
alpar@1321
   275
	//FIXME error
alpar@1321
   276
      }
athos@1261
   277
    }
athos@1261
   278
alpar@1321
   279
  }
athos@2328
   280
athos@2328
   281
  LpGlpk::Value LpGlpk::_getColLowerBound(int i)
athos@2328
   282
  {
athos@2328
   283
    int b=lpx_get_col_type(lp, i);
athos@2328
   284
      switch (b) {
athos@2328
   285
      case LPX_LO:
athos@2328
   286
      case LPX_DB:
athos@2328
   287
      case LPX_FX:
athos@2328
   288
	return lpx_get_col_lb(lp, i);	
athos@2328
   289
      default: ;
athos@2328
   290
	return -INF;
athos@2328
   291
      }
athos@2328
   292
  }
alpar@1321
   293
  
alpar@1321
   294
  void LpGlpk::_setColUpperBound(int i, Value up)
alpar@1321
   295
  {
alpar@1321
   296
    if (up==-INF) {
alpar@1321
   297
      //FIXME error
athos@1261
   298
    }
alpar@1321
   299
    int b=lpx_get_col_type(lp, i);
alpar@1321
   300
    double lo=lpx_get_col_lb(lp, i);
alpar@1321
   301
    if (up==INF) {
alpar@1321
   302
      switch (b) {
alpar@1321
   303
      case LPX_FR:
alpar@1321
   304
      case LPX_LO:
alpar@1321
   305
	break;
alpar@1321
   306
      case LPX_UP:
alpar@1321
   307
	lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
alpar@1321
   308
	break;
alpar@1321
   309
      case LPX_DB:
alpar@1321
   310
      case LPX_FX:
alpar@1321
   311
	lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
alpar@1321
   312
	break;
alpar@1321
   313
      default: ;
athos@1261
   314
	//FIXME error
athos@1261
   315
      }
alpar@1321
   316
    } else {
alpar@1321
   317
      switch (b) {
alpar@1321
   318
      case LPX_FR:
alpar@1321
   319
	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
alpar@1321
   320
	break;
alpar@1321
   321
      case LPX_UP:
alpar@1321
   322
	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
alpar@1321
   323
	break;
alpar@1321
   324
      case LPX_LO:
alpar@1321
   325
      case LPX_DB:
alpar@1321
   326
      case LPX_FX:
alpar@1321
   327
	if (lo==up) 
alpar@1321
   328
	  lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
alpar@1321
   329
	else 
alpar@1321
   330
	  lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
alpar@1321
   331
	break;
alpar@1321
   332
      default: ;
athos@1261
   333
	//FIXME error
athos@1261
   334
      }
alpar@1321
   335
    }
alpar@1321
   336
  }
athos@2328
   337
athos@2328
   338
  LpGlpk::Value LpGlpk::_getColUpperBound(int i)
athos@2328
   339
  {
athos@2328
   340
    int b=lpx_get_col_type(lp, i);
athos@2328
   341
      switch (b) {
athos@2328
   342
      case LPX_UP:
athos@2328
   343
      case LPX_DB:
athos@2328
   344
      case LPX_FX:
athos@2328
   345
	return lpx_get_col_ub(lp, i);	
athos@2328
   346
      default: ;
athos@2328
   347
	return INF;
athos@2328
   348
      }
athos@2328
   349
  }
alpar@1321
   350
  
athos@1405
   351
//   void LpGlpk::_setRowLowerBound(int i, Value lo)
athos@1405
   352
//   {
athos@1405
   353
//     if (lo==INF) {
athos@1405
   354
//       //FIXME error
athos@1405
   355
//     }
athos@1405
   356
//     int b=lpx_get_row_type(lp, i);
athos@1405
   357
//     double up=lpx_get_row_ub(lp, i);	
athos@1405
   358
//     if (lo==-INF) {
athos@1405
   359
//       switch (b) {
athos@1405
   360
//       case LPX_FR:
athos@1405
   361
//       case LPX_LO:
athos@1405
   362
// 	lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
athos@1405
   363
// 	break;
athos@1405
   364
//       case LPX_UP:
athos@1405
   365
// 	break;
athos@1405
   366
//       case LPX_DB:
athos@1405
   367
//       case LPX_FX:
athos@1405
   368
// 	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
athos@1405
   369
// 	break;
athos@1405
   370
//       default: ;
athos@1405
   371
// 	//FIXME error
athos@1405
   372
//       }
athos@1405
   373
//     } else {
athos@1405
   374
//       switch (b) {
athos@1405
   375
//       case LPX_FR:
athos@1405
   376
//       case LPX_LO:
athos@1405
   377
// 	lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
athos@1405
   378
// 	break;
athos@1405
   379
//       case LPX_UP:	  
athos@1405
   380
//       case LPX_DB:
athos@1405
   381
//       case LPX_FX:
athos@1405
   382
// 	if (lo==up) 
athos@1405
   383
// 	  lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
athos@1405
   384
// 	else 
athos@1405
   385
// 	  lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
athos@1405
   386
// 	break;
athos@1405
   387
//       default: ;
athos@1405
   388
// 	//FIXME error
athos@1405
   389
//       }
athos@1405
   390
//     }
athos@1405
   391
//   }
athos@1261
   392
  
athos@1405
   393
//   void LpGlpk::_setRowUpperBound(int i, Value up)
athos@1405
   394
//   {
athos@1405
   395
//     if (up==-INF) {
athos@1405
   396
//       //FIXME error
athos@1405
   397
//     }
athos@1405
   398
//     int b=lpx_get_row_type(lp, i);
athos@1405
   399
//     double lo=lpx_get_row_lb(lp, i);
athos@1405
   400
//     if (up==INF) {
athos@1405
   401
//       switch (b) {
athos@1405
   402
//       case LPX_FR:
athos@1405
   403
//       case LPX_LO:
athos@1405
   404
// 	break;
athos@1405
   405
//       case LPX_UP:
athos@1405
   406
// 	lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
athos@1405
   407
// 	break;
athos@1405
   408
//       case LPX_DB:
athos@1405
   409
//       case LPX_FX:
athos@1405
   410
// 	lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
athos@1405
   411
// 	break;
athos@1405
   412
//       default: ;
athos@1405
   413
// 	//FIXME error
athos@1405
   414
//       }
athos@1405
   415
//     } else {
athos@1405
   416
//       switch (b) {
athos@1405
   417
//       case LPX_FR:
athos@1405
   418
// 	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
athos@1405
   419
// 	break;
athos@1405
   420
//       case LPX_UP:
athos@1405
   421
// 	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
athos@1405
   422
// 	break;
athos@1405
   423
//       case LPX_LO:
athos@1405
   424
//       case LPX_DB:
athos@1405
   425
//       case LPX_FX:
athos@1405
   426
// 	if (lo==up) 
athos@1405
   427
// 	  lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
athos@1405
   428
// 	else 
athos@1405
   429
// 	  lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
athos@1405
   430
// 	break;
athos@1405
   431
//       default: ;
athos@1405
   432
// 	//FIXME error
athos@1405
   433
//       }
athos@1405
   434
//     }
athos@1405
   435
//   }
athos@1379
   436
athos@1379
   437
  void LpGlpk::_setRowBounds(int i, Value lb, Value ub)
athos@1379
   438
  {
athos@1379
   439
    //Bad parameter
athos@1379
   440
    if (lb==INF || ub==-INF) {
athos@1379
   441
      //FIXME error
athos@1379
   442
    }
athos@1379
   443
athos@1379
   444
    if (lb == -INF){
athos@1379
   445
      if (ub == INF){
athos@1379
   446
	lpx_set_row_bnds(lp, i, LPX_FR, lb, ub);
athos@1379
   447
      }
athos@1379
   448
      else{
athos@1379
   449
	lpx_set_row_bnds(lp, i, LPX_UP, lb, ub);
athos@1379
   450
      }
athos@1379
   451
    }
athos@1379
   452
    else{
athos@1379
   453
      if (ub==INF){
athos@1379
   454
	lpx_set_row_bnds(lp, i, LPX_LO, lb, ub);
athos@1379
   455
athos@1379
   456
      }
athos@1379
   457
      else{
athos@1379
   458
	if (lb == ub){
athos@1379
   459
	  lpx_set_row_bnds(lp, i, LPX_FX, lb, ub);
athos@1379
   460
	}
athos@1379
   461
	else{
athos@1379
   462
	  lpx_set_row_bnds(lp, i, LPX_DB, lb, ub);
athos@1379
   463
	}
athos@1379
   464
      }
athos@1379
   465
    }
athos@1379
   466
athos@1379
   467
  }
athos@2328
   468
athos@2328
   469
  void LpGlpk::_getRowBounds(int i, Value &lb, Value &ub)
athos@2328
   470
  {
athos@2328
   471
athos@2328
   472
    int b=lpx_get_row_type(lp, i);
athos@2328
   473
    switch (b) {
athos@2328
   474
    case LPX_FR:
athos@2328
   475
    case LPX_UP:
athos@2328
   476
      lb = -INF;
athos@2328
   477
	break;
athos@2328
   478
    default: 
athos@2328
   479
      lb=lpx_get_row_lb(lp, i);
athos@2328
   480
    }
athos@2328
   481
athos@2328
   482
    switch (b) {
athos@2328
   483
    case LPX_FR:
athos@2328
   484
    case LPX_LO:
athos@2328
   485
      ub = INF;
athos@2328
   486
	break;
athos@2328
   487
    default: 
athos@2328
   488
      ub=lpx_get_row_ub(lp, i);
athos@2328
   489
    }
athos@2328
   490
    
athos@2328
   491
  }
athos@1261
   492
  
athos@1298
   493
  void LpGlpk::_setObjCoeff(int i, Value obj_coef)
athos@1298
   494
  {
athos@1376
   495
    //i=0 means the constant term (shift)
athos@1298
   496
    lpx_set_obj_coef(lp, i, obj_coef);
athos@1298
   497
  }
athos@1261
   498
athos@2324
   499
  LpGlpk::Value LpGlpk::_getObjCoeff(int i){
athos@2324
   500
    //i=0 means the constant term (shift)
athos@2324
   501
    return lpx_get_obj_coef(lp, i);
athos@2324
   502
  }
athos@2324
   503
athos@1377
   504
  void LpGlpk::_clearObj()
athos@1376
   505
  {
athos@1377
   506
    for (int i=0;i<=lpx_get_num_cols(lp);++i){
athos@1377
   507
      lpx_set_obj_coef(lp, i, 0);
athos@1376
   508
    }
athos@1376
   509
  }
athos@1377
   510
//   void LpGlpk::_setObj(int length,
athos@1377
   511
// 		       int  const * indices, 
athos@1377
   512
// 		       Value  const * values )
athos@1377
   513
//   {
athos@1377
   514
//     Value new_values[1+lpx_num_cols()];
athos@1377
   515
//     for (i=0;i<=lpx_num_cols();++i){
athos@1377
   516
//       new_values[i]=0;
athos@1377
   517
//     }
athos@1377
   518
//     for (i=1;i<=length;++i){
athos@1377
   519
//       new_values[indices[i]]=values[i];
athos@1377
   520
//     }
athos@1377
   521
    
athos@1377
   522
//     for (i=0;i<=lpx_num_cols();++i){
athos@1377
   523
//       lpx_set_obj_coef(lp, i, new_values[i]);
athos@1377
   524
//     }
athos@1377
   525
//   }
alpar@1263
   526
alpar@1303
   527
  LpGlpk::SolveExitStatus LpGlpk::_solve()
alpar@1263
   528
  {
athos@1458
   529
    int i =  lpx_simplex(lp);
athos@1298
   530
    switch (i) {
athos@1298
   531
    case LPX_E_OK: 
athos@1298
   532
      return SOLVED;
athos@1298
   533
    default:
athos@1298
   534
      return UNSOLVED;
athos@1298
   535
    }
alpar@1263
   536
  }
alpar@1263
   537
alpar@1293
   538
  LpGlpk::Value LpGlpk::_getPrimal(int i)
alpar@1263
   539
  {
athos@1298
   540
    return lpx_get_col_prim(lp,i);
alpar@1263
   541
  }
marci@1787
   542
marci@1787
   543
  LpGlpk::Value LpGlpk::_getDual(int i)
marci@1787
   544
  {
marci@1787
   545
    return lpx_get_row_dual(lp,i);
marci@1787
   546
  }
alpar@1263
   547
  
alpar@1312
   548
  LpGlpk::Value LpGlpk::_getPrimalValue()
alpar@1312
   549
  {
athos@1314
   550
    return lpx_get_obj_val(lp);
alpar@1312
   551
  }
marci@1840
   552
  bool LpGlpk::_isBasicCol(int i) {
marci@1840
   553
    return (lpx_get_col_stat(lp, i)==LPX_BS);
marci@1840
   554
  }
alpar@1312
   555
  
athos@1298
   556
 
alpar@1312
   557
  LpGlpk::SolutionStatus LpGlpk::_getPrimalStatus()
alpar@1294
   558
  {
athos@1298
   559
    int stat=  lpx_get_status(lp);
athos@1298
   560
    switch (stat) {
athos@1298
   561
    case LPX_UNDEF://Undefined (no solve has been run yet)
athos@1298
   562
      return UNDEFINED;
athos@1458
   563
    case LPX_NOFEAS://There is no feasible solution (primal, I guess)
athos@1458
   564
    case LPX_INFEAS://Infeasible 
athos@1458
   565
      return INFEASIBLE;
athos@1458
   566
    case LPX_UNBND://Unbounded
athos@1458
   567
      return INFINITE;
athos@1458
   568
    case LPX_FEAS://Feasible
athos@1458
   569
      return FEASIBLE;
athos@1458
   570
    case LPX_OPT://Feasible
athos@1458
   571
      return OPTIMAL;
athos@1458
   572
    default:
athos@1458
   573
      return UNDEFINED; //to avoid gcc warning
athos@1458
   574
      //FIXME error
athos@1458
   575
    }
athos@1458
   576
  }
athos@1458
   577
athos@1458
   578
  LpGlpk::SolutionStatus LpGlpk::_getDualStatus()
athos@1458
   579
  {
athos@1473
   580
//     std::cout<<"Itt megy: "<<lpx_get_dual_stat(lp)<<std::endl;
athos@1473
   581
//     std::cout<<"Itt a primal: "<<lpx_get_prim_stat(lp)<<std::endl;
athos@1473
   582
alpar@1466
   583
    switch (lpx_get_dual_stat(lp)) {
athos@1458
   584
    case LPX_D_UNDEF://Undefined (no solve has been run yet)
athos@1458
   585
      return UNDEFINED;
athos@1540
   586
    case LPX_D_NOFEAS://There is no dual feasible solution 
athos@1460
   587
//    case LPX_D_INFEAS://Infeasible 
athos@1458
   588
      return INFEASIBLE;
athos@1473
   589
    case LPX_D_FEAS://Feasible    
athos@1473
   590
      switch (lpx_get_status(lp)) {
athos@1473
   591
      case LPX_NOFEAS:
athos@1458
   592
	return INFINITE;
athos@1458
   593
      case LPX_OPT:
athos@1458
   594
	return OPTIMAL;
athos@1458
   595
      default:
athos@1458
   596
	return FEASIBLE;
athos@1458
   597
      }
athos@1458
   598
    default:
athos@1458
   599
      return UNDEFINED; //to avoid gcc warning
athos@1458
   600
      //FIXME error
athos@1458
   601
    }
athos@1458
   602
  }
athos@1458
   603
athos@1463
   604
  LpGlpk::ProblemTypes LpGlpk::_getProblemType()
athos@1458
   605
  {
athos@1460
   606
      //int stat=  lpx_get_status(lp);
athos@1458
   607
    int statp=  lpx_get_prim_stat(lp);
athos@1458
   608
    int statd=  lpx_get_dual_stat(lp);
athos@1464
   609
    if (statp==LPX_P_FEAS && statd==LPX_D_FEAS)
athos@1460
   610
	return PRIMAL_DUAL_FEASIBLE;
athos@1464
   611
    if (statp==LPX_P_FEAS && statd==LPX_D_NOFEAS)
athos@1460
   612
	return PRIMAL_FEASIBLE_DUAL_INFEASIBLE;
athos@1464
   613
    if (statp==LPX_P_NOFEAS && statd==LPX_D_FEAS)
athos@1460
   614
	return PRIMAL_INFEASIBLE_DUAL_FEASIBLE;
athos@1464
   615
    if (statp==LPX_P_NOFEAS && statd==LPX_D_NOFEAS)
athos@1460
   616
	return PRIMAL_DUAL_INFEASIBLE;
athos@1460
   617
    //In all other cases
athos@1460
   618
    return UNKNOWN;
alpar@1294
   619
  }
alpar@1263
   620
alpar@1312
   621
  void LpGlpk::_setMax()
alpar@1312
   622
  {
alpar@1321
   623
    lpx_set_obj_dir(lp, LPX_MAX);
alpar@1321
   624
  }
alpar@1321
   625
alpar@1312
   626
  void LpGlpk::_setMin()
alpar@1312
   627
  {
alpar@1321
   628
    lpx_set_obj_dir(lp, LPX_MIN);
alpar@1321
   629
  }
alpar@1321
   630
athos@2324
   631
  bool LpGlpk::_isMax()
athos@2324
   632
  {
athos@2324
   633
    return (lpx_get_obj_dir(lp)==LPX_MAX);
athos@2324
   634
  }
athos@2324
   635
alpar@1321
   636
 
athos@2324
   637
alpar@1321
   638
  void LpGlpk::messageLevel(int m)
alpar@1321
   639
  {
alpar@1321
   640
    lpx_set_int_parm(lp, LPX_K_MSGLEV, m);
alpar@1321
   641
  }
alpar@1312
   642
alpar@1326
   643
  void LpGlpk::presolver(bool b)
alpar@1326
   644
  {
alpar@1326
   645
    lpx_set_int_parm(lp, LPX_K_PRESOL, b);
alpar@1326
   646
  }
alpar@1326
   647
alpar@1312
   648
 
athos@1261
   649
} //END OF NAMESPACE LEMON