Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
indicator_constraint.h
Go to the documentation of this file.
1// Copyright 2010-2024 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"
30
32
33// A value type that references an indicator constraint from ModelStorage.
34// Usually this type is passed by copy.
35//
36// This type implements https://abseil.io/docs/cpp/guides/hash.
38 public:
39 // The typed integer used for ids.
40 using IdType = IndicatorConstraintId;
41
42 inline IndicatorConstraint(const ModelStorage* storage,
43 IndicatorConstraintId id);
44
45 inline int64_t id() const;
46
47 inline IndicatorConstraintId typed_id() const;
48 inline const ModelStorage* storage() const;
49
50 inline absl::string_view name() const;
51
52 // Returns nullopt if the indicator variable is unset (this is a valid state,
53 // in which the constraint is functionally ignored).
54 inline std::optional<Variable> indicator_variable() const;
55
56 // The value the indicator variable takes to activate the implied constraint.
57 inline bool activate_on_zero() const;
58
60
61 // Returns all variables that appear in the indicator constraint with a
62 // nonzero coefficient. Order is not defined.
63 inline std::vector<Variable> NonzeroVariables() const;
64
65 // Returns a detailed string description of the contents of the constraint
66 // (not its name, use `<<` for that instead).
67 std::string ToString() const;
68
69 friend inline bool operator==(const IndicatorConstraint& lhs,
70 const IndicatorConstraint& rhs);
71 friend inline bool operator!=(const IndicatorConstraint& lhs,
72 const IndicatorConstraint& rhs);
73 template <typename H>
74 friend H AbslHashValue(H h, const IndicatorConstraint& constraint);
75 friend std::ostream& operator<<(std::ostream& ostr,
76 const IndicatorConstraint& constraint);
77
78 private:
79 const ModelStorage* storage_;
80 IndicatorConstraintId id_;
81};
82
83// Streams the name of the constraint, as registered upon constraint creation,
84// or a short default if none was provided.
85inline std::ostream& operator<<(std::ostream& ostr,
86 const IndicatorConstraint& constraint);
87
89// Inline function implementations
91
92int64_t IndicatorConstraint::id() const { return id_.value(); }
93
94IndicatorConstraintId IndicatorConstraint::typed_id() const { return id_; }
95
96const ModelStorage* IndicatorConstraint::storage() const { return storage_; }
97
98absl::string_view IndicatorConstraint::name() const {
99 if (storage_->has_constraint(id_)) {
100 return storage_->constraint_data(id_).name;
101 }
103}
104
105std::optional<Variable> IndicatorConstraint::indicator_variable() const {
106 const std::optional<VariableId> maybe_indicator =
107 storage_->constraint_data(id_).indicator;
108 if (!maybe_indicator.has_value()) {
109 return std::nullopt;
110 }
111 return Variable(storage_, *maybe_indicator);
112}
113
115 return storage_->constraint_data(id_).activate_on_zero;
117
118std::vector<Variable> IndicatorConstraint::NonzeroVariables() const {
119 return AtomicConstraintNonzeroVariables(*storage_, id_);
121
122bool operator==(const IndicatorConstraint& lhs,
123 const IndicatorConstraint& rhs) {
124 return lhs.id_ == rhs.id_ && lhs.storage_ == rhs.storage_;
125}
126
127bool operator!=(const IndicatorConstraint& lhs,
128 const IndicatorConstraint& rhs) {
129 return !(lhs == rhs);
130}
131
132template <typename H>
133H AbslHashValue(H h, const IndicatorConstraint& constraint) {
134 return H::combine(std::move(h), constraint.id_.value(), constraint.storage_);
136
137std::ostream& operator<<(std::ostream& ostr,
138 const IndicatorConstraint& constraint) {
139 // TODO(b/170992529): handle quoting of invalid characters in the name.
140 const absl::string_view name = constraint.name();
141 if (name.empty()) {
142 ostr << "__indic_con#" << constraint.id() << "__";
143 } else {
144 ostr << name;
145 }
146 return ostr;
147}
148
149IndicatorConstraint::IndicatorConstraint(const ModelStorage* const storage,
150 const IndicatorConstraintId id)
151 : storage_(storage), id_(id) {}
152
153} // namespace operations_research::math_opt
154
155#endif // OR_TOOLS_MATH_OPT_CONSTRAINTS_INDICATOR_INDICATOR_CONSTRAINT_H_
IndicatorConstraintId IdType
The typed integer used for ids.
friend bool operator==(const IndicatorConstraint &lhs, const IndicatorConstraint &rhs)
IndicatorConstraint(const ModelStorage *storage, IndicatorConstraintId id)
friend H AbslHashValue(H h, const IndicatorConstraint &constraint)
friend bool operator!=(const IndicatorConstraint &lhs, const IndicatorConstraint &rhs)
bool activate_on_zero() const
The value the indicator variable takes to activate the implied constraint.
friend std::ostream & operator<<(std::ostream &ostr, const IndicatorConstraint &constraint)
const std::string name
A name for logging purposes.
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
constexpr absl::string_view kDeletedConstraintDefaultDescription
Definition model_util.h:30
std::ostream & operator<<(std::ostream &ostr, const IndicatorConstraint &constraint)
bool operator==(const IndicatorConstraint &lhs, const IndicatorConstraint &rhs)
bool operator!=(const IndicatorConstraint &lhs, const IndicatorConstraint &rhs)
std::vector< Variable > AtomicConstraintNonzeroVariables(const ModelStorage &storage, const IdType id)
Definition model_util.h:42
H AbslHashValue(H h, const IndicatorConstraint &constraint)
A LinearExpression with upper and lower bounds.