OR-Tools  9.6
indicator/validator.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 <cmath>
17 
18 #include "absl/status/status.h"
21 #include "ortools/math_opt/model.pb.h"
24 
26 
27 absl::Status ValidateConstraint(const IndicatorConstraintProto& constraint,
28  const IdNameBiMap& variable_universe) {
29  if (constraint.has_indicator_id() &&
30  !variable_universe.HasId(constraint.indicator_id())) {
32  << "Invalid indicator variable id in indicator constraint: "
33  << constraint.indicator_id();
34  }
36  MakeView(constraint.expression()), /*options=*/
37  {.allow_positive_infinity = false, .allow_negative_infinity = false}))
38  << "expression of implied constraint in indicator constraint";
39  for (const int64_t var_id : constraint.expression().ids()) {
40  if (!variable_universe.HasId(var_id)) {
42  << "Invalid variable id in implied constraint in indicator "
43  "constraint: "
44  << var_id;
45  }
46  }
48  constraint.lower_bound(),
49  {.allow_positive_infinity = false, .allow_negative_infinity = true}))
50  << "invalid lower bound in indicator constraint: "
51  << constraint.lower_bound();
53  constraint.upper_bound(),
54  {.allow_positive_infinity = true, .allow_negative_infinity = false}))
55  << "invalid upper bound in indicator constraint: "
56  << constraint.upper_bound();
57  return absl::OkStatus();
58 }
59 
60 } // namespace operations_research::math_opt
#define RETURN_IF_ERROR(expr)
absl::Status CheckScalar(const double value, const DoubleOptions &options)
absl::Status ValidateConstraint(const IndicatorConstraintProto &constraint, const IdNameBiMap &variable_universe)
absl::Status CheckIdsAndValues(const SparseVectorView< T > &vector_view, absl::string_view value_name="values")
SparseVectorView< T > MakeView(absl::Span< const int64_t > ids, const Collection &values)
StatusBuilder InvalidArgumentErrorBuilder()