Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
linear_constraint.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// IWYU pragma: private, include "ortools/math_opt/cpp/math_opt.h"
15// IWYU pragma: friend "ortools/math_opt/cpp/.*"
16
17// An object oriented wrapper for linear constraints in ModelStorage.
18#ifndef OR_TOOLS_MATH_OPT_CPP_LINEAR_CONSTRAINT_H_
19#define OR_TOOLS_MATH_OPT_CPP_LINEAR_CONSTRAINT_H_
20
21#include <cstdint>
22#include <ostream>
23#include <sstream>
24#include <string>
25#include <utility>
26
27#include "absl/log/check.h"
28#include "absl/strings/string_view.h"
35
36namespace operations_research {
37namespace math_opt {
38
39// A value type that references a linear constraint from ModelStorage. Usually
40// this type is passed by copy.
41//
42// This type implements https://abseil.io/docs/cpp/guides/hash.
44 public:
45 // The typed integer used for ids.
46 using IdType = LinearConstraintId;
47
48 inline LinearConstraint(const ModelStorage* storage, LinearConstraintId id);
49
50 inline int64_t id() const;
51
52 inline LinearConstraintId typed_id() const;
53 inline const ModelStorage* storage() const;
54
55 inline double lower_bound() const;
56 inline double upper_bound() const;
57 inline absl::string_view name() const;
58
59 inline bool is_coefficient_nonzero(Variable variable) const;
60
61 // Returns 0.0 if the variable is not used in the constraint.
62 inline double coefficient(Variable variable) const;
63
64 // Returns the constraints as a bounded linear expression.
65 //
66 // The linear expression will have a zero offset, even if the constraint was
67 // created with a non-zero one. For example:
68 //
69 // const LinearConstraint c =
70 // model.AddLinearConstraint(3.2 <= x + 1.0 <= 4.2);
71 //
72 // // Here `e` will contain 3.2 - 1.0 <= x <= 4.2 - 1.0.
73 // const BoundedLinearExpression e = c.AsBoundedLinearExpression();
75
76 // Returns a detailed string description of the contents of the constraint
77 // (not its name, use `<<` for that instead).
78 inline std::string ToString() const;
79
80 friend inline bool operator==(const LinearConstraint& lhs,
81 const LinearConstraint& rhs);
82 friend inline bool operator!=(const LinearConstraint& lhs,
83 const LinearConstraint& rhs);
84 template <typename H>
85 friend H AbslHashValue(H h, const LinearConstraint& linear_constraint);
86 friend std::ostream& operator<<(std::ostream& ostr,
87 const LinearConstraint& linear_constraint);
88
89 private:
90 const ModelStorage* storage_;
91 LinearConstraintId id_;
92};
93
94template <typename V>
95using LinearConstraintMap = absl::flat_hash_map<LinearConstraint, V>;
96
97// Streams the name of the constraint, as registered upon constraint creation,
98// or a short default if none was provided.
99inline std::ostream& operator<<(std::ostream& ostr,
100 const LinearConstraint& linear_constraint);
101
103// Inline function implementations
105
106int64_t LinearConstraint::id() const { return id_.value(); }
107
108LinearConstraintId LinearConstraint::typed_id() const { return id_; }
109
110const ModelStorage* LinearConstraint::storage() const { return storage_; }
111
113 return storage_->linear_constraint_lower_bound(id_);
115
116double LinearConstraint::upper_bound() const {
117 return storage_->linear_constraint_upper_bound(id_);
119
120absl::string_view LinearConstraint::name() const {
121 if (storage()->has_linear_constraint(id_)) {
122 return storage_->linear_constraint_name(id_);
123 }
125}
126
127bool LinearConstraint::is_coefficient_nonzero(const Variable variable) const {
128 CHECK_EQ(variable.storage(), storage_)
130 return storage_->is_linear_constraint_coefficient_nonzero(
131 id_, variable.typed_id());
132}
133
134double LinearConstraint::coefficient(const Variable variable) const {
135 CHECK_EQ(variable.storage(), storage_)
137 return storage_->linear_constraint_coefficient(id_, variable.typed_id());
138}
139
141 LinearExpression terms;
142 for (const VariableId var :
143 storage()->variables_in_linear_constraint(typed_id())) {
144 terms += Variable(storage(), var) *
145 storage()->linear_constraint_coefficient(typed_id(), var);
146 }
147 return storage()->linear_constraint_lower_bound(typed_id()) <=
148 std::move(terms) <=
149 storage()->linear_constraint_upper_bound(typed_id());
150}
151
152std::string LinearConstraint::ToString() const {
153 if (!storage()->has_linear_constraint(id_)) {
155 }
156 std::stringstream str;
158 return str.str();
159}
160
161bool operator==(const LinearConstraint& lhs, const LinearConstraint& rhs) {
162 return lhs.id_ == rhs.id_ && lhs.storage_ == rhs.storage_;
164
165bool operator!=(const LinearConstraint& lhs, const LinearConstraint& rhs) {
166 return !(lhs == rhs);
168
169template <typename H>
170H AbslHashValue(H h, const LinearConstraint& linear_constraint) {
171 return H::combine(std::move(h), linear_constraint.id_.value(),
172 linear_constraint.storage_);
173}
174
175std::ostream& operator<<(std::ostream& ostr,
176 const LinearConstraint& linear_constraint) {
177 // TODO(b/170992529): handle quoting of invalid characters in the name.
178 const absl::string_view name = linear_constraint.name();
179 if (name.empty()) {
180 ostr << "__lin_con#" << linear_constraint.id() << "__";
181 } else {
182 ostr << name;
183 }
184 return ostr;
185}
186
187LinearConstraint::LinearConstraint(const ModelStorage* const storage,
188 const LinearConstraintId id)
189 : storage_(storage), id_(id) {}
190
191} // namespace math_opt
192} // namespace operations_research
193
194#endif // OR_TOOLS_MATH_OPT_CPP_LINEAR_CONSTRAINT_H_
LinearConstraintId IdType
The typed integer used for ids.
friend bool operator!=(const LinearConstraint &lhs, const LinearConstraint &rhs)
BoundedLinearExpression AsBoundedLinearExpression() const
LinearConstraint(const ModelStorage *storage, LinearConstraintId id)
bool is_coefficient_nonzero(Variable variable) const
friend bool operator==(const LinearConstraint &lhs, const LinearConstraint &rhs)
friend H AbslHashValue(H h, const LinearConstraint &linear_constraint)
double coefficient(Variable variable) const
Returns 0.0 if the variable is not used in the constraint.
friend std::ostream & operator<<(std::ostream &ostr, const LinearConstraint &linear_constraint)
const std::string name
A name for logging purposes.
IntVar * var
constexpr absl::string_view kObjectsFromOtherModelStorage
Definition key_types.h:156
absl::flat_hash_map< LinearConstraint, V > LinearConstraintMap
constexpr absl::string_view kDeletedConstraintDefaultDescription
Definition model_util.h:30
std::ostream & operator<<(std::ostream &ostr, const IndicatorConstraint &constraint)
bool operator==(const IndicatorConstraint &lhs, const IndicatorConstraint &rhs)
bool operator!=(const IndicatorConstraint &lhs, const IndicatorConstraint &rhs)
H AbslHashValue(H h, const IndicatorConstraint &constraint)
In SWIG mode, we don't want anything besides these top-level includes.
int64_t coefficient
A LinearExpression with upper and lower bounds.