lemon/lp_glpk.cc
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 12 Jan 2009 12:25:55 +0000
changeset 460 76ec7bd57026
parent 458 7afc121e0689
permissions -rw-r--r--
Bypass warnings with gcc 4.3
deba@458
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@458
     2
 *
deba@458
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@458
     4
 *
deba@458
     5
 * Copyright (C) 2003-2008
deba@458
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@458
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@458
     8
 *
deba@458
     9
 * Permission to use, modify and distribute this software is granted
deba@458
    10
 * provided that this copyright notice appears in all copies. For
deba@458
    11
 * precise terms see the accompanying LICENSE file.
deba@458
    12
 *
deba@458
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@458
    14
 * express or implied, and with no claim as to its suitability for any
deba@458
    15
 * purpose.
deba@458
    16
 *
deba@458
    17
 */
deba@458
    18
deba@458
    19
///\file
deba@459
    20
///\brief Implementation of the LEMON GLPK LP and MIP solver interface.
deba@458
    21
deba@458
    22
#include <lemon/lp_glpk.h>
deba@459
    23
#include <glpk.h>
deba@458
    24
deba@459
    25
#include <lemon/assert.h>
deba@458
    26
deba@458
    27
namespace lemon {
deba@458
    28
deba@459
    29
  // GlpkBase members
deba@459
    30
deba@459
    31
  GlpkBase::GlpkBase() : LpBase() {
deba@459
    32
    lp = glp_create_prob();
deba@459
    33
    glp_create_index(lp);
deba@458
    34
  }
deba@458
    35
deba@459
    36
  GlpkBase::GlpkBase(const GlpkBase &other) : LpBase() {
deba@459
    37
    lp = glp_create_prob();
deba@459
    38
    glp_copy_prob(lp, other.lp, GLP_ON);
deba@459
    39
    glp_create_index(lp);
deba@459
    40
    rows = other.rows;
deba@459
    41
    cols = other.cols;
deba@458
    42
  }
deba@458
    43
deba@459
    44
  GlpkBase::~GlpkBase() {
deba@459
    45
    glp_delete_prob(lp);
deba@458
    46
  }
deba@458
    47
deba@459
    48
  int GlpkBase::_addCol() {
deba@459
    49
    int i = glp_add_cols(lp, 1);
deba@459
    50
    glp_set_col_bnds(lp, i, GLP_FR, 0.0, 0.0);
deba@458
    51
    return i;
deba@458
    52
  }
deba@458
    53
deba@459
    54
  int GlpkBase::_addRow() {
deba@459
    55
    int i = glp_add_rows(lp, 1);
deba@459
    56
    glp_set_row_bnds(lp, i, GLP_FR, 0.0, 0.0);
deba@458
    57
    return i;
deba@458
    58
  }
deba@458
    59
deba@459
    60
  void GlpkBase::_eraseCol(int i) {
deba@458
    61
    int ca[2];
deba@459
    62
    ca[1] = i;
deba@459
    63
    glp_del_cols(lp, 1, ca);
deba@458
    64
  }
deba@458
    65
deba@459
    66
  void GlpkBase::_eraseRow(int i) {
deba@458
    67
    int ra[2];
deba@459
    68
    ra[1] = i;
deba@459
    69
    glp_del_rows(lp, 1, ra);
deba@458
    70
  }
deba@458
    71
deba@459
    72
  void GlpkBase::_eraseColId(int i) {
deba@459
    73
    cols.eraseIndex(i);
deba@459
    74
    cols.shiftIndices(i);
deba@458
    75
  }
deba@458
    76
deba@459
    77
  void GlpkBase::_eraseRowId(int i) {
deba@459
    78
    rows.eraseIndex(i);
deba@459
    79
    rows.shiftIndices(i);
deba@459
    80
  }
deba@458
    81
deba@459
    82
  void GlpkBase::_getColName(int c, std::string& name) const {
deba@459
    83
    const char *str = glp_get_col_name(lp, c);
deba@459
    84
    if (str) name = str;
deba@459
    85
    else name.clear();
deba@459
    86
  }
deba@459
    87
deba@459
    88
  void GlpkBase::_setColName(int c, const std::string & name) {
deba@459
    89
    glp_set_col_name(lp, c, const_cast<char*>(name.c_str()));
deba@458
    90
deba@458
    91
  }
deba@458
    92
deba@459
    93
  int GlpkBase::_colByName(const std::string& name) const {
deba@459
    94
    int k = glp_find_col(lp, const_cast<char*>(name.c_str()));
deba@458
    95
    return k > 0 ? k : -1;
deba@458
    96
  }
deba@458
    97
deba@459
    98
  void GlpkBase::_getRowName(int r, std::string& name) const {
deba@459
    99
    const char *str = glp_get_row_name(lp, r);
deba@459
   100
    if (str) name = str;
deba@459
   101
    else name.clear();
deba@459
   102
  }
deba@458
   103
deba@459
   104
  void GlpkBase::_setRowName(int r, const std::string & name) {
deba@459
   105
    glp_set_row_name(lp, r, const_cast<char*>(name.c_str()));
deba@459
   106
deba@459
   107
  }
deba@459
   108
deba@459
   109
  int GlpkBase::_rowByName(const std::string& name) const {
deba@459
   110
    int k = glp_find_row(lp, const_cast<char*>(name.c_str()));
deba@459
   111
    return k > 0 ? k : -1;
deba@459
   112
  }
deba@459
   113
deba@459
   114
  void GlpkBase::_setRowCoeffs(int i, ExprIterator b, ExprIterator e) {
deba@459
   115
    std::vector<int> indexes;
deba@458
   116
    std::vector<Value> values;
deba@458
   117
deba@459
   118
    indexes.push_back(0);
deba@458
   119
    values.push_back(0);
deba@458
   120
deba@459
   121
    for(ExprIterator it = b; it != e; ++it) {
deba@459
   122
      indexes.push_back(it->first);
deba@458
   123
      values.push_back(it->second);
deba@458
   124
    }
deba@458
   125
deba@459
   126
    glp_set_mat_row(lp, i, values.size() - 1,
deba@459
   127
                    &indexes.front(), &values.front());
deba@458
   128
  }
deba@458
   129
deba@459
   130
  void GlpkBase::_getRowCoeffs(int ix, InsertIterator b) const {
deba@459
   131
    int length = glp_get_mat_row(lp, ix, 0, 0);
deba@458
   132
deba@459
   133
    std::vector<int> indexes(length + 1);
deba@458
   134
    std::vector<Value> values(length + 1);
deba@458
   135
deba@459
   136
    glp_get_mat_row(lp, ix, &indexes.front(), &values.front());
deba@458
   137
deba@458
   138
    for (int i = 1; i <= length; ++i) {
deba@459
   139
      *b = std::make_pair(indexes[i], values[i]);
deba@458
   140
      ++b;
deba@458
   141
    }
deba@458
   142
  }
deba@458
   143
deba@459
   144
  void GlpkBase::_setColCoeffs(int ix, ExprIterator b,
deba@459
   145
                                     ExprIterator e) {
deba@458
   146
deba@459
   147
    std::vector<int> indexes;
deba@458
   148
    std::vector<Value> values;
deba@458
   149
deba@459
   150
    indexes.push_back(0);
deba@458
   151
    values.push_back(0);
deba@458
   152
deba@459
   153
    for(ExprIterator it = b; it != e; ++it) {
deba@459
   154
      indexes.push_back(it->first);
deba@458
   155
      values.push_back(it->second);
deba@458
   156
    }
deba@458
   157
deba@459
   158
    glp_set_mat_col(lp, ix, values.size() - 1,
deba@459
   159
                    &indexes.front(), &values.front());
deba@458
   160
  }
deba@458
   161
deba@459
   162
  void GlpkBase::_getColCoeffs(int ix, InsertIterator b) const {
deba@459
   163
    int length = glp_get_mat_col(lp, ix, 0, 0);
deba@458
   164
deba@459
   165
    std::vector<int> indexes(length + 1);
deba@458
   166
    std::vector<Value> values(length + 1);
deba@458
   167
deba@459
   168
    glp_get_mat_col(lp, ix, &indexes.front(), &values.front());
deba@458
   169
deba@459
   170
    for (int i = 1; i  <= length; ++i) {
deba@459
   171
      *b = std::make_pair(indexes[i], values[i]);
deba@458
   172
      ++b;
deba@458
   173
    }
deba@458
   174
  }
deba@458
   175
deba@459
   176
  void GlpkBase::_setCoeff(int ix, int jx, Value value) {
deba@458
   177
deba@459
   178
    if (glp_get_num_cols(lp) < glp_get_num_rows(lp)) {
deba@458
   179
deba@459
   180
      int length = glp_get_mat_row(lp, ix, 0, 0);
deba@458
   181
deba@459
   182
      std::vector<int> indexes(length + 2);
deba@458
   183
      std::vector<Value> values(length + 2);
deba@458
   184
deba@459
   185
      glp_get_mat_row(lp, ix, &indexes.front(), &values.front());
deba@458
   186
deba@458
   187
      //The following code does not suppose that the elements of the
deba@459
   188
      //array indexes are sorted
deba@459
   189
      bool found = false;
deba@459
   190
      for (int i = 1; i  <= length; ++i) {
deba@459
   191
        if (indexes[i] == jx) {
deba@459
   192
          found = true;
deba@459
   193
          values[i] = value;
deba@458
   194
          break;
deba@458
   195
        }
deba@458
   196
      }
deba@459
   197
      if (!found) {
deba@458
   198
        ++length;
deba@459
   199
        indexes[length] = jx;
deba@459
   200
        values[length] = value;
deba@458
   201
      }
deba@458
   202
deba@459
   203
      glp_set_mat_row(lp, ix, length, &indexes.front(), &values.front());
deba@458
   204
deba@458
   205
    } else {
deba@458
   206
deba@459
   207
      int length = glp_get_mat_col(lp, jx, 0, 0);
deba@458
   208
deba@459
   209
      std::vector<int> indexes(length + 2);
deba@458
   210
      std::vector<Value> values(length + 2);
deba@458
   211
deba@459
   212
      glp_get_mat_col(lp, jx, &indexes.front(), &values.front());
deba@458
   213
deba@458
   214
      //The following code does not suppose that the elements of the
deba@459
   215
      //array indexes are sorted
deba@459
   216
      bool found = false;
deba@458
   217
      for (int i = 1; i <= length; ++i) {
deba@459
   218
        if (indexes[i] == ix) {
deba@459
   219
          found = true;
deba@459
   220
          values[i] = value;
deba@458
   221
          break;
deba@458
   222
        }
deba@458
   223
      }
deba@459
   224
      if (!found) {
deba@458
   225
        ++length;
deba@459
   226
        indexes[length] = ix;
deba@459
   227
        values[length] = value;
deba@458
   228
      }
deba@458
   229
deba@459
   230
      glp_set_mat_col(lp, jx, length, &indexes.front(), &values.front());
deba@458
   231
    }
deba@458
   232
deba@458
   233
  }
deba@458
   234
deba@459
   235
  GlpkBase::Value GlpkBase::_getCoeff(int ix, int jx) const {
deba@458
   236
deba@459
   237
    int length = glp_get_mat_row(lp, ix, 0, 0);
deba@458
   238
deba@459
   239
    std::vector<int> indexes(length + 1);
deba@458
   240
    std::vector<Value> values(length + 1);
deba@458
   241
deba@459
   242
    glp_get_mat_row(lp, ix, &indexes.front(), &values.front());
deba@458
   243
deba@459
   244
    for (int i = 1; i  <= length; ++i) {
deba@459
   245
      if (indexes[i] == jx) {
deba@458
   246
        return values[i];
deba@458
   247
      }
deba@458
   248
    }
deba@459
   249
deba@458
   250
    return 0;
deba@459
   251
  }
deba@459
   252
deba@459
   253
  void GlpkBase::_setColLowerBound(int i, Value lo) {
deba@459
   254
    LEMON_ASSERT(lo != INF, "Invalid bound");
deba@459
   255
deba@459
   256
    int b = glp_get_col_type(lp, i);
deba@459
   257
    double up = glp_get_col_ub(lp, i);
deba@459
   258
    if (lo == -INF) {
deba@459
   259
      switch (b) {
deba@459
   260
      case GLP_FR:
deba@459
   261
      case GLP_LO:
deba@459
   262
        glp_set_col_bnds(lp, i, GLP_FR, lo, up);
deba@459
   263
        break;
deba@459
   264
      case GLP_UP:
deba@459
   265
        break;
deba@459
   266
      case GLP_DB:
deba@459
   267
      case GLP_FX:
deba@459
   268
        glp_set_col_bnds(lp, i, GLP_UP, lo, up);
deba@459
   269
        break;
deba@459
   270
      default:
deba@459
   271
        break;
deba@459
   272
      }
deba@459
   273
    } else {
deba@459
   274
      switch (b) {
deba@459
   275
      case GLP_FR:
deba@459
   276
      case GLP_LO:
deba@459
   277
        glp_set_col_bnds(lp, i, GLP_LO, lo, up);
deba@459
   278
        break;
deba@459
   279
      case GLP_UP:
deba@459
   280
      case GLP_DB:
deba@459
   281
      case GLP_FX:
deba@459
   282
        if (lo == up)
deba@459
   283
          glp_set_col_bnds(lp, i, GLP_FX, lo, up);
deba@459
   284
        else
deba@459
   285
          glp_set_col_bnds(lp, i, GLP_DB, lo, up);
deba@459
   286
        break;
deba@459
   287
      default:
deba@459
   288
        break;
deba@459
   289
      }
deba@459
   290
    }
deba@459
   291
  }
deba@459
   292
deba@459
   293
  GlpkBase::Value GlpkBase::_getColLowerBound(int i) const {
deba@459
   294
    int b = glp_get_col_type(lp, i);
deba@459
   295
    switch (b) {
deba@459
   296
    case GLP_LO:
deba@459
   297
    case GLP_DB:
deba@459
   298
    case GLP_FX:
deba@459
   299
      return glp_get_col_lb(lp, i);
deba@459
   300
    default:
deba@459
   301
      return -INF;
deba@459
   302
    }
deba@459
   303
  }
deba@459
   304
deba@459
   305
  void GlpkBase::_setColUpperBound(int i, Value up) {
deba@459
   306
    LEMON_ASSERT(up != -INF, "Invalid bound");
deba@459
   307
deba@459
   308
    int b = glp_get_col_type(lp, i);
deba@459
   309
    double lo = glp_get_col_lb(lp, i);
deba@459
   310
    if (up == INF) {
deba@459
   311
      switch (b) {
deba@459
   312
      case GLP_FR:
deba@459
   313
      case GLP_LO:
deba@459
   314
        break;
deba@459
   315
      case GLP_UP:
deba@459
   316
        glp_set_col_bnds(lp, i, GLP_FR, lo, up);
deba@459
   317
        break;
deba@459
   318
      case GLP_DB:
deba@459
   319
      case GLP_FX:
deba@459
   320
        glp_set_col_bnds(lp, i, GLP_LO, lo, up);
deba@459
   321
        break;
deba@459
   322
      default:
deba@459
   323
        break;
deba@459
   324
      }
deba@459
   325
    } else {
deba@459
   326
      switch (b) {
deba@459
   327
      case GLP_FR:
deba@459
   328
        glp_set_col_bnds(lp, i, GLP_UP, lo, up);
deba@459
   329
        break;
deba@459
   330
      case GLP_UP:
deba@459
   331
        glp_set_col_bnds(lp, i, GLP_UP, lo, up);
deba@459
   332
        break;
deba@459
   333
      case GLP_LO:
deba@459
   334
      case GLP_DB:
deba@459
   335
      case GLP_FX:
deba@459
   336
        if (lo == up)
deba@459
   337
          glp_set_col_bnds(lp, i, GLP_FX, lo, up);
deba@459
   338
        else
deba@459
   339
          glp_set_col_bnds(lp, i, GLP_DB, lo, up);
deba@459
   340
        break;
deba@459
   341
      default:
deba@459
   342
        break;
deba@459
   343
      }
deba@459
   344
    }
deba@458
   345
deba@458
   346
  }
deba@458
   347
deba@459
   348
  GlpkBase::Value GlpkBase::_getColUpperBound(int i) const {
deba@459
   349
    int b = glp_get_col_type(lp, i);
deba@458
   350
      switch (b) {
deba@459
   351
      case GLP_UP:
deba@459
   352
      case GLP_DB:
deba@459
   353
      case GLP_FX:
deba@459
   354
        return glp_get_col_ub(lp, i);
deba@459
   355
      default:
deba@458
   356
        return INF;
deba@458
   357
      }
deba@458
   358
  }
deba@458
   359
deba@459
   360
  void GlpkBase::_setRowLowerBound(int i, Value lo) {
deba@459
   361
    LEMON_ASSERT(lo != INF, "Invalid bound");
deba@458
   362
deba@459
   363
    int b = glp_get_row_type(lp, i);
deba@459
   364
    double up = glp_get_row_ub(lp, i);
deba@459
   365
    if (lo == -INF) {
deba@459
   366
      switch (b) {
deba@459
   367
      case GLP_FR:
deba@459
   368
      case GLP_LO:
deba@459
   369
        glp_set_row_bnds(lp, i, GLP_FR, lo, up);
deba@459
   370
        break;
deba@459
   371
      case GLP_UP:
deba@459
   372
        break;
deba@459
   373
      case GLP_DB:
deba@459
   374
      case GLP_FX:
deba@459
   375
        glp_set_row_bnds(lp, i, GLP_UP, lo, up);
deba@459
   376
        break;
deba@459
   377
      default:
deba@459
   378
        break;
deba@458
   379
      }
deba@459
   380
    } else {
deba@459
   381
      switch (b) {
deba@459
   382
      case GLP_FR:
deba@459
   383
      case GLP_LO:
deba@459
   384
        glp_set_row_bnds(lp, i, GLP_LO, lo, up);
deba@459
   385
        break;
deba@459
   386
      case GLP_UP:
deba@459
   387
      case GLP_DB:
deba@459
   388
      case GLP_FX:
deba@459
   389
        if (lo == up)
deba@459
   390
          glp_set_row_bnds(lp, i, GLP_FX, lo, up);
deba@459
   391
        else
deba@459
   392
          glp_set_row_bnds(lp, i, GLP_DB, lo, up);
deba@459
   393
        break;
deba@459
   394
      default:
deba@459
   395
        break;
deba@458
   396
      }
deba@458
   397
    }
deba@458
   398
deba@458
   399
  }
deba@458
   400
deba@459
   401
  GlpkBase::Value GlpkBase::_getRowLowerBound(int i) const {
deba@459
   402
    int b = glp_get_row_type(lp, i);
deba@459
   403
    switch (b) {
deba@459
   404
    case GLP_LO:
deba@459
   405
    case GLP_DB:
deba@459
   406
    case GLP_FX:
deba@459
   407
      return glp_get_row_lb(lp, i);
deba@458
   408
    default:
deba@459
   409
      return -INF;
deba@458
   410
    }
deba@458
   411
  }
deba@458
   412
deba@459
   413
  void GlpkBase::_setRowUpperBound(int i, Value up) {
deba@459
   414
    LEMON_ASSERT(up != -INF, "Invalid bound");
deba@458
   415
deba@459
   416
    int b = glp_get_row_type(lp, i);
deba@459
   417
    double lo = glp_get_row_lb(lp, i);
deba@459
   418
    if (up == INF) {
deba@459
   419
      switch (b) {
deba@459
   420
      case GLP_FR:
deba@459
   421
      case GLP_LO:
deba@459
   422
        break;
deba@459
   423
      case GLP_UP:
deba@459
   424
        glp_set_row_bnds(lp, i, GLP_FR, lo, up);
deba@459
   425
        break;
deba@459
   426
      case GLP_DB:
deba@459
   427
      case GLP_FX:
deba@459
   428
        glp_set_row_bnds(lp, i, GLP_LO, lo, up);
deba@459
   429
        break;
deba@459
   430
      default:
deba@459
   431
        break;
deba@459
   432
      }
deba@459
   433
    } else {
deba@459
   434
      switch (b) {
deba@459
   435
      case GLP_FR:
deba@459
   436
        glp_set_row_bnds(lp, i, GLP_UP, lo, up);
deba@459
   437
        break;
deba@459
   438
      case GLP_UP:
deba@459
   439
        glp_set_row_bnds(lp, i, GLP_UP, lo, up);
deba@459
   440
        break;
deba@459
   441
      case GLP_LO:
deba@459
   442
      case GLP_DB:
deba@459
   443
      case GLP_FX:
deba@459
   444
        if (lo == up)
deba@459
   445
          glp_set_row_bnds(lp, i, GLP_FX, lo, up);
deba@459
   446
        else
deba@459
   447
          glp_set_row_bnds(lp, i, GLP_DB, lo, up);
deba@459
   448
        break;
deba@459
   449
      default:
deba@459
   450
        break;
deba@459
   451
      }
deba@458
   452
    }
deba@458
   453
  }
deba@458
   454
deba@459
   455
  GlpkBase::Value GlpkBase::_getRowUpperBound(int i) const {
deba@459
   456
    int b = glp_get_row_type(lp, i);
deba@459
   457
    switch (b) {
deba@459
   458
    case GLP_UP:
deba@459
   459
    case GLP_DB:
deba@459
   460
    case GLP_FX:
deba@459
   461
      return glp_get_row_ub(lp, i);
deba@458
   462
    default:
deba@459
   463
      return INF;
deba@458
   464
    }
deba@458
   465
  }
deba@458
   466
deba@459
   467
  void GlpkBase::_setObjCoeffs(ExprIterator b, ExprIterator e) {
deba@459
   468
    for (int i = 1; i <= glp_get_num_cols(lp); ++i) {
deba@459
   469
      glp_set_obj_coef(lp, i, 0.0);
deba@459
   470
    }
deba@459
   471
    for (ExprIterator it = b; it != e; ++it) {
deba@459
   472
      glp_set_obj_coef(lp, it->first, it->second);
deba@459
   473
    }
deba@458
   474
  }
deba@458
   475
deba@459
   476
  void GlpkBase::_getObjCoeffs(InsertIterator b) const {
deba@459
   477
    for (int i = 1; i <= glp_get_num_cols(lp); ++i) {
deba@459
   478
      Value val = glp_get_obj_coef(lp, i);
deba@459
   479
      if (val != 0.0) {
deba@459
   480
        *b = std::make_pair(i, val);
deba@459
   481
        ++b;
deba@459
   482
      }
deba@459
   483
    }
deba@458
   484
  }
deba@458
   485
deba@459
   486
  void GlpkBase::_setObjCoeff(int i, Value obj_coef) {
deba@459
   487
    //i = 0 means the constant term (shift)
deba@459
   488
    glp_set_obj_coef(lp, i, obj_coef);
deba@458
   489
  }
deba@458
   490
deba@459
   491
  GlpkBase::Value GlpkBase::_getObjCoeff(int i) const {
deba@459
   492
    //i = 0 means the constant term (shift)
deba@459
   493
    return glp_get_obj_coef(lp, i);
deba@458
   494
  }
deba@458
   495
deba@459
   496
  void GlpkBase::_setSense(GlpkBase::Sense sense) {
deba@459
   497
    switch (sense) {
deba@459
   498
    case MIN:
deba@459
   499
      glp_set_obj_dir(lp, GLP_MIN);
deba@459
   500
      break;
deba@459
   501
    case MAX:
deba@459
   502
      glp_set_obj_dir(lp, GLP_MAX);
deba@459
   503
      break;
deba@459
   504
    }
deba@458
   505
  }
deba@458
   506
deba@459
   507
  GlpkBase::Sense GlpkBase::_getSense() const {
deba@459
   508
    switch(glp_get_obj_dir(lp)) {
deba@459
   509
    case GLP_MIN:
deba@459
   510
      return MIN;
deba@459
   511
    case GLP_MAX:
deba@459
   512
      return MAX;
deba@459
   513
    default:
deba@459
   514
      LEMON_ASSERT(false, "Wrong sense");
deba@459
   515
      return GlpkBase::Sense();
deba@459
   516
    }
deba@458
   517
  }
deba@458
   518
deba@459
   519
  void GlpkBase::_clear() {
deba@459
   520
    glp_erase_prob(lp);
deba@459
   521
    rows.clear();
deba@459
   522
    cols.clear();
deba@459
   523
  }
deba@459
   524
deba@459
   525
  // LpGlpk members
deba@459
   526
deba@459
   527
  LpGlpk::LpGlpk()
deba@459
   528
    : LpBase(), GlpkBase(), LpSolver() {
deba@459
   529
    messageLevel(MESSAGE_NO_OUTPUT);
deba@459
   530
  }
deba@459
   531
deba@459
   532
  LpGlpk::LpGlpk(const LpGlpk& other)
deba@459
   533
    : LpBase(other), GlpkBase(other), LpSolver(other) {
deba@459
   534
    messageLevel(MESSAGE_NO_OUTPUT);
deba@459
   535
  }
deba@459
   536
deba@459
   537
  LpGlpk* LpGlpk::_newSolver() const { return new LpGlpk; }
deba@459
   538
  LpGlpk* LpGlpk::_cloneSolver() const { return new LpGlpk(*this); }
deba@459
   539
deba@459
   540
  const char* LpGlpk::_solverName() const { return "LpGlpk"; }
deba@459
   541
deba@459
   542
  void LpGlpk::_clear_temporals() {
deba@459
   543
    _primal_ray.clear();
deba@459
   544
    _dual_ray.clear();
deba@459
   545
  }
deba@459
   546
deba@459
   547
  LpGlpk::SolveExitStatus LpGlpk::_solve() {
deba@459
   548
    return solvePrimal();
deba@459
   549
  }
deba@459
   550
deba@459
   551
  LpGlpk::SolveExitStatus LpGlpk::solvePrimal() {
deba@459
   552
    _clear_temporals();
deba@459
   553
deba@459
   554
    glp_smcp smcp;
deba@459
   555
    glp_init_smcp(&smcp);
deba@459
   556
deba@459
   557
    switch (_message_level) {
deba@459
   558
    case MESSAGE_NO_OUTPUT:
deba@459
   559
      smcp.msg_lev = GLP_MSG_OFF;
deba@459
   560
      break;
deba@459
   561
    case MESSAGE_ERROR_MESSAGE:
deba@459
   562
      smcp.msg_lev = GLP_MSG_ERR;
deba@459
   563
      break;
deba@459
   564
    case MESSAGE_NORMAL_OUTPUT:
deba@459
   565
      smcp.msg_lev = GLP_MSG_ON;
deba@459
   566
      break;
deba@459
   567
    case MESSAGE_FULL_OUTPUT:
deba@459
   568
      smcp.msg_lev = GLP_MSG_ALL;
deba@459
   569
      break;
deba@459
   570
    }
deba@459
   571
deba@459
   572
    if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
deba@459
   573
    return SOLVED;
deba@459
   574
  }
deba@459
   575
deba@459
   576
  LpGlpk::SolveExitStatus LpGlpk::solveDual() {
deba@459
   577
    _clear_temporals();
deba@459
   578
deba@459
   579
    glp_smcp smcp;
deba@459
   580
    glp_init_smcp(&smcp);
deba@459
   581
deba@459
   582
    switch (_message_level) {
deba@459
   583
    case MESSAGE_NO_OUTPUT:
deba@459
   584
      smcp.msg_lev = GLP_MSG_OFF;
deba@459
   585
      break;
deba@459
   586
    case MESSAGE_ERROR_MESSAGE:
deba@459
   587
      smcp.msg_lev = GLP_MSG_ERR;
deba@459
   588
      break;
deba@459
   589
    case MESSAGE_NORMAL_OUTPUT:
deba@459
   590
      smcp.msg_lev = GLP_MSG_ON;
deba@459
   591
      break;
deba@459
   592
    case MESSAGE_FULL_OUTPUT:
deba@459
   593
      smcp.msg_lev = GLP_MSG_ALL;
deba@459
   594
      break;
deba@459
   595
    }
deba@459
   596
    smcp.meth = GLP_DUAL;
deba@459
   597
deba@459
   598
    if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
deba@459
   599
    return SOLVED;
deba@459
   600
  }
deba@459
   601
deba@459
   602
  LpGlpk::Value LpGlpk::_getPrimal(int i) const {
deba@459
   603
    return glp_get_col_prim(lp, i);
deba@459
   604
  }
deba@459
   605
deba@459
   606
  LpGlpk::Value LpGlpk::_getDual(int i) const {
deba@459
   607
    return glp_get_row_dual(lp, i);
deba@459
   608
  }
deba@459
   609
deba@459
   610
  LpGlpk::Value LpGlpk::_getPrimalValue() const {
deba@459
   611
    return glp_get_obj_val(lp);
deba@459
   612
  }
deba@459
   613
deba@459
   614
  LpGlpk::VarStatus LpGlpk::_getColStatus(int i) const {
deba@459
   615
    switch (glp_get_col_stat(lp, i)) {
deba@459
   616
    case GLP_BS:
deba@459
   617
      return BASIC;
deba@459
   618
    case GLP_UP:
deba@459
   619
      return UPPER;
deba@459
   620
    case GLP_LO:
deba@459
   621
      return LOWER;
deba@459
   622
    case GLP_NF:
deba@459
   623
      return FREE;
deba@459
   624
    case GLP_NS:
deba@459
   625
      return FIXED;
deba@459
   626
    default:
deba@459
   627
      LEMON_ASSERT(false, "Wrong column status");
deba@459
   628
      return LpGlpk::VarStatus();
deba@459
   629
    }
deba@459
   630
  }
deba@459
   631
deba@459
   632
  LpGlpk::VarStatus LpGlpk::_getRowStatus(int i) const {
deba@459
   633
    switch (glp_get_row_stat(lp, i)) {
deba@459
   634
    case GLP_BS:
deba@459
   635
      return BASIC;
deba@459
   636
    case GLP_UP:
deba@459
   637
      return UPPER;
deba@459
   638
    case GLP_LO:
deba@459
   639
      return LOWER;
deba@459
   640
    case GLP_NF:
deba@459
   641
      return FREE;
deba@459
   642
    case GLP_NS:
deba@459
   643
      return FIXED;
deba@459
   644
    default:
deba@459
   645
      LEMON_ASSERT(false, "Wrong row status");
deba@459
   646
      return LpGlpk::VarStatus();
deba@459
   647
    }
deba@459
   648
  }
deba@459
   649
deba@459
   650
  LpGlpk::Value LpGlpk::_getPrimalRay(int i) const {
deba@459
   651
    if (_primal_ray.empty()) {
deba@459
   652
      int row_num = glp_get_num_rows(lp);
deba@459
   653
      int col_num = glp_get_num_cols(lp);
deba@459
   654
deba@459
   655
      _primal_ray.resize(col_num + 1, 0.0);
deba@459
   656
deba@459
   657
      int index = glp_get_unbnd_ray(lp);
deba@459
   658
      if (index != 0) {
deba@459
   659
        // The primal ray is found in primal simplex second phase
deba@459
   660
        LEMON_ASSERT((index <= row_num ? glp_get_row_stat(lp, index) :
deba@459
   661
                      glp_get_col_stat(lp, index - row_num)) != GLP_BS,
deba@459
   662
                     "Wrong primal ray");
deba@459
   663
deba@459
   664
        bool negate = glp_get_obj_dir(lp) == GLP_MAX;
deba@459
   665
deba@459
   666
        if (index > row_num) {
deba@459
   667
          _primal_ray[index - row_num] = 1.0;
deba@459
   668
          if (glp_get_col_dual(lp, index - row_num) > 0) {
deba@459
   669
            negate = !negate;
deba@459
   670
          }
deba@459
   671
        } else {
deba@459
   672
          if (glp_get_row_dual(lp, index) > 0) {
deba@459
   673
            negate = !negate;
deba@459
   674
          }
deba@459
   675
        }
deba@459
   676
deba@459
   677
        std::vector<int> ray_indexes(row_num + 1);
deba@459
   678
        std::vector<Value> ray_values(row_num + 1);
deba@459
   679
        int ray_length = glp_eval_tab_col(lp, index, &ray_indexes.front(),
deba@459
   680
                                          &ray_values.front());
deba@459
   681
deba@459
   682
        for (int i = 1; i <= ray_length; ++i) {
deba@459
   683
          if (ray_indexes[i] > row_num) {
deba@459
   684
            _primal_ray[ray_indexes[i] - row_num] = ray_values[i];
deba@459
   685
          }
deba@459
   686
        }
deba@459
   687
deba@459
   688
        if (negate) {
deba@459
   689
          for (int i = 1; i <= col_num; ++i) {
deba@459
   690
            _primal_ray[i] = - _primal_ray[i];
deba@459
   691
          }
deba@459
   692
        }
deba@459
   693
      } else {
deba@459
   694
        for (int i = 1; i <= col_num; ++i) {
deba@459
   695
          _primal_ray[i] = glp_get_col_prim(lp, i);
deba@459
   696
        }
deba@459
   697
      }
deba@459
   698
    }
deba@459
   699
    return _primal_ray[i];
deba@459
   700
  }
deba@459
   701
deba@459
   702
  LpGlpk::Value LpGlpk::_getDualRay(int i) const {
deba@459
   703
    if (_dual_ray.empty()) {
deba@459
   704
      int row_num = glp_get_num_rows(lp);
deba@459
   705
deba@459
   706
      _dual_ray.resize(row_num + 1, 0.0);
deba@459
   707
deba@459
   708
      int index = glp_get_unbnd_ray(lp);
deba@459
   709
      if (index != 0) {
deba@459
   710
        // The dual ray is found in dual simplex second phase
deba@459
   711
        LEMON_ASSERT((index <= row_num ? glp_get_row_stat(lp, index) :
deba@459
   712
                      glp_get_col_stat(lp, index - row_num)) == GLP_BS,
deba@459
   713
deba@459
   714
                     "Wrong dual ray");
deba@459
   715
deba@459
   716
        int idx;
deba@459
   717
        bool negate = false;
deba@459
   718
deba@459
   719
        if (index > row_num) {
deba@459
   720
          idx = glp_get_col_bind(lp, index - row_num);
deba@459
   721
          if (glp_get_col_prim(lp, index - row_num) >
deba@459
   722
              glp_get_col_ub(lp, index - row_num)) {
deba@459
   723
            negate = true;
deba@459
   724
          }
deba@459
   725
        } else {
deba@459
   726
          idx = glp_get_row_bind(lp, index);
deba@459
   727
          if (glp_get_row_prim(lp, index) > glp_get_row_ub(lp, index)) {
deba@459
   728
            negate = true;
deba@459
   729
          }
deba@459
   730
        }
deba@459
   731
deba@459
   732
        _dual_ray[idx] = negate ?  - 1.0 : 1.0;
deba@459
   733
deba@459
   734
        glp_btran(lp, &_dual_ray.front());
deba@459
   735
      } else {
deba@459
   736
        double eps = 1e-7;
deba@459
   737
        // The dual ray is found in primal simplex first phase
deba@459
   738
        // We assume that the glpk minimizes the slack to get feasible solution
deba@459
   739
        for (int i = 1; i <= row_num; ++i) {
deba@459
   740
          int index = glp_get_bhead(lp, i);
deba@459
   741
          if (index <= row_num) {
deba@459
   742
            double res = glp_get_row_prim(lp, index);
deba@459
   743
            if (res > glp_get_row_ub(lp, index) + eps) {
deba@459
   744
              _dual_ray[i] = -1;
deba@459
   745
            } else if (res < glp_get_row_lb(lp, index) - eps) {
deba@459
   746
              _dual_ray[i] = 1;
deba@459
   747
            } else {
deba@459
   748
              _dual_ray[i] = 0;
deba@459
   749
            }
deba@459
   750
            _dual_ray[i] *= glp_get_rii(lp, index);
deba@459
   751
          } else {
deba@459
   752
            double res = glp_get_col_prim(lp, index - row_num);
deba@459
   753
            if (res > glp_get_col_ub(lp, index - row_num) + eps) {
deba@459
   754
              _dual_ray[i] = -1;
deba@459
   755
            } else if (res < glp_get_col_lb(lp, index - row_num) - eps) {
deba@459
   756
              _dual_ray[i] = 1;
deba@459
   757
            } else {
deba@459
   758
              _dual_ray[i] = 0;
deba@459
   759
            }
deba@459
   760
            _dual_ray[i] /= glp_get_sjj(lp, index - row_num);
deba@459
   761
          }
deba@459
   762
        }
deba@459
   763
deba@459
   764
        glp_btran(lp, &_dual_ray.front());
deba@459
   765
deba@459
   766
        for (int i = 1; i <= row_num; ++i) {
deba@459
   767
          _dual_ray[i] /= glp_get_rii(lp, i);
deba@459
   768
        }
deba@459
   769
      }
deba@459
   770
    }
deba@459
   771
    return _dual_ray[i];
deba@459
   772
  }
deba@459
   773
deba@459
   774
  LpGlpk::ProblemType LpGlpk::_getPrimalType() const {
deba@459
   775
    if (glp_get_status(lp) == GLP_OPT)
deba@459
   776
      return OPTIMAL;
deba@459
   777
    switch (glp_get_prim_stat(lp)) {
deba@459
   778
    case GLP_UNDEF:
deba@459
   779
      return UNDEFINED;
deba@459
   780
    case GLP_FEAS:
deba@459
   781
    case GLP_INFEAS:
deba@459
   782
      if (glp_get_dual_stat(lp) == GLP_NOFEAS) {
deba@459
   783
        return UNBOUNDED;
deba@459
   784
      } else {
deba@459
   785
        return UNDEFINED;
deba@459
   786
      }
deba@459
   787
    case GLP_NOFEAS:
deba@459
   788
      return INFEASIBLE;
deba@459
   789
    default:
deba@459
   790
      LEMON_ASSERT(false, "Wrong primal type");
deba@459
   791
      return  LpGlpk::ProblemType();
deba@459
   792
    }
deba@459
   793
  }
deba@459
   794
deba@459
   795
  LpGlpk::ProblemType LpGlpk::_getDualType() const {
deba@459
   796
    if (glp_get_status(lp) == GLP_OPT)
deba@459
   797
      return OPTIMAL;
deba@459
   798
    switch (glp_get_dual_stat(lp)) {
deba@459
   799
    case GLP_UNDEF:
deba@459
   800
      return UNDEFINED;
deba@459
   801
    case GLP_FEAS:
deba@459
   802
    case GLP_INFEAS:
deba@459
   803
      if (glp_get_prim_stat(lp) == GLP_NOFEAS) {
deba@459
   804
        return UNBOUNDED;
deba@459
   805
      } else {
deba@459
   806
        return UNDEFINED;
deba@459
   807
      }
deba@459
   808
    case GLP_NOFEAS:
deba@459
   809
      return INFEASIBLE;
deba@459
   810
    default:
deba@459
   811
      LEMON_ASSERT(false, "Wrong primal type");
deba@459
   812
      return  LpGlpk::ProblemType();
deba@459
   813
    }
deba@459
   814
  }
deba@459
   815
deba@459
   816
  void LpGlpk::presolver(bool b) {
deba@459
   817
    lpx_set_int_parm(lp, LPX_K_PRESOL, b ? 1 : 0);
deba@459
   818
  }
deba@459
   819
deba@459
   820
  void LpGlpk::messageLevel(MessageLevel m) {
deba@459
   821
    _message_level = m;
deba@459
   822
  }
deba@459
   823
deba@459
   824
  // MipGlpk members
deba@459
   825
deba@459
   826
  MipGlpk::MipGlpk()
deba@459
   827
    : LpBase(), GlpkBase(), MipSolver() {
deba@459
   828
    messageLevel(MESSAGE_NO_OUTPUT);
deba@459
   829
  }
deba@459
   830
deba@459
   831
  MipGlpk::MipGlpk(const MipGlpk& other)
deba@459
   832
    : LpBase(), GlpkBase(other), MipSolver() {
deba@459
   833
    messageLevel(MESSAGE_NO_OUTPUT);
deba@459
   834
  }
deba@459
   835
deba@459
   836
  void MipGlpk::_setColType(int i, MipGlpk::ColTypes col_type) {
deba@459
   837
    switch (col_type) {
deba@459
   838
    case INTEGER:
deba@459
   839
      glp_set_col_kind(lp, i, GLP_IV);
deba@459
   840
      break;
deba@459
   841
    case REAL:
deba@459
   842
      glp_set_col_kind(lp, i, GLP_CV);
deba@459
   843
      break;
deba@459
   844
    }
deba@459
   845
  }
deba@459
   846
deba@459
   847
  MipGlpk::ColTypes MipGlpk::_getColType(int i) const {
deba@459
   848
    switch (glp_get_col_kind(lp, i)) {
deba@459
   849
    case GLP_IV:
deba@459
   850
    case GLP_BV:
deba@459
   851
      return INTEGER;
deba@459
   852
    default:
deba@459
   853
      return REAL;
deba@459
   854
    }
deba@459
   855
deba@459
   856
  }
deba@459
   857
deba@459
   858
  MipGlpk::SolveExitStatus MipGlpk::_solve() {
deba@459
   859
    glp_smcp smcp;
deba@459
   860
    glp_init_smcp(&smcp);
deba@459
   861
deba@459
   862
    switch (_message_level) {
deba@459
   863
    case MESSAGE_NO_OUTPUT:
deba@459
   864
      smcp.msg_lev = GLP_MSG_OFF;
deba@459
   865
      break;
deba@459
   866
    case MESSAGE_ERROR_MESSAGE:
deba@459
   867
      smcp.msg_lev = GLP_MSG_ERR;
deba@459
   868
      break;
deba@459
   869
    case MESSAGE_NORMAL_OUTPUT:
deba@459
   870
      smcp.msg_lev = GLP_MSG_ON;
deba@459
   871
      break;
deba@459
   872
    case MESSAGE_FULL_OUTPUT:
deba@459
   873
      smcp.msg_lev = GLP_MSG_ALL;
deba@459
   874
      break;
deba@459
   875
    }
deba@459
   876
    smcp.meth = GLP_DUAL;
deba@459
   877
deba@459
   878
    if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
deba@459
   879
    if (glp_get_status(lp) != GLP_OPT) return SOLVED;
deba@459
   880
deba@459
   881
    glp_iocp iocp;
deba@459
   882
    glp_init_iocp(&iocp);
deba@459
   883
deba@459
   884
    switch (_message_level) {
deba@459
   885
    case MESSAGE_NO_OUTPUT:
deba@459
   886
      iocp.msg_lev = GLP_MSG_OFF;
deba@459
   887
      break;
deba@459
   888
    case MESSAGE_ERROR_MESSAGE:
deba@459
   889
      iocp.msg_lev = GLP_MSG_ERR;
deba@459
   890
      break;
deba@459
   891
    case MESSAGE_NORMAL_OUTPUT:
deba@459
   892
      iocp.msg_lev = GLP_MSG_ON;
deba@459
   893
      break;
deba@459
   894
    case MESSAGE_FULL_OUTPUT:
deba@459
   895
      iocp.msg_lev = GLP_MSG_ALL;
deba@459
   896
      break;
deba@459
   897
    }
deba@459
   898
deba@459
   899
    if (glp_intopt(lp, &iocp) != 0) return UNSOLVED;
deba@459
   900
    return SOLVED;
deba@459
   901
  }
deba@459
   902
deba@459
   903
deba@459
   904
  MipGlpk::ProblemType MipGlpk::_getType() const {
deba@459
   905
    switch (glp_get_status(lp)) {
deba@459
   906
    case GLP_OPT:
deba@459
   907
      switch (glp_mip_status(lp)) {
deba@459
   908
      case GLP_UNDEF:
deba@459
   909
        return UNDEFINED;
deba@459
   910
      case GLP_NOFEAS:
deba@459
   911
        return INFEASIBLE;
deba@459
   912
      case GLP_FEAS:
deba@459
   913
        return FEASIBLE;
deba@459
   914
      case GLP_OPT:
deba@459
   915
        return OPTIMAL;
deba@459
   916
      default:
deba@459
   917
        LEMON_ASSERT(false, "Wrong problem type.");
deba@459
   918
        return MipGlpk::ProblemType();
deba@459
   919
      }
deba@459
   920
    case GLP_NOFEAS:
deba@459
   921
      return INFEASIBLE;
deba@459
   922
    case GLP_INFEAS:
deba@459
   923
    case GLP_FEAS:
deba@459
   924
      if (glp_get_dual_stat(lp) == GLP_NOFEAS) {
deba@459
   925
        return UNBOUNDED;
deba@459
   926
      } else {
deba@459
   927
        return UNDEFINED;
deba@459
   928
      }
deba@459
   929
    default:
deba@459
   930
      LEMON_ASSERT(false, "Wrong problem type.");
deba@459
   931
      return MipGlpk::ProblemType();
deba@459
   932
    }
deba@459
   933
  }
deba@459
   934
deba@459
   935
  MipGlpk::Value MipGlpk::_getSol(int i) const {
deba@459
   936
    return glp_mip_col_val(lp, i);
deba@459
   937
  }
deba@459
   938
deba@459
   939
  MipGlpk::Value MipGlpk::_getSolValue() const {
deba@459
   940
    return glp_mip_obj_val(lp);
deba@459
   941
  }
deba@459
   942
deba@459
   943
  MipGlpk* MipGlpk::_newSolver() const { return new MipGlpk; }
deba@459
   944
  MipGlpk* MipGlpk::_cloneSolver() const {return new MipGlpk(*this); }
deba@459
   945
deba@459
   946
  const char* MipGlpk::_solverName() const { return "MipGlpk"; }
deba@459
   947
deba@459
   948
  void MipGlpk::messageLevel(MessageLevel m) {
deba@459
   949
    _message_level = m;
deba@459
   950
  }
deba@458
   951
deba@458
   952
} //END OF NAMESPACE LEMON