OR-Tools  9.6
sos/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 "absl/container/flat_hash_set.h"
17 #include "absl/status/status.h"
21 #include "ortools/math_opt/model.pb.h"
22 #include "ortools/math_opt/sparse_containers.pb.h"
25 
27 
28 absl::Status ValidateConstraint(const SosConstraintProto& constraint,
29  const IdNameBiMap& variable_universe) {
30  if (!constraint.weights().empty() &&
31  constraint.weights_size() != constraint.expressions_size()) {
33  << "Length mismatch between weights and expressions: "
34  << constraint.weights_size() << " vs. "
35  << constraint.expressions_size();
36  }
37  for (const LinearExpressionProto& expression : constraint.expressions()) {
38  RETURN_IF_ERROR(ValidateLinearExpression(expression, variable_universe))
39  << "Invalid SOS expression";
40  }
41  // Check weights for uniqueness.
42  absl::flat_hash_set<double> weights;
43  for (const double weight : constraint.weights()) {
44  RETURN_IF_ERROR(CheckScalarNoNanNoInf(weight)) << "Invalid SOS weight";
45  if (!weights.insert(weight).second) {
47  << "SOS weights must be unique, but encountered duplicate weight: "
48  << weight;
49  }
50  }
51  return absl::OkStatus();
52 }
53 
54 } // namespace operations_research::math_opt
#define RETURN_IF_ERROR(expr)
absl::Status ValidateConstraint(const IndicatorConstraintProto &constraint, const IdNameBiMap &variable_universe)
absl::Status CheckScalarNoNanNoInf(const double d)
absl::Status ValidateLinearExpression(const LinearExpressionProto &expression, const IdNameBiMap &variable_universe)
StatusBuilder InvalidArgumentErrorBuilder()
int64_t weight
Definition: pack.cc:510