OR-Tools  9.6
glpk_formatters.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
15 
16 #include <sstream>
17 #include <string>
18 #include <string_view>
19 
20 #include "absl/strings/ascii.h"
21 #include "absl/strings/str_cat.h"
22 #include "ortools/base/logging.h"
23 
24 extern "C" {
25 #include <glpk.h>
26 }
27 
28 namespace operations_research {
29 
30 std::string SolutionStatusString(const int status) {
31  switch (status) {
32  case GLP_UNDEF:
33  return "undefined (UNDEF)";
34  case GLP_FEAS:
35  return "feasible (FEAS)";
36  case GLP_INFEAS:
37  return "infeasible (INFEAS)";
38  case GLP_NOFEAS:
39  return "no feasible solution (NOFEAS)";
40  case GLP_OPT:
41  return "optimal (OPT)";
42  case GLP_UNBND:
43  return "unbounded (UNBND)";
44  default:
45  return absl::StrCat("? (", status, ")");
46  }
47 }
48 
49 std::string BasisStatusString(const int stat) {
50  switch (stat) {
51  case GLP_BS:
52  return "basic (BS)";
53  case GLP_NL:
54  return "lower bound (NL)";
55  case GLP_NU:
56  return "upper bound (NU)";
57  case GLP_NF:
58  return "unbounded (NF)";
59  case GLP_NS:
60  return "fixed (NS)";
61  default:
62  return absl::StrCat("? (", stat, ")");
63  }
64 }
65 
66 std::string ReturnCodeString(const int rc) {
67  switch (rc) {
68  case GLP_EBADB:
69  return "[GLP_EBADB] invalid basis";
70  case GLP_ESING:
71  return "[GLP_ESING] singular matrix";
72  case GLP_ECOND:
73  return "[GLP_ECOND] ill-conditioned matrix";
74  case GLP_EBOUND:
75  return "[GLP_EBOUND] invalid bounds";
76  case GLP_EFAIL:
77  return "[GLP_EFAIL] solver failed";
78  case GLP_EOBJLL:
79  return "[GLP_EOBJLL] objective lower limit reached";
80  case GLP_EOBJUL:
81  return "[GLP_EOBJUL] objective upper limit reached";
82  case GLP_EITLIM:
83  return "[GLP_EITLIM] iteration limit exceeded";
84  case GLP_ETMLIM:
85  return "[GLP_ETMLIM] time limit exceeded";
86  case GLP_ENOPFS:
87  return "[GLP_ENOPFS] no primal feasible solution";
88  case GLP_ENODFS:
89  return "[GLP_ENODFS] no dual feasible solution";
90  case GLP_EROOT:
91  return "[GLP_EROOT] root LP optimum not provided";
92  case GLP_ESTOP:
93  return "[GLP_ESTOP] search terminated by application";
94  case GLP_EMIPGAP:
95  return "[GLP_EMIPGAP] relative mip gap tolerance reached";
96  case GLP_ENOFEAS:
97  return "[GLP_ENOFEAS] no primal/dual feasible solution";
98  case GLP_ENOCVG:
99  return "[GLP_ENOCVG] no convergence";
100  case GLP_EINSTAB:
101  return "[GLP_EINSTAB] numerical instability";
102  case GLP_EDATA:
103  return "[GLP_EDATA] invalid data";
104  case GLP_ERANGE:
105  return "[GLP_ERANGE] result out of range";
106  default:
107  return absl::StrCat("[?] unknown return code ", rc);
108  }
109 }
110 
111 std::string TruncateAndQuoteGLPKName(const std::string_view original_name) {
112  std::ostringstream oss;
113  std::size_t current_size = 0;
114  for (const char c : original_name) {
115  // We use \ for escape sequences; thus we must escape it too.
116  if (c == '\\') {
117  if (current_size + 2 > kMaxGLPKNameLen) {
118  break;
119  }
120  oss << "\\\\";
121  current_size += 2;
122  continue;
123  }
124 
125  // Simply insert non-control characters (that are not the escape character
126  // above).
127  if (!absl::ascii_iscntrl(c)) {
128  if (current_size + 1 > kMaxGLPKNameLen) {
129  break;
130  }
131  oss << c;
132  ++current_size;
133  continue;
134  }
135 
136  // Escape control characters.
137  const std::string escaped_c =
138  absl::StrCat("\\x", absl::Hex(c, absl::kZeroPad2));
139  if (current_size + escaped_c.size() > kMaxGLPKNameLen) {
140  break;
141  }
142  oss << escaped_c;
143  current_size += escaped_c.size();
144  }
145 
146  const std::string ret = oss.str();
147  DCHECK_EQ(ret.size(), current_size);
148 
149  return ret;
150 }
151 
152 } // namespace operations_research
absl::Status status
Definition: g_gurobi.cc:41
Collection of objects used to extend the Constraint Solver library.
std::string BasisStatusString(const int stat)
constexpr std::size_t kMaxGLPKNameLen
std::string ReturnCodeString(const int rc)
std::string SolutionStatusString(const int status)
std::string TruncateAndQuoteGLPKName(const std::string_view original_name)