forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol_reader.cc
169 lines (142 loc) · 5.63 KB
/
sol_reader.cc
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
// Copyright 2010-2022 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/lp_data/sol_reader.h"
#include <limits>
#include <string>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h"
#include "ortools/base/numbers.h"
#include "ortools/base/status_macros.h"
#include "ortools/linear_solver/linear_solver.pb.h"
#include "ortools/lp_data/lp_types.h"
#include "ortools/lp_data/proto_utils.h"
#include "ortools/util/file_util.h"
namespace operations_research {
absl::StatusOr<glop::DenseRow> ParseSolFile(const std::string& file_name,
const glop::LinearProgram& model) {
ASSIGN_OR_RETURN(std::string sol_file, ReadFileToString(file_name));
return ParseSolString(sol_file, model);
}
absl::StatusOr<MPSolutionResponse> ParseSolFile(const std::string& file_name,
const MPModelProto& model) {
ASSIGN_OR_RETURN(std::string sol_file, ReadFileToString(file_name));
return ParseSolString(sol_file, model);
}
absl::StatusOr<glop::DenseRow> ParseSolString(
const std::string& solution, const glop::LinearProgram& model) {
constexpr double kInfinity = std::numeric_limits<double>::infinity();
absl::flat_hash_map<std::string, glop::ColIndex> var_index_by_name;
for (glop::ColIndex col(0); col < model.num_variables(); ++col) {
var_index_by_name[model.GetVariableName(col)] = col;
}
glop::ColIndex num_variables = model.num_variables();
glop::DenseRow dense_row(num_variables.value(), 0);
std::vector<std::string> lines =
absl::StrSplit(solution, '\n', absl::SkipEmpty());
for (const std::string& line : lines) {
std::vector<std::string> fields =
absl::StrSplit(line, absl::ByAnyChar(" \t"), absl::SkipEmpty());
// Remove the comments.
for (int i = 0; i < fields.size(); ++i) {
if (fields[i][0] == '#') {
fields.resize(i);
break;
}
}
if (fields.empty()) continue;
if (fields.size() == 1) {
return absl::InvalidArgumentError(
absl::StrCat("Found only one field on line '", line, "'."));
}
if (fields.size() > 2) {
return absl::InvalidArgumentError(
absl::StrCat("Found more than two fields on line '", line, "'."));
}
const std::string var_name = fields[0];
const double var_value =
strings::ParseLeadingDoubleValue(fields[1], kInfinity);
if (var_value == kInfinity) {
return absl::InvalidArgumentError(
absl::StrCat("Couldn't parse value on line '", line, "'."));
}
if (var_name == "=obj=") continue;
auto iter = var_index_by_name.find(var_name);
if (iter == var_index_by_name.end()) {
return absl::InvalidArgumentError(absl::StrCat(
"Couldn't find variable named '", var_name, "' in the model."));
}
dense_row[iter->second] = var_value;
}
return dense_row;
}
absl::StatusOr<MPSolutionResponse> ParseSolString(const std::string& solution,
const MPModelProto& model) {
constexpr double kInfinity = std::numeric_limits<double>::infinity();
absl::flat_hash_map<std::string, int> var_index_by_name;
for (int var_index = 0; var_index < model.variable_size(); ++var_index) {
if (model.variable(var_index).name().empty()) {
return absl::InvalidArgumentError("Found variable without name.");
}
var_index_by_name[model.variable(var_index).name()] = var_index;
}
MPSolutionResponse response;
std::vector<double> var_values(model.variable_size(), 0);
std::vector<std::string> lines =
absl::StrSplit(solution, '\n', absl::SkipEmpty());
for (const std::string& line : lines) {
std::vector<std::string> fields =
absl::StrSplit(line, absl::ByAnyChar(" \t"), absl::SkipEmpty());
// Remove the comments.
for (int i = 0; i < fields.size(); ++i) {
if (fields[i][0] == '#') {
fields.resize(i);
break;
}
}
if (fields.empty()) continue;
if (fields.size() == 1) {
return absl::InvalidArgumentError(
absl::StrCat("Found only one field on line '", line, "'."));
}
if (fields.size() > 2) {
return absl::InvalidArgumentError(
absl::StrCat("Found more than two fields on line '", line, "'."));
}
const std::string var_name = fields[0];
const double var_value =
strings::ParseLeadingDoubleValue(fields[1], kInfinity);
if (var_value == kInfinity) {
return absl::InvalidArgumentError(
absl::StrCat("Couldn't parse value on line '", line, "'."));
}
if (var_name == "=obj=") {
response.set_objective_value(var_value);
continue;
}
auto iter = var_index_by_name.find(var_name);
if (iter == var_index_by_name.end()) {
return absl::InvalidArgumentError(absl::StrCat(
"Couldn't find variable named '", var_name, "' in the model."));
}
var_values[iter->second] = var_value;
}
for (const double value : var_values) {
response.add_variable_value(value);
}
return response;
}
} // namespace operations_research