Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
lp_model.h
Go to the documentation of this file.
1// Copyright 2010-2025 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#ifndef OR_TOOLS_MATH_OPT_IO_LP_LP_MODEL_H_
15#define OR_TOOLS_MATH_OPT_IO_LP_LP_MODEL_H_
16
17#include <ostream>
18#include <string>
19#include <utility>
20#include <vector>
21
22#include "absl/container/flat_hash_map.h"
25
27
28DEFINE_STRONG_INT_TYPE(VariableIndex, int64_t);
29DEFINE_STRONG_INT_TYPE(ConstraintIndex, int64_t);
30using Term = std::pair<double, VariableIndex>;
32
33std::ostream& operator<<(std::ostream& ostr, Relation relation);
34
35struct Constraint {
36 std::vector<Term> terms;
38 double rhs = 0.0;
39 std::string name;
40};
41
42// Note: this prints an exact representation of the data in Constraint, not
43// the string form of the constraint in LP format.
44std::ostream& operator<<(std::ostream& ostr, const Constraint& constraint);
45
46// The contents of an optimization model in LP file format.
47//
48// You can convert this to a string in the LP file format using <<, and read
49// from a string in the LP file format using ParseLp from parse_lp.h.
50class LpModel {
51 public:
52 LpModel() = default;
53
54 // Adds a new variable to the model and returns it. Errors if name:
55 // * is empty
56 // * is the same as any existing variable name
57 // * has invalid characters for the LP file format
58 //
59 // Variable names are case sensitive.
60 absl::StatusOr<VariableIndex> AddVariable(absl::string_view name);
61
62 // Adds a new constraint to the model and returns its index.
63 //
64 // Errors if:
65 // * a variable id from constraint.bounds is out of bounds
66 // * constraint.relation is an invalid enum
67 // * a coefficient on constraint.terms is Inf or NaN
68 // * the name has invalid characters
69 // * there are no terms in the constraint
70 //
71 // Constraint names can be repeated but this is not recommended.
72 absl::StatusOr<ConstraintIndex> AddConstraint(Constraint constraint);
73
74 const absl::flat_hash_map<std::string, VariableIndex>& variable_names()
75 const {
76 return variable_names_;
77 }
79 const {
80 return variables_;
81 }
83 const {
84 return constraints_;
85 }
86
87 private:
88 absl::flat_hash_map<std::string, VariableIndex> variable_names_;
91};
92
93// Prints the model in LP format to ostr.
94std::ostream& operator<<(std::ostream& ostr, const LpModel& model);
95
96} // namespace operations_research::lp_format
97
98#endif // OR_TOOLS_MATH_OPT_IO_LP_LP_MODEL_H_
absl::StatusOr< ConstraintIndex > AddConstraint(Constraint constraint)
Definition lp_model.cc:77
const util_intops::StrongVector< ConstraintIndex, Constraint > & constraints() const
Definition lp_model.h:82
const util_intops::StrongVector< VariableIndex, std::string > & variables() const
Definition lp_model.h:78
absl::StatusOr< VariableIndex > AddVariable(absl::string_view name)
Definition lp_model.cc:106
const absl::flat_hash_map< std::string, VariableIndex > & variable_names() const
Definition lp_model.h:74
std::pair< double, VariableIndex > Term
Definition lp_model.h:30
std::ostream & operator<<(std::ostream &ostr, const Relation relation)
Definition lp_model.cc:48
#define DEFINE_STRONG_INT_TYPE(type_name, value_type)