File: GLPK_mixed_integer_program_traits.h

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (274 lines) | stat: -rw-r--r-- 9,816 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright (c) 2018  Liangliang Nan. All rights reserved.
//
// This file is part of CGAL (www.cgal.org)
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1/Solver_interface/include/CGAL/GLPK_mixed_integer_program_traits.h $
// $Id: include/CGAL/GLPK_mixed_integer_program_traits.h b26b07a1242 $
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s) : Liangliang Nan

#ifndef CGAL_GLPK_MIXED_INTEGER_PROGRAM_TRAITS_H
#define CGAL_GLPK_MIXED_INTEGER_PROGRAM_TRAITS_H

#include <CGAL/Mixed_integer_program_traits.h>

#if defined(CGAL_USE_GLPK) || defined(DOXYGEN_RUNNING)

#include <glpk.h>

#include <iostream>
#include <vector>

namespace CGAL {
namespace internal {

// Infers "bound type" (required by GLPK) from the bounds values
template <typename FT>
int bound_type(FT lb, FT ub)
{
  typedef CGAL::Variable<FT> Variable;

  if (lb <= -Variable::infinity() && ub >= Variable::infinity()) {
    return GLP_FR; // free (unbounded) variable
  } else if (lb > -Variable::infinity() && ub >= Variable::infinity()) {
    return GLP_LO; // variable with lower bound
  } else if (lb <= -Variable::infinity() && ub < Variable::infinity()) {
    return GLP_UP; // variable with upper bound
  } else {
    // lb > -Variable::infinity() && ub < Variable::infinity()
    if (lb == ub)
      return GLP_FX; // fixed variable
    else
      return GLP_DB; // double-bounded variable
  }
}

} // namespace internal

/// \ingroup PkgSolverInterfaceMIP
///
/// This class provides an interface for formulating and solving
/// constrained or unconstrained mixed integer programs using
/// \ref thirdpartyGLPK, which must be available on the system.
///
/// \note For better performance, please consider using
///      `CGAL::SCIP_mixed_integer_program_traits`, or derive a new
///      model from `CGAL::Mixed_integer_program_traits`.
///
/// \cgalModels{MixedIntegerProgramTraits}
///
/// \sa `SCIP_mixed_integer_program_traits`
template <typename FT>
class GLPK_mixed_integer_program_traits
  : public Mixed_integer_program_traits<FT>
{
  /// \cond SKIP_IN_MANUAL
public:
  typedef CGAL::Mixed_integer_program_traits<FT>          Base_class;
  typedef typename Base_class::Variable                   Variable;
  typedef typename Base_class::Linear_constraint          Linear_constraint;
  typedef typename Base_class::Linear_objective           Linear_objective;
  typedef typename Linear_objective::Sense                Sense;
  typedef typename Variable::Variable_type                Variable_type;

public:
  /// Solves the program. Returns `false` if fails.
  virtual bool solve()
  {
    Base_class::error_message_.clear();

    glp_prob* lp = glp_create_prob();
    if (!lp) {
      Base_class::error_message_ = "failed creating GLPK program";
      return false;
    }

    std::size_t num_integer_variables = 0;

    // Creates variables

    // This "static_cast<>" suppresses many annoying warnings: "conversion from 'size_t' to 'int', possible loss of data"
    int num_variables = static_cast<int>(Base_class::variables_.size());

    glp_add_cols(lp, num_variables);
    for (int i = 0; i < num_variables; ++i) {
      const Variable* var = Base_class::variables_[i];
      glp_set_col_name(lp, i + 1, var->name().c_str());

      if (var->variable_type() == Variable::INTEGER) {
        glp_set_col_kind(lp, i + 1, GLP_IV);  // glpk uses 1-based arrays
        ++num_integer_variables;
      } else if (var->variable_type() == Variable::BINARY) {
        glp_set_col_kind(lp, i + 1, GLP_BV);  // glpk uses 1-based arrays
        ++num_integer_variables;
      } else {
        glp_set_col_kind(lp, i + 1, GLP_CV);  // Continuous variable
      }

      FT lb, ub;
      var->get_bounds(lb, ub);

      int type = internal::bound_type(lb, ub);
      glp_set_col_bnds(lp, i + 1, type, lb, ub);
    }

    // Adds constraints

    // This "static_cast<>" suppresses many annoying warnings: "conversion from 'size_t' to 'int', possible loss of data"
    int num_constraints = static_cast<int>(Base_class::constraints_.size());
    glp_add_rows(lp, num_constraints);

    for (int i = 0; i < num_constraints; ++i) {
      const Linear_constraint* c = Base_class::constraints_[i];
      const std::unordered_map<const Variable*, FT>& coeffs = c->coefficients();
      typename std::unordered_map<const Variable*, FT>::const_iterator cur = coeffs.begin();

      std::vector<int>  indices(coeffs.size() + 1, 0); // glpk uses 1-based arrays
      std::vector<FT> coefficients(coeffs.size() + 1, 0.0); // glpk uses 1-based arrays
      std::size_t idx = 1; // glpk uses 1-based arrays
      for (; cur != coeffs.end(); ++cur) {
        int var_idx = cur->first->index();
        FT coeff = cur->second;

        indices[idx] = var_idx + 1;   // glpk uses 1-based arrays
        coefficients[idx] = coeff;
        ++idx;
      }

      glp_set_mat_row(lp, i + 1, static_cast<int>(coeffs.size()), indices.data(), coefficients.data());

      FT lb, ub;
      c->get_bounds(lb, ub);

      int type = internal::bound_type(lb, ub);
      glp_set_row_bnds(lp, i + 1, type, lb, ub);

      glp_set_row_name(lp, i + 1, c->name().c_str());
    }

    // Sets objective

    // Determines the coefficient of each variable in the objective function
    const std::unordered_map<const Variable*, FT>& obj_coeffs = Base_class::objective_->coefficients();
    typename std::unordered_map<const Variable*, FT>::const_iterator cur = obj_coeffs.begin();
    for (; cur != obj_coeffs.end(); ++cur) {
      int var_idx = cur->first->index();
      FT coeff = cur->second;
      glp_set_obj_coef(lp, var_idx + 1, coeff); // glpk uses 1-based arrays
    }

    // Sets objective function sense
    bool minimize = (Base_class::objective_->sense() == Linear_objective::MINIMIZE);
    glp_set_obj_dir(lp, minimize ? GLP_MIN : GLP_MAX);
    int msg_level = GLP_MSG_ERR;
    int status = -1;
    if (num_integer_variables == 0) { // Continuous problem
      glp_smcp parm;
      glp_init_smcp(&parm);
      parm.msg_lev = msg_level;
      status = glp_simplex(lp, &parm);
    }
    else { // Solves as MIP problem
      glp_iocp parm;
      glp_init_iocp(&parm);
      parm.msg_lev = msg_level;
      parm.presolve = GLP_ON;
      // The routine glp_intopt is a driver to the MIP solver based on the branch-and-cut method,
      // which is a hybrid of branch-and-bound and cutting plane methods.
      status = glp_intopt(lp, &parm);
    }

    switch (status) {
      case 0: {
        if (num_integer_variables == 0) { // continuous problem
          Base_class::result_.resize(num_variables);
          for (int i = 0; i < num_variables; ++i) {
            FT x = glp_get_col_prim(lp, i + 1);  // glpk uses 1-based arrays
            Variable* v = Base_class::variables_[i];
            v->set_solution_value(x);
            Base_class::result_[i] = x;
          }
        }
        else { // MIP problem
          Base_class::result_.resize(num_variables);
          for (int i = 0; i < num_variables; ++i) {
            FT x = glp_mip_col_val(lp, i + 1); // glpk uses 1-based arrays

            Variable* v = Base_class::variables_[i];
            if (v->variable_type() != Variable::CONTINUOUS)
              x = static_cast<int>(std::round(x));

            v->set_solution_value(x);
            Base_class::result_[i] = x;
          }
        }
        break;
      }

      case GLP_EBOUND:
        Base_class::error_message_ =
            "Unable to start the search, because some FT-bounded variables have incorrect"
            "bounds or some integer variables have non - integer(fractional) bounds.";
        break;

      case GLP_EROOT:
        Base_class::error_message_ =
            "Unable to start the search, because optimal basis for initial LP relaxation is not"
            "provided. (This code may appear only if the presolver is disabled.)";
        break;

      case GLP_ENOPFS:
        Base_class::error_message_ =
            "Unable to start the search, because LP relaxation of the MIP problem instance has"
            "no primal feasible solution. (This code may appear only if the presolver is enabled.)";
        break;

      case GLP_ENODFS:
        Base_class::error_message_ =
            "Unable to start the search, because LP relaxation of the MIP problem instance has"
            "no dual feasible solution.In other word, this code means that if the LP relaxation"
            "has at least one primal feasible solution, its optimal solution is unbounded, so if the"
            "MIP problem has at least one integer feasible solution, its(integer) optimal solution"
            "is also unbounded. (This code may appear only if the presolver is enabled.)";
        break;

      case GLP_EFAIL:
        Base_class::error_message_ =
            "The search was prematurely terminated due to the solver failure.";
        break;

      case GLP_EMIPGAP:
        Base_class::error_message_ =
            "The search was prematurely terminated, because the relative mip gap tolerance has been reached.";
        break;

      case GLP_ETMLIM:
        Base_class::error_message_ =
            "The search was prematurely terminated, because the time limit has been exceeded.";
        break;

      case GLP_ESTOP:
        Base_class::error_message_ =
            "The search was prematurely terminated by application. (This code may appear only"
            "if the advanced solver interface is used.)";
        break;

      default:
        Base_class::error_message_ =
            "optimization was stopped with status code " + std::to_string(status);
        break;
    }

    glp_delete_prob(lp);

    return (status == 0);
  }
  /// \endcond
};

} // namespace CGAL

#endif // CGAL_USE_GLPK or DOXYGEN_RUNNING

#endif // CGAL_GLPK_MIXED_INTEGER_PROGRAM_TRAITS_H