Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
linear_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 linear constraints in ModelStorage.
18#ifndef OR_TOOLS_MATH_OPT_CPP_LINEAR_CONSTRAINT_H_
19#define OR_TOOLS_MATH_OPT_CPP_LINEAR_CONSTRAINT_H_
20
21#include <sstream>
22#include <string>
23#include <utility>
24
25#include "absl/container/flat_hash_map.h"
26#include "absl/log/check.h"
27#include "absl/strings/string_view.h"
34
35namespace operations_research {
36namespace math_opt {
37
38// A value type that references a linear constraint from ModelStorage. Usually
39// this type is passed by copy.
41 : public ModelStorageElement<ElementType::kLinearConstraint,
42 LinearConstraint> {
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_coefficient_nonzero(Variable variable) const;
51
52 // Returns 0.0 if the variable is not used in the constraint.
53 inline double coefficient(Variable variable) const;
54
55 // Returns the constraints as a bounded linear expression.
56 //
57 // The linear expression will have a zero offset, even if the constraint was
58 // created with a non-zero one. For example:
59 //
60 // const LinearConstraint c =
61 // model.AddLinearConstraint(3.2 <= x + 1.0 <= 4.2);
62 //
63 // // Here `e` will contain 3.2 - 1.0 <= x <= 4.2 - 1.0.
64 // const BoundedLinearExpression e = c.AsBoundedLinearExpression();
66
67 // Returns a detailed string description of the contents of the constraint
68 // (not its name, use `<<` for that instead).
69 inline std::string ToString() const;
70};
71
72template <typename V>
73using LinearConstraintMap = absl::flat_hash_map<LinearConstraint, V>;
74
76// Inline function implementations
78
79double LinearConstraint::lower_bound() const {
80 return storage()->linear_constraint_lower_bound(typed_id());
82
83double LinearConstraint::upper_bound() const {
84 return storage()->linear_constraint_upper_bound(typed_id());
86
87absl::string_view LinearConstraint::name() const {
88 if (storage()->has_linear_constraint(typed_id())) {
89 return storage()->linear_constraint_name(typed_id());
90 }
92}
93
94bool LinearConstraint::is_coefficient_nonzero(const Variable variable) const {
95 CHECK_EQ(variable.storage(), storage())
97 return storage()->is_linear_constraint_coefficient_nonzero(
98 typed_id(), variable.typed_id());
99}
100
101double LinearConstraint::coefficient(const Variable variable) const {
102 CHECK_EQ(variable.storage(), storage())
104 return storage()->linear_constraint_coefficient(typed_id(),
105 variable.typed_id());
106}
107
109 LinearExpression terms;
110 for (const VariableId var :
111 storage()->variables_in_linear_constraint(typed_id())) {
112 terms += Variable(storage(), var) *
113 storage()->linear_constraint_coefficient(typed_id(), var);
114 }
115 return storage()->linear_constraint_lower_bound(typed_id()) <=
116 std::move(terms) <=
117 storage()->linear_constraint_upper_bound(typed_id());
118}
119
120std::string LinearConstraint::ToString() const {
121 if (!storage()->has_linear_constraint(typed_id())) {
123 }
124 std::stringstream str;
126 return str.str();
127}
128
129} // namespace math_opt
130} // namespace operations_research
131
132#endif // OR_TOOLS_MATH_OPT_CPP_LINEAR_CONSTRAINT_H_
BoundedLinearExpression AsBoundedLinearExpression() const
bool is_coefficient_nonzero(Variable variable) const
ModelStorageElement(ModelStorageCPtr storage, IdType id)
double coefficient(Variable variable) const
Returns 0.0 if the variable is not used in the constraint.
ModelStorageElement(ModelStorageCPtr storage, IdType id)
constexpr absl::string_view kObjectsFromOtherModelStorage
Definition key_types.h:157
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
absl::flat_hash_map< LinearConstraint, V > LinearConstraintMap
constexpr absl::string_view kDeletedConstraintDefaultDescription
Definition model_util.h:30
ElementId< ElementType::kVariable > VariableId
Definition elements.h:264
In SWIG mode, we don't want anything besides these top-level includes.
A LinearExpression with upper and lower bounds.