Google OR-Tools v9.11
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-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_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 <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 a second-order cone constraint from
34// ModelStorage. 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 = SecondOrderConeConstraintId;
41
42 inline SecondOrderConeConstraint(const ModelStorage* storage,
43 SecondOrderConeConstraintId id);
44
45 inline int64_t id() const;
46
47 inline SecondOrderConeConstraintId typed_id() const;
48 inline const ModelStorage* storage() const;
49
50 inline absl::string_view name() const;
51
52 // Returns "upper_bound" with respect to a constraint of the form
53 // ||arguments_to_norm||₂ ≤ upper_bound.
55
56 // Returns "arguments_to_norm" with respect to a constraint of the form
57 // ||arguments_to_norm||₂ ≤ upper_bound.
58 std::vector<LinearExpression> ArgumentsToNorm() const;
59
60 // Returns all variables that appear in the second-order cone constraint with
61 // a 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 SecondOrderConeConstraint& lhs,
69 const SecondOrderConeConstraint& rhs);
70 friend inline bool operator!=(const SecondOrderConeConstraint& lhs,
71 const SecondOrderConeConstraint& rhs);
72 template <typename H>
73 friend H AbslHashValue(H h, const SecondOrderConeConstraint& constraint);
74 friend std::ostream& operator<<(std::ostream& ostr,
75 const SecondOrderConeConstraint& constraint);
76
77 private:
78 const ModelStorage* storage_;
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
97const ModelStorage* SecondOrderConeConstraint::storage() const {
98 return storage_;
100
101absl::string_view SecondOrderConeConstraint::name() const {
102 if (storage_->has_constraint(id_)) {
103 return storage_->constraint_data(id_).name;
104 }
106}
107
108std::vector<Variable> SecondOrderConeConstraint::NonzeroVariables() const {
109 return AtomicConstraintNonzeroVariables(*storage_, id_);
111
113 const SecondOrderConeConstraint& rhs) {
114 return lhs.id_ == rhs.id_ && lhs.storage_ == rhs.storage_;
115}
116
118 const SecondOrderConeConstraint& rhs) {
119 return !(lhs == rhs);
120}
121
122template <typename H>
123H AbslHashValue(H h, const SecondOrderConeConstraint& constraint) {
124 return H::combine(std::move(h), constraint.id_.value(), constraint.storage_);
126
127std::ostream& operator<<(std::ostream& ostr,
128 const SecondOrderConeConstraint& 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 << "__soc_con#" << constraint.id() << "__";
133 } else {
134 ostr << name;
135 }
136 return ostr;
137}
138
140 const ModelStorage* const storage, const SecondOrderConeConstraintId id)
141 : storage_(storage), id_(id) {}
142
143} // namespace operations_research::math_opt
144
145#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(const ModelStorage *storage, SecondOrderConeConstraintId id)
friend bool operator==(const SecondOrderConeConstraint &lhs, const SecondOrderConeConstraint &rhs)
SecondOrderConeConstraintId IdType
The typed integer used for ids.
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)