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