Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
indicator_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_INDICATOR_INDICATOR_CONSTRAINT_H_
17#define OR_TOOLS_MATH_OPT_CONSTRAINTS_INDICATOR_INDICATOR_CONSTRAINT_H_
18
19#include <optional>
20#include <string>
21#include <vector>
22
23#include "absl/strings/string_view.h"
29
31
32// A value type that references an indicator constraint from ModelStorage.
33// Usually this type is passed by copy.
35 : public ModelStorageElement<ElementType::kIndicatorConstraint,
36 IndicatorConstraint> {
37 public:
39
40 absl::string_view name() const;
41
42 // Returns nullopt if the indicator variable is unset (this is a valid state,
43 // in which the constraint is functionally ignored).
44 inline std::optional<Variable> indicator_variable() const;
45
46 // The value the indicator variable takes to activate the implied constraint.
47 inline bool activate_on_zero() const;
48
50
51 // Returns all variables that appear in the indicator constraint with a
52 // nonzero coefficient. Order is not defined.
53 inline std::vector<Variable> NonzeroVariables() const;
54
55 // Returns a detailed string description of the contents of the constraint
56 // (not its name, use `<<` for that instead).
57 std::string ToString() const;
58};
59
61// Inline function implementations
63
64inline absl::string_view IndicatorConstraint::name() const {
65 if (storage()->has_constraint(typed_id())) {
66 return storage()->constraint_data(typed_id()).name;
67 }
69}
70
71std::optional<Variable> IndicatorConstraint::indicator_variable() const {
72 const std::optional<VariableId> maybe_indicator =
73 storage()->constraint_data(typed_id()).indicator;
74 if (!maybe_indicator.has_value()) {
75 return std::nullopt;
76 }
77 return Variable(storage(), *maybe_indicator);
78}
79
81 return storage()->constraint_data(typed_id()).activate_on_zero;
83
84std::vector<Variable> IndicatorConstraint::NonzeroVariables() const {
87
88} // namespace operations_research::math_opt
89
90#endif // OR_TOOLS_MATH_OPT_CONSTRAINTS_INDICATOR_INDICATOR_CONSTRAINT_H_
ModelStorageElement(ModelStorageCPtr storage, IdType id)
bool activate_on_zero() const
The value the indicator variable takes to activate the implied constraint.
ModelStorageElement(ModelStorageCPtr storage, IdType id)
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 LinearExpression with upper and lower bounds.