20#include "absl/container/flat_hash_map.h"
21#include "absl/status/status.h"
22#include "absl/status/statusor.h"
23#include "absl/strings/str_cat.h"
24#include "absl/strings/str_split.h"
25#include "absl/strings/string_view.h"
35absl::StatusOr<glop::DenseRow>
ParseSolFile(absl::string_view file_name,
41absl::StatusOr<MPSolutionResponse>
ParseSolFile(absl::string_view file_name,
49 constexpr double kInfinity = std::numeric_limits<double>::infinity();
51 absl::flat_hash_map<std::string, glop::ColIndex> var_index_by_name;
52 for (glop::ColIndex col(0); col < model.
num_variables(); ++col) {
58 std::vector<std::string> lines =
59 absl::StrSplit(
solution,
'\n', absl::SkipEmpty());
60 for (
const std::string& line : lines) {
61 std::vector<std::string> fields =
62 absl::StrSplit(line, absl::ByAnyChar(
" \t"), absl::SkipEmpty());
65 for (
int i = 0; i < fields.size(); ++i) {
66 if (fields[i][0] ==
'#') {
72 if (fields.empty())
continue;
74 if (fields.size() == 1) {
75 return absl::InvalidArgumentError(
76 absl::StrCat(
"Found only one field on line '", line,
"'."));
78 if (fields.size() > 2) {
79 return absl::InvalidArgumentError(
80 absl::StrCat(
"Found more than two fields on line '", line,
"'."));
83 const std::string var_name = fields[0];
84 const double var_value =
87 return absl::InvalidArgumentError(
88 absl::StrCat(
"Couldn't parse value on line '", line,
"'."));
91 if (var_name ==
"=obj=")
continue;
93 auto iter = var_index_by_name.find(var_name);
94 if (iter == var_index_by_name.end()) {
95 return absl::InvalidArgumentError(absl::StrCat(
96 "Couldn't find variable named '", var_name,
"' in the model."));
98 dense_row[iter->second] = var_value;
106 constexpr double kInfinity = std::numeric_limits<double>::infinity();
108 absl::flat_hash_map<std::string, int> var_index_by_name;
109 for (
int var_index = 0; var_index < model.
variable_size(); ++var_index) {
111 return absl::InvalidArgumentError(
"Found variable without name.");
113 var_index_by_name[model.
variable(var_index).
name()] = var_index;
118 std::vector<std::string> lines =
119 absl::StrSplit(
solution,
'\n', absl::SkipEmpty());
120 for (
const std::string& line : lines) {
121 std::vector<std::string> fields =
122 absl::StrSplit(line, absl::ByAnyChar(
" \t"), absl::SkipEmpty());
125 for (
int i = 0; i < fields.size(); ++i) {
126 if (fields[i][0] ==
'#') {
132 if (fields.empty())
continue;
134 if (fields.size() == 1) {
135 return absl::InvalidArgumentError(
136 absl::StrCat(
"Found only one field on line '", line,
"'."));
138 if (fields.size() > 2) {
139 return absl::InvalidArgumentError(
140 absl::StrCat(
"Found more than two fields on line '", line,
"'."));
143 const std::string var_name = fields[0];
144 const double var_value =
147 return absl::InvalidArgumentError(
148 absl::StrCat(
"Couldn't parse value on line '", line,
"'."));
151 if (var_name ==
"=obj=") {
156 auto iter = var_index_by_name.find(var_name);
157 if (iter == var_index_by_name.end()) {
158 return absl::InvalidArgumentError(absl::StrCat(
159 "Couldn't find variable named '", var_name,
"' in the model."));
161 var_values[iter->second] = var_value;
164 for (
const double value : var_values) {
#define ASSIGN_OR_RETURN(lhs, rexpr)
const ::operations_research::MPVariableProto & variable(int index) const
int variable_size() const
repeated .operations_research.MPVariableProto variable = 3;
void add_variable_value(double value)
void set_objective_value(double value)
const ::std::string & name() const
std::string GetVariableName(ColIndex col) const
ColIndex num_variables() const
Returns the number of variables.
StrictITIVector< ColIndex, Fractional > DenseRow
Row-vector types. Row-vector types are indexed by a column index.
In SWIG mode, we don't want anything besides these top-level includes.
static constexpr double kInfinity
Select next search node to expand Select next item_i to add this new search node to the search Generate a new search node where item_i is not in the knapsack Check validity of this new partial solution(using propagators) - If valid
absl::StatusOr< glop::DenseRow > ParseSolFile(absl::string_view file_name, const glop::LinearProgram &model)
Parse a solution to model from a file.
absl::StatusOr< std::string > ReadFileToString(absl::string_view filename)
Reads a file, optionally gzipped, to a string.
absl::StatusOr< glop::DenseRow > ParseSolString(const std::string &solution, const glop::LinearProgram &model)
Parse a solution to model from a string.
double ParseLeadingDoubleValue(const char *str, double deflt)