lemon/lp_glpk.cc
author deba
Thu, 15 Feb 2007 13:06:23 +0000
changeset 2362 eb37b9774ef6
parent 2345 bfcaad2b84e8
child 2363 2aabce558574
permissions -rw-r--r--
Small changes
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()));
athos@2349
   122
alpar@1895
   123
  }
alpar@1895
   124
  
deba@2312
   125
  void LpGlpk::_setRowCoeffs(int i, LpRowIterator b, LpRowIterator e) 
alpar@1321
   126
  {
deba@2312
   127
    std::vector<int> indices;
deba@2312
   128
    std::vector<Value> values;
deba@2312
   129
deba@2312
   130
    indices.push_back(0);
deba@2312
   131
    values.push_back(0);
deba@2312
   132
deba@2312
   133
    for(LpRowIterator it=b; it!=e; ++it) {
deba@2312
   134
      indices.push_back(it->first);
deba@2312
   135
      values.push_back(it->second);
deba@2312
   136
    }
deba@2312
   137
deba@2312
   138
    lpx_set_mat_row(lp, i, values.size() - 1, &indices[0], &values[0]);
alpar@1321
   139
  }
alpar@1321
   140
  
deba@2312
   141
  void LpGlpk::_setColCoeffs(int i, LpColIterator b, LpColIterator e) {
deba@2312
   142
deba@2312
   143
    std::vector<int> indices;
deba@2312
   144
    std::vector<Value> values;
deba@2312
   145
deba@2312
   146
    indices.push_back(0);
deba@2312
   147
    values.push_back(0);
deba@2312
   148
deba@2312
   149
    for(LpColIterator it=b; it!=e; ++it) {
deba@2312
   150
      indices.push_back(it->first);
deba@2312
   151
      values.push_back(it->second);
deba@2312
   152
    }
deba@2312
   153
    
deba@2312
   154
    lpx_set_mat_col(lp, i, values.size() - 1, &indices[0], &values[0]);
alpar@1321
   155
  }
athos@1431
   156
athos@1431
   157
athos@1431
   158
  void LpGlpk::_setCoeff(int row, int col, Value value) 
athos@1431
   159
  {
deba@2312
   160
deba@2312
   161
    if (lpx_get_num_cols(lp) < lpx_get_num_rows(lp)) {
deba@2312
   162
deba@2312
   163
      int length=lpx_get_mat_row(lp, row, 0, 0);
deba@2312
   164
      
deba@2312
   165
      std::vector<int> indices(length + 2);
deba@2312
   166
      std::vector<Value> values(length + 2);
deba@2312
   167
      
deba@2312
   168
      lpx_get_mat_row(lp, row, &indices[0], &values[0]);
deba@2312
   169
      
deba@2312
   170
      //The following code does not suppose that the elements of the
deba@2312
   171
      //array indices are sorted
deba@2312
   172
      bool found=false;
deba@2312
   173
      for (int i = 1; i <= length; ++i) {
deba@2312
   174
        if (indices[i]==col){
deba@2312
   175
          found=true;
deba@2312
   176
          values[i]=value;
deba@2312
   177
          break;
deba@2312
   178
        }
deba@2312
   179
      }
deba@2312
   180
      if (!found){
deba@2312
   181
        ++length;
deba@2312
   182
        indices[length]=col;
deba@2312
   183
        values[length]=value;
deba@2312
   184
      }
athos@1431
   185
    
deba@2312
   186
      lpx_set_mat_row(lp, row, length, &indices[0], &values[0]);
deba@2312
   187
deba@2312
   188
    } else {
deba@2312
   189
deba@2312
   190
      int length=lpx_get_mat_col(lp, col, 0, 0);
deba@2312
   191
      
deba@2312
   192
      std::vector<int> indices(length + 2);
deba@2312
   193
      std::vector<Value> values(length + 2);
deba@2312
   194
      
deba@2312
   195
      lpx_get_mat_col(lp, col, &indices[0], &values[0]);
deba@2312
   196
      
deba@2312
   197
      //The following code does not suppose that the elements of the
deba@2312
   198
      //array indices are sorted
deba@2312
   199
      bool found=false;
deba@2312
   200
      for (int i = 1; i <= length; ++i) {
deba@2312
   201
        if (indices[i]==col){
deba@2312
   202
          found=true;
deba@2312
   203
          values[i]=value;
deba@2312
   204
          break;
deba@2312
   205
        }
deba@2312
   206
      }
deba@2312
   207
      if (!found){
deba@2312
   208
        ++length;
deba@2312
   209
        indices[length]=row;
deba@2312
   210
        values[length]=value;
deba@2312
   211
      }
athos@1431
   212
    
deba@2312
   213
      lpx_set_mat_col(lp, col, length, &indices[0], &values[0]);
athos@1431
   214
    }
athos@1431
   215
  }
athos@1431
   216
athos@2328
   217
  LpGlpk::Value LpGlpk::_getCoeff(int row, int col)
athos@2324
   218
  {
athos@2328
   219
athos@2328
   220
    int length=lpx_get_mat_row(lp, row, 0, 0);
athos@2328
   221
    
athos@2328
   222
    std::vector<int> indices(length + 2);
athos@2328
   223
    std::vector<Value> values(length + 2);
athos@2328
   224
    
athos@2328
   225
    lpx_get_mat_row(lp, row, &indices[0], &values[0]);
athos@2328
   226
    
athos@2328
   227
    //The following code does not suppose that the elements of the
athos@2328
   228
    //array indices are sorted
athos@2328
   229
    for (int i = 1; i <= length; ++i) {
athos@2328
   230
      if (indices[i]==col){
athos@2328
   231
	return values[i];
athos@2328
   232
      }
athos@2328
   233
    }
athos@2324
   234
    return 0;
athos@2328
   235
athos@2324
   236
  }
athos@2324
   237
athos@2324
   238
alpar@1321
   239
  void LpGlpk::_setColLowerBound(int i, Value lo)
alpar@1321
   240
  {
alpar@1321
   241
    if (lo==INF) {
alpar@1321
   242
      //FIXME error
alpar@1321
   243
    }
alpar@1321
   244
    int b=lpx_get_col_type(lp, i);
alpar@1321
   245
    double up=lpx_get_col_ub(lp, i);	
alpar@1321
   246
    if (lo==-INF) {
alpar@1321
   247
      switch (b) {
alpar@1321
   248
      case LPX_FR:
alpar@1321
   249
      case LPX_LO:
alpar@1321
   250
	lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
alpar@1321
   251
	break;
alpar@1321
   252
      case LPX_UP:
alpar@1321
   253
	break;
alpar@1321
   254
      case LPX_DB:
alpar@1321
   255
      case LPX_FX:
alpar@1321
   256
	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
alpar@1321
   257
	break;
alpar@1321
   258
      default: ;
alpar@1321
   259
	//FIXME error
alpar@1321
   260
      }
alpar@1321
   261
    } else {
alpar@1321
   262
      switch (b) {
alpar@1321
   263
      case LPX_FR:
alpar@1321
   264
      case LPX_LO:
alpar@1321
   265
	lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
alpar@1321
   266
	break;
alpar@1321
   267
      case LPX_UP:	  
alpar@1321
   268
      case LPX_DB:
alpar@1321
   269
      case LPX_FX:
alpar@1321
   270
	if (lo==up) 
alpar@1321
   271
	  lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
alpar@1321
   272
	else 
alpar@1321
   273
	  lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
alpar@1321
   274
	break;
alpar@1321
   275
      default: ;
alpar@1321
   276
	//FIXME error
alpar@1321
   277
      }
athos@1261
   278
    }
athos@1261
   279
alpar@1321
   280
  }
athos@2328
   281
athos@2328
   282
  LpGlpk::Value LpGlpk::_getColLowerBound(int i)
athos@2328
   283
  {
athos@2328
   284
    int b=lpx_get_col_type(lp, i);
athos@2328
   285
      switch (b) {
athos@2328
   286
      case LPX_LO:
athos@2328
   287
      case LPX_DB:
athos@2328
   288
      case LPX_FX:
athos@2328
   289
	return lpx_get_col_lb(lp, i);	
athos@2328
   290
      default: ;
athos@2328
   291
	return -INF;
athos@2328
   292
      }
athos@2328
   293
  }
alpar@1321
   294
  
alpar@1321
   295
  void LpGlpk::_setColUpperBound(int i, Value up)
alpar@1321
   296
  {
alpar@1321
   297
    if (up==-INF) {
alpar@1321
   298
      //FIXME error
athos@1261
   299
    }
alpar@1321
   300
    int b=lpx_get_col_type(lp, i);
alpar@1321
   301
    double lo=lpx_get_col_lb(lp, i);
alpar@1321
   302
    if (up==INF) {
alpar@1321
   303
      switch (b) {
alpar@1321
   304
      case LPX_FR:
alpar@1321
   305
      case LPX_LO:
alpar@1321
   306
	break;
alpar@1321
   307
      case LPX_UP:
alpar@1321
   308
	lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
alpar@1321
   309
	break;
alpar@1321
   310
      case LPX_DB:
alpar@1321
   311
      case LPX_FX:
alpar@1321
   312
	lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
alpar@1321
   313
	break;
alpar@1321
   314
      default: ;
athos@1261
   315
	//FIXME error
athos@1261
   316
      }
alpar@1321
   317
    } else {
alpar@1321
   318
      switch (b) {
alpar@1321
   319
      case LPX_FR:
alpar@1321
   320
	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
alpar@1321
   321
	break;
alpar@1321
   322
      case LPX_UP:
alpar@1321
   323
	lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
alpar@1321
   324
	break;
alpar@1321
   325
      case LPX_LO:
alpar@1321
   326
      case LPX_DB:
alpar@1321
   327
      case LPX_FX:
alpar@1321
   328
	if (lo==up) 
alpar@1321
   329
	  lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
alpar@1321
   330
	else 
alpar@1321
   331
	  lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
alpar@1321
   332
	break;
alpar@1321
   333
      default: ;
athos@1261
   334
	//FIXME error
athos@1261
   335
      }
alpar@1321
   336
    }
alpar@1321
   337
  }
athos@2328
   338
athos@2328
   339
  LpGlpk::Value LpGlpk::_getColUpperBound(int i)
athos@2328
   340
  {
athos@2328
   341
    int b=lpx_get_col_type(lp, i);
athos@2328
   342
      switch (b) {
athos@2328
   343
      case LPX_UP:
athos@2328
   344
      case LPX_DB:
athos@2328
   345
      case LPX_FX:
athos@2328
   346
	return lpx_get_col_ub(lp, i);	
athos@2328
   347
      default: ;
athos@2328
   348
	return INF;
athos@2328
   349
      }
athos@2328
   350
  }
alpar@1321
   351
  
athos@1405
   352
//   void LpGlpk::_setRowLowerBound(int i, Value lo)
athos@1405
   353
//   {
athos@1405
   354
//     if (lo==INF) {
athos@1405
   355
//       //FIXME error
athos@1405
   356
//     }
athos@1405
   357
//     int b=lpx_get_row_type(lp, i);
athos@1405
   358
//     double up=lpx_get_row_ub(lp, i);	
athos@1405
   359
//     if (lo==-INF) {
athos@1405
   360
//       switch (b) {
athos@1405
   361
//       case LPX_FR:
athos@1405
   362
//       case LPX_LO:
athos@1405
   363
// 	lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
athos@1405
   364
// 	break;
athos@1405
   365
//       case LPX_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_UP, 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
//       case LPX_LO:
athos@1405
   378
// 	lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
athos@1405
   379
// 	break;
athos@1405
   380
//       case LPX_UP:	  
athos@1405
   381
//       case LPX_DB:
athos@1405
   382
//       case LPX_FX:
athos@1405
   383
// 	if (lo==up) 
athos@1405
   384
// 	  lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
athos@1405
   385
// 	else 
athos@1405
   386
// 	  lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
athos@1405
   387
// 	break;
athos@1405
   388
//       default: ;
athos@1405
   389
// 	//FIXME error
athos@1405
   390
//       }
athos@1405
   391
//     }
athos@1405
   392
//   }
athos@1261
   393
  
athos@1405
   394
//   void LpGlpk::_setRowUpperBound(int i, Value up)
athos@1405
   395
//   {
athos@1405
   396
//     if (up==-INF) {
athos@1405
   397
//       //FIXME error
athos@1405
   398
//     }
athos@1405
   399
//     int b=lpx_get_row_type(lp, i);
athos@1405
   400
//     double lo=lpx_get_row_lb(lp, i);
athos@1405
   401
//     if (up==INF) {
athos@1405
   402
//       switch (b) {
athos@1405
   403
//       case LPX_FR:
athos@1405
   404
//       case LPX_LO:
athos@1405
   405
// 	break;
athos@1405
   406
//       case LPX_UP:
athos@1405
   407
// 	lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
athos@1405
   408
// 	break;
athos@1405
   409
//       case LPX_DB:
athos@1405
   410
//       case LPX_FX:
athos@1405
   411
// 	lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
athos@1405
   412
// 	break;
athos@1405
   413
//       default: ;
athos@1405
   414
// 	//FIXME error
athos@1405
   415
//       }
athos@1405
   416
//     } else {
athos@1405
   417
//       switch (b) {
athos@1405
   418
//       case LPX_FR:
athos@1405
   419
// 	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
athos@1405
   420
// 	break;
athos@1405
   421
//       case LPX_UP:
athos@1405
   422
// 	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
athos@1405
   423
// 	break;
athos@1405
   424
//       case LPX_LO:
athos@1405
   425
//       case LPX_DB:
athos@1405
   426
//       case LPX_FX:
athos@1405
   427
// 	if (lo==up) 
athos@1405
   428
// 	  lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
athos@1405
   429
// 	else 
athos@1405
   430
// 	  lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
athos@1405
   431
// 	break;
athos@1405
   432
//       default: ;
athos@1405
   433
// 	//FIXME error
athos@1405
   434
//       }
athos@1405
   435
//     }
athos@1405
   436
//   }
athos@1379
   437
athos@1379
   438
  void LpGlpk::_setRowBounds(int i, Value lb, Value ub)
athos@1379
   439
  {
athos@1379
   440
    //Bad parameter
athos@1379
   441
    if (lb==INF || ub==-INF) {
athos@1379
   442
      //FIXME error
athos@1379
   443
    }
athos@1379
   444
athos@1379
   445
    if (lb == -INF){
athos@1379
   446
      if (ub == INF){
athos@1379
   447
	lpx_set_row_bnds(lp, i, LPX_FR, lb, ub);
athos@1379
   448
      }
athos@1379
   449
      else{
athos@1379
   450
	lpx_set_row_bnds(lp, i, LPX_UP, lb, ub);
athos@1379
   451
      }
athos@1379
   452
    }
athos@1379
   453
    else{
athos@1379
   454
      if (ub==INF){
athos@1379
   455
	lpx_set_row_bnds(lp, i, LPX_LO, lb, ub);
athos@1379
   456
athos@1379
   457
      }
athos@1379
   458
      else{
athos@1379
   459
	if (lb == ub){
athos@1379
   460
	  lpx_set_row_bnds(lp, i, LPX_FX, lb, ub);
athos@1379
   461
	}
athos@1379
   462
	else{
athos@1379
   463
	  lpx_set_row_bnds(lp, i, LPX_DB, lb, ub);
athos@1379
   464
	}
athos@1379
   465
      }
athos@1379
   466
    }
athos@1379
   467
athos@1379
   468
  }
athos@2328
   469
athos@2328
   470
  void LpGlpk::_getRowBounds(int i, Value &lb, Value &ub)
athos@2328
   471
  {
athos@2328
   472
athos@2328
   473
    int b=lpx_get_row_type(lp, i);
athos@2328
   474
    switch (b) {
athos@2328
   475
    case LPX_FR:
athos@2328
   476
    case LPX_UP:
athos@2328
   477
      lb = -INF;
athos@2328
   478
	break;
athos@2328
   479
    default: 
athos@2328
   480
      lb=lpx_get_row_lb(lp, i);
athos@2328
   481
    }
athos@2328
   482
athos@2328
   483
    switch (b) {
athos@2328
   484
    case LPX_FR:
athos@2328
   485
    case LPX_LO:
athos@2328
   486
      ub = INF;
athos@2328
   487
	break;
athos@2328
   488
    default: 
athos@2328
   489
      ub=lpx_get_row_ub(lp, i);
athos@2328
   490
    }
athos@2328
   491
    
athos@2328
   492
  }
athos@1261
   493
  
athos@1298
   494
  void LpGlpk::_setObjCoeff(int i, Value obj_coef)
athos@1298
   495
  {
athos@1376
   496
    //i=0 means the constant term (shift)
athos@1298
   497
    lpx_set_obj_coef(lp, i, obj_coef);
athos@1298
   498
  }
athos@1261
   499
athos@2324
   500
  LpGlpk::Value LpGlpk::_getObjCoeff(int i){
athos@2324
   501
    //i=0 means the constant term (shift)
athos@2324
   502
    return lpx_get_obj_coef(lp, i);
athos@2324
   503
  }
athos@2324
   504
athos@1377
   505
  void LpGlpk::_clearObj()
athos@1376
   506
  {
athos@1377
   507
    for (int i=0;i<=lpx_get_num_cols(lp);++i){
athos@1377
   508
      lpx_set_obj_coef(lp, i, 0);
athos@1376
   509
    }
athos@1376
   510
  }
athos@1377
   511
//   void LpGlpk::_setObj(int length,
athos@1377
   512
// 		       int  const * indices, 
athos@1377
   513
// 		       Value  const * values )
athos@1377
   514
//   {
athos@1377
   515
//     Value new_values[1+lpx_num_cols()];
athos@1377
   516
//     for (i=0;i<=lpx_num_cols();++i){
athos@1377
   517
//       new_values[i]=0;
athos@1377
   518
//     }
athos@1377
   519
//     for (i=1;i<=length;++i){
athos@1377
   520
//       new_values[indices[i]]=values[i];
athos@1377
   521
//     }
athos@1377
   522
    
athos@1377
   523
//     for (i=0;i<=lpx_num_cols();++i){
athos@1377
   524
//       lpx_set_obj_coef(lp, i, new_values[i]);
athos@1377
   525
//     }
athos@1377
   526
//   }
alpar@1263
   527
alpar@1303
   528
  LpGlpk::SolveExitStatus LpGlpk::_solve()
alpar@1263
   529
  {
athos@2345
   530
    // A way to check the problem to be solved
athos@2345
   531
    //lpx_write_cpxlp(lp,"naittvan.cpx");    
athos@2345
   532
athos@1458
   533
    int i =  lpx_simplex(lp);
athos@1298
   534
    switch (i) {
athos@1298
   535
    case LPX_E_OK: 
athos@1298
   536
      return SOLVED;
athos@1298
   537
    default:
athos@1298
   538
      return UNSOLVED;
athos@1298
   539
    }
alpar@1263
   540
  }
alpar@1263
   541
alpar@1293
   542
  LpGlpk::Value LpGlpk::_getPrimal(int i)
alpar@1263
   543
  {
athos@1298
   544
    return lpx_get_col_prim(lp,i);
alpar@1263
   545
  }
marci@1787
   546
marci@1787
   547
  LpGlpk::Value LpGlpk::_getDual(int i)
marci@1787
   548
  {
marci@1787
   549
    return lpx_get_row_dual(lp,i);
marci@1787
   550
  }
alpar@1263
   551
  
alpar@1312
   552
  LpGlpk::Value LpGlpk::_getPrimalValue()
alpar@1312
   553
  {
athos@1314
   554
    return lpx_get_obj_val(lp);
alpar@1312
   555
  }
marci@1840
   556
  bool LpGlpk::_isBasicCol(int i) {
marci@1840
   557
    return (lpx_get_col_stat(lp, i)==LPX_BS);
marci@1840
   558
  }
alpar@1312
   559
  
athos@1298
   560
 
alpar@1312
   561
  LpGlpk::SolutionStatus LpGlpk::_getPrimalStatus()
alpar@1294
   562
  {
athos@1298
   563
    int stat=  lpx_get_status(lp);
athos@1298
   564
    switch (stat) {
athos@1298
   565
    case LPX_UNDEF://Undefined (no solve has been run yet)
athos@1298
   566
      return UNDEFINED;
athos@1458
   567
    case LPX_NOFEAS://There is no feasible solution (primal, I guess)
athos@1458
   568
    case LPX_INFEAS://Infeasible 
athos@1458
   569
      return INFEASIBLE;
athos@1458
   570
    case LPX_UNBND://Unbounded
athos@1458
   571
      return INFINITE;
athos@1458
   572
    case LPX_FEAS://Feasible
athos@1458
   573
      return FEASIBLE;
athos@1458
   574
    case LPX_OPT://Feasible
athos@1458
   575
      return OPTIMAL;
athos@1458
   576
    default:
athos@1458
   577
      return UNDEFINED; //to avoid gcc warning
athos@1458
   578
      //FIXME error
athos@1458
   579
    }
athos@1458
   580
  }
athos@1458
   581
athos@1458
   582
  LpGlpk::SolutionStatus LpGlpk::_getDualStatus()
athos@1458
   583
  {
athos@1473
   584
//     std::cout<<"Itt megy: "<<lpx_get_dual_stat(lp)<<std::endl;
athos@1473
   585
//     std::cout<<"Itt a primal: "<<lpx_get_prim_stat(lp)<<std::endl;
athos@1473
   586
alpar@1466
   587
    switch (lpx_get_dual_stat(lp)) {
athos@1458
   588
    case LPX_D_UNDEF://Undefined (no solve has been run yet)
athos@1458
   589
      return UNDEFINED;
athos@1540
   590
    case LPX_D_NOFEAS://There is no dual feasible solution 
athos@1460
   591
//    case LPX_D_INFEAS://Infeasible 
athos@1458
   592
      return INFEASIBLE;
athos@1473
   593
    case LPX_D_FEAS://Feasible    
athos@1473
   594
      switch (lpx_get_status(lp)) {
athos@1473
   595
      case LPX_NOFEAS:
athos@1458
   596
	return INFINITE;
athos@1458
   597
      case LPX_OPT:
athos@1458
   598
	return OPTIMAL;
athos@1458
   599
      default:
athos@1458
   600
	return FEASIBLE;
athos@1458
   601
      }
athos@1458
   602
    default:
athos@1458
   603
      return UNDEFINED; //to avoid gcc warning
athos@1458
   604
      //FIXME error
athos@1458
   605
    }
athos@1458
   606
  }
athos@1458
   607
athos@1463
   608
  LpGlpk::ProblemTypes LpGlpk::_getProblemType()
athos@1458
   609
  {
athos@1460
   610
      //int stat=  lpx_get_status(lp);
athos@1458
   611
    int statp=  lpx_get_prim_stat(lp);
athos@1458
   612
    int statd=  lpx_get_dual_stat(lp);
athos@1464
   613
    if (statp==LPX_P_FEAS && statd==LPX_D_FEAS)
athos@1460
   614
	return PRIMAL_DUAL_FEASIBLE;
athos@1464
   615
    if (statp==LPX_P_FEAS && statd==LPX_D_NOFEAS)
athos@1460
   616
	return PRIMAL_FEASIBLE_DUAL_INFEASIBLE;
athos@1464
   617
    if (statp==LPX_P_NOFEAS && statd==LPX_D_FEAS)
athos@1460
   618
	return PRIMAL_INFEASIBLE_DUAL_FEASIBLE;
athos@1464
   619
    if (statp==LPX_P_NOFEAS && statd==LPX_D_NOFEAS)
athos@1460
   620
	return PRIMAL_DUAL_INFEASIBLE;
athos@1460
   621
    //In all other cases
athos@1460
   622
    return UNKNOWN;
alpar@1294
   623
  }
alpar@1263
   624
alpar@1312
   625
  void LpGlpk::_setMax()
alpar@1312
   626
  {
alpar@1321
   627
    lpx_set_obj_dir(lp, LPX_MAX);
alpar@1321
   628
  }
alpar@1321
   629
alpar@1312
   630
  void LpGlpk::_setMin()
alpar@1312
   631
  {
alpar@1321
   632
    lpx_set_obj_dir(lp, LPX_MIN);
alpar@1321
   633
  }
alpar@1321
   634
athos@2324
   635
  bool LpGlpk::_isMax()
athos@2324
   636
  {
athos@2324
   637
    return (lpx_get_obj_dir(lp)==LPX_MAX);
athos@2324
   638
  }
athos@2324
   639
alpar@1321
   640
 
athos@2324
   641
alpar@1321
   642
  void LpGlpk::messageLevel(int m)
alpar@1321
   643
  {
alpar@1321
   644
    lpx_set_int_parm(lp, LPX_K_MSGLEV, m);
alpar@1321
   645
  }
alpar@1312
   646
alpar@1326
   647
  void LpGlpk::presolver(bool b)
alpar@1326
   648
  {
alpar@1326
   649
    lpx_set_int_parm(lp, LPX_K_PRESOL, b);
alpar@1326
   650
  }
alpar@1326
   651
alpar@1312
   652
 
athos@1261
   653
} //END OF NAMESPACE LEMON