OR-Tools  9.2
lp_parser.h
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 
14 // A simple parser of a linear program from string.
15 //
16 // We accept a format produced by LinearProgram::Dump(), which is similar to
17 // LP file used by lp_solve (see http://lpsolve.sourceforge.net/5.1/index.htm).
18 // Example:
19 // 1: min: 1 + x1 + 2 * x2;
20 // 2: 0 <= x1 <= 1;
21 // 3: x2 >= 2;
22 // 4: r1: 1 <= x1 - x2 <= 2;
23 // 5: 0 <= x1 + x2 <= inf;
24 // 6: int x1, x3;
25 // 7: bin x2;
26 //
27 // Line 1 is the objective, line 2 and 3 define variable bounds, line 4 is a
28 // named constraint, line 5 is an unnamed constraint. Line 6 is the list of
29 // integer variables. Line 7 is the list of binary variables. The lines can be
30 // in any order, the line numbers do _not_ belong to the string being parsed.
31 //
32 // Caveats:
33 // 1. Plus sign and multiplication sign are optional. Thus, "min: 1 x1 x2" is
34 // the same as "min: 1*x1 + x2". All consecutive signs will be compacted into
35 // one sign using mathematical rules (i.e., the parity of minus sign).
36 // E.g., "min: ++---+ - +x1" is the same as "min: x1".
37 // 2. A constraint consists of two or three parts. A two part constraint has
38 // a bound on the left (resp. right) side and variables on the right
39 // (resp. left) side, with the two parts being separated by any of the
40 // relation signs "<", "<=", "=", ">=", ">".
41 // 3. A three part constraint has the variables in the middle part, and two
42 // bounds on the left and right side, with all three parts being separated by
43 // any of "<", "<=", ">=", ">".
44 // 4. "<" means "<=", and ">" means ">=".
45 // 5. An unnamed constraint involving exactly one variable with coefficient
46 // equal to 1, defines the variable bound(s). Otherwise, the constraint
47 // defines a new constraint.
48 // 6. If there is no bound defined for a variable, it will be assumed to be
49 // unbounded (i.e., from -inf to +inf).
50 // 7. A bound must be a number or "inf". A coefficient must be finite and
51 // cannot overflow. A number can be represented in a scientific notation,
52 // e.g., +1.2E-2. Consequently,
53 // "min: 1e2" means minimization of 100,
54 // "min: 1 e2" means minimization of 1*e2, where e2 is a variable,
55 // "min: 1 + e2" means minimization of 1 + e2, where e2 is a variable,
56 // "min: 1 1*e2" means minimization of 1 + e2, where e2 is a variable.
57 // "min: 1 1e2" is invalid as it would mean minimization of 1 + 100.
58 // 8. In a constraint, in the part with variables, all elements must be
59 // variables with optional coefficients and signs (i.e., no offset is
60 // allowed).
61 // 9. Variables in the objective, and in each of the constraint cannot repeat.
62 // E.g., this is invalid: "min: x + x".
63 // 10. The offset in the objective must be specified at the beginning, i.e.,
64 // after min: or max: and before any variables.
65 // 11. The parsing will fail if due to bounding of a variable the lower bound
66 // becomes strictly greater than the upper bound. E.g., these fail to
67 // parse: "min x; 1 <= x <= 0;", "min x; 0 <= x <= 1; 2 <= x <= 3". On the
68 // other hand the parser does _not_ attempt to "round" the bounds for integer
69 // variables. E.g., "min x; 0.5 <= x <= 0.8; int x" results in bounding the x
70 // variable between 0.5 and 0.8, despite there is no integer value it can
71 // take. Similarly, "min x; bin x; x <= 0.5" results in bounding the x
72 // variable between 0.0 and 0.5, despite the only value it can take is 0.
73 
74 #ifndef OR_TOOLS_LP_DATA_LP_PARSER_H_
75 #define OR_TOOLS_LP_DATA_LP_PARSER_H_
76 
77 #include <string>
78 #include <vector>
79 
80 #include "absl/base/port.h"
81 #include "absl/status/statusor.h"
82 #include "absl/strings/string_view.h"
85 
86 namespace operations_research {
87 
88 // This calls ParseLp() under the hood. See below.
89 absl::StatusOr<MPModelProto> ModelProtoFromLpFormat(absl::string_view model);
90 
91 namespace glop {
92 
93 // Like ModelProtoFromLpFormat(), but outputs a glop::LinearProgram.
94 ABSL_MUST_USE_RESULT bool ParseLp(absl::string_view model, LinearProgram* lp);
95 
96 // Represents a constraint parsed from the LP file format (used by
97 // LinearProgram::Dump()).
99  // The name of the constraint. Empty if the constraint has no name.
100  std::string name;
101  // Contains the names of the variables used in the constraint, in the order in
102  // which they appear in the string representation.
103  std::vector<std::string> variable_names;
104  // Contains the coefficients of the variables used in the constraint. Note
105  // that variable_names and coefficients are parallel arrays, i.e.
106  // coefficients[i] is the coefficient for variable_names[i].
107  std::vector<Fractional> coefficients;
108  // The lower bound of the constraint. Set to -infinity when the constraint has
109  // no lower bound.
111  // The upper bound of the constraint. Set to +infinity when the constraint has
112  // no upper bound.
114 };
115 
116 // Parses a constraint in the format used by LinearProgram::Dump(). Returns an
117 // InvalidArgumentError with an appropriate error message when the parsing
118 // fails.
119 absl::StatusOr<ParsedConstraint> ParseConstraint(absl::string_view constraint);
120 
121 } // namespace glop
122 } // namespace operations_research
123 
124 #endif // OR_TOOLS_LP_DATA_LP_PARSER_H_
bool ParseLp(absl::string_view model, LinearProgram *lp)
Definition: lp_parser.cc:443
GRBmodel * model
std::vector< Fractional > coefficients
Definition: lp_parser.h:107
std::vector< std::string > variable_names
Definition: lp_parser.h:103
absl::StatusOr< MPModelProto > ModelProtoFromLpFormat(absl::string_view model)
Definition: lp_parser.cc:450
Collection of objects used to extend the Constraint Solver library.
StatusOr< ParsedConstraint > ParseConstraint(absl::string_view constraint)
Definition: lp_parser.cc:343