49 constexpr double kInfinity = std::numeric_limits<double>::infinity();
51 absl::flat_hash_map<std::string, glop::ColIndex> var_index_by_name;
53 var_index_by_name[
model.GetVariableName(
col)] =
col;
56 glop::ColIndex num_variables =
model.num_variables();
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 =
86 if (var_value == kInfinity) {
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;
105 const MPModelProto&
model) {
106 constexpr double kInfinity = std::numeric_limits<double>::infinity();
108 absl::flat_hash_map<std::string, int> var_index_by_name;
111 return absl::InvalidArgumentError(
"Found variable without name.");
116 MPSolutionResponse response;
117 std::vector<double> var_values(
model.variable_size(), 0);
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 =
146 if (var_value == kInfinity) {
147 return absl::InvalidArgumentError(
148 absl::StrCat(
"Couldn't parse value on line '",
line,
"'."));
151 if (var_name ==
"=obj=") {
152 response.set_objective_value(var_value);
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) {
165 response.add_variable_value(
value);