OR-Tools  9.6
math_opt/cpp/linear_constraint.h
Go to the documentation of this file.
1 // Copyright 2010-2022 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 <sstream>
23 #include <string>
24 #include <utility>
25 
26 #include "absl/strings/string_view.h"
27 #include "absl/log/check.h"
30 #include "ortools/math_opt/cpp/id_map.h" // IWYU pragma: export
35 
36 namespace operations_research {
37 namespace math_opt {
38 
39 // A value type that references a linear constraint from ModelStorage. Usually
40 // this type is passed by copy.
42  public:
43  // The typed integer used for ids.
44  using IdType = LinearConstraintId;
45 
46  inline LinearConstraint(const ModelStorage* storage, LinearConstraintId id);
47 
48  inline int64_t id() const;
49 
50  inline LinearConstraintId typed_id() const;
51  inline const ModelStorage* storage() const;
52 
53  inline double lower_bound() const;
54  inline double upper_bound() const;
55  inline absl::string_view name() const;
56 
57  inline bool is_coefficient_nonzero(Variable variable) const;
58 
59  // Returns 0.0 if the variable is not used in the constraint.
60  inline double coefficient(Variable variable) const;
61 
63 
64  // Returns a detailed string description of the contents of the constraint
65  // (not its name, use `<<` for that instead).
66  inline std::string ToString() const;
67 
68  friend inline bool operator==(const LinearConstraint& lhs,
69  const LinearConstraint& rhs);
70  friend inline bool operator!=(const LinearConstraint& lhs,
71  const LinearConstraint& rhs);
72  template <typename H>
73  friend H AbslHashValue(H h, const LinearConstraint& linear_constraint);
74  friend std::ostream& operator<<(std::ostream& ostr,
75  const LinearConstraint& linear_constraint);
76 
77  private:
78  const ModelStorage* storage_;
79  LinearConstraintId id_;
80 };
81 
82 // Implements the API of std::unordered_map<LinearConstraint, V>, but forbids
83 // LinearConstraints from different models in the same map.
84 template <typename V>
86 
87 // Streams the name of the constraint, as registered upon constraint creation,
88 // or a short default if none was provided.
89 inline std::ostream& operator<<(std::ostream& ostr,
90  const LinearConstraint& linear_constraint);
91 
93 // Inline function implementations
95 
96 int64_t LinearConstraint::id() const { return id_.value(); }
97 
98 LinearConstraintId LinearConstraint::typed_id() const { return id_; }
99 
100 const ModelStorage* LinearConstraint::storage() const { return storage_; }
101 
103  return storage_->linear_constraint_lower_bound(id_);
104 }
105 
107  return storage_->linear_constraint_upper_bound(id_);
108 }
109 
110 absl::string_view LinearConstraint::name() const {
111  if (storage()->has_linear_constraint(id_)) {
112  return storage_->linear_constraint_name(id_);
113  }
115 }
116 
118  CHECK_EQ(variable.storage(), storage_)
121  id_, variable.typed_id());
122 }
123 
124 double LinearConstraint::coefficient(const Variable variable) const {
125  CHECK_EQ(variable.storage(), storage_)
127  return storage_->linear_constraint_coefficient(id_, variable.typed_id());
128 }
129 
131  LinearExpression terms;
132  for (const VariableId var :
133  storage()->variables_in_linear_constraint(typed_id())) {
134  terms += Variable(storage(), var) *
136  }
138  std::move(terms) <=
140 }
141 
142 std::string LinearConstraint::ToString() const {
143  if (!storage()->has_linear_constraint(id_)) {
144  return std::string(kDeletedConstraintDefaultDescription);
145  }
146  std::stringstream str;
147  str << AsBoundedLinearExpression();
148  return str.str();
149 }
150 
151 bool operator==(const LinearConstraint& lhs, const LinearConstraint& rhs) {
152  return lhs.id_ == rhs.id_ && lhs.storage_ == rhs.storage_;
153 }
154 
155 bool operator!=(const LinearConstraint& lhs, const LinearConstraint& rhs) {
156  return !(lhs == rhs);
157 }
158 
159 template <typename H>
160 H AbslHashValue(H h, const LinearConstraint& linear_constraint) {
161  return H::combine(std::move(h), linear_constraint.id_.value(),
162  linear_constraint.storage_);
163 }
164 
165 std::ostream& operator<<(std::ostream& ostr,
166  const LinearConstraint& linear_constraint) {
167  // TODO(b/170992529): handle quoting of invalid characters in the name.
168  const absl::string_view name = linear_constraint.name();
169  if (name.empty()) {
170  ostr << "__lin_con#" << linear_constraint.id() << "__";
171  } else {
172  ostr << name;
173  }
174  return ostr;
175 }
176 
178  const LinearConstraintId id)
179  : storage_(storage), id_(id) {}
180 
181 } // namespace math_opt
182 } // namespace operations_research
183 
184 #endif // OR_TOOLS_MATH_OPT_CPP_LINEAR_CONSTRAINT_H_
friend bool operator!=(const LinearConstraint &lhs, const LinearConstraint &rhs)
friend bool operator==(const LinearConstraint &lhs, const LinearConstraint &rhs)
LinearConstraint(const ModelStorage *storage, LinearConstraintId id)
friend std::ostream & operator<<(std::ostream &ostr, const LinearConstraint &linear_constraint)
friend H AbslHashValue(H h, const LinearConstraint &linear_constraint)
double linear_constraint_coefficient(LinearConstraintId constraint, VariableId variable) const
double linear_constraint_lower_bound(LinearConstraintId id) const
double linear_constraint_upper_bound(LinearConstraintId id) const
bool is_linear_constraint_coefficient_nonzero(LinearConstraintId constraint, VariableId variable) const
const std::string & linear_constraint_name(LinearConstraintId id) const
const std::string name
IntVar * var
Definition: expr_array.cc:1874
constexpr absl::string_view kObjectsFromOtherModelStorage
Definition: key_types.h:57
constexpr absl::string_view kDeletedConstraintDefaultDescription
Definition: model_util.h:30
bool operator==(const IndicatorConstraint &lhs, const IndicatorConstraint &rhs)
bool operator!=(const IndicatorConstraint &lhs, const IndicatorConstraint &rhs)
std::ostream & operator<<(std::ostream &ostr, const IndicatorConstraint &constraint)
H AbslHashValue(H h, const IndicatorConstraint &constraint)
Collection of objects used to extend the Constraint Solver library.