21#include "absl/base/attributes.h"
22#include "absl/container/flat_hash_set.h"
23#include "absl/log/check.h"
24#include "absl/status/status.h"
25#include "absl/status/statusor.h"
26#include "absl/strings/match.h"
27#include "absl/strings/numbers.h"
28#include "absl/strings/str_cat.h"
29#include "absl/strings/str_split.h"
30#include "absl/strings/string_view.h"
32#include "ortools/linear_solver/linear_solver.pb.h"
36#if defined(USE_LP_PARSER)
40#if defined(USE_LP_PARSER)
46using StringPiece = ::re2::StringPiece;
47using ::absl::StatusOr;
62bool TokenIsBound(TokenType token_type) {
63 if (token_type == TokenType::VALUE || token_type == TokenType::INF) {
75 ABSL_MUST_USE_RESULT
bool Parse(absl::string_view
model, LinearProgram* lp);
78 bool ParseEmptyLine(StringPiece
line);
79 bool ParseObjective(StringPiece objective);
80 bool ParseIntegerVariablesList(StringPiece
line);
81 bool ParseConstraint(StringPiece constraint);
82 TokenType ConsumeToken(StringPiece* sp);
83 bool SetVariableBounds(ColIndex
col, Fractional lb, Fractional ub);
91 std::string consumed_name_;
94 std::set<ColIndex> bounded_variables_;
97bool LPParser::Parse(absl::string_view
model, LinearProgram* lp) {
99 bounded_variables_.clear();
102 std::vector<StringPiece> lines =
103 absl::StrSplit(
model,
';', absl::SkipEmpty());
104 bool has_objective =
false;
106 for (StringPiece
line : lines) {
107 if (!has_objective && ParseObjective(
line)) {
108 has_objective =
true;
109 }
else if (!ParseConstraint(
line) && !ParseIntegerVariablesList(
line) &&
110 !ParseEmptyLine(
line)) {
111 LOG(INFO) <<
"Error in line: " <<
line;
118 for (ColIndex
col(0);
col < lp_->num_variables(); ++
col) {
119 if (bounded_variables_.find(
col) == bounded_variables_.end()) {
120 lp_->SetVariableBounds(
col, -kInfinity, +kInfinity);
128bool LPParser::ParseEmptyLine(StringPiece
line) {
129 if (ConsumeToken(&
line) == TokenType::END)
return true;
133bool LPParser::ParseObjective(StringPiece objective) {
135 if (ConsumeToken(&objective) != TokenType::NAME)
return false;
136 if (absl::EqualsIgnoreCase(consumed_name_,
"min")) {
137 lp_->SetMaximizationProblem(
false);
138 }
else if (absl::EqualsIgnoreCase(consumed_name_,
"max")) {
139 lp_->SetMaximizationProblem(
true);
145 TokenType token_type = ConsumeToken(&objective);
146 if (token_type == TokenType::VALUE) {
147 lp_->SetObjectiveOffset(consumed_coeff_);
148 token_type = ConsumeToken(&objective);
150 lp_->SetObjectiveOffset(0.0);
154 while (token_type == TokenType::ADDAND) {
155 const ColIndex
col = lp_->FindOrCreateVariable(consumed_name_);
156 if (lp_->objective_coefficients()[
col] != 0.0)
return false;
157 lp_->SetObjectiveCoefficient(
col, consumed_coeff_);
158 token_type = ConsumeToken(&objective);
160 return token_type == TokenType::END;
163bool LPParser::ParseIntegerVariablesList(StringPiece
line) {
165 bool binary_list =
false;
166 if (ConsumeToken(&
line) != TokenType::NAME)
return false;
167 if (absl::EqualsIgnoreCase(consumed_name_,
"bin")) {
169 }
else if (!absl::EqualsIgnoreCase(consumed_name_,
"int")) {
174 TokenType token_type = ConsumeToken(&
line);
175 while (token_type == TokenType::ADDAND) {
176 if (consumed_coeff_ != 1.0)
return false;
177 const ColIndex
col = lp_->FindOrCreateVariable(consumed_name_);
179 if (binary_list && !SetVariableBounds(
col, 0.0, 1.0))
return false;
180 token_type = ConsumeToken(&
line);
181 if (token_type == TokenType::COMA) {
182 token_type = ConsumeToken(&
line);
187 if (token_type != TokenType::END)
return false;
191bool LPParser::ParseConstraint(StringPiece constraint) {
192 const StatusOr<ParsedConstraint> parsed_constraint_or_status =
193 ::operations_research::glop::ParseConstraint(constraint);
194 if (!parsed_constraint_or_status.ok())
return false;
195 const ParsedConstraint& parsed_constraint =
196 parsed_constraint_or_status.value();
199 if (parsed_constraint.name.empty() &&
200 parsed_constraint.coefficients.size() == 1 &&
201 parsed_constraint.coefficients[0] == 1.0) {
203 lp_->FindOrCreateVariable(parsed_constraint.variable_names[0]);
204 if (!SetVariableBounds(
col, parsed_constraint.lower_bound,
205 parsed_constraint.upper_bound)) {
209 const RowIndex num_constraints_before_adding_variable =
210 lp_->num_constraints();
217 parsed_constraint.name.empty()
218 ? lp_->CreateNewConstraint()
219 : lp_->FindOrCreateConstraint(parsed_constraint.name);
220 if (lp_->num_constraints() == num_constraints_before_adding_variable) {
222 LOG(INFO) <<
"Two constraints with the same name: "
223 << parsed_constraint.name;
227 parsed_constraint.upper_bound)) {
230 lp_->SetConstraintBounds(
row, parsed_constraint.lower_bound,
231 parsed_constraint.upper_bound);
232 for (
int i = 0;
i < parsed_constraint.variable_names.size(); ++
i) {
233 const ColIndex variable =
234 lp_->FindOrCreateVariable(parsed_constraint.variable_names[i]);
235 lp_->SetCoefficient(
row, variable, parsed_constraint.coefficients[i]);
242 if (bounded_variables_.find(
col) == bounded_variables_.end()) {
244 bounded_variables_.insert(
col);
245 lp_->SetVariableBounds(
col, -kInfinity, kInfinity);
248 lb = std::max(lb, lp_->variable_lower_bounds()[
col]);
249 ub = std::min(ub, lp_->variable_upper_bounds()[
col]);
251 lp_->SetVariableBounds(
col, lb, ub);
255TokenType ConsumeToken(StringPiece* sp, std::string* consumed_name,
256 double* consumed_coeff) {
257 DCHECK(consumed_name !=
nullptr);
258 DCHECK(consumed_coeff !=
nullptr);
262 static const LazyRE2 kEndPattern = {R
"(\s*)"};
265 if (sp->empty() || RE2::FullMatch(*sp, *kEndPattern)) {
266 return TokenType::END;
271 static const LazyRE2 kNamePattern1 = {R
"(\s*(\w[\w[\]]*):)"};
272 static const LazyRE2 kNamePattern2 = {R
"((?i)\s*(int)\s*:?)"};
273 static const LazyRE2 kNamePattern3 = {R
"((?i)\s*(bin)\s*:?)"};
274 if (RE2::Consume(sp, *kNamePattern1, consumed_name))
return TokenType::NAME;
275 if (RE2::Consume(sp, *kNamePattern2, consumed_name))
return TokenType::NAME;
276 if (RE2::Consume(sp, *kNamePattern3, consumed_name))
return TokenType::NAME;
279 static const LazyRE2 kLePattern = {R
"(\s*<=?)"};
280 if (RE2::Consume(sp, *kLePattern))
return TokenType::SIGN_LE;
281 static const LazyRE2 kEqPattern = {R
"(\s*=)"};
282 if (RE2::Consume(sp, *kEqPattern))
return TokenType::SIGN_EQ;
283 static const LazyRE2 kGePattern = {R
"(\s*>=?)"};
284 if (RE2::Consume(sp, *kGePattern))
return TokenType::SIGN_GE;
287 static const LazyRE2 kComaPattern = {R
"(\s*\,)"};
288 if (RE2::Consume(sp, *kComaPattern))
return TokenType::COMA;
293 static const LazyRE2 kSignPattern = {R
"(\s*([-+]{1}))"};
294 while (RE2::Consume(sp, *kSignPattern, &sign)) {
295 if (sign ==
"-") minus_count++;
299 static const LazyRE2 kInfPattern = {R
"((?i)\s*inf)"};
300 if (RE2::Consume(sp, *kInfPattern)) {
302 return TokenType::INF;
307 bool has_value =
false;
308 static const LazyRE2 kValuePattern = {
309 R
"(\s*([0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?))"};
310 if (RE2::Consume(sp, *kValuePattern, &coeff)) {
311 if (!absl::SimpleAtod(coeff, consumed_coeff)) {
314 LOG(ERROR) <<
"Text: " << coeff <<
" was matched by RE2 to be "
315 <<
"a floating point number, but absl::SimpleAtod() failed.";
316 return TokenType::ERROR;
319 VLOG(1) <<
"Value " << coeff <<
" treated as infinite.";
320 return TokenType::INF;
324 *consumed_coeff = 1.0;
326 if (minus_count % 2 == 1) *consumed_coeff *= -1.0;
331 std::string multiplication;
332 static const LazyRE2 kAddandPattern = {R
"(\s*(\*?)\s*([a-zA-Z_)][\w[\])]*))"};
333 if (RE2::Consume(sp, *kAddandPattern, &multiplication, consumed_name)) {
334 if (!multiplication.empty() && !has_value)
return TokenType::ERROR;
335 return TokenType::ADDAND;
336 }
else if (has_value) {
337 return TokenType::VALUE;
340 return TokenType::ERROR;
343TokenType LPParser::ConsumeToken(StringPiece* sp) {
344 using ::operations_research::glop::ConsumeToken;
345 return ConsumeToken(sp, &consumed_name_, &consumed_coeff_);
350StatusOr<ParsedConstraint> ParseConstraint(absl::string_view constraint) {
351 ParsedConstraint parsed_constraint;
353 StringPiece constraint_copy{constraint};
354 std::string consumed_name;
356 if (ConsumeToken(&constraint_copy, &consumed_name, &consumed_coeff) ==
358 parsed_constraint.name = consumed_name;
359 constraint = constraint_copy;
364 TokenType left_sign(TokenType::END);
365 TokenType right_sign(TokenType::END);
366 absl::flat_hash_set<std::string> used_variables;
369 TokenType token_type =
370 ConsumeToken(&constraint, &consumed_name, &consumed_coeff);
371 if (TokenIsBound(token_type)) {
372 left_bound = consumed_coeff;
373 left_sign = ConsumeToken(&constraint, &consumed_name, &consumed_coeff);
374 if (left_sign != TokenType::SIGN_LE && left_sign != TokenType::SIGN_EQ &&
375 left_sign != TokenType::SIGN_GE) {
376 return absl::InvalidArgumentError(
377 "Expected an equality/inequality sign for the left bound.");
379 token_type = ConsumeToken(&constraint, &consumed_name, &consumed_coeff);
383 while (token_type == TokenType::ADDAND) {
384 if (used_variables.contains(consumed_name)) {
385 return absl::InvalidArgumentError(
386 absl::StrCat(
"Duplicate variable name: ", consumed_name));
388 used_variables.insert(consumed_name);
389 parsed_constraint.variable_names.push_back(consumed_name);
390 parsed_constraint.coefficients.push_back(consumed_coeff);
391 token_type = ConsumeToken(&constraint, &consumed_name, &consumed_coeff);
395 if (left_sign == TokenType::SIGN_EQ && token_type != TokenType::END) {
396 return absl::InvalidArgumentError(
397 "Equality constraints can have only one bound.");
401 if (token_type != TokenType::END) {
402 right_sign = token_type;
403 if (right_sign != TokenType::SIGN_LE && right_sign != TokenType::SIGN_EQ &&
404 right_sign != TokenType::SIGN_GE) {
405 return absl::InvalidArgumentError(
406 "Expected an equality/inequality sign for the right bound.");
409 if (left_sign != TokenType::END && right_sign == TokenType::SIGN_EQ) {
410 return absl::InvalidArgumentError(
411 "Equality constraints can have only one bound.");
414 ConsumeToken(&constraint, &consumed_name, &consumed_coeff))) {
415 return absl::InvalidArgumentError(
"Bound value was expected.");
417 right_bound = consumed_coeff;
418 if (ConsumeToken(&constraint, &consumed_name, &consumed_coeff) !=
420 return absl::InvalidArgumentError(
421 absl::StrCat(
"End of input was expected, found: ", constraint));
426 if (left_sign == TokenType::END && right_sign == TokenType::END) {
427 return absl::InvalidArgumentError(
"The input constraint was empty.");
431 parsed_constraint.lower_bound = -
kInfinity;
432 parsed_constraint.upper_bound =
kInfinity;
433 if (left_sign == TokenType::SIGN_LE || left_sign == TokenType::SIGN_EQ) {
434 parsed_constraint.lower_bound = left_bound;
436 if (left_sign == TokenType::SIGN_GE || left_sign == TokenType::SIGN_EQ) {
437 parsed_constraint.upper_bound = left_bound;
439 if (right_sign == TokenType::SIGN_LE || right_sign == TokenType::SIGN_EQ) {
440 parsed_constraint.upper_bound =
441 std::min(parsed_constraint.upper_bound, right_bound);
443 if (right_sign == TokenType::SIGN_GE || right_sign == TokenType::SIGN_EQ) {
444 parsed_constraint.lower_bound =
445 std::max(parsed_constraint.lower_bound, right_bound);
447 return parsed_constraint;
450bool ParseLp(absl::string_view
model, LinearProgram* lp) {
452 return parser.Parse(
model, lp);
457absl::StatusOr<MPModelProto> ModelProtoFromLpFormat(absl::string_view
model) {
458 glop::LinearProgram lp;
459 if (!ParseLp(
model, &lp)) {
460 return absl::InvalidArgumentError(
"Parsing error, see LOGs for details.");
462 MPModelProto model_proto;
@ INTEGER
The variable must only take integer values.
constexpr double kInfinity
Infinity for type Fractional.
bool AreBoundsValid(Fractional lower_bound, Fractional upper_bound)
void LinearProgramToMPModelProto(const LinearProgram &input, MPModelProto *output)
Converts a LinearProgram to a MPModelProto.
bool IsFinite(Fractional value)
In SWIG mode, we don't want anything besides these top-level includes.