OR-Tools  9.6
sos1_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_SOS1_CONSTRAINT_H_
17 #define OR_TOOLS_MATH_OPT_CONSTRAINTS_SOS_SOS1_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 SOS1 constraint from ModelStorage.
35 // Usually this type is passed by copy.
37  public:
38  // The typed integer used for ids.
39  using IdType = Sos1ConstraintId;
40 
41  inline Sos1Constraint(const ModelStorage* storage, Sos1ConstraintId id);
42 
43  inline int64_t id() const;
44 
45  inline Sos1ConstraintId 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 SOS1 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 constraint.
59  inline std::string ToString() const;
60 
61  friend inline bool operator==(const Sos1Constraint& lhs,
62  const Sos1Constraint& rhs);
63  friend inline bool operator!=(const Sos1Constraint& lhs,
64  const Sos1Constraint& rhs);
65  template <typename H>
66  friend H AbslHashValue(H h, const Sos1Constraint& constraint);
67  friend std::ostream& operator<<(std::ostream& ostr,
68  const Sos1Constraint& constraint);
69 
70  private:
71  const ModelStorage* storage_;
72  Sos1ConstraintId id_;
73 };
74 
75 // Implements the API of std::unordered_map<Sos1Constraint, V>, but forbids
76 // Sos1Constraints from different models in the same map.
77 template <typename V>
79 
80 // Streams the name of the constraint, as registered upon constraint creation,
81 // or a short default if none was provided.
82 inline std::ostream& operator<<(std::ostream& ostr,
83  const Sos1Constraint& constraint);
84 
86 // Inline function implementations
88 
89 int64_t Sos1Constraint::id() const { return id_.value(); }
90 
91 Sos1ConstraintId Sos1Constraint::typed_id() const { return id_; }
92 
93 const ModelStorage* Sos1Constraint::storage() const { return storage_; }
94 
96  return storage_->constraint_data(id_).num_expressions();
97 }
98 
100  return storage_->constraint_data(id_).has_weights();
101 }
102 
103 double Sos1Constraint::weight(int index) const {
104  return storage_->constraint_data(id_).weight(index);
105 }
106 
107 absl::string_view Sos1Constraint::name() const {
108  if (storage_->has_constraint(id_)) {
109  return storage_->constraint_data(id_).name();
110  }
112 }
113 
114 std::vector<Variable> Sos1Constraint::NonzeroVariables() const {
115  return AtomicConstraintNonzeroVariables(*storage_, id_);
116 }
117 
118 bool operator==(const Sos1Constraint& lhs, const Sos1Constraint& rhs) {
119  return lhs.id_ == rhs.id_ && lhs.storage_ == rhs.storage_;
120 }
121 
122 bool operator!=(const Sos1Constraint& lhs, const Sos1Constraint& rhs) {
123  return !(lhs == rhs);
124 }
125 
126 template <typename H>
127 H AbslHashValue(H h, const Sos1Constraint& constraint) {
128  return H::combine(std::move(h), constraint.id_.value(), constraint.storage_);
129 }
130 
131 std::ostream& operator<<(std::ostream& ostr, const Sos1Constraint& constraint) {
132  // TODO(b/170992529): handle quoting of invalid characters in the name.
133  const absl::string_view name = constraint.name();
134  if (name.empty()) {
135  ostr << "__sos1_con#" << constraint.id() << "__";
136  } else {
137  ostr << name;
138  }
139  return ostr;
140 }
141 
142 std::string Sos1Constraint::ToString() const {
143  if (storage_->has_constraint(id_)) {
144  return internal::SosConstraintToString(*this, "SOS1");
145  }
146  return std::string(kDeletedConstraintDefaultDescription);
147 }
148 
150  const Sos1ConstraintId id)
151  : storage_(storage), id_(id) {}
152 
153 } // namespace operations_research::math_opt
154 
155 #endif // OR_TOOLS_MATH_OPT_CONSTRAINTS_SOS_SOS1_CONSTRAINT_H_
const AtomicConstraintTraits< IdType >::ConstraintData & constraint_data(IdType id) const
friend bool operator!=(const Sos1Constraint &lhs, const Sos1Constraint &rhs)
LinearExpression Expression(int index) const
friend bool operator==(const Sos1Constraint &lhs, const Sos1Constraint &rhs)
friend H AbslHashValue(H h, const Sos1Constraint &constraint)
Sos1Constraint(const ModelStorage *storage, Sos1ConstraintId id)
friend std::ostream & operator<<(std::ostream &ostr, const Sos1Constraint &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)