Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
lp_parser.h
Go to the documentation of this file.
1// Copyright 2010-2024 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#if defined(USE_LP_PARSER)
78
79#include <string>
80#include <vector>
81
82#include "absl/base/attributes.h"
83#include "absl/base/port.h"
84#include "absl/status/statusor.h"
85#include "absl/strings/string_view.h"
86#include "ortools/linear_solver/linear_solver.pb.h"
89
90namespace operations_research {
91
92// This calls ParseLp() under the hood. See below.
93absl::StatusOr<MPModelProto> ModelProtoFromLpFormat(absl::string_view model);
94
95namespace glop {
96
97// Like ModelProtoFromLpFormat(), but outputs a glop::LinearProgram.
98ABSL_MUST_USE_RESULT bool ParseLp(absl::string_view model, LinearProgram* lp);
99
100// Represents a constraint parsed from the LP file format (used by
101// LinearProgram::Dump()).
102struct ParsedConstraint {
103 // The name of the constraint. Empty if the constraint has no name.
104 std::string name;
105 // Contains the names of the variables used in the constraint, in the order in
106 // which they appear in the string representation.
107 std::vector<std::string> variable_names;
108 // Contains the coefficients of the variables used in the constraint. Note
109 // that variable_names and coefficients are parallel arrays, i.e.
110 // coefficients[i] is the coefficient for variable_names[i].
111 std::vector<Fractional> coefficients;
112 // The lower bound of the constraint. Set to -infinity when the constraint has
113 // no lower bound.
115 // The upper bound of the constraint. Set to +infinity when the constraint has
116 // no upper bound.
118};
119
120// Parses a constraint in the format used by LinearProgram::Dump(). Returns an
121// InvalidArgumentError with an appropriate error message when the parsing
122// fails.
123absl::StatusOr<ParsedConstraint> ParseConstraint(absl::string_view constraint);
124
125} // namespace glop
126} // namespace operations_research
127
128#endif // defined(USE_LP_PARSER)
129
130#endif // OR_TOOLS_LP_DATA_LP_PARSER_H_
const std::string name
A name for logging purposes.
double upper_bound
double lower_bound
absl::Span< const double > coefficients
GRBmodel * model
In SWIG mode, we don't want anything besides these top-level includes.