Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
sos2_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_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"
30
32
33// A value type that references a SOS2 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 = Sos2ConstraintId;
41
42 inline Sos2Constraint(const ModelStorage* storage, Sos2ConstraintId id);
43
44 inline int64_t id() const;
45
46 inline Sos2ConstraintId typed_id() const;
47 inline const ModelStorage* storage() const;
48
49 inline int64_t num_expressions() const;
51 inline bool has_weights() const;
52 inline double weight(int index) const;
53 inline absl::string_view name() const;
54
55 // All variables that appear in the SOS2 constraint with a nonzero coefficient
56 // in any of the expressions. Order is not defined.
57 inline std::vector<Variable> NonzeroVariables() const;
58
59 // Returns a detailed string description of the contents of the constraint
60 // (not its name, use `<<` for that instead).
61 inline std::string ToString() const;
62
63 friend inline bool operator==(const Sos2Constraint& lhs,
64 const Sos2Constraint& rhs);
65 friend inline bool operator!=(const Sos2Constraint& lhs,
66 const Sos2Constraint& rhs);
67 template <typename H>
68 friend H AbslHashValue(H h, const Sos2Constraint& constraint);
69 friend std::ostream& operator<<(std::ostream& ostr,
70 const Sos2Constraint& constraint);
71
72 private:
73 const ModelStorage* storage_;
74 Sos2ConstraintId id_;
75};
76
77// Streams the name of the constraint, as registered upon constraint creation,
78// or a short default if none was provided.
79inline std::ostream& operator<<(std::ostream& ostr,
80 const Sos2Constraint& constraint);
81
83// Inline function implementations
85
86int64_t Sos2Constraint::id() const { return id_.value(); }
87
88Sos2ConstraintId Sos2Constraint::typed_id() const { return id_; }
89
90const ModelStorage* Sos2Constraint::storage() const { return storage_; }
91
93 return storage_->constraint_data(id_).num_expressions();
95
96bool Sos2Constraint::has_weights() const {
97 return storage_->constraint_data(id_).has_weights();
99
100double Sos2Constraint::weight(int index) const {
101 return storage_->constraint_data(id_).weight(index);
103
104absl::string_view Sos2Constraint::name() const {
105 if (storage_->has_constraint(id_)) {
106 return storage_->constraint_data(id_).name();
107 }
109}
110
111std::vector<Variable> Sos2Constraint::NonzeroVariables() const {
112 return AtomicConstraintNonzeroVariables(*storage_, id_);
114
115bool operator==(const Sos2Constraint& lhs, const Sos2Constraint& rhs) {
116 return lhs.id_ == rhs.id_ && lhs.storage_ == rhs.storage_;
118
119bool operator!=(const Sos2Constraint& lhs, const Sos2Constraint& rhs) {
120 return !(lhs == rhs);
122
123template <typename H>
124H AbslHashValue(H h, const Sos2Constraint& constraint) {
125 return H::combine(std::move(h), constraint.id_.value(), constraint.storage_);
127
128std::ostream& operator<<(std::ostream& ostr, const Sos2Constraint& constraint) {
129 // TODO(b/170992529): handle quoting of invalid characters in the name.
130 const absl::string_view name = constraint.name();
131 if (name.empty()) {
132 ostr << "__sos2_con#" << constraint.id() << "__";
133 } else {
134 ostr << name;
135 }
136 return ostr;
137}
138
139std::string Sos2Constraint::ToString() const {
140 if (storage_->has_constraint(id_)) {
141 return internal::SosConstraintToString(*this, "SOS2");
142 }
143 return std::string(kDeletedConstraintDefaultDescription);
144}
145
146Sos2Constraint::Sos2Constraint(const ModelStorage* const storage,
147 const Sos2ConstraintId id)
148 : storage_(storage), id_(id) {}
149
150} // namespace operations_research::math_opt
151
152#endif // OR_TOOLS_MATH_OPT_CONSTRAINTS_SOS_SOS2_CONSTRAINT_H_
friend bool operator==(const Sos2Constraint &lhs, const Sos2Constraint &rhs)
friend H AbslHashValue(H h, const Sos2Constraint &constraint)
friend bool operator!=(const Sos2Constraint &lhs, const Sos2Constraint &rhs)
Sos2Constraint(const ModelStorage *storage, Sos2ConstraintId id)
LinearExpression Expression(int index) const
friend std::ostream & operator<<(std::ostream &ostr, const Sos2Constraint &constraint)
Sos2ConstraintId IdType
The typed integer used for ids.
std::vector< Variable > NonzeroVariables() const
const std::string name
A name for logging purposes.
int index
std::string SosConstraintToString(SosConstraint constraint, absl::string_view sos_type_name)
Definition util.h:44
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)
int64_t weight
Definition pack.cc:510