OR-Tools  9.3
glpk_formatters.cc
Go to the documentation of this file.
1// Copyright 2010-2021 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 <string>
17#include <string_view>
18
19#include "absl/strings/ascii.h"
20#include "absl/strings/str_cat.h"
22
23extern "C" {
24#include <glpk.h>
25}
26
27namespace operations_research {
28
29std::string SolutionStatusString(const int status) {
30 switch (status) {
31 case GLP_UNDEF:
32 return "undefined (UNDEF)";
33 case GLP_FEAS:
34 return "feasible (FEAS)";
35 case GLP_INFEAS:
36 return "infeasible (INFEAS)";
37 case GLP_NOFEAS:
38 return "no feasible solution (NOFEAS)";
39 case GLP_OPT:
40 return "optimal (OPT)";
41 case GLP_UNBND:
42 return "unbounded (UNBND)";
43 default:
44 return absl::StrCat("? (", status, ")");
45 }
46}
47
48std::string BasisStatusString(const int stat) {
49 switch (stat) {
50 case GLP_BS:
51 return "basic (BS)";
52 case GLP_NL:
53 return "lower bound (NL)";
54 case GLP_NU:
55 return "upper bound (NU)";
56 case GLP_NF:
57 return "unbounded (NF)";
58 case GLP_NS:
59 return "fixed (NS)";
60 default:
61 return absl::StrCat("? (", stat, ")");
62 }
63}
64
65std::string ReturnCodeString(const int rc) {
66 switch (rc) {
67 case GLP_EBADB:
68 return "[GLP_EBADB] invalid basis";
69 case GLP_ESING:
70 return "[GLP_ESING] singular matrix";
71 case GLP_ECOND:
72 return "[GLP_ECOND] ill-conditioned matrix";
73 case GLP_EBOUND:
74 return "[GLP_EBOUND] invalid bounds";
75 case GLP_EFAIL:
76 return "[GLP_EFAIL] solver failed";
77 case GLP_EOBJLL:
78 return "[GLP_EOBJLL] objective lower limit reached";
79 case GLP_EOBJUL:
80 return "[GLP_EOBJUL] objective upper limit reached";
81 case GLP_EITLIM:
82 return "[GLP_EITLIM] iteration limit exceeded";
83 case GLP_ETMLIM:
84 return "[GLP_ETMLIM] time limit exceeded";
85 case GLP_ENOPFS:
86 return "[GLP_ENOPFS] no primal feasible solution";
87 case GLP_ENODFS:
88 return "[GLP_ENODFS] no dual feasible solution";
89 case GLP_EROOT:
90 return "[GLP_EROOT] root LP optimum not provided";
91 case GLP_ESTOP:
92 return "[GLP_ESTOP] search terminated by application";
93 case GLP_EMIPGAP:
94 return "[GLP_EMIPGAP] relative mip gap tolerance reached";
95 case GLP_ENOFEAS:
96 return "[GLP_ENOFEAS] no primal/dual feasible solution";
97 case GLP_ENOCVG:
98 return "[GLP_ENOCVG] no convergence";
99 case GLP_EINSTAB:
100 return "[GLP_EINSTAB] numerical instability";
101 case GLP_EDATA:
102 return "[GLP_EDATA] invalid data";
103 case GLP_ERANGE:
104 return "[GLP_ERANGE] result out of range";
105 default:
106 return absl::StrCat("[?] unknown return code ", rc);
107 }
108}
109
110std::string TruncateAndQuoteGLPKName(const std::string_view original_name) {
111 std::ostringstream oss;
112 std::size_t current_size = 0;
113 for (const char c : original_name) {
114 // We use \ for escape sequences; thus we must escape it too.
115 if (c == '\\') {
116 if (current_size + 2 > kMaxGLPKNameLen) {
117 break;
118 }
119 oss << "\\\\";
120 current_size += 2;
121 continue;
122 }
123
124 // Simply insert non-control characters (that are not the escape character
125 // above).
126 if (!absl::ascii_iscntrl(c)) {
127 if (current_size + 1 > kMaxGLPKNameLen) {
128 break;
129 }
130 oss << c;
131 ++current_size;
132 continue;
133 }
134
135 // Escape control characters.
136 const std::string escaped_c =
137 absl::StrCat("\\x", absl::Hex(c, absl::kZeroPad2));
138 if (current_size + escaped_c.size() > kMaxGLPKNameLen) {
139 break;
140 }
141 oss << escaped_c;
142 current_size += escaped_c.size();
143 }
144
145 const std::string ret = oss.str();
146 DCHECK_EQ(ret.size(), current_size);
147
148 return ret;
149}
150
151} // namespace operations_research
#define DCHECK_EQ(val1, val2)
Definition: base/logging.h:891
absl::Status status
Definition: g_gurobi.cc:35
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)