OR-Tools  9.6
indicator_constraint.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 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 
15 
16 #include <optional>
17 #include <sstream>
18 #include <string>
19 #include <utility>
20 
25 
27 
29  const IndicatorConstraintData& data = storage()->constraint_data(id_);
30  LinearExpression expr = ToLinearExpression(*storage_, data.linear_terms, 0.0);
31  return data.lower_bound <= std::move(expr) <= data.upper_bound;
32 }
33 
34 std::string IndicatorConstraint::ToString() const {
35  if (!storage()->has_constraint(id_)) {
36  return std::string(kDeletedConstraintDefaultDescription);
37  }
38  const IndicatorConstraintData& data = storage()->constraint_data(id_);
39  std::stringstream str;
40  if (data.indicator.has_value()) {
41  str << Variable(storage_, *data.indicator)
42  << (data.activate_on_zero ? " = 0" : " = 1");
43  } else {
44  str << "[unset indicator variable]";
45  }
46  str << " ⇒ " << ImpliedConstraint();
47  return str.str();
48 }
49 
50 } // namespace operations_research::math_opt
const AtomicConstraintTraits< IdType >::ConstraintData & constraint_data(IdType id) const
LinearExpression ToLinearExpression(const ModelStorage &storage, const SparseCoefficientMap &coeffs, const double offset)
Definition: model_util.cc:23
constexpr absl::string_view kDeletedConstraintDefaultDescription
Definition: model_util.h:30