Google OR-Tools v9.14
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-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
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 <sstream>
22#include <string>
23#include <vector>
24
25#include "absl/log/check.h"
26#include "absl/strings/string_view.h"
35
37
38// A value type that references a quadratic constraint from ModelStorage.
39// Usually this type is passed by copy.
41 : public ModelStorageElement<ElementType::kQuadraticConstraint,
42 QuadraticConstraint> {
43 public:
45
46 inline double lower_bound() const;
47 inline double upper_bound() const;
48 inline absl::string_view name() const;
49
50 inline bool is_linear_coefficient_nonzero(Variable variable) const;
51 inline bool is_quadratic_coefficient_nonzero(Variable first_variable,
52 Variable second_variable) const;
53
54 // Returns 0.0 if the variable does not appear in the linear part of the
55 // constraint.
56 inline double linear_coefficient(Variable variable) const;
57 // Returns 0.0 if the variable does not appear in the quadratic part of the
58 // constraint.
59 inline double quadratic_coefficient(Variable first_variable,
60 Variable second_variable) const;
61
62 // All variables that appear in the quadratic constraint with a nonzero
63 // coefficient: in the linear terms, the quadratic terms, or both. Order is
64 // not defined.
65 inline std::vector<Variable> NonzeroVariables() const;
66
67 // Returns the constraints as a bounded quadratic expression.
68 //
69 // The quadratic expression will have a zero offset, even if the constraint
70 // was created with a non-zero one. For example:
71 //
72 // const QuadraticConstraint c =
73 // model.AddQuadraticConstraint(3.2 <= x*x + 1.0 <= 4.2);
74 //
75 // // Here `e` will contain 3.2 - 1.0 <= x*x <= 4.2 - 1.0.
76 // const BoundedQuadraticExpression e = c.AsBoundedQuadraticExpression();
78
79 // Returns a detailed string description of the contents of the constraint
80 // (not its name, use `<<` for that instead).
81 inline std::string ToString() const;
82};
83
85// Inline function implementations
87
89 return storage()->constraint_data(typed_id()).lower_bound;
91
93 return storage()->constraint_data(typed_id()).upper_bound;
95
96absl::string_view QuadraticConstraint::name() const {
97 if (storage()->has_constraint(typed_id())) {
98 return storage()->constraint_data(typed_id()).name;
99 }
101}
102
104 const Variable variable) const {
109 const Variable first_variable, const Variable second_variable) const {
110 return quadratic_coefficient(first_variable, second_variable) != 0.0;
111}
112
113double QuadraticConstraint::linear_coefficient(const Variable variable) const {
114 CHECK_EQ(variable.storage(), storage())
116 return storage()
117 ->constraint_data(typed_id())
118 .linear_terms.get(variable.typed_id());
119}
120
122 const Variable first_variable, const Variable second_variable) const {
123 CHECK_EQ(first_variable.storage(), storage())
125 CHECK_EQ(second_variable.storage(), storage())
127 return storage()
128 ->constraint_data(typed_id())
129 .quadratic_terms.get(first_variable.typed_id(),
130 second_variable.typed_id());
131}
132
133std::vector<Variable> QuadraticConstraint::NonzeroVariables() const {
136
137std::string QuadraticConstraint::ToString() const {
138 if (!storage()->has_constraint(typed_id())) {
140 }
141 std::stringstream str;
143 return str.str();
144}
145
146} // namespace operations_research::math_opt
147
148#endif // OR_TOOLS_MATH_OPT_CONSTRAINTS_QUADRATIC_QUADRATIC_CONSTRAINT_H_
ModelStorageElement(ModelStorageCPtr storage, IdType id)
bool is_quadratic_coefficient_nonzero(Variable first_variable, Variable second_variable) const
double quadratic_coefficient(Variable first_variable, Variable second_variable) const
ModelStorageElement(ModelStorageCPtr storage, IdType id)
BoundedQuadraticExpression AsBoundedQuadraticExpression() const
constexpr absl::string_view kObjectsFromOtherModelStorage
Definition key_types.h:157
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::vector< Variable > AtomicConstraintNonzeroVariables(const ModelStorage &storage, const IdType id)
Definition model_util.h:42
A QuadraticExpression with upper and lower bounds.