OR-Tools  9.6
quadratic_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 quadratic constraints in ModelStorage.
18 #ifndef OR_TOOLS_MATH_OPT_CONSTRAINTS_QUADRATIC_QUADRATIC_CONSTRAINT_H_
19 #define OR_TOOLS_MATH_OPT_CONSTRAINTS_QUADRATIC_QUADRATIC_CONSTRAINT_H_
20 
21 #include <cstdint>
22 #include <ostream>
23 #include <sstream>
24 #include <string>
25 #include <vector>
26 
27 #include "absl/strings/string_view.h"
28 #include "absl/log/check.h"
31 #include "ortools/math_opt/cpp/id_map.h" // IWYU pragma: export
37 
39 
40 // A value type that references a quadratic constraint from ModelStorage.
41 // Usually this type is passed by copy.
43  public:
44  // The typed integer used for ids.
45  using IdType = QuadraticConstraintId;
46 
48  QuadraticConstraintId id);
49 
50  inline int64_t id() const;
51 
52  inline QuadraticConstraintId 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_linear_coefficient_nonzero(Variable variable) const;
60  inline bool is_quadratic_coefficient_nonzero(Variable first_variable,
61  Variable second_variable) const;
62 
63  // Returns 0.0 if the variable does not appear in the linear part of the
64  // constraint.
65  inline double linear_coefficient(Variable variable) const;
66  // Returns 0.0 if the variable does not appear in the quadratic part of the
67  // constraint.
68  inline double quadratic_coefficient(Variable first_variable,
69  Variable second_variable) const;
70 
71  // All variables that appear in the quadratic constraint with a nonzero
72  // coefficient: in the linear terms, the quadratic terms, or both. Order is
73  // not defined.
74  inline std::vector<Variable> NonzeroVariables() const;
75 
77 
78  // Returns a detailed string description of the contents of the constraint
79  // (not its name, use `<<` for that instead).
80  inline std::string ToString() const;
81 
82  friend inline bool operator==(const QuadraticConstraint& lhs,
83  const QuadraticConstraint& rhs);
84  friend inline bool operator!=(const QuadraticConstraint& lhs,
85  const QuadraticConstraint& rhs);
86  template <typename H>
87  friend H AbslHashValue(H h, const QuadraticConstraint& quadratic_constraint);
88  friend std::ostream& operator<<(
89  std::ostream& ostr, const QuadraticConstraint& quadratic_constraint);
90 
91  private:
92  const ModelStorage* storage_;
93  QuadraticConstraintId id_;
94 };
95 
96 // Implements the API of std::unordered_map<QuadraticConstraint, V>, but forbids
97 // QuadraticConstraints from different models in the same map.
98 template <typename V>
100 
101 // Streams the name of the constraint, as registered upon constraint creation,
102 // or a short default if none was provided.
103 inline std::ostream& operator<<(std::ostream& ostr,
104  const QuadraticConstraint& constraint);
105 
107 // Inline function implementations
109 
110 int64_t QuadraticConstraint::id() const { return id_.value(); }
111 
112 QuadraticConstraintId QuadraticConstraint::typed_id() const { return id_; }
113 
114 const ModelStorage* QuadraticConstraint::storage() const { return storage_; }
115 
117  return storage_->constraint_data(id_).lower_bound;
118 }
119 
121  return storage_->constraint_data(id_).upper_bound;
122 }
123 
124 absl::string_view QuadraticConstraint::name() const {
125  if (storage_->has_constraint(id_)) {
126  return storage_->constraint_data(id_).name;
127  }
129 }
130 
132  const Variable variable) const {
133  return linear_coefficient(variable) != 0.0;
134 }
135 
137  const Variable first_variable, const Variable second_variable) const {
138  return quadratic_coefficient(first_variable, second_variable) != 0.0;
139 }
140 
141 double QuadraticConstraint::linear_coefficient(const Variable variable) const {
142  CHECK_EQ(variable.storage(), storage_)
144  return storage_->constraint_data(id_).linear_terms.get(variable.typed_id());
145 }
146 
148  const Variable first_variable, const Variable second_variable) const {
149  CHECK_EQ(first_variable.storage(), storage_)
151  CHECK_EQ(second_variable.storage(), storage_)
153  return storage_->constraint_data(id_).quadratic_terms.get(
154  first_variable.typed_id(), second_variable.typed_id());
155 }
156 
157 std::vector<Variable> QuadraticConstraint::NonzeroVariables() const {
158  return AtomicConstraintNonzeroVariables(*storage_, id_);
159 }
160 
161 std::string QuadraticConstraint::ToString() const {
162  if (!storage()->has_constraint(id_)) {
163  return std::string(kDeletedConstraintDefaultDescription);
164  }
165  std::stringstream str;
167  return str.str();
168 }
169 
171  const QuadraticConstraint& rhs) {
172  return lhs.id_ == rhs.id_ && lhs.storage_ == rhs.storage_;
173 }
174 
176  const QuadraticConstraint& rhs) {
177  return !(lhs == rhs);
178 }
179 
180 template <typename H>
181 H AbslHashValue(H h, const QuadraticConstraint& quadratic_constraint) {
182  return H::combine(std::move(h), quadratic_constraint.id_.value(),
183  quadratic_constraint.storage_);
184 }
185 
186 std::ostream& operator<<(std::ostream& ostr,
187  const QuadraticConstraint& constraint) {
188  // TODO(b/170992529): handle quoting of invalid characters in the name.
189  const absl::string_view name = constraint.name();
190  if (name.empty()) {
191  ostr << "__quad_con#" << constraint.id() << "__";
192  } else {
193  ostr << name;
194  }
195  return ostr;
196 }
197 
199  const QuadraticConstraintId id)
200  : storage_(storage), id_(id) {}
201 
202 } // namespace operations_research::math_opt
203 
204 #endif // OR_TOOLS_MATH_OPT_CONSTRAINTS_QUADRATIC_QUADRATIC_CONSTRAINT_H_
const AtomicConstraintTraits< IdType >::ConstraintData & constraint_data(IdType id) const
friend H AbslHashValue(H h, const QuadraticConstraint &quadratic_constraint)
friend bool operator!=(const QuadraticConstraint &lhs, const QuadraticConstraint &rhs)
BoundedQuadraticExpression AsBoundedQuadraticExpression() const
friend bool operator==(const QuadraticConstraint &lhs, const QuadraticConstraint &rhs)
bool is_quadratic_coefficient_nonzero(Variable first_variable, Variable second_variable) const
QuadraticConstraint(const ModelStorage *storage, QuadraticConstraintId id)
friend std::ostream & operator<<(std::ostream &ostr, const QuadraticConstraint &quadratic_constraint)
double quadratic_coefficient(Variable first_variable, Variable second_variable) const
const std::string name
constexpr absl::string_view kObjectsFromOtherModelStorage
Definition: key_types.h:57
std::vector< Variable > AtomicConstraintNonzeroVariables(const ModelStorage &storage, const IdType id)
Definition: model_util.h:44
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)