src/lemon/lp_glpk.cc
author ladanyi
Tue, 05 Apr 2005 22:37:19 +0000
changeset 1308 0274efa2222f
parent 1303 9bcc455da4f5
child 1312 48f9299b390d
permissions -rw-r--r--
Applied the changes which somehow vanished during my last merge. Thanks goes
to Marci for noticing this. In detail:
- added amsmath and amssymb latex packages for latex documentation
- src/demo is also scanned for doxygen input files
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
athos@1261
     5
 * (Egervary Combinatorial Optimization Research Group, 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
athos@1261
    27
    /// \e
athos@1261
    28
    int LpGlpk::_addCol() { 
athos@1261
    29
	int i=lpx_add_cols(lp, 1);
athos@1261
    30
	_setColLowerBound(i, -INF);
athos@1261
    31
	_setColUpperBound(i, INF);
athos@1261
    32
	return i;
athos@1261
    33
    }
athos@1261
    34
athos@1261
    35
    /// \e
athos@1261
    36
    int LpGlpk::_addRow() { 
athos@1261
    37
	int i=lpx_add_rows(lp, 1);
athos@1261
    38
	return i;
athos@1261
    39
    }
athos@1261
    40
athos@1261
    41
  
athos@1261
    42
    void LpGlpk::_setRowCoeffs(int i, 
athos@1261
    43
			       int length,
alpar@1263
    44
			       const int   * indices, 
alpar@1263
    45
			       const Value   * values )
athos@1261
    46
    {
alpar@1263
    47
      lpx_set_mat_row(lp, i, length,
alpar@1263
    48
		      const_cast<int * >(indices) ,
alpar@1263
    49
		      const_cast<Value * >(values));
athos@1261
    50
    }
athos@1261
    51
  
athos@1261
    52
    void LpGlpk::_setColCoeffs(int i, 
athos@1261
    53
			       int length,
alpar@1263
    54
			       const int   * indices, 
alpar@1263
    55
			       const Value   * values)
athos@1261
    56
    {
alpar@1263
    57
      lpx_set_mat_col(lp, i, length,
alpar@1263
    58
		      const_cast<int * >(indices),
alpar@1263
    59
		      const_cast<Value * >(values));
athos@1261
    60
    }
athos@1261
    61
  
athos@1261
    62
    void LpGlpk::_setColLowerBound(int i, Value lo)
athos@1261
    63
    {
athos@1261
    64
      if (lo==INF) {
athos@1261
    65
	//FIXME error
athos@1261
    66
      }
athos@1261
    67
      int b=lpx_get_col_type(lp, i);
athos@1261
    68
      double up=lpx_get_col_ub(lp, i);	
athos@1261
    69
      if (lo==-INF) {
athos@1261
    70
	switch (b) {
athos@1261
    71
	case LPX_FR:
athos@1261
    72
	case LPX_LO:
athos@1261
    73
	  lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
athos@1261
    74
	  break;
athos@1261
    75
	case LPX_UP:
athos@1261
    76
	  break;
athos@1261
    77
	case LPX_DB:
athos@1261
    78
	case LPX_FX:
athos@1261
    79
	  lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
athos@1261
    80
	  break;
athos@1261
    81
	default: ;
athos@1261
    82
	  //FIXME error
athos@1261
    83
	}
athos@1261
    84
      } else {
athos@1261
    85
	switch (b) {
athos@1261
    86
	case LPX_FR:
athos@1261
    87
	case LPX_LO:
athos@1261
    88
	  lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
athos@1261
    89
	  break;
athos@1261
    90
	case LPX_UP:	  
athos@1261
    91
	case LPX_DB:
athos@1261
    92
	case LPX_FX:
athos@1261
    93
	  if (lo==up) 
athos@1261
    94
	    lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
athos@1261
    95
	  else 
athos@1261
    96
	    lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
athos@1261
    97
	  break;
athos@1261
    98
	default: ;
athos@1261
    99
	  //FIXME error
athos@1261
   100
	}
athos@1261
   101
      }
athos@1261
   102
athos@1261
   103
    }
athos@1261
   104
  
athos@1261
   105
    void LpGlpk::_setColUpperBound(int i, Value up)
athos@1261
   106
    {
athos@1261
   107
      if (up==-INF) {
athos@1261
   108
	//FIXME error
athos@1261
   109
      }
athos@1261
   110
      int b=lpx_get_col_type(lp, i);
athos@1261
   111
      double lo=lpx_get_col_lb(lp, i);
athos@1261
   112
      if (up==INF) {
athos@1261
   113
	switch (b) {
athos@1261
   114
	case LPX_FR:
athos@1261
   115
	case LPX_LO:
athos@1261
   116
	  break;
athos@1261
   117
	case LPX_UP:
athos@1261
   118
	  lpx_set_col_bnds(lp, i, LPX_FR, lo, up);
athos@1261
   119
	  break;
athos@1261
   120
	case LPX_DB:
athos@1261
   121
	case LPX_FX:
athos@1261
   122
	  lpx_set_col_bnds(lp, i, LPX_LO, lo, up);
athos@1261
   123
	  break;
athos@1261
   124
	default: ;
athos@1261
   125
	  //FIXME error
athos@1261
   126
	}
athos@1261
   127
      } else {
athos@1261
   128
	switch (b) {
athos@1261
   129
	case LPX_FR:
athos@1261
   130
	  lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
athos@1261
   131
	  break;
athos@1261
   132
	case LPX_UP:
athos@1261
   133
	  lpx_set_col_bnds(lp, i, LPX_UP, lo, up);
athos@1261
   134
	  break;
athos@1261
   135
	case LPX_LO:
athos@1261
   136
	case LPX_DB:
athos@1261
   137
	case LPX_FX:
athos@1261
   138
	  if (lo==up) 
athos@1261
   139
	    lpx_set_col_bnds(lp, i, LPX_FX, lo, up);
athos@1261
   140
	  else 
athos@1261
   141
	    lpx_set_col_bnds(lp, i, LPX_DB, lo, up);
athos@1261
   142
	  break;
athos@1261
   143
	default: ;
athos@1261
   144
	  //FIXME error
athos@1261
   145
	}
athos@1261
   146
      }
athos@1261
   147
    }
athos@1261
   148
  
athos@1261
   149
    void LpGlpk::_setRowLowerBound(int i, Value lo)
athos@1261
   150
    {
athos@1261
   151
      if (lo==INF) {
athos@1261
   152
	//FIXME error
athos@1261
   153
      }
athos@1261
   154
      int b=lpx_get_row_type(lp, i);
athos@1261
   155
      double up=lpx_get_row_ub(lp, i);	
athos@1261
   156
      if (lo==-INF) {
athos@1261
   157
	switch (b) {
athos@1261
   158
	case LPX_FR:
athos@1261
   159
	case LPX_LO:
athos@1261
   160
	  lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
athos@1261
   161
	  break;
athos@1261
   162
	case LPX_UP:
athos@1261
   163
	  break;
athos@1261
   164
	case LPX_DB:
athos@1261
   165
	case LPX_FX:
athos@1261
   166
	  lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
athos@1261
   167
	  break;
athos@1261
   168
	default: ;
athos@1261
   169
	  //FIXME error
athos@1261
   170
	}
athos@1261
   171
      } else {
athos@1261
   172
	switch (b) {
athos@1261
   173
	case LPX_FR:
athos@1261
   174
	case LPX_LO:
athos@1261
   175
	  lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
athos@1261
   176
	  break;
athos@1261
   177
	case LPX_UP:	  
athos@1261
   178
	case LPX_DB:
athos@1261
   179
	case LPX_FX:
athos@1261
   180
	  if (lo==up) 
athos@1261
   181
	    lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
athos@1261
   182
	  else 
athos@1261
   183
	    lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
athos@1261
   184
	  break;
athos@1261
   185
	default: ;
athos@1261
   186
	  //FIXME error
athos@1261
   187
	}
athos@1261
   188
      }
athos@1261
   189
    }
athos@1261
   190
  
athos@1298
   191
  void LpGlpk::_setRowUpperBound(int i, Value up)
athos@1298
   192
  {
athos@1298
   193
    if (up==-INF) {
athos@1298
   194
      //FIXME error
athos@1298
   195
    }
athos@1298
   196
    int b=lpx_get_row_type(lp, i);
athos@1298
   197
    double lo=lpx_get_row_lb(lp, i);
athos@1298
   198
    if (up==INF) {
athos@1298
   199
      switch (b) {
athos@1298
   200
      case LPX_FR:
athos@1298
   201
      case LPX_LO:
athos@1298
   202
	break;
athos@1298
   203
      case LPX_UP:
athos@1298
   204
	lpx_set_row_bnds(lp, i, LPX_FR, lo, up);
athos@1298
   205
	break;
athos@1298
   206
      case LPX_DB:
athos@1298
   207
      case LPX_FX:
athos@1298
   208
	lpx_set_row_bnds(lp, i, LPX_LO, lo, up);
athos@1298
   209
	break;
athos@1298
   210
      default: ;
athos@1261
   211
	//FIXME error
athos@1261
   212
      }
athos@1298
   213
    } else {
athos@1298
   214
      switch (b) {
athos@1298
   215
      case LPX_FR:
athos@1298
   216
	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
athos@1298
   217
	break;
athos@1298
   218
      case LPX_UP:
athos@1298
   219
	lpx_set_row_bnds(lp, i, LPX_UP, lo, up);
athos@1298
   220
	break;
athos@1298
   221
      case LPX_LO:
athos@1298
   222
      case LPX_DB:
athos@1298
   223
      case LPX_FX:
athos@1298
   224
	if (lo==up) 
athos@1298
   225
	  lpx_set_row_bnds(lp, i, LPX_FX, lo, up);
athos@1298
   226
	else 
athos@1298
   227
	  lpx_set_row_bnds(lp, i, LPX_DB, lo, up);
athos@1298
   228
	break;
athos@1298
   229
      default: ;
athos@1298
   230
	//FIXME error
athos@1261
   231
      }
athos@1261
   232
    }
athos@1298
   233
  }
athos@1261
   234
  
athos@1298
   235
  void LpGlpk::_setObjCoeff(int i, Value obj_coef)
athos@1298
   236
  {
athos@1298
   237
    lpx_set_obj_coef(lp, i, obj_coef);
athos@1298
   238
  }
athos@1261
   239
alpar@1263
   240
alpar@1303
   241
  LpGlpk::SolveExitStatus LpGlpk::_solve()
alpar@1263
   242
  {
athos@1298
   243
    int i=  lpx_simplex(lp);
athos@1298
   244
    switch (i) {
athos@1298
   245
    case LPX_E_OK: 
athos@1298
   246
      return SOLVED;
athos@1298
   247
      break;
athos@1298
   248
    default:
athos@1298
   249
      return UNSOLVED;
athos@1298
   250
    }
alpar@1263
   251
  }
alpar@1263
   252
alpar@1293
   253
  LpGlpk::Value LpGlpk::_getPrimal(int i)
alpar@1263
   254
  {
athos@1298
   255
    return lpx_get_col_prim(lp,i);
alpar@1263
   256
  }
alpar@1263
   257
  
athos@1298
   258
 
alpar@1303
   259
  LpGlpk::SolutionStatus LpGlpk::_getPrimalType()
alpar@1294
   260
  {
athos@1298
   261
    int stat=  lpx_get_status(lp);
athos@1298
   262
    switch (stat) {
athos@1298
   263
    case LPX_UNDEF://Undefined (no solve has been run yet)
athos@1298
   264
      return UNDEFINED;
athos@1298
   265
      break;
athos@1298
   266
    case LPX_NOFEAS://There is no feasible solution (primal, I guess)
athos@1298
   267
    case LPX_INFEAS://Infeasible 
athos@1298
   268
      return INFEASIBLE;
athos@1298
   269
      break;
athos@1298
   270
    case LPX_UNBND://Unbounded
athos@1298
   271
      return INFINITE;
athos@1298
   272
      break;
athos@1298
   273
    case LPX_FEAS://Feasible
alpar@1300
   274
      return FEASIBLE;
athos@1298
   275
      break;
athos@1298
   276
    case LPX_OPT://Feasible
athos@1298
   277
      return OPTIMAL;
athos@1298
   278
      break;
alpar@1300
   279
    default:
alpar@1300
   280
      return UNDEFINED; //to avoid gcc warning
athos@1298
   281
      //FIXME error
athos@1298
   282
    }
alpar@1294
   283
  }
alpar@1263
   284
alpar@1263
   285
athos@1261
   286
} //END OF NAMESPACE LEMON
athos@1261
   287
athos@1261
   288
#endif //LEMON_LP_GLPK_CC