Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
validator.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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 <cstdint>
17
18#include "absl/status/status.h"
23#include "ortools/math_opt/model.pb.h"
24#include "ortools/math_opt/sparse_containers.pb.h"
29
31
32absl::Status ValidateConstraint(const QuadraticConstraintProto& constraint,
33 const IdNameBiMap& variable_universe) {
34 // Step 1: Validate linear terms.
36 MakeView(constraint.linear_terms()),
37 {.allow_positive_infinity = false, .allow_negative_infinity = false}))
38 << "bad linear term in quadratic constraint";
40 CheckIdsSubset(constraint.linear_terms().ids(), variable_universe))
41 << "bad linear term ID in quadratic constraint";
42
43 // Step 2: Validate quadratic terms.
44 RETURN_IF_ERROR(SparseMatrixValid(constraint.quadratic_terms(),
45 /*enforce_upper_triangular=*/true))
46 << "bad quadratic term in quadratic constraint";
47 RETURN_IF_ERROR(SparseMatrixIdsAreKnown(constraint.quadratic_terms(),
48 variable_universe, variable_universe))
49 << "bad quadratic term ID in quadratic constraint";
50
51 // Step 3: Validate bounds.
52 {
53 const double lb = constraint.lower_bound();
54 const double ub = constraint.upper_bound();
55 RETURN_IF_ERROR(CheckScalar(lb, {.allow_positive_infinity = false}))
56 << "bad quadratic constraint lower bound";
57 RETURN_IF_ERROR(CheckScalar(ub, {.allow_negative_infinity = false}))
58 << "bad quadratic constraint upper bound";
59 if (lb > ub) {
61 << "Quadratic constraint bounds are inverted, rendering model "
62 "trivially infeasible: lb = "
63 << lb << " > " << ub << " = ub";
64 }
65 }
66
67 return absl::OkStatus();
68}
69
70} // namespace operations_research::math_opt
#define RETURN_IF_ERROR(expr)
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
absl::Status SparseMatrixIdsAreKnown(const SparseDoubleMatrixProto &matrix, const IdNameBiMap &row_ids, const IdNameBiMap &column_ids)
SparseVectorView< T > MakeView(absl::Span< const int64_t > ids, const Collection &values)
absl::Status CheckIdsAndValues(const SparseVectorView< T > &vector_view, absl::string_view value_name="values")
absl::Status ValidateConstraint(const IndicatorConstraintProto &constraint, const IdNameBiMap &variable_universe)
Definition validator.cc:27
absl::Status CheckScalar(const double value, const DoubleOptions &options)
Checks value is not NaN and satisfies the additional conditions in options.
absl::Status CheckIdsSubset(absl::Span< const int64_t > ids, const IdNameBiMap &universe, std::optional< int64_t > upper_bound)
absl::Status SparseMatrixValid(const SparseDoubleMatrixProto &matrix, const bool enforce_upper_triangular)
StatusBuilder InvalidArgumentErrorBuilder()