OR-Tools  9.6
sos2_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 #ifndef OR_TOOLS_MATH_OPT_CONSTRAINTS_SOS_SOS2_CONSTRAINT_H_
17 #define OR_TOOLS_MATH_OPT_CONSTRAINTS_SOS_SOS2_CONSTRAINT_H_
18 
19 #include <cstdint>
20 #include <ostream>
21 #include <string>
22 #include <vector>
23 
24 #include "absl/strings/string_view.h"
28 #include "ortools/math_opt/cpp/id_map.h" // IWYU pragma: export
31 
33 
34 // A value type that references a SOS2 constraint from ModelStorage.
35 // Usually this type is passed by copy.
37  public:
38  // The typed integer used for ids.
39  using IdType = Sos2ConstraintId;
40 
41  inline Sos2Constraint(const ModelStorage* storage, Sos2ConstraintId id);
42 
43  inline int64_t id() const;
44 
45  inline Sos2ConstraintId typed_id() const;
46  inline const ModelStorage* storage() const;
47 
48  inline int64_t num_expressions() const;
50  inline bool has_weights() const;
51  inline double weight(int index) const;
52  inline absl::string_view name() const;
53 
54  // All variables that appear in the SOS2 constraint with a nonzero coefficient
55  // in any of the expressions. Order is not defined.
56  inline std::vector<Variable> NonzeroVariables() const;
57 
58  // Returns a detailed string description of the contents of the constraint
59  // (not its name, use `<<` for that instead).
60  inline std::string ToString() const;
61 
62  friend inline bool operator==(const Sos2Constraint& lhs,
63  const Sos2Constraint& rhs);
64  friend inline bool operator!=(const Sos2Constraint& lhs,
65  const Sos2Constraint& rhs);
66  template <typename H>
67  friend H AbslHashValue(H h, const Sos2Constraint& constraint);
68  friend std::ostream& operator<<(std::ostream& ostr,
69  const Sos2Constraint& constraint);
70 
71  private:
72  const ModelStorage* storage_;
73  Sos2ConstraintId id_;
74 };
75 
76 // Implements the API of std::unordered_map<Sos2Constraint, V>, but forbids
77 // Sos2Constraints from different models in the same map.
78 template <typename V>
80 
81 // Streams the name of the constraint, as registered upon constraint creation,
82 // or a short default if none was provided.
83 inline std::ostream& operator<<(std::ostream& ostr,
84  const Sos2Constraint& constraint);
85 
87 // Inline function implementations
89 
90 int64_t Sos2Constraint::id() const { return id_.value(); }
91 
92 Sos2ConstraintId Sos2Constraint::typed_id() const { return id_; }
93 
94 const ModelStorage* Sos2Constraint::storage() const { return storage_; }
95 
97  return storage_->constraint_data(id_).num_expressions();
98 }
99 
101  return storage_->constraint_data(id_).has_weights();
102 }
103 
104 double Sos2Constraint::weight(int index) const {
105  return storage_->constraint_data(id_).weight(index);
106 }
107 
108 absl::string_view Sos2Constraint::name() const {
109  if (storage_->has_constraint(id_)) {
110  return storage_->constraint_data(id_).name();
111  }
113 }
114 
115 std::vector<Variable> Sos2Constraint::NonzeroVariables() const {
116  return AtomicConstraintNonzeroVariables(*storage_, id_);
117 }
118 
119 bool operator==(const Sos2Constraint& lhs, const Sos2Constraint& rhs) {
120  return lhs.id_ == rhs.id_ && lhs.storage_ == rhs.storage_;
121 }
122 
123 bool operator!=(const Sos2Constraint& lhs, const Sos2Constraint& rhs) {
124  return !(lhs == rhs);
125 }
126 
127 template <typename H>
128 H AbslHashValue(H h, const Sos2Constraint& constraint) {
129  return H::combine(std::move(h), constraint.id_.value(), constraint.storage_);
130 }
131 
132 std::ostream& operator<<(std::ostream& ostr, const Sos2Constraint& constraint) {
133  // TODO(b/170992529): handle quoting of invalid characters in the name.
134  const absl::string_view name = constraint.name();
135  if (name.empty()) {
136  ostr << "__sos2_con#" << constraint.id() << "__";
137  } else {
138  ostr << name;
139  }
140  return ostr;
141 }
142 
143 std::string Sos2Constraint::ToString() const {
144  if (storage_->has_constraint(id_)) {
145  return internal::SosConstraintToString(*this, "SOS2");
146  }
147  return std::string(kDeletedConstraintDefaultDescription);
148 }
149 
151  const Sos2ConstraintId id)
152  : storage_(storage), id_(id) {}
153 
154 } // namespace operations_research::math_opt
155 
156 #endif // OR_TOOLS_MATH_OPT_CONSTRAINTS_SOS_SOS2_CONSTRAINT_H_
const AtomicConstraintTraits< IdType >::ConstraintData & constraint_data(IdType id) const
friend bool operator==(const Sos2Constraint &lhs, const Sos2Constraint &rhs)
Sos2Constraint(const ModelStorage *storage, Sos2ConstraintId id)
LinearExpression Expression(int index) const
friend H AbslHashValue(H h, const Sos2Constraint &constraint)
friend bool operator!=(const Sos2Constraint &lhs, const Sos2Constraint &rhs)
friend std::ostream & operator<<(std::ostream &ostr, const Sos2Constraint &constraint)
std::vector< Variable > NonzeroVariables() const
const std::string name
int index
std::string SosConstraintToString(SosConstraint constraint, absl::string_view sos_type_name)
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)