Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
second_order_cone_constraint.h
Go to the documentation of this file.
1// Copyright 2010-2025 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_SECOND_ORDER_CONE_SECOND_ORDER_CONE_CONSTRAINT_H_
17#define OR_TOOLS_MATH_OPT_CONSTRAINTS_SECOND_ORDER_CONE_SECOND_ORDER_CONE_CONSTRAINT_H_
18
19#include <cstdint>
20#include <ostream>
21#include <string>
22#include <vector>
23
24#include "absl/strings/string_view.h"
26#include "ortools/math_opt/constraints/second_order_cone/storage.h" // IWYU pragma: keep (`AtomicConstraintTraits<SecondOrderConeConstraintId>`)
32
34
35// A value type that references a second-order cone constraint from
36// ModelStorage. Usually this type is passed by copy.
37//
38// This type implements https://abseil.io/docs/cpp/guides/hash.
40 public:
41 // The typed integer used for ids.
42 using IdType = SecondOrderConeConstraintId;
43
45 SecondOrderConeConstraintId id);
46
47 inline int64_t id() const;
48
49 inline SecondOrderConeConstraintId typed_id() const;
50
51 inline absl::string_view name() const;
52
53 // Returns "upper_bound" with respect to a constraint of the form
54 // ||arguments_to_norm||₂ ≤ upper_bound.
56
57 // Returns "arguments_to_norm" with respect to a constraint of the form
58 // ||arguments_to_norm||₂ ≤ upper_bound.
59 std::vector<LinearExpression> ArgumentsToNorm() const;
60
61 // Returns all variables that appear in the second-order cone constraint with
62 // a 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 SecondOrderConeConstraint& lhs,
70 const SecondOrderConeConstraint& rhs);
71 friend inline bool operator!=(const SecondOrderConeConstraint& lhs,
72 const SecondOrderConeConstraint& rhs);
73 template <typename H>
74 friend H AbslHashValue(H h, const SecondOrderConeConstraint& constraint);
75 friend std::ostream& operator<<(std::ostream& ostr,
76 const SecondOrderConeConstraint& constraint);
77
78 private:
79 SecondOrderConeConstraintId id_;
80};
81
82// Streams the name of the constraint, as registered upon constraint creation,
83// or a short default if none was provided.
84inline std::ostream& operator<<(std::ostream& ostr,
85 const SecondOrderConeConstraint& constraint);
86
88// Inline function implementations
90
91int64_t SecondOrderConeConstraint::id() const { return id_.value(); }
92
93SecondOrderConeConstraintId SecondOrderConeConstraint::typed_id() const {
94 return id_;
96
97absl::string_view SecondOrderConeConstraint::name() const {
98 if (storage()->has_constraint(id_)) {
99 return storage()->constraint_data(id_).name;
100 }
102}
103
104std::vector<Variable> SecondOrderConeConstraint::NonzeroVariables() const {
107
109 const SecondOrderConeConstraint& rhs) {
110 return lhs.id_ == rhs.id_ && lhs.storage() == rhs.storage();
111}
112
114 const SecondOrderConeConstraint& rhs) {
115 return !(lhs == rhs);
116}
117
118template <typename H>
119H AbslHashValue(H h, const SecondOrderConeConstraint& constraint) {
120 return H::combine(std::move(h), constraint.id_.value(), constraint.storage());
122
123std::ostream& operator<<(std::ostream& ostr,
124 const SecondOrderConeConstraint& constraint) {
125 // TODO(b/170992529): handle quoting of invalid characters in the name.
126 const absl::string_view name = constraint.name();
127 if (name.empty()) {
128 ostr << "__soc_con#" << constraint.id() << "__";
129 } else {
130 ostr << name;
131 }
132 return ostr;
133}
134
136 const ModelStorageCPtr storage, const SecondOrderConeConstraintId id)
138
139} // namespace operations_research::math_opt
140
141#endif // OR_TOOLS_MATH_OPT_CONSTRAINTS_SECOND_ORDER_CONE_SECOND_ORDER_CONE_CONSTRAINT_H_
friend H AbslHashValue(H h, const SecondOrderConeConstraint &constraint)
friend std::ostream & operator<<(std::ostream &ostr, const SecondOrderConeConstraint &constraint)
friend bool operator!=(const SecondOrderConeConstraint &lhs, const SecondOrderConeConstraint &rhs)
SecondOrderConeConstraint(ModelStorageCPtr storage, SecondOrderConeConstraintId id)
friend bool operator==(const SecondOrderConeConstraint &lhs, const SecondOrderConeConstraint &rhs)
SecondOrderConeConstraintId IdType
The typed integer used for ids.
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
absl::Nonnull< const ModelStorage * > ModelStorageCPtr
constexpr absl::string_view kDeletedConstraintDefaultDescription
Definition model_util.h:30
bool operator==(const SecondOrderConeConstraint &lhs, const SecondOrderConeConstraint &rhs)
bool operator!=(const SecondOrderConeConstraint &lhs, const SecondOrderConeConstraint &rhs)
std::ostream & operator<<(std::ostream &ostr, const SecondOrderConeConstraint &constraint)
std::vector< Variable > AtomicConstraintNonzeroVariables(const ModelStorage &storage, const IdType id)
Definition model_util.h:42
H AbslHashValue(H h, const SecondOrderConeConstraint &constraint)