OR-Tools  9.6
indicator_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_INDICATOR_INDICATOR_CONSTRAINT_H_
17 #define OR_TOOLS_MATH_OPT_CONSTRAINTS_INDICATOR_INDICATOR_CONSTRAINT_H_
18 
19 #include <cstdint>
20 #include <optional>
21 #include <ostream>
22 #include <string>
23 #include <vector>
24 
25 #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 an indicator constraint from ModelStorage.
35 // Usually this type is passed by copy.
37  public:
38  // The typed integer used for ids.
39  using IdType = IndicatorConstraintId;
40 
42  IndicatorConstraintId id);
43 
44  inline int64_t id() const;
45 
46  inline IndicatorConstraintId typed_id() const;
47  inline const ModelStorage* storage() const;
48 
49  inline absl::string_view name() const;
50 
51  // Returns nullopt if the indicator variable is unset (this is a valid state,
52  // in which the constraint is functionally ignored).
53  inline std::optional<Variable> indicator_variable() const;
54 
55  // The value the indicator variable takes to activate the implied constraint.
56  inline bool activate_on_zero() const;
57 
59 
60  // Returns all variables that appear in the indicator constraint with a
61  // nonzero coefficient. Order is not defined.
62  inline std::vector<Variable> NonzeroVariables() const;
63 
64  // Returns a detailed string description of the contents of the constraint
65  // (not its name, use `<<` for that instead).
66  std::string ToString() const;
67 
68  friend inline bool operator==(const IndicatorConstraint& lhs,
69  const IndicatorConstraint& rhs);
70  friend inline bool operator!=(const IndicatorConstraint& lhs,
71  const IndicatorConstraint& rhs);
72  template <typename H>
73  friend H AbslHashValue(H h, const IndicatorConstraint& constraint);
74  friend std::ostream& operator<<(std::ostream& ostr,
75  const IndicatorConstraint& constraint);
76 
77  private:
78  const ModelStorage* storage_;
79  IndicatorConstraintId id_;
80 };
81 
82 // Implements the API of std::unordered_map<IndicatorConstraint, V>, but forbids
83 // IndicatorConstraints 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 IndicatorConstraint& constraint);
91 
93 // Inline function implementations
95 
96 int64_t IndicatorConstraint::id() const { return id_.value(); }
97 
98 IndicatorConstraintId IndicatorConstraint::typed_id() const { return id_; }
99 
100 const ModelStorage* IndicatorConstraint::storage() const { return storage_; }
101 
102 absl::string_view IndicatorConstraint::name() const {
103  if (storage_->has_constraint(id_)) {
104  return storage_->constraint_data(id_).name;
105  }
107 }
108 
109 std::optional<Variable> IndicatorConstraint::indicator_variable() const {
110  const std::optional<VariableId> maybe_indicator =
111  storage_->constraint_data(id_).indicator;
112  if (!maybe_indicator.has_value()) {
113  return std::nullopt;
114  }
115  return Variable(storage_, *maybe_indicator);
116 }
117 
119  return storage_->constraint_data(id_).activate_on_zero;
120 }
121 
122 std::vector<Variable> IndicatorConstraint::NonzeroVariables() const {
123  return AtomicConstraintNonzeroVariables(*storage_, id_);
124 }
125 
127  const IndicatorConstraint& rhs) {
128  return lhs.id_ == rhs.id_ && lhs.storage_ == rhs.storage_;
129 }
130 
132  const IndicatorConstraint& rhs) {
133  return !(lhs == rhs);
134 }
135 
136 template <typename H>
137 H AbslHashValue(H h, const IndicatorConstraint& constraint) {
138  return H::combine(std::move(h), constraint.id_.value(), constraint.storage_);
139 }
140 
141 std::ostream& operator<<(std::ostream& ostr,
142  const IndicatorConstraint& constraint) {
143  // TODO(b/170992529): handle quoting of invalid characters in the name.
144  const absl::string_view name = constraint.name();
145  if (name.empty()) {
146  ostr << "__indic_con#" << constraint.id() << "__";
147  } else {
148  ostr << name;
149  }
150  return ostr;
151 }
152 
154  const IndicatorConstraintId id)
155  : storage_(storage), id_(id) {}
156 
157 } // namespace operations_research::math_opt
158 
159 #endif // OR_TOOLS_MATH_OPT_CONSTRAINTS_INDICATOR_INDICATOR_CONSTRAINT_H_
friend bool operator==(const IndicatorConstraint &lhs, const IndicatorConstraint &rhs)
std::optional< Variable > indicator_variable() const
IndicatorConstraint(const ModelStorage *storage, IndicatorConstraintId id)
friend H AbslHashValue(H h, const IndicatorConstraint &constraint)
friend bool operator!=(const IndicatorConstraint &lhs, const IndicatorConstraint &rhs)
friend std::ostream & operator<<(std::ostream &ostr, const IndicatorConstraint &constraint)
const AtomicConstraintTraits< IdType >::ConstraintData & constraint_data(IdType id) const
const std::string 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)