Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
quadratic_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
17// An object oriented wrapper for quadratic constraints in ModelStorage.
18#ifndef OR_TOOLS_MATH_OPT_CONSTRAINTS_QUADRATIC_QUADRATIC_CONSTRAINT_H_
19#define OR_TOOLS_MATH_OPT_CONSTRAINTS_QUADRATIC_QUADRATIC_CONSTRAINT_H_
20
21#include <cstdint>
22#include <ostream>
23#include <sstream>
24#include <string>
25#include <vector>
26
27#include "absl/log/check.h"
28#include "absl/strings/string_view.h"
36
38
39// A value type that references a quadratic constraint from ModelStorage.
40// Usually this type is passed by copy.
41//
42// This type implements https://abseil.io/docs/cpp/guides/hash.
44 public:
45 // The typed integer used for ids.
46 using IdType = QuadraticConstraintId;
47
48 inline QuadraticConstraint(const ModelStorage* storage,
49 QuadraticConstraintId id);
50
51 inline int64_t id() const;
52
53 inline QuadraticConstraintId typed_id() const;
54 inline const ModelStorage* storage() const;
55
56 inline double lower_bound() const;
57 inline double upper_bound() const;
58 inline absl::string_view name() const;
59
60 inline bool is_linear_coefficient_nonzero(Variable variable) const;
61 inline bool is_quadratic_coefficient_nonzero(Variable first_variable,
62 Variable second_variable) const;
63
64 // Returns 0.0 if the variable does not appear in the linear part of the
65 // constraint.
66 inline double linear_coefficient(Variable variable) const;
67 // Returns 0.0 if the variable does not appear in the quadratic part of the
68 // constraint.
69 inline double quadratic_coefficient(Variable first_variable,
70 Variable second_variable) const;
71
72 // All variables that appear in the quadratic constraint with a nonzero
73 // coefficient: in the linear terms, the quadratic terms, or both. Order is
74 // not defined.
75 inline std::vector<Variable> NonzeroVariables() const;
76
77 // Returns the constraints as a bounded quadratic expression.
78 //
79 // The quadratic expression will have a zero offset, even if the constraint
80 // was created with a non-zero one. For example:
81 //
82 // const LinearConstraint c =
83 // model.AddQuadraticConstraint(3.2 <= x*x + 1.0 <= 4.2);
84 //
85 // // Here `e` will contain 3.2 - 1.0 <= x*x <= 4.2 - 1.0.
86 // const BoundedQuadraticExpression e = c.AsBoundedQuadraticExpression();
88
89 // Returns a detailed string description of the contents of the constraint
90 // (not its name, use `<<` for that instead).
91 inline std::string ToString() const;
92
93 friend inline bool operator==(const QuadraticConstraint& lhs,
94 const QuadraticConstraint& rhs);
95 friend inline bool operator!=(const QuadraticConstraint& lhs,
96 const QuadraticConstraint& rhs);
97 template <typename H>
98 friend H AbslHashValue(H h, const QuadraticConstraint& quadratic_constraint);
99 friend std::ostream& operator<<(
100 std::ostream& ostr, const QuadraticConstraint& quadratic_constraint);
101
102 private:
103 const ModelStorage* storage_;
104 QuadraticConstraintId id_;
105};
106
107// Streams the name of the constraint, as registered upon constraint creation,
108// or a short default if none was provided.
109inline std::ostream& operator<<(std::ostream& ostr,
110 const QuadraticConstraint& constraint);
111
113// Inline function implementations
115
116int64_t QuadraticConstraint::id() const { return id_.value(); }
117
118QuadraticConstraintId QuadraticConstraint::typed_id() const { return id_; }
119
120const ModelStorage* QuadraticConstraint::storage() const { return storage_; }
121
123 return storage_->constraint_data(id_).lower_bound;
125
127 return storage_->constraint_data(id_).upper_bound;
129
130absl::string_view QuadraticConstraint::name() const {
131 if (storage_->has_constraint(id_)) {
132 return storage_->constraint_data(id_).name;
133 }
135}
136
138 const Variable variable) const {
143 const Variable first_variable, const Variable second_variable) const {
144 return quadratic_coefficient(first_variable, second_variable) != 0.0;
145}
146
147double QuadraticConstraint::linear_coefficient(const Variable variable) const {
148 CHECK_EQ(variable.storage(), storage_)
150 return storage_->constraint_data(id_).linear_terms.get(variable.typed_id());
151}
152
154 const Variable first_variable, const Variable second_variable) const {
155 CHECK_EQ(first_variable.storage(), storage_)
157 CHECK_EQ(second_variable.storage(), storage_)
159 return storage_->constraint_data(id_).quadratic_terms.get(
160 first_variable.typed_id(), second_variable.typed_id());
161}
162
163std::vector<Variable> QuadraticConstraint::NonzeroVariables() const {
164 return AtomicConstraintNonzeroVariables(*storage_, id_);
166
167std::string QuadraticConstraint::ToString() const {
168 if (!storage()->has_constraint(id_)) {
170 }
171 std::stringstream str;
173 return str.str();
174}
175
176bool operator==(const QuadraticConstraint& lhs,
177 const QuadraticConstraint& rhs) {
178 return lhs.id_ == rhs.id_ && lhs.storage_ == rhs.storage_;
179}
180
181bool operator!=(const QuadraticConstraint& lhs,
182 const QuadraticConstraint& rhs) {
183 return !(lhs == rhs);
184}
185
186template <typename H>
187H AbslHashValue(H h, const QuadraticConstraint& quadratic_constraint) {
188 return H::combine(std::move(h), quadratic_constraint.id_.value(),
189 quadratic_constraint.storage_);
190}
191
192std::ostream& operator<<(std::ostream& ostr,
193 const QuadraticConstraint& constraint) {
194 // TODO(b/170992529): handle quoting of invalid characters in the name.
195 const absl::string_view name = constraint.name();
196 if (name.empty()) {
197 ostr << "__quad_con#" << constraint.id() << "__";
198 } else {
199 ostr << name;
200 }
201 return ostr;
202}
203
204QuadraticConstraint::QuadraticConstraint(const ModelStorage* const storage,
205 const QuadraticConstraintId id)
206 : storage_(storage), id_(id) {}
207
208} // namespace operations_research::math_opt
209
210#endif // OR_TOOLS_MATH_OPT_CONSTRAINTS_QUADRATIC_QUADRATIC_CONSTRAINT_H_
bool is_quadratic_coefficient_nonzero(Variable first_variable, Variable second_variable) const
friend std::ostream & operator<<(std::ostream &ostr, const QuadraticConstraint &quadratic_constraint)
friend H AbslHashValue(H h, const QuadraticConstraint &quadratic_constraint)
QuadraticConstraint(const ModelStorage *storage, QuadraticConstraintId id)
friend bool operator!=(const QuadraticConstraint &lhs, const QuadraticConstraint &rhs)
double quadratic_coefficient(Variable first_variable, Variable second_variable) const
QuadraticConstraintId IdType
The typed integer used for ids.
friend bool operator==(const QuadraticConstraint &lhs, const QuadraticConstraint &rhs)
BoundedQuadraticExpression AsBoundedQuadraticExpression() const
const std::string name
A name for logging purposes.
constexpr absl::string_view kObjectsFromOtherModelStorage
Definition key_types.h:156
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 QuadraticExpression with upper and lower bounds.